163 #include "StDbSql.hh"
164 #include "StDbManager.hh"
165 #include "StDbDefaults.hh"
166 #include "StDbConfigNodeImpl.hh"
167 #include "StDbTableIter.hh"
168 #include "StDbTable.h"
169 #include "StDbFastSqlWriter.h"
171 #include "stdb_streams.h"
173 #define __CLASS__ "StDbSql"
175 static const char* DbQFailed =
"Query Failed = ";
176 static const char* DbQInfo =
" Query = ";
177 static const char* DbQResult =
" Query Result = ";
197 StDbSql::~StDbSql() {
198 if(mretString)
delete [] mretString;
199 if(mdefaultEndDateTime)
delete [] mdefaultEndDateTime;
212 #define __METHOD__ "QueryDb(StDbConfigNode*)"
215 curNode.setDbName(mdbName);
216 curNode.setDbType(mdbType);
217 curNode.setDbDomain(mdbDomain);
219 int branchID=node->getBranchID();
221 if(!((NodeID)=prepareNode(node)))
return 0;
225 Db<<
"Select subNode.*, NodeRelation.ID as branchID from Nodes ";
226 Db<<
"LEFT JOIN NodeRelation ON Nodes.ID=NodeRelation.ParentID ";
227 Db<<
"LEFT JOIN Nodes as subNode ON NodeRelation.NodeID=subNode.ID ";
228 Db<<
" Where Nodes.ID="<<NodeID;
229 Db<<
" and NodeRelation.BranchID="<<branchID<<endsql;
231 if(!Db.QueryStatus())
232 return sendMess(DbQFailed,Db.printQuery(),dbMWarn,__LINE__,__CLASS__,__METHOD__);
235 sendMess(node->printName(),
" Node has no subnodes",dbMDebug,__LINE__,__CLASS__,__METHOD__);
237 sendMess(DbQInfo,Db.printQuery(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
242 while(Db.Output(&buff)){
243 curNode.setConfigured(
false);
244 if(readNodeInfo(&curNode)){
247 fs<<
"Found "<<curNode.printNodeType()<<
" Node "<<curNode.printName();
248 fs<<
" of parent "<<node->printName();
249 sendMess(DbQResult,(fs.str()).c_str(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
252 if(strcmp(curNode.printNodeType(),
"table")!=0){
255 if(!child->isDbNode())readConfigNodeInfo(child);
259 StDbTable* table = node->addTable(&curNode);
260 readTableInfo(table);
273 #define __METHOD__ "QueryDb(StDbNode*)"
275 if(!prepareNode(node))
return 0;
282 return node->getNodeID();
288 StDbSql::QueryDb(
StDbTable* table,
unsigned int reqTime){
290 #define __METHOD__ "QueryDb(table,time)"
297 char* tName = table->printName();
298 table->clearStoreInfo();
300 if(!((nodeID)=table->getNodeID())){
301 if(!(prepareNode((
StDbNode*)table)))
302 return sendMess(tName,
" Table not found in DB",dbMErr,__LINE__,__CLASS__,__METHOD__);
304 readTableInfo(table);
308 char* checkString=checkTablePrepForQuery(table,
true);
310 return sendMess(tName,checkString,dbMErr,__LINE__,__CLASS__,__METHOD__);
316 bs<<
" Where nodeID="<<nodeID;
318 bs<<
" AND "<<getFlavorQuery(table->printFlavor());
320 bs<<
" AND "<<getProdTimeQuery(table->getProdTime());
324 string baseString = bs.str();
329 int* elementID=table->getElementID(numRows);
331 return sendMess(tName,
"doesn't have an Element List",dbMErr,__LINE__,__CLASS__,__METHOD__);
333 char* elementString=getElementList(elementID,numRows);
334 char* dataTable=getDataTable(table,reqTime);
337 Db <<
" select beginTime + 0 as mendDateTime, ";
338 Db <<
" unix_timestamp(beginTime) as mendTime from "<<dataTable;
339 Db << baseString <<
" AND beginTime>from_unixtime("<<reqTime<<
")";
341 Db <<
" And elementID "<<elementString;
342 Db <<
" Order by beginTime limit 1"<<endsql;
344 sendMess(DbQInfo,Db.printQuery(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
346 if(!Db.QueryStatus())
347 return sendMess(DbQFailed,Db.printQuery(),dbMWarn,__LINE__,__CLASS__,__METHOD__);
349 if(Db.NbRows()==1 && Db.Output(&buff)){
352 buff.ReadScalar(edTime,
"mendDateTime");
353 buff.ReadScalar(eTime,
"mendTime");
354 table->setEndTime(eTime);
355 table->setEndTime(edTime);
356 if(edTime)
delete [] edTime;
359 setDefaultEndTime(table);
367 for(i=0;i<numRows;i++)if(elementID[i]>maxID)maxID=elementID[i];
368 int* idMap =
new int[maxID+1];
369 int* dataIDList =
new int[numRows];
370 unsigned int* timeValues =
new unsigned int[numRows];
371 for(i=0;i<numRows;i++){
372 idMap[elementID[i]]=i;
385 int rowsLeft=numRows;
389 Db <<
" select unix_timestamp(beginTime) as bTime,"<<dataTable<<
".* from ";
390 Db << dataTable << baseString;
391 Db <<
" AND beginTime<=from_unixtime("<<reqTime<<
")";
393 Db <<
" AND elementID "<<elementString;
394 Db <<
" Order by beginTime desc limit "<< rowsLeft <<endsql;
396 sendMess(DbQInfo,Db.printQuery(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
398 if(!Db.QueryStatus()){
399 sendMess(DbQFailed,Db.printQuery(),dbMWarn,__LINE__,__CLASS__,__METHOD__);
403 if(Db.NbRows()==0)
break;
406 while(Db.Output(&buff)){
407 buff.ReadScalar(eID,
"elementID");
409 if(!dataIDList[idMap[eID]]){
410 buff.ReadScalar(dataIDList[idMap[eID]],
"dataID");
411 buff.ReadScalar(timeValues[idMap[eID]],
"bTime");
412 table->setRowNumber(idMap[eID]);
413 table->dbStreamer(&buff,
true);
419 rowsLeft=rowsLeft-numRowsFound;
421 if(!done && (rowsLeft>0)){
422 int* elementsLeft =
new int[rowsLeft];
424 for(i=0;i<numRows;i++){
426 elementsLeft[j]=elementID[i];
430 elementString = getElementListIN(elementsLeft,rowsLeft);
431 delete [] elementsLeft;
438 if(rowsLeft==numRows){
439 sendMess(tName,
" has No data for query",dbMWarn,__LINE__,__CLASS__,__METHOD__);
440 setDefaultBeginTime(table,reqTime);
442 }
else if(rowsLeft>0){
444 tp<<
" Not all rows filled from DB, Requested="<<numRows;
445 tp<<
" Returned="<<numRows-rowsLeft<<
" for Table="<<tName;
446 mgr->printInfo((tp.str()).c_str(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
452 table->addWrittenRows(dataIDList,numRows);
455 tp<<
"Fixing row size, setting it to " << (numRows-rowsLeft) <<
" for Table = " << tName;
456 mgr->printInfo((tp.str()).c_str(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
457 table->resizeNumRows(numRows-rowsLeft);
459 table->setTimeValues(timeValues);
460 unsigned int t1=table->getMaxTime();
461 table->setBeginTime(t1);
462 char* dt=getDateTime(t1);
463 table->setBeginTime(dt);
delete [] dt;
464 table->setRowNumber();
466 delete [] timeValues;
472 if(retVal) retVal=(int)updateEndTime(table,dataTable,reqTime);
475 delete [] dataIDList;
483 bool StDbSql::checkColumn(
const char* tableName,
const char* columnName){
486 Db<<
"show columns from "<<tableName<<
" like '"<<columnName<<
"'"<<endsql;
487 if(Db.NbRows()==1)retVal=
true;
493 bool StDbSql::updateEndTime(
StDbTable* table,
const char* dataTable,
unsigned int requestTime){
501 #define __METHOD__ "updateEndTime(table,table,time)"
503 if(!checkColumn(dataTable,
"endTime"))
return retVal;
505 int* wrows = table->getWrittenRows(nrows);
510 Db<<
" select unix_timestamp(Min(endTime)) as mendTime from "<<dataTable;
512 Db<<
" where dataID "<<getElementList(wrows,nrows)<<endsql;
514 sendMess(DbQInfo,Db.printQuery(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
517 if( (Db.NbRows()>0) && Db.Output(&buff)){
518 unsigned int t1 = table->getEndTime();
520 buff.ReadScalar(t2,
"mendTime");
521 if(t2<t1)table->setEndTime(t2);
522 if(t2<requestTime) retVal=
false;
533 StDbSql::QueryDb(
StDbTable* table,
const char* whereClause){
535 #define __METHOD__ "QueryDb(StDbTable*, const char* where)"
537 unsigned int* timeSet=QueryDbTimes(table,whereClause);
538 if(!timeSet)
return 0;
540 return table->GetNRows();
546 StDbSql::QueryDbTimes(
StDbTable* table,
const char* whereClause,
int opt){
548 #define __METHOD__ "QueryDb(StDbTable*, const char* where)"
561 unsigned int* retVal=0;
562 char* tName=table->printName();
564 char* checkString=checkTablePrepForQuery(table);
566 sendMess(tName,checkString,dbMErr,__LINE__,__CLASS__,__METHOD__);
570 setDefaultReturnValues(table,0);
571 int numRows = table->getRowLimit();
573 int numRowsReturned=0;
577 table->setElementID((
int*)retVal,0);
578 table->setRowNumber();
580 char** dataTables=getDataTables(table,numTables);
582 for(i=0;i<numTables;i++){
585 if(!opt)columnList=getColumnList(table,dataTables[i]);
586 if(!opt && !columnList){
587 sendMess(tName,
" has no elements?",dbMErr,__LINE__,__CLASS__,__METHOD__);
591 Db<<
" select unix_timestamp("<<dataTables[i]<<
".beginTime) as bTime,";
593 if(!opt)Db<<
","<<columnList;
594 Db<<
" from "<<dataTables[i]<<
" "<<whereClause;
595 if(numRows)Db<<
" limit "<<numRows;
597 sendMess(DbQInfo,Db.printQuery(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
598 if(!Db.QueryStatus()){
599 sendMess(DbQFailed,Db.printQuery(),dbMWarn,__LINE__,__CLASS__,__METHOD__);
603 int retRows=Db.NbRows();
604 if(retRows==0)
continue;
606 int* elements =
new int[retRows];
607 int* dataIDList =
new int[retRows];
608 unsigned int* timeList =
new unsigned int[retRows];
610 table->addNRows(retRows);
612 table->resizeElementID(retRows+table->GetNRows());
617 while(Db.Output(&buff)){
618 buff.ReadScalar(timeList[j],
"bTime");
619 buff.ReadScalar(elements[j],
"elementID");
620 buff.ReadScalar(dataIDList[j],
"dataID");
621 if(!opt)table->dbStreamer(&buff,
true);
622 if(timeList[j]>t1)t1=timeList[j];
626 table->addNElements(elements,retRows);
627 table->addWrittenRows(dataIDList,retRows);
628 table->setBeginTime(t1);
630 unsigned int* tmpRet=
new unsigned int[numRowsReturned+retRows];
632 memcpy(tmpRet,retVal,numRowsReturned*
sizeof(
int));
635 tmpRet+=numRowsReturned;
636 memcpy(tmpRet,timeList,retRows*
sizeof(
int));
638 numRowsReturned+=retRows;
641 if(table->IsIndexed() && t1>0 && !opt){
642 Db<<
" select unix_timestamp(beginTime) as eTime from "<<dataTables[i];
643 Db<<
" where beginTime>from_unixtime("<<t1<<
")";
645 Db<<
" and elementID "<<getElementList(elements,retRows);
646 Db<<
" Order by beginTime limit 1"<<endsql;
648 sendMess(DbQInfo,Db.printQuery(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
650 if(Db.Output(&buff)){
651 unsigned int eTime=0;
652 if(buff.ReadScalar(eTime,
"eTime") && eTime && eTime<table->getEndTime())table->setEndTime(eTime);
658 delete [] dataIDList;
662 for(i=0;i<numTables;i++)
delete [] dataTables[i];
663 delete [] dataTables;
665 char* dateTime=getDateTime(table->getBeginTime());
666 table->setBeginTime(dateTime);
if(dateTime)
delete [] dateTime;
667 dateTime=getDateTime(table->getEndTime());
668 table->setEndTime(dateTime);
if(dateTime)
delete [] dateTime;
669 table->setRowNumber();
677 StDbSql::QueryDbFunction(
StDbTable* table,
const char* whereClause,
char* funcName) {
678 #define __METHOD__ "QueryDbFunction(table,whereClause,functionName)"
685 char* checkString=checkTablePrepForQuery(table);
687 return sendMess(table->printName(),checkString,dbMErr,__LINE__,__CLASS__,__METHOD__);
690 int numRowsReturned=0;
692 char** dataTables=getDataTables(table,numTables);
694 for(i=0;i<numTables;i++){
696 char* columnList=getColumnList(table,dataTables[i],funcName);
697 if(!columnList)
return 0;
699 Db<<
" select "<<columnList<<
" from "<<dataTables[i];
700 Db<<
" "<<whereClause<<endsql;
702 sendMess(DbQInfo,Db.printQuery(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
704 if(!Db.QueryStatus())
705 return sendMess(DbQFailed,Db.printQuery(),dbMWarn,__LINE__,__CLASS__,__METHOD__);
707 int retRows=Db.NbRows();
708 if(retRows==0)
continue;
709 numRowsReturned+=retRows;
711 while(Db.Output(&buff)){ table->dbStreamer(&buff,
true); buff.Raz(); }
715 for(i=0;i<numTables;i++)
delete [] dataTables[i];
716 delete [] dataTables;
717 table->setRowNumber();
719 return numRowsReturned;
725 StDbSql::WriteDb(
StDbTable* table,
unsigned int storeTime){
727 #define __METHOD__ "WriteDb(StDbTable*,uint storeTime)"
730 char* tName=table->printName();
732 if(!table->hasData())
733 return sendMess(tName,
" has no data to store",dbMWarn,__LINE__,__CLASS__,__METHOD__)+1;
736 if(!((nodeID)=table->getNodeID()))
737 if(!(prepareNode((
StDbNode*)table)))
738 return sendMess(tName,
" Not found in DB",dbMErr,__LINE__,__CLASS__,__METHOD__);
740 readTableInfo(table);
744 if(table->IsBaseLine() && hasInstance(table))
745 return sendMess(
"BaseLine instance already exists",tName,dbMErr,__LINE__,__CLASS__,__METHOD__);
747 if(!QueryDescriptor(table))
748 return sendMess(tName,
" doesn't have a descriptor",dbMErr,__LINE__,__CLASS__,__METHOD__);
751 table->setRowNumber();
752 if(!table->IsIndexed())
return WriteDbNoIndex(table,storeTime);
755 if(!((dataTable)=getDataTable(table,storeTime)))
756 return sendMess(tName,
" has no storage table",dbMErr,__LINE__,__CLASS__,__METHOD__);
759 int* elements = table->getElementID(numRows);
762 return sendMess(tName,
"doesn't have an Element List",dbMErr,__LINE__,__CLASS__,__METHOD__);
764 int* storedData =
new int[numRows];
765 memset(storedData,0,numRows*
sizeof(
int));
766 char* sTime = getDateTime(storeTime);
774 table->clearStoreInfo();
775 table->setDataTable(dataTable);
778 if( table->getEndStoreTime() && checkColumn(dataTable,
"endTime"))
779 eTime=getDateTime(table->getEndStoreTime());
781 for(
int i=0;i<numRows;i++){
783 buff.WriteScalar(nodeID,
"nodeID");
784 buff.WriteScalar(table->getSchemaID(),
"schemaID");
785 buff.WriteScalar(sTime,
"beginTime");
786 buff.WriteScalar(elements[i],
"elementID");
787 if(eTime)buff.WriteScalar(eTime,
"endTime");
788 if(!table->defaultFlavor())buff.WriteScalar(table->printFlavor(),
"flavor");
789 table->dbStreamer(&buff,
false);
791 if(!Db.Input(dataTable,&buff)){
792 deleteRows(dataTable,storedData,i);
796 storedData[i]=Db.GetLastInsertID();
801 if(rowsWritten==numRows)table->addWrittenRows(storedData,numRows,
true);
803 delete [] storedData;
805 if(eTime)
delete [] eTime;
807 table->setRowNumber();
810 if(!retVal)sendMess(
" Write failed for table=",tName,dbMWarn,__LINE__,__CLASS__,__METHOD__);
817 StDbSql::WriteDbNoIndex(
StDbTable* table,
unsigned int storeTime){
818 #define __METHOD__ "WriteDbNoIndex(table,storeTime)"
822 if(!((dataTable)=getDataTable(table,storeTime)))
823 return sendMess(table->printName(),
" has no storage table",dbMErr,__LINE__,__CLASS__,__METHOD__);
826 cList<<
"beginTime,"<<getColumnList(table);
828 char* sTime=getDateTime(storeTime);
830 int numRows=table->GetNRows();
831 char* colList =
new char[strlen((cList.str()).c_str())+1];
832 strcpy(colList,(cList.str()).c_str());
835 bool hasBinary=
false;
836 if(Db.InputStart(dataTable,&buff,colList,numRows,hasBinary)){
840 cout<<
" In Binary write???"<<endl;
842 for(i=0;i<numRows;i++){
843 buff.WriteScalar(sTime,
"beginTime");
844 table->dbStreamerWrite(&buff);
845 if(!Db.InputRow(&buff,i))
break;
847 if( i==numRows && Db.InputEnd() ){
853 table->setBeginTime(storeTime);
856 writer.ioTable(table);
868 table->setRowNumber();
874 if(!retVal)sendMess(
" Write failed for Non-Indexed table=",table->printName(),dbMWarn,__LINE__,__CLASS__,__METHOD__);
882 StDbSql::QueryDescriptor(
StDbTable* table){
883 #define __METHOD__ "QueryDescriptor(StDbTable*)"
889 if(table->hasDescriptor())
return 1;
893 Db<<
" SELECT structure.lastSchemaID, structure.ID from structure left join Nodes on structure.name=Nodes.structName";
894 Db<<
" WHERE Nodes.name='"<<table->printName() <<
"'"<<endsql;
896 if(!Db.Output(&buff))
return 0;
900 buff.ReadScalar(schemaID,
"lastSchemaID");
901 buff.ReadScalar(structID,
"ID");
905 if(!(requestSchemaID=table->getSchemaID())){
906 requestSchemaID=schemaID;
907 table->setSchemaID(schemaID);
911 table->setDescriptor(descriptor);
913 if(descriptor->IsValid())
return 1;
915 Db<<
"SELECT `schema`.schemaID, `schema`.name, `schema`.type, `schema`.length, ";
916 Db<<
"`schema`.position from `schema` WHERE `schema`.structID="<<structID;
917 Db<<
" AND `schema`.schemaID="<<requestSchemaID;
918 Db<<
" ORDER by `schema`.position"<<endsql;
920 sendMess(DbQInfo,Db.printQuery(),dbMDebug,__LINE__,__CLASS__,__METHOD__);
922 if(!Db.QueryStatus())
923 return sendMess(DbQFailed,Db.printQuery(),dbMWarn,__LINE__,__CLASS__,__METHOD__);
927 deleteDescriptor(structID,requestSchemaID);
931 while(Db.Output(&buff)){
932 descriptor->fillElement(&buff,requestSchemaID);
936 descriptor->endRowPadding();
938 addDescriptor(descriptor);
946 StDbSql::WriteDb(
StDbConfigNode* node,
int parentID,
int& configID){
947 #define __METHOD__ "WriteDb(node,parentID,configID)"
951 char* nName = node->printName();
955 if(strcmp(node->printNodeType(),
"Config")!=0)
956 return sendMess(
"No Config tag for new config=",nName,dbMErr,__LINE__,__CLASS__,__METHOD__);
958 if(!node->printVersion())
959 return sendMess(
"No version label for new config=",nName,dbMErr,__LINE__,__CLASS__,__METHOD__);
962 node->setNodeType(
"directory");
966 if(!((nodeID)=storeConfigNode(node)))
967 return sendMess(
" Could not store ",nName,dbMErr,__LINE__,__CLASS__,__METHOD__);
971 insertNodeRelation(configID,parentID,nodeID);
973 configID=node->getNodeID();
982 table->setNodeType(
"table");
983 if(!((childID)=storeTableNode(table)))
984 return sendMess(
" Could not store table in Node=",nName,dbMErr,__LINE__,__CLASS__,__METHOD__);
985 insertNodeRelation(configID,nodeID,childID);
994 StDbSql::deleteRows(
const char* tableName,
int* rowID,
int nrows){
996 if(!rowID || nrows==0)
return;
997 Db<<
" delete from "<<tableName;
998 Db<<
" where dataID In("<<getElementList(rowID,nrows)<<
")"<<endsql;
1005 if(!(node->canRollBack()) || !(node->getNodeID()))
return false;
1006 Db<<
"delete from Nodes where ID="<<node->getNodeID()<<endsql;
1007 return Db.QueryStatus();
1015 int* numWrittenRows = table->getWrittenRows(numRows);
1016 char* dataTable = table->getDataTable();
1017 char* elementList = getElementList(numWrittenRows,numRows);
1019 Db<<
"delete from "<<dataTable<<
" where dataID In("<<elementList<<
")"<<endsql;
1022 bool retVal=Db.QueryStatus();
1024 delete [] dataTable;
1031 StDbSql::selectElements(
const char* elementName,
StDbElementIndex* inval,
int& numElements){
1038 int* retElements = 0;
1040 if(!elementName)
return retElements;
1041 if(strcmp(elementName,
"None")==0){
1042 retElements =
new int[1]; retElements[0]=0;
1046 int numIndeces = inval->getNumIndeces();
1049 Db<<
" select elementID from "<<elementName<<
"IDs";
1051 Db<<
" where "<<inval->printIndexName(0)<<
"="<<inval->getIndexVal(0);
1052 for(
int i=1; i<numIndeces; i++)
1053 Db<<
" AND "<<inval->printIndexName(i)<<
"="<<inval->getIndexVal(i);
1056 numElements=Db.NbRows();
1057 if(numElements<=0)
return retElements;
1058 retElements =
new int[numElements];
1060 while(Db.Output(&buff)){
1061 buff.ReadScalar(retElements[j],
"elementID");
1071 StDbSql::getIndexNames(
const char* elementName,
int& numIndexes){
1073 Db<<
"select * from elementIndexes ";
1074 Db<<
" where elementName='"<<elementName<<
"'"<<endsql;
1076 char** indexNames = 0;
1077 if(!((numIndexes)=Db.NbRows()))
return indexNames;
1079 indexNames =
new char*[numIndexes];
1081 while(Db.Output(&buff)){
1082 buff.ReadScalar(indexNames[i],
"indexName");
1093 StDbSql::findDescriptor(
int structID,
int schemaID){
1096 for(DescList::iterator itr = mdescriptors.begin();
1097 itr != mdescriptors.end(); ++itr){
1098 if( ((*itr)->getSchemaID()==schemaID) && ((*itr)->getStructID()==structID)){
1108 StDbSql::getDescriptor(
int structID,
int schemaID){
1123 StDbSql::deleteDescriptors() {
1124 for(
auto &it : mdescriptors )
delete it;
1125 mdescriptors.clear();
1130 StDbSql::deleteDescriptor(
int structID,
int schemaID) {
1133 for(DescList::iterator itr = mdescriptors.begin();
1134 itr != mdescriptors.end(); ++itr){
1135 if(structID==(*itr)->getStructID() && schemaID==(*itr)->getSchemaID()){
1137 mdescriptors.erase(itr);
1146 StDbSql::setDefaultReturnValues(
StDbTable* table,
unsigned int reqTime){
1148 setDefaultBeginTime(table,reqTime);
1149 setDefaultEndTime(table);
1150 table->clearStoreInfo();
1154 void StDbSql::setDefaultBeginTime(
StDbTable* table,
unsigned int reqTime){
1156 char* stime=getDateTime(reqTime);
1157 table->setBeginTime(reqTime);
1158 table->setBeginTime(stime);
1159 if(stime)
delete [] stime;
1163 void StDbSql::setDefaultEndTime(
StDbTable* table){
1164 if(!mdefaultEndDateTime)initEndTime();
1165 table->setEndTime(mdefaultEndDateTime);
1166 table->setEndTime(mdefaultEndTime);
1170 void StDbSql::initEndTime(){
1171 mdefaultEndTime=StDbDefaults::Instance()->getEndTime();
1172 mdefaultEndDateTime=getDateTime(mdefaultEndTime);
1177 StDbSql::prepareNode(
StDbNode* dbNode){
1183 if( (strcmp(dbNode->printNodeType(),
"DB")!=0) &&
1184 (dbNode->IsConfigured()))
return dbNode->getNodeID();
1186 dbNode->setDbName(mdbName);
1187 dbNode->setDbType(mdbType);
1188 dbNode->setDbDomain(mdbDomain);
1190 return queryNode(dbNode);;
1195 StDbSql::queryNode(
StDbNode* node){
1199 Db<<
"Select * from Nodes where Nodes.name='"<<node->printName()<<
"'";
1200 Db<<
" AND Nodes.versionKey='"<<node->printVersion()<<
"'"<<endsql;
1202 if(Db.Output(&buff) && readNodeInfo(node))retVal=node->getNodeID();
1203 if(!retVal) clear();
1210 StDbSql::readNodeInfo(
StDbNode* node){
1215 if(!buff.ReadScalar(nodeID,
"ID"))
return false;
1216 if(!buff.ReadScalar(tmpString,
"nodeType"))
return false;
1217 node->setNodeType(tmpString);
1218 delete [] tmpString;
1221 if(!buff.ReadScalar(tmpString,
"name"))
return false;
1222 node->setName(tmpString);
1223 delete [] tmpString;
1226 if(!buff.ReadScalar(tmpString,
"versionKey"))
return false;
1227 node->setVersion(tmpString);
1228 delete [] tmpString;
1231 node->setNodeID(nodeID);
1232 node->setConfigured(
true);
1242 if(!buff.ReadScalar(iname,
"indexName"))
return false;
1243 if(strcmp(iname,
"None")!=0){
1245 buff.ReadScalar(
id,
"indexVal");
1246 node->setElementIndexInfo(iname,
id);
1251 buff.ReadScalar(branchID,
"branchID");
1252 node->setBranchID(branchID);
1259 StDbSql::readTableInfo(
StDbTable* table){
1263 if(!buff.ReadScalar(tmpString,
"structName"))
return false;
1264 table->setCstructName(tmpString);
delete [] tmpString;
1268 if(buff.ReadScalar(tmpString,
"elementID")){
1269 hy=strstr(tmpString,
"-");
1273 int first=atoi(tmpString);
1274 int last = atoi(hy);
1277 int len = last-first+1;
1278 int * tmpElements =
new int[len];
1281 for(k=first; k<=last;k++){
1285 table->setElementID(tmpElements,len);
1286 delete [] tmpElements;
1289 delete [] tmpString;
1291 if(!buff.ReadScalar(tmpString,
"indexName"))
return false;
1292 table->setElementName(tmpString);
1294 delete [] tmpString;
1299 table->setBaseLine(checkValue(
"baseLine",
"Y"));
1300 table->setBinary(checkValue(
"isBinary",
"Y"));
1301 table->setIndexed(checkValue(
"isIndexed",
"Y"));
1303 table->setDbName(mdbName);
1304 table->setDbType(mdbType);
1305 table->setDbDomain(mdbDomain);
1312 StDbSql::insertNodeString(
StDbNode* node){
1315 dqs<<
"insert into Nodes set name='"<<node->printName()<<
"' ";
1316 if(!StDbDefaults::Instance()->IsDefaultVersion(node->printVersion()))
1317 dqs<<
", versionKey='"<<node->printVersion()<<
"' ";
1319 dqs<<
", nodeType='"<<node->printNodeType()<<
"'";
1321 return mRetString(dqs);
1330 Db<<insertNodeString((
StDbNode*) node);
1332 if(node->getNumIndeces()){
1333 char* ename=0;
int eid;
1334 node->getElementIndexInfo(ename,eid);
1336 Db<<
", indexName='"<<ename<<
"'";
1337 Db<<
", indexVal="<<eid;
1343 if(Db.QueryStatus())retVal=Db.GetLastInsertID();
1351 StDbSql::storeTableNode(
StDbTable* table){
1354 if(!table->printCstructName())
return retVal;
1356 Db<<insertNodeString((
StDbNode*) table);
1357 Db<<
", structName='"<<table->printCstructName()<<
"'";
1359 if(table->IsBaseLine())Db<<
", baseLine='Y'";
1360 if(table->IsBinary())Db<<
", isBinary='Y'";
1361 if(!table->IsIndexed())Db<<
", isIndexed='N'";
1365 if(Db.QueryStatus())retVal=Db.GetLastInsertID();
1373 StDbSql::insertNodeRelation(
int configID,
int parent,
int child){
1375 Db<<
" insert into NodeRelation set ParentID="<<parent;
1376 Db<<
", NodeID="<<child<<
", ConfigID="<<configID<<endsql;
1378 bool retVal=Db.QueryStatus();
1386 StDbSql::getUnixTime(
const char* time){
1388 unsigned int retVal = 0;
1390 Db<<
"select unix_timestamp('"<<time<<
"') as requestTime"<<endsql;
1391 if(Db.Output(&buff)) buff.ReadScalar(retVal,
"requestTime");
1399 StDbSql::getDateTime(
unsigned int time){
1406 Db<<
"select from_unixtime("<<time<<
") + 0 as requestTime"<<endsql;
1407 if(Db.Output(&buff)) buff.ReadScalar(retVal,
"requestTime");
1415 StDbSql::checkValue(
const char* colName,
const char* colValue){
1417 bool retVal =
false;
1419 if(buff.ReadScalar(tmpS,colName) && strcmp(tmpS,colValue)==0)retVal=
true;
1420 if(tmpS)
delete [] tmpS;
1427 StDbSql::getFlavorQuery(
const char* flavor){
1431 char *id1,*id2,*id3;
1432 id1 =
new char[strlen(flavor)+1];
1438 while(( (id2)=strstr(id3,
"+")) ){
1448 return mRetString(fs);
1453 StDbSql::getProdTimeQuery(
unsigned int prodTime){
1460 pt<<
" (deactive=0 OR deactive>="<<prodTime<<
")";
1461 pt<<
" AND unix_timestamp(entryTime)<="<<prodTime;
1463 return mRetString(pt);
1468 StDbSql::getElementList(
int* e,
int num){
1475 if (!e || num == 0) {
1477 }
else if (num == 1) {
1478 es <<
" = " << e[0];
1481 bool ordered =
true;
1482 for (
int i = 0; i < num; i++) {
1483 if (e[i] != (e[0]+i)) {
1488 if (ordered ==
true) {
1489 es<<
" BETWEEN "<<e[0]<<
" AND "<< (e[0] + num - 1);
1492 return getElementListIN(e,num);
1496 return mRetString(es);
1500 StDbSql::getElementListIN(
int* e,
int num){
1510 for(
int i=0;i<num-1;i++) es<<e[i]<<
",";
1514 return mRetString(es);
1519 StDbSql::getColumnList(
StDbTable* table,
char* tableName,
char* funcName){
1522 int numElements=desc->getNumElements();
1527 for(
int i=0;i<numElements;i++){
1528 if(funcName && (desc->getElementLength(i)>1))
continue;
1529 char* name=desc->getElementName(i);
1530 if(funcName)es<<funcName<<
"(";
1531 if(tableName)es<<tableName<<
".";
1533 if(funcName)es<<
") as "<<name;
1534 if(i<(numElements-1))es<<
", ";
1539 if(icount)
return mRetString(es);
1545 StDbSql::getEmptyString(){
1548 return mRetString(es);
1554 StDbSql::checkTablePrepForQuery(
StDbTable* table,
bool checkIndexed){
1557 delete [] mretString;
1558 mretString=
new char[256];
1559 }
else{mretString=
new char[256];}
1561 if(!QueryDescriptor(table))
1562 return strcpy(mretString,
" doesn't have a descriptor ");
1564 if(table->IsBinary())
1565 return strcpy(mretString,
" Binary Store is currently disabled ");
1567 if(checkIndexed && !table->IsIndexed())
1568 return strcpy(mretString,
" Table is not time indexed");
1570 delete [] mretString;
1571 return mretString=0;
1576 StDbSql::checkForNull(
const char* src){
1577 return (strstr(src,
"null")) ?
true :
false;
1582 StDbSql::getDataTable(
StDbTable* table,
unsigned int time){
1584 if(mtableCatalog==0) checkTableCatalog();
1585 if(mtableCatalog==1)
return table->getCstructName();
1590 char* tableName=table->printName();
1592 Db<<
" select * from tableCatalog where nodeName='"<<tableName<<
"'";
1593 Db<<
" AND unix_timestamp(beginTime)<="<<time;
1594 Db<<
" Order by beginTime desc limit 1"<<endsql;
1596 if(Db.Output(&buff))buff.ReadScalar(retVal,
"tableName");
1599 return (retVal) ? retVal : table->getCstructName();
1604 StDbSql::getDataTables(
StDbTable* table,
int& numTables){
1607 if(mtableCatalog==0) checkTableCatalog();
1608 if(mtableCatalog==1){
1610 retVal=
new char*[1];
1611 retVal[0]=table->getCstructName();
1617 char* tableName=table->printName();
1618 Db<<
" select * from tableCatalog where nodeName='"<<tableName<<
"'"<<endsql;
1620 if(!((numTables)=Db.NbRows()) ){
1622 retVal=
new char*[numTables];
1623 retVal[0]=
new char[strlen(tableName)+1];
1624 strcpy(retVal[0],tableName);
1628 retVal=
new char*[numTables];
1630 while(Db.Output(&buff))
if(buff.ReadScalar(retVal[i],
"tableName"))i++;
1646 char** dataTables=getDataTables(table,numTables);
1647 if(numTables==0)
return retVal;
1649 for(
int i=0; i<numTables;i++){
1651 Db<<
" select * from "<<dataTables[i];
1652 Db<<
" where "<<getFlavorQuery(table->printFlavor());
1653 Db<<
" AND "<<getProdTimeQuery(table->getProdTime())<<endsql;
1655 if(Db.NbRows() !=0 ){
1660 for(
int k=0;k<numTables;k++)
delete [] dataTables[k];
1661 delete [] dataTables;
1667 StDbSql::checkTableCatalog(){
1670 Db<<
"show tables like 'tableCatalog'"<<endsql;
static StDbManager * Instance()
strdup(..) is not ANSI