BRAT 2.4.5
Class index
Full class index
brahmlib
BRAHMS
ROOT page
//____________________________________________________________________
// 
// 
// 

//____________________________________________________________________
//
// $Id: BrRootDb.cxx,v 1.1 2001/10/08 10:31:38 cholm Exp $
// $Author: cholm $
// $Date: 2001/10/08 10:31:38 $
// $Copyright: (C) 2001 BRAHMS Collaboration <brahmlib@rhic.bnl.gov>
//
#ifndef BRAT_BrRootDb
#include "BrRootDb.h"
#endif
#ifndef ROOT_TROOT
#include "TROOT.h"
#endif

//____________________________________________________________________
ClassImp(BrRootDb);

//____________________________________________________________________
 BrRootDb::BrRootDb(const Char_t* name, const Char_t* title) 
  : BrVirtualDb(name, title)
{
  // Default constructor
  fConnection = 0;
  fRdbmType   = "root";
  fSequence   = 0;
}

//____________________________________________________________________
 BrRootDb::~BrRootDb() 
{
  Close();
}

//____________________________________________________________________
 Bool_t BrRootDb::Connect(Option_t* option="") 
{
  // Connect to the file 
  if (IsConnected()) {
    Warning("Connect", "already connected");
    return kTRUE;
  }
  
  // Return 0 if no name is set. 
  if (fTitle.IsNull()) {
    Error("Connect", "file name not set"); 
    return kFALSE; 
  }

  // Construct the file name from the parameters. 
  if (fHostName.IsNull()) {
    fFileName = "file:"; 
  }
  else {
    fFileName = fRdbmType; 
    fFileName += "://"; 
    fFileName += fHostName;
    fFileName += "/";
  }
  fFileName += GetDbName();

  TDirectory* savdir = gDirectory; 

  fConnection = TFile::Open(fFileName, "UPDATE"); 
  if (!fConnection) 
    Error("Connect", "could not open database "%s"",
	  fFileName.Data());

  // Get a pointer to the sequence. 
  fSequence = fConnection->Get("sequence"); 
  if (!fSequence) 
    CreateSequence();

  gDirectory = savdir; 
  return kTRUE;
}

//____________________________________________________________________
 void BrRootDb::Close(Option_t* option="") 
{
  // Close the connection to the database - i.e., file. 
  if (!fConnection) 
    return;

  TDirectory* savdir = gDirectory; 

  fConnection->Write(fConnection->GetName(), kOverwrite);
  fConnection->Close();
  fConnection = 0;

  gDirectory = savdir; 
}

//____________________________________________________________________
 TSQLResult* BrRootDb::Query(BrDbQuery* q) 
{
  // This will execute the query q, as if it was a root script.  Care
  // must be take when using this method.  Not that a return value of
  // zero does not nessecarily mean a failure has occured, since this
  // method returns 0. 
  if (!IsConnected()) {
    Error("Query", "isn't connected");
    return 0;
  }
  
  TDirectory* savdir = gDirectory;

  // Go to the file 
  fConnection->cd();
  // execute the query via the interpretor. 
  gROOT->ProcessLine(q->Data());
  // Force a write to disk;
  fConnection->Write();

  gDirectory         = savdir;

  return 0;
}

//____________________________________________________________________
 TSQLRow* BrRootDb::GetSingle(const Char_t* tableName, 
			     const Char_t* condition)
{
  // 
  Warning("GetSingle", "not implmented for ROOT files");
  return 0;
}

//____________________________________________________________________
 TSQLResult* BrRootDb::GetMultiple(const Char_t* table, 
				  const Char_t* condition)
{
  //
  Warning("GetSingle", "not implmented for ROOT files");
  return 0;
}

//____________________________________________________________________
 void BrRootDb::CreateSequence() 
{
  if (fSequence) 
    return;

  TDirectory* savdir = gDirectory; 
  fConnection->cd();
  fSequence = new TObject;
  fSequence->SetUniqueID(0);
  Increment();

  gDirectory = savdir;
}

//____________________________________________________________________
 Int_t BrRootDb::Increment() 
{
  if (fSequence) {
    Error("Increment", "no sequence in DB - very bad!");
    return -1;
  }
  Int_t n = fSequence->GetUniqueID() + 1;
  fSequence->SetUniqueID(n);
  TDirectory* savdir = gDirectory;
  fConnection->cd();
  fSequence->Write("sequence", TObject::kOverwrite);
  gDirectory = savdir;
  
  return n;
}

//____________________________________________________________________
 void BrRootDb::LockTables(const Char_t* writetables,int i=0)
{
  // Not implmented - and my never be
}

//____________________________________________________________________
 void BrRootDb::UnLockTables()
{
  // Not implmented - and my never be
}

  
//____________________________________________________________________
//
// $Log: BrRootDb.cxx,v $
// Revision 1.1  2001/10/08 10:31:38  cholm
// Changed to allow polymorphic access classes.  The class BrDb defines the
// interface, while BrRdbmDb and BrRootDb implements the interface.  Other
// implmentations could be BrAsciiDb, BrObjyDb (shudder), BrXmlDb, and so on.
// The table representation of the BrahmsMain.DB table has been moved from
// BrDb to BrDbDb.
//
//

This page automatically generated by script docBrat by Christian Holm

Copyright ; 2002 BRAHMS Collaboration <brahmlib@rcf.rhic.bnl.gov>
Last Update on by

Validate HTML
Validate CSS