212 #include <StString.h>
217 #include "StMessMgr.h"
223 #include "St_Table.h"
224 #include "TRealData.h"
225 #include "TDataMember.h"
226 #include "TDataType.h"
228 #include "StDbBroker.h"
229 #include "StDbLib/StDbManager.hh"
230 #include "StDbLib/StDbConfigNode.hh"
231 #include "StDbLib/StDbTable.h"
233 #include "StDbLib/StDbTableIter.hh"
234 #include "StDbLib/StDbBuffer.h"
235 #include "StDbLib/StDbTableDescriptor.h"
236 #include "StDbWrappedMessenger.hh"
238 #define __CLASS__ "StDbBroker"
250 TClass *classPtr = parentTable->GetRowClass();
251 if (!classPtr)
return NULL;
253 if (!classPtr->GetListOfRealData()) classPtr->BuildRealData();
255 TIter next(classPtr->GetListOfDataMembers());
256 TDataMember *member = 0;
258 char **ElementComment =
new char*[m_nElements];
260 while ( (member = (TDataMember *) next()) ) {
261 ElementComment[i] = strdup(member->GetTitle());
263 j=strlen(ElementComment[i]);
264 while (j != 0 && ElementComment[i][j-1] ==
' ') j--;
265 ElementComment[i][j]=
'\0';
268 return ElementComment;
272 StDbBroker::StDbBroker(): m_structName(0), m_tableName(0), m_requestTimeStamp(0), m_tableVersion(0), m_database(0), m_ParentType(0), m_isVerbose(0), m_Nodes(0), m_Tree(0), m_flavor(0), m_prodTime(0) {
278 mgr->setMessenger(ms);
283 StDbBroker::~StDbBroker(){
285 if(m_tableName)
delete [] m_tableName;
286 if(m_structName)
delete [] m_structName;
287 if(m_tableVersion)
delete [] m_tableVersion;
288 if(m_database)
delete [] m_database;
289 if(m_flavor)
delete [] m_flavor;
296 void StDbBroker::printStatistics(){
297 if(m_Tree)m_Tree->printNumberStats();
298 if(mgr) mgr->printTimeStats();
302 void StDbBroker::CloseAllConnections(){
304 mgr->closeAllConnections();
312 void StDbBroker::Release(){
316 StDbBroker::CloseAllConnections();
352 const char *StDbBroker::GetFlavor()
354 return (m_node)? m_node->getFlavor():0;
359 StDbBroker::GetTableDescriptor(){
363 unsigned int numElements = mdescriptor->NumberOfColumns();
365 for(
int i=0;i<(int)numElements;i++){
367 buff.WriteScalar(mdescriptor->ColumnName(i),
"name");
370 if(!mdescriptor->Dimensions(i)){
371 buff.WriteScalar(
"1",
"length");
374 const unsigned int* index = mdescriptor->IndexArray(i);
375 for(
int k=0; k<(int)mdescriptor->Dimensions(i)-1;k++)
377 os<<index[mdescriptor->Dimensions(i)-1];
378 const char* lengthString = (os.str()).c_str();
379 buff.WriteScalar(lengthString,
"length");
385 buff.WriteScalar(i+1,
"position");
388 switch ((EColumnType)mdescriptor->ColumnType(i)) {
391 buff.WriteScalar(
"float",
"type");
396 buff.WriteScalar(
"int",
"type");
401 buff.WriteScalar(
"long",
"type");
406 buff.WriteScalar(
"short",
"type");
411 buff.WriteScalar(
"double",
"type");
416 buff.WriteScalar(
"uint",
"type");
421 buff.WriteScalar(
"ulong",
"type");
426 buff.WriteScalar(
"ushort",
"type");
431 buff.WriteScalar(
"uchar",
"type");
436 buff.WriteScalar(
"char",
"type");
445 descriptor->fillElement(&buff,0);
455 StDbBroker::addBlacklistedDomain(
const char* domainName) {
456 mgr->blacklistDbDomain(domainName);
461 StDbBroker::SetDateTime(Int_t date, Int_t time)
464 m_DateTime[0] = date;
471 int len = (ts.str()).length();
473 for(
int i=0;i<6-len;i++)ds<<
"0";
476 const char* dateTime = (ds.str()).c_str();
477 mgr->setRequestTime(dateTime);
481 void StDbBroker::SetProdTime(UInt_t ptime){
482 if(m_Tree)m_Tree->setProdTime(ptime);
487 void StDbBroker::AddProdTimeOverride(UInt_t ptime,
char*
dbType,
char*
dbDomain) {
489 m_Tree->setProdTimeOverride(ptime, dbType, dbDomain);
494 m_prodTimeOverride.insert( std::make_pair( std::make_pair(dbType,dbDomain), ptime ) );
498 void StDbBroker::SetFlavor(
const char* flavor){
501 m_flavor =
new char[strlen(flavor)+1];
502 strcpy(m_flavor,flavor);
503 if(m_Tree)m_Tree->setFlavor(m_flavor);
508 void StDbBroker::SetTableFlavor(
const char* flavor,
int tabID,
int parID)
510 StDbNode* anode = m_Nodes->getNode(tabID);
514 node->setFlavor(flavor);
519 void * StDbBroker::Use(
int tabID,
int parID)
530 SetBeginDate(19950101);
531 SetBeginTimeStamp(788918400);
533 SetEndDate(20380101);
534 SetEndTimeStamp(2145916799);
540 TD->storeRowSize(m_sizeOfStruct);
543 StDbNode* anode = m_Nodes->getNode(tabID);
546 if(!m_node)
return pData;
547 if(!m_node->hasDescriptor())m_node->setDescriptor(GetTableDescriptor());
553 if(m_node->getDbType()==dbRunLog &&
554 m_node->getDbDomain() != dbStar &&
555 m_runNumber>1000000 ){
556 fetchStatus=UseRunLog(m_node);
558 fetchStatus=mgr->fetchDbTable(m_node);
566 m_endTimeStamp = m_node->getEndTime();
567 thisTime = m_node->getEndDateTime();
568 makeDateTime(thisTime,m_EndDate,m_EndTime);
569 m_nRows= m_node->GetNRows();
570 pData = m_node->GetTableCpy();
572 m_beginTimeStamp = m_node->getBeginTime();
573 thisTime = m_node->getBeginDateTime();
574 makeDateTime(thisTime,m_BeginDate,m_BeginTime);
584 void StDbBroker::makeDateTime(
const char* dateTime,Int_t & iDate,Int_t & iTime){
586 char* tmp1 =
new char[strlen(dateTime)+1];
587 char* tmp2 =
new char[strlen(dateTime)+1];
588 strcpy(tmp1,dateTime);
589 strcpy(tmp2,tmp1); tmp1[8]=
'\0';tmp2+=8;
593 delete [] tmp1; tmp2-=8;
delete [] tmp2;
598 bool StDbBroker::UseRunLog(
StDbTable* table){
600 unsigned int prodTime=table->getProdTime();
602 rq<<
" where runNumber="<<m_runNumber;
605 rq<<
" AND deactive=0 ";
607 rq<<
" AND (deactive=0 OR deactive>="<<prodTime<<
")";
608 rq<<
" AND unix_timestamp(entryTime)<="<<prodTime;
611 bool fetchStatus=mgr->fetchDbTable(table,(
char*)(rq.str()).c_str());
618 Int_t StDbBroker::WriteToDb(
void* pArray,
int tabID){
619 #define __METHOD__ "WriteToDb(pArray,tabID)"
622 if(!pArray || tabID==0) {
623 em<<
" Write Failed -> either data-array or tableID is incomplete";
624 return mgr->printInfo((em.str()).c_str(),dbMErr,__LINE__,__CLASS__,__METHOD__);
627 em<<
"Write Failed -> incomplete table context. Try InitConfig() 1st";
628 return mgr->printInfo((em.str()).c_str(),dbMErr,__LINE__,__CLASS__,__METHOD__);
630 StDbNode* anode= m_Nodes->getNode(tabID);
633 em<<
"Write Failed -> tableID="<<tabID<<
" is not known ";
634 return mgr->printInfo((em.str()).c_str(),dbMErr,__LINE__,__CLASS__,__METHOD__);
637 if(!table->hasDescriptor())table->setDescriptor(GetTableDescriptor());
638 table->
SetTable((
char*)pArray,m_nRows);
642 mgr->setStoreTime(mgr->getUnixCheckTime());
643 if(!mgr->storeDbTable(table))
return 0;
649 Int_t StDbBroker::WriteToDb(
void* pArray,
const char* fullPath,
int* idList){
650 #define __METHOD__ "WriteToDb(pArray,fullPath,idList)"
653 if(!pArray || !fullPath) {
654 em<<
" Write Failed:: either data-array or path is incomplete";
655 return mgr->printInfo((em.str()).c_str(),dbMErr,__LINE__,__CLASS__,__METHOD__);
658 char* path=
new char[strlen(fullPath)+1];
659 strcpy(path,fullPath);
664 if(*tmp==
'/')*tmp=
'\0';
673 char** aword =
new char*[20];
677 while((a2=strstr(a1,
"/"))){
687 for(
int i=icount;i>0;i--){
691 sprintf(tmpName,
"%s_%s",aword[i-1],aword[i]);
692 dbName=(
char*)tmpName;
694 table=findTable(dbName);
699 em<<
"Write Failed table="<<m_tableName<<
" not found in db="<<dbName;
702 return mgr->printInfo((em.str()).c_str(),dbMErr,__LINE__,__CLASS__,__METHOD__);
705 table->setDescriptor(GetTableDescriptor());
706 table->
SetTable((
char*)pArray,m_nRows,idList);
707 mgr->setStoreTime(mgr->getUnixCheckTime());
708 bool iswritten=mgr->storeDbTable(table);
712 return (iswritten) ? 1 : 0;
718 StDbBroker::findTable(
const char* databaseName){
722 StDbTable* tmp=node->addDbTable(m_tableName);
730 void * StDbBroker::Use()
735 date = m_DateTime[0]; time = m_DateTime[1];
736 uint datetime[4]={0,0,0,0};
743 for (i=0;i<m_nElements;i++) {
744 m_descriptor[i].fColumnName[31]=
'\0';
748 char*
id = strstr(m_tableName,
"_hierarchy");
750 char* tmpName =
new char[strlen(m_tableName)+1];
751 strcpy(tmpName,m_tableName);
752 char* id2 = strstr(tmpName,
"_hierarchy");
754 if(strcmp(tmpName,
"Calib")==0){
755 m_database =
new char[strlen(
"Calibrations_tpc")+1];
756 strcpy(m_database,
"Calibrations_tpc");
757 }
else if(strstr(tmpName,
"Geom")){
758 m_database =
new char[strlen(
"Geometry_tpc")+1];
759 strcpy(m_database,
"Geometry_tpc");
760 }
else if(strstr(tmpName,
"RunParam")){
761 m_database =
new char[strlen(
"RunParams_tpc")+1];
762 strcpy(m_database,
"RunParams_tpc");
764 m_database =
new char[strlen(
"params")+1];
765 strcpy(m_database,
"params");
773 if(
id || strcmp(m_database,
"params")==0){
775 cout <<
"Looking for Table "<<m_tableName<<
" In Db= "<<m_database<<endl;
777 pDbData = ::DbUse(&nRows, datetime, (
const char*)m_tableName,(
const char*)m_structName,m_nElements,m_sizeOfStruct,m_descriptor);
781 pDbData = ::DbRead(&nRows, datetime,(
const char*)m_tableName,(
const char*)m_structName,m_nElements,m_sizeOfStruct,m_descriptor,(
const char*)m_database, (
const char*)m_tableVersion );
788 SetBeginDate(19950101);
790 SetEndDate(20380101);
795 SetNRows((UInt_t)nRows);
796 SetBeginDate(datetime[0]);
797 SetBeginTime(datetime[1]);
798 SetEndDate (datetime[2]);
799 SetEndTime (datetime[3]);
807 StDbBroker::InitConfig(
const char* configName,
int& numRows,
char* versionName)
815 if(m_Tree)
delete m_Tree;
818 char* dbDomainName=0;
819 if(m_ParentType)
delete [] m_ParentType;
822 if(mgr->getDataBaseInfo(configName,dbTypeName,dbDomainName)){
823 if(strcmp(dbDomainName,
"Star")!=0){
824 int tlen = strlen(dbTypeName);
825 m_ParentType =
new char[tlen+1];
826 strcpy(m_ParentType,dbTypeName);
830 delete [] dbTypeName;
831 delete [] dbDomainName;
833 if(m_isVerbose)mgr->setVerbose(
true);
835 m_Tree=mgr->initConfig(configName,
"reconV0",1);
837 m_Tree=mgr->initConfig(configName,versionName,1);
841 if(!m_Tree)
return configTable;
844 m_Tree->setProdTime(m_prodTime);
845 for (std::map<std::pair<char*,char*>,
unsigned int>::iterator it = m_prodTimeOverride.begin(); it != m_prodTimeOverride.end(); it++ ) {
851 m_Tree->setProdTimeOverride((*it).second, (*it).first.first, (*it).first.second);
854 if(m_flavor)m_Tree->setFlavor(m_flavor);
857 cout <<
"****************************************************************"<<endl;
858 cout <<
"*** Will Print the Tree "<<endl;
859 bool verbCheck = mgr->IsVerbose();
860 if(!verbCheck)mgr->setVerbose(
true);
861 m_Tree->printTree(0);
862 if(!verbCheck)mgr->setVerbose(
false);
863 cout <<
"*** End Print the Tree "<<endl;
864 cout <<
"****************************************************************"<<endl;
868 if(!buildNodes(m_Tree,0))
return configTable;
871 return buildConfig(numRows);
879 if(!parentNode)
return 0;
882 m_Nodes->addNode(parentNode,0);
888 if( (parentNode->hasData()) ){
890 while( !itr->done() ) { m_Nodes->addNode(itr->next(),pID); }
897 if((parentNode->hasChildren())){
898 next=parentNode->getFirstChildNode();
899 cID=m_Nodes->addNode(next,pID);
900 if(!buildNodes(next, cID))
return 0;
905 if( (next=parentNode->getNextNode()) ){
906 parID=m_Nodes->getParentID(pID);
907 cID=m_Nodes->addNode(next,parID);
908 if(!buildNodes(next, cID))
return 0;
916 StDbBroker::buildConfig(
int& numRows){
920 int numNodes = m_Nodes->getNumNodes();
927 unsigned int parsize=
sizeof(cTab[0].parname)-1;
928 unsigned int tabsize=
sizeof(cTab[0].tabname)-1;
929 unsigned int typsize=
sizeof(cTab[0].tabtype)-1;
936 node = m_Nodes->getNode(0);
937 strncpy(cTab[0].tabname,node->printName(),tabsize);
938 cTab[0].tabname[tabsize]=
'\0';
939 strncpy(cTab[0].tabtype,
".node",typsize);
940 cTab[0].tabtype[typsize]=
'\0';
941 cTab[0].parID=cTab[0].tabID=0;
945 strncpy(cTab[0].parname,m_ParentType,parsize);
947 strncpy(cTab[0].parname,node->printName(),parsize);
949 cTab[0].parname[parsize]=
'\0';
951 for(
int i=1; i<numNodes;i++){
953 node = m_Nodes->getNode(i);
954 parent= m_Nodes->getParent(i);
955 parID = m_Nodes->getParentID(i);
957 nodeName = node->printName();
958 parName = parent->printName();
960 strncpy(cTab[cRow].parname,parName,parsize);
961 cTab[cRow].parname[parsize]=
'\0';
962 strncpy(cTab[cRow].tabname,nodeName,tabsize);
963 cTab[cRow].tabname[tabsize]=
'\0';
965 id=cTab[cRow].tabtype; *
id=
'.';
id++;
967 strcpy(
id,((
StDbTable*)node)->printCstructName());
971 cTab[cRow].tabtype[typsize]=
'\0';
974 cTab[cRow].parID=parID;
979 cout <<
"****************************************************"<<endl;
980 cout <<
"********* Will print dbConfig table "<<endl;
981 for(
int k=0; k<numRows; k++) {
982 cout <<
"row "<<k<<
" name =" << cTab[k].tabname<<
" tid= "<< cTab[k].tabID;
983 cout <<
" type = "<<cTab[k].tabtype;
984 cout <<
" parent =" << cTab[k].parname <<
" pid= " << cTab[k].parID<<endl;
986 cout <<
"********* End print dbConfig table "<<endl;
987 cout <<
"****************************************************"<<endl;
994 int StDbBroker::DbInit(
const char * dbName) { return ::DbInit(dbName) ;}
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc'd version of data for StRoot
static StDbManager * Instance()
strdup(..) is not ANSI