14 #include <boost/algorithm/string.hpp> 15 #include <boost/algorithm/string/case_conv.hpp> 16 #include <boost/date_time/posix_time/posix_time.hpp> 25 LibwdaSentry() { wda_global_init(); }
26 ~LibwdaSentry() { wda_global_cleanup(); }
39 fMaxTSVld(0),fMinTSVld(0),
76 char* tmpStr = getenv(
"DBITIMEOUT");
78 int tmpTO = atoi(tmpStr);
85 const char* wsHost = getenv(
"DBIWSURL");
86 if (wsHost)
fWSURL = std::string(wsHost);
89 const char* ucondbHost = getenv(
"DBIUCONDBURL");
90 if (ucondbHost)
fUConDBURL = std::string(ucondbHost);
93 const char* qeHost = getenv(
"DBIQEURL");
94 if (qeHost)
fQEURL = std::string(qeHost);
97 tmpStr = getenv(
"DBIVERB");
107 std::string dbhost, std::string dbname,
108 std::string dbport, std::string dbuser)
126 char* tmpStr = getenv(
"DBIVERB");
136 tmpStr = getenv(
"DBITIMEOUT");
138 int tmpTO = atoi(tmpStr);
159 if (!dbuser.empty())
SetUser(dbuser);
162 fSchema = std::string(schemaName);
165 std::string stName =
fSchema + std::string(
".") + std::string(tableName);
169 errStr =
"Table::Table(): table \'" + stName +
"\' not found in database!";
170 throw std::runtime_error(errStr);
182 std::vector<std::string> pkeyList;
184 std::string cmd =
"SELECT pg_attribute.attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) FROM pg_index, pg_class, pg_attribute WHERE indrelid = pg_class.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = any(pg_index.indkey) AND indisprimary AND pg_class.oid = '" + stName +
"'::regclass";
188 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
189 errStr =
"Table::Table(): command failed: " + std::string(PQerrorMessage(
fConnection));
191 std::cerr << errStr << std::endl;
194 throw std::runtime_error(errStr);
197 int nRow = PQntuples(res);
199 errStr = std::string(
"Table::Table(): no primary keys defined for table \'") + tableName + std::string(
"\', unable to proceed.");
203 throw std::runtime_error(errStr);
206 for (
int i=0; i<nRow; ++i) {
207 std::string key = std::string(PQgetvalue(res,i,0));
208 pkeyList.push_back(key);
220 tmpStr = getenv(
"DBICACHEDIR");
224 tmpStr = getenv(
"PWD");
248 std::cerr <<
"Table::GetColsFromDB() currently disabled for unstructured conditions tables." << std::endl;
259 std::string cmd =
"SELECT column_name, data_type from information_schema.columns where table_name = \'" + std::string(
fTableName);
262 cmd +=
"\' and table_schema=\'" +
fSchema +
"\'";
266 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
267 std::string errStr =
"Table::Table() command failed: " + std::string(PQerrorMessage(
fConnection));
269 std::cerr << errStr << std::endl;
273 throw std::runtime_error(errStr);
276 int nRow = PQntuples(res);
278 for (
int i=0; i<nRow; ++i) {
279 std::string cname = std::string(PQgetvalue(res,i,0));
280 std::string ctype = std::string(PQgetvalue(res,i,1));
283 if (cname ==
"__snapshot_id")
continue;
284 if (cname ==
"__tr")
continue;
285 if (cname ==
"__channel")
continue;
286 if (cname ==
"__tv")
continue;
289 if (ctype ==
"smallint") ctype=
"short";
290 else if (ctype ==
"double precision") ctype=
"double";
291 else if (ctype ==
"boolean") ctype=
"bool";
292 else if (ctype ==
"timestamp without time zone") ctype=
"timestamp";
293 else if (ctype.substr(0,7) ==
"varchar" || ctype ==
"text")
299 cmd =
"SELECT pg_get_serial_sequence(\'" + stName +
300 "\',\'" + cname +
"\')";
302 int nRow2 = PQntuples(res2);
303 for (
int j=0; j<nRow2; ++j) {
304 std::string tStr = std::string(PQgetvalue(res2,j,0));
305 if (tStr !=
"") ctype =
"auto_incr";
313 if (find(pkeyList.begin(),pkeyList.end(),cname) != pkeyList.end()) {
335 for (
size_t i=0; i<
fCol.size(); ++i) {
337 std::cerr <<
"Table::AddCol: column \'" << cname <<
"\' already exists! Fatal, aborting..." << std::endl;
344 fCol.push_back(cdef);
351 return fCol.size()-1;
362 for (
unsigned int i=0; i<
fCol.size(); ++i) {
363 if (
fCol[i].
Name() ==
"inserttime" ||
364 fCol[i].Name() ==
"insertuser" ||
365 fCol[i].Name() ==
"updatetime" ||
366 fCol[i].Name() ==
"updateuser" )
continue;
367 if (!
fCol[i].CanBeNull())
394 if (i < 0)
return false;
398 if (j >=
fRow.size())
return false;
401 for (
unsigned int k=0; k<kEnd; ++k)
415 if (i >= 0 && i < (
int)
fRow.size())
427 for (
unsigned int i=0; i<
fNullList.size(); ++i)
429 std::cerr <<
fCol[
fNullList[i].second].Name() <<
" is NULL in row " 443 fout << cmd << std::endl;
496 std::cerr << PQerrorMessage(
fConnection) << std::endl;
525 boost::to_lower(tname);
531 std::string tnameStr = tname;
532 boost::to_lower(tnameStr);
538 if (ds == std::string(
"DAQ"))
540 else if (ds == std::string(
"DCS"))
542 else if (ds == std::string(
"Offline"))
580 for ( ; i <
fCol.size(); ++i)
583 if (i >=
fCol.size())
return 0;
591 for (
unsigned int i=0; i<
fCol.size(); ++i)
592 if (
fCol[i].
Name() == cname)
return (
int)i;
593 std::cerr <<
"No such column \"" << cname <<
"\". Returning -1" << std::endl;
600 std::map<std::string,int> tmap;
601 for (
unsigned int i=0; i<
fCol.size(); ++i) {
602 tmap[
fCol[i].Name()] = int(i);
610 std::vector<std::string> nameList;
612 for (
unsigned int i=0; i<
fCol.size(); ++i)
613 nameList.push_back(
fCol[i].Name());
622 for ( ; i <
fCol.size(); ++i)
625 if (i >=
fCol.size())
return;
627 fCol[i].SetTolerance(t);
635 for ( ; i <
fCol.size(); ++i)
638 if (i >=
fCol.size())
return 0.;
640 return fCol[i].Tolerance();
647 std::cout << std::endl;
649 std::vector<int> len(0);
653 for ( ; i<this->
NCol(); ++i) {
655 if ((
int)this->
GetCol(i)->Type().length() > tlen)
664 while (i < this->
NCol()) {
665 for ( ; i<this->
NCol() && nsp<78; ++i)
668 for (
int k=0; k<nsp; ++k) {
671 std::cout << std::endl;
676 std::cout <<
"|" << std::endl;
678 for (
int k=0; k<nsp; ++k) {
681 std::cout << std::endl;
686 std::cout <<
"|" << std::endl;
688 for (
int k=0; k<nsp; ++k) {
691 std::cout << std::endl;
707 tmpStr = getenv(
"DBIHOST");
712 sprintf(hname,
"DBIHOST%d",ntry);
713 tmpStr = getenv(hname);
715 std::cerr <<
"Switching to " << tmpStr << std::endl;
722 tmpStr = getenv(
"DBINAME");
725 tmpStr = getenv(
"DBIPORT");
728 tmpStr = getenv(
"DBIUSER");
734 tmpStr = getenv(
"USER");
737 std::cerr <<
"Table::GetConnectionInfo: DB User undefined. Setting to \"" 738 <<
fUser <<
"\"" << std::endl;
741 throw std::runtime_error(
"Table::GetConnectionInfo: DB USER undefined.");
746 throw std::runtime_error(
"Table::GetConnectionInfo: DB HOST undefined.");
749 throw std::runtime_error(
"Table::GetConnectionInfo: DB NAME undefined.");
763 bool gotConnInfo =
false;
767 catch (std::runtime_error&
e) {
768 std::cerr << e.what() << std::endl;
772 if (!gotConnInfo)
return false;
777 char* tmpStr = getenv(
"DBIGRIDPWDFILE");
785 char* tmpStr = getenv(
"DBIPWDFILE");
805 time_t
t0 = time(NULL);
810 std::cerr <<
"Connection to " <<
fDBHost <<
":" 815 sleepTime = 1 + ((double)random()/(double)RAND_MAX)*(1 << nTry++);
823 std::cerr <<
"Too many attempts to connect to the database, " 824 <<
", giving up." << std::endl;
831 std::cout <<
"Got new connection" << std::endl;
842 std::cout <<
"Closed connection" << std::endl;
861 std::string fNameStr =
"";
864 char* tmpStr = getenv(
"DBIPWDFILE");
868 std::cerr <<
"DBIPWDFILE env. variable is not set, disabling " 869 <<
"password-access to the dB." << std::endl;
878 fin.open(fNameStr.c_str());
879 if (!fin.is_open() || !fin.good()) {
880 std::cerr <<
"Could not open password file " << fNameStr
881 <<
". Disabling password-access to the dB." << std::endl;
899 std::string tname = this->
Name();
909 std::string cmd =
"SELECT tablename FROM pg_tables WHERE schemaname=\'" +
914 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
916 std::cerr <<
"Table::ExistsInDB command failed: " 925 int nRow = PQntuples(res);
928 std::vector<std::string> tList;
929 tList.push_back(tname+std::string(
"_snapshot"));
930 tList.push_back(tname+std::string(
"_snapshot_data"));
931 tList.push_back(tname+std::string(
"_tag"));
932 tList.push_back(tname+std::string(
"_tag_snapshot"));
933 tList.push_back(tname+std::string(
"_update"));
935 for (
int i=0; i<nRow; ++i) {
937 std::string tStr = std::string(PQgetvalue(res,i,0));
946 if (std::string(PQgetvalue(res,i,0)) == tList[0] ||
947 std::string(PQgetvalue(res,i,0)) == tList[1] ||
948 std::string(PQgetvalue(res,i,0)) == tList[2] ||
949 std::string(PQgetvalue(res,i,0)) == tList[3] ||
950 std::string(PQgetvalue(res,i,0)) == tList[4] )
982 std::string cmd =
"SELECT last_value FROM ";
984 cmd +=
"_" + col +
"_seq";
987 std::cerr <<
"Table::GetCurrSeqVal: Executing PGSQL command: \n\t" 991 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
993 std::cerr <<
"SELECT failed: " << PQerrorMessage(
fConnection) << std::endl;
999 if (PQnfields(res) != 1) {
1005 int nRow = PQntuples(res);
1012 if (! PQgetisnull(res,0,0)) {
1013 std::string vstr = PQgetvalue(res,0,0);
1015 iseq = boost::lexical_cast<
long>(vstr);
1017 catch (boost::bad_lexical_cast &) {
1041 std::cerr <<
"Table::ExecuteSQL: No connection to the database!" << std::endl;
1045 if (cmd ==
"")
return false;
1048 std::cerr <<
"Executing SQL query: " << cmd << std::endl;
1050 boost::posix_time::ptime ctt1;
1051 boost::posix_time::ptime ctt2;
1054 ctt1 = boost::posix_time::microsec_clock::local_time();
1059 ctt2 = boost::posix_time::microsec_clock::local_time();
1060 boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1061 std::cerr <<
"Table::ExecuteSQL(" << cmd <<
"): query took " 1062 << tdiff.total_milliseconds() <<
" ms" << std::endl;
1077 std::cerr <<
"Table::LoadFromDB: Detector not set! Table::SetDetector()" 1078 <<
" must be called first!" << std::endl;
1092 std::cerr <<
"Table::LoadFromDB: No connection to the database!" << std::endl;
1097 std::cerr <<
"Table::LoadFromDB: Table \"" <<
Name()
1098 <<
"\" not found in database!" << std::endl;
1107 std::ostringstream outs;
1110 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1111 std::cerr <<
"BEGIN command failed: " << PQerrorMessage(
fConnection) << std::endl;
1120 outs <<
"DECLARE myportal CURSOR FOR SELECT ";
1122 outs <<
"DISTINCT ON (";
1137 if (fValiditySQL !=
"" && !
fValidityStart.empty()) outs <<
" and ";
1141 bool needsQuotes=
false;
1153 if (needsQuotes) outs <<
"'";
1155 if (needsQuotes) outs <<
"'";
1160 if (needsQuotes) outs <<
"'";
1162 if (needsQuotes) outs <<
"'";
1170 outs <<
" ORDER BY ";
1180 for (
unsigned int i=0; i<
fOrderCol.size(); ++i) {
1182 if (i<(
fOrderCol.size()-1)) outs <<
", ";
1193 outs <<
" LIMIT " << boost::lexical_cast<std::string>(
fSelectLimit);
1197 outs <<
" OFFSET " << boost::lexical_cast<std::string>(
fSelectOffset);
1201 std::cerr <<
"Table::LoadFromDB: Executing PGSQL command: \n\t" << outs.str() << std::endl;
1204 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1205 std::cerr <<
"DECLARE CURSOR failed: " << PQerrorMessage(
fConnection) << std::endl;
1213 boost::posix_time::ptime ctt1;
1214 boost::posix_time::ptime ctt2;
1217 ctt1 = boost::posix_time::microsec_clock::local_time();
1220 res = PQexec(
fConnection,
"FETCH ALL in myportal");
1222 ctt2 = boost::posix_time::microsec_clock::local_time();
1223 boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1224 std::cerr <<
"Table::LoadFromDB(" <<
Name() <<
"): query took " 1225 << tdiff.total_milliseconds() <<
" ms" << std::endl;
1228 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
1229 std::cerr <<
"FETCH ALL failed: %" << PQerrorMessage(
fConnection) << std::endl;
1236 int nRow = PQntuples(res);
1238 std::cerr <<
"Table::LoadFromDB(" <<
Name() <<
"): got " << nRow
1239 <<
" rows of data." << std::endl;
1242 ctt1 = boost::posix_time::microsec_clock::local_time();
1245 std::vector<int> colMap(
fCol.size());
1247 for (
unsigned int i=0; i<
fCol.size(); ++i) {
1248 colMap[i] = PQfnumber(res,
fCol[i].
Name().c_str());
1253 unsigned int ioff =
fRow.size();
1256 for (
int i=0; i < nRow; i++) {
1257 for (
unsigned int j=0; j <
fCol.size(); j++) {
1260 if (! PQgetisnull(res,i,k)) {
1261 std::string vstr = PQgetvalue(res,i,k);
1262 fRow[ioff+i].Col(j).FastSet(vstr);
1268 fRow[ioff+i].SetInDB();
1273 ctt2 = boost::posix_time::microsec_clock::local_time();
1274 boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1275 std::cerr <<
"Table::LoadFromDB(" <<
Name() <<
"): parsing took " 1276 << tdiff.total_milliseconds() <<
" ms" << std::endl;
1300 std::cout <<
"Reading " << fname << std::endl;
1303 fin.open(fname.c_str());
1304 if (!fin.is_open()) {
1305 std::cerr <<
"Could not open " << fname << std::endl;
1309 std::cerr <<
"Stream not good " << fname << std::endl;
1318 std::vector<int> colMap(
fCol.size());
1319 for (
unsigned int i=0; i<
fCol.size(); ++i) {
1323 bool hasColNames =
true;
1324 bool hasTols =
true;
1331 std::getline(fin,s);
1338 for (
int j=0; ic<s.length(); ++j) {
1341 while (s[ic++] !=
',' && ic<s.length())
1342 buff[k++] = s[ic-1];
1344 if (ic==s.length()) buff[k++] = s[s.length()-1];
1348 boost::algorithm::trim(value);
1349 if (value ==
"channel") { chanIdx=j; ++joff;}
1350 else if (value ==
"tv") { tvIdx=j; ++joff;}
1351 else if (value ==
"tvend") { tvEndIdx=j; ++joff;}
1353 for (
unsigned int jc=0; jc<
fCol.size(); ++jc)
1355 colMap[j-joff] = jc;
1363 hasColNames =
false;
1369 std::getline(fin,s);
1375 for (
int j=0; ic<s.length(); ++j) {
1377 while (s[ic] !=
',' && ic<s.length())
1378 buff[k++] = s[ic++];
1381 if (ic < s.length())
1387 if (value.length() > 0) {
1388 if (j==chanIdx || j==tvIdx || j==tvEndIdx)
1391 fCol[colMap[j-joff]].SetTolerance(atof(buff));
1404 unsigned int ioff=
fRow.size();
1405 unsigned int irow=0;
1408 while (std::getline(fin,s))
1414 std::getline(fin,s);
1418 std::getline(fin,s);
1422 std::cout <<
"Table::LoadFromCSV() found no rows in " 1423 << fname << std::endl;
1428 std::cout <<
"Added " << nRow <<
" empty rows" << std::endl;
1430 for (
int jrow=0; jrow<nRow; ++jrow) {
1431 std::getline(fin,s);
1437 for (
int j=0; ic<s.length(); ++j) {
1440 while (s[ic++] !=
',' && ic<s.length()) {
1441 buff[k++] = s[ic-1];
1442 if (buff[k-1] ==
'x') hasX=
true;
1444 if (ic==s.length()) buff[k++] = s[s.length()-1];
1449 fRow[ioff+irow].SetChannel(strtoull(buff,NULL,10));
1452 else if (j==tvIdx) {
1453 fRow[ioff+irow].SetVldTime(strtoull(buff,NULL,10));
1456 else if (j==tvEndIdx) {
1457 fRow[ioff+irow].SetVldTimeEnd(strtoull(buff,NULL,10));
1462 if (
fCol[j-joff].Type() ==
"bigint" ||
1463 fCol[j-joff].Type() ==
"long") {
1465 std::istringstream iss(value);
1466 uint64_t ulongValue;
1467 iss >> std::hex >> ulongValue;
1468 int64_t longValue = (int64_t) ulongValue;
1469 value = boost::lexical_cast<std::string>(longValue);
1475 else if (
fCol[j-joff].Type() ==
"int") {
1477 std::istringstream iss(value);
1479 iss >> std::hex >> uintValue;
1480 int32_t intValue = (int32_t) uintValue;
1481 value = boost::lexical_cast<std::string>(intValue);
1487 else if (
fCol[j-joff].Type() ==
"short") {
1489 std::istringstream iss(value);
1490 uint16_t ushortValue;
1491 iss >> std::hex >> ushortValue;
1492 int16_t shortValue = (int16_t) ushortValue;
1493 value = boost::lexical_cast<std::string>(shortValue);
1500 if (
fCol[j-joff].Type() ==
"text") {
1501 boost::algorithm::trim(value);
1502 if ((value[0] ==
'"' && value[value.length()-1] ==
'"') ||
1503 (value[0] ==
'\'' && value[value.length()-1] ==
'\''))
1504 value = value.substr(1,value.length()-2);
1506 fRow[ioff+irow].Col(colMap[j-joff]).FastSet(value);
1510 fRow[ioff+irow].SetInDB();
1524 int i = strlen(line);
1525 while (*line < '0' || *line >
'9') line++;
1535 FILE*
file = fopen(
"/proc/self/status",
"r");
1540 while (fgets(line, 128, file) != NULL){
1541 if (strncmp(line,
"VmSize:", 7) == 0){
1547 std::cerr <<
Schema() <<
"." <<
Name() <<
": this process using " 1548 << result/1024 <<
" MB of VirtualMemory" << std::endl;
1554 FILE*
file = fopen(
"/proc/self/status",
"r");
1559 while (fgets(line, 128, file) != NULL){
1560 if (strncmp(line,
"VmRSS:", 6) == 0){
1566 std::cerr <<
Schema() <<
"." <<
Name() <<
": this process using " 1567 << result/1024 <<
" MB of PhysicalMemory" << std::endl;
1578 std::vector<int> colMap(
fCol.size());
1579 std::vector<bool> isString(
fCol.size());
1580 std::vector<bool> isKnownField(
fCol.size());
1582 const char* uagent = NULL;
1585 std::cout <<
"DBWeb query: " << myss << std::endl;
1587 boost::posix_time::ptime ctt1;
1588 boost::posix_time::ptime ctt2;
1591 ctt1 = boost::posix_time::microsec_clock::local_time();
1594 ds = getDataWithTimeout(myss.c_str(), uagent,
1598 ctt2 = boost::posix_time::microsec_clock::local_time();
1599 boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1600 std::cerr <<
"Table::Load(" <<
Name() <<
"): query took " 1601 << tdiff.total_milliseconds() <<
" ms" << std::endl;
1604 int httpStatus = getHTTPstatus(ds);
1606 if (httpStatus == 504) {
1609 time_t
t0 = time(NULL);
1612 while (httpStatus == 504 && ((t1-t0) < fConnectionTimeout) ) {
1613 sleepTime = 1 + ((double)random()/(double)RAND_MAX)*(1 << nTry++);
1615 std::cerr <<
"Table::Load() for " <<
Name()
1616 <<
" failed with error 504, retrying in " << sleepTime
1617 <<
" seconds." << std::endl;
1622 ctt1 = boost::posix_time::microsec_clock::local_time();
1624 ds = getDataWithTimeout(myss.c_str(), uagent,
1628 ctt2 = boost::posix_time::microsec_clock::local_time();
1629 boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1630 std::cerr <<
"Table::Load(" <<
Name() <<
"): query took " 1631 << tdiff.total_milliseconds() <<
" ms" << std::endl;
1633 httpStatus = getHTTPstatus(ds);
1637 if (httpStatus != 200) {
1638 std::cerr <<
"Table::Load: Web Service returned HTTP status " 1639 << httpStatus <<
": " << getHTTPmessage(ds) << std::endl;
1644 ctt1 = boost::posix_time::microsec_clock::local_time();
1646 int ntup = getNtuples(ds);
1651 std::cout <<
"Got zero rows from database. Is that expected?" << std::endl;
1659 std::cout <<
"Got " << ntup-1 <<
" rows from database" << std::endl;
1661 int ioff=
fRow.size();
1665 tu = getFirstTuple(ds);
1667 std::cerr <<
"Table::Load(" <<
Name() <<
") has NULL first tuple!" 1671 int ncol2 = getNfields(tu);
1672 std::string chanStr =
"channel";
1673 std::string tvStr =
"tv";
1674 std::string tvEndStr =
"tvend";
1678 for (
int i=0; i<ncol2; ++i) {
1679 getStringValue(tu,i,ss,
sizeof(ss),&err);
1680 if (chanStr == ss) { chanIdx=i;
continue;}
1681 if (tvStr == ss) { tvIdx=i;
continue;}
1682 if (tvEndStr == ss) { tvEndIdx=i;
continue;}
1684 bool foundMatch=
false;
1685 for (
unsigned int icol=0; icol<
fCol.size(); ++icol) {
1688 isString[i] =
false;
1689 if (
fCol[icol].Type() ==
"string" ||
fCol[icol].Type() ==
"text")
1697 isKnownField[i] =
false;
1699 isKnownField[i] =
true;
1703 tu = getNextTuple(ds);
1705 while (tu != NULL) {
1706 for (
int i=0; i<ncol2; ++i) {
1707 getStringValue(tu,i,ss,
sizeof(ss),&err);
1709 uint64_t chan = strtoull(ss,NULL,10);
1710 fRow[ioff+irow].SetChannel(chan);
1713 else if (i == tvIdx) {
1714 double t1 = strtod(ss,NULL);
1715 fRow[ioff+irow].SetVldTime(t1);
1717 else if (i == tvEndIdx) {
1718 double t1 = strtod(ss,NULL);
1719 fRow[ioff+irow].SetVldTimeEnd(t1);
1722 if (isKnownField[i]) {
1723 if (isString[i] && (ss[0]==
'\'' || ss[0]==
'\"')) {
1725 strncpy(ss2,&ss[1],k-2);
1727 fRow[ioff+irow].Col(colMap[i]).FastSet(ss2);
1730 fRow[ioff+irow].Col(colMap[i]).FastSet(ss);
1735 tu = getNextTuple(ds);
1740 ctt2 = boost::posix_time::microsec_clock::local_time();
1741 boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1742 std::cerr <<
"Table::Load(" <<
Name() <<
"): parsing took " 1743 << tdiff.total_milliseconds() <<
" ms" << std::endl;
1749 while(
int(
fRow.size()) > irow)
fRow.pop_back();
1761 std::cerr <<
"Table::LoadNonConditionsTable: Query Engine URL is not set! using Table::LoadFromDB() instead." << std::endl;
1766 std::cerr <<
"Table::LoadNonConditionsTable: pure SQL statements are not supported, using Table::LoadFromDB() instead." << std::endl;
1770 std::stringstream myss;
1774 int ncol = this->
NCol();
1778 for (
int i=0; i<ncol; ++i) {
1780 bool skipCol =
false;
1787 if (skipCol)
continue;
1801 std::cerr <<
"Table::LoadNonConditionsTable: validity strings are not supported, using Table::LoadFromDB() instead." << std::endl;
1825 for (
unsigned int i=0; i<
fOrderCol.size(); ++i) {
1827 if (i<(
fOrderCol.size()-1)) myss <<
", ";
1853 std::cerr <<
"Table::LoadUnstructuredConditionsTable: No validity time is set!" << std::endl;
1858 std::cerr <<
"Table::LoadConditionsTable: Web Service URL is not set!" << std::endl;
1863 std::string interactiveURL = getenv(
"DBIUCONDBURLINT");
1864 if (!interactiveURL.empty())
1870 std::stringstream myss;
1883 std::cerr <<
"Table::LoadConditionsTable: Data type mask is not set!" << std::endl;
1888 std::cerr <<
"Table::LoadConditionsTable: No validity time is set!" << std::endl;
1893 std::cerr <<
"Table::LoadConditionsTable: Web Service URL is not set!" << std::endl;
1898 std::string interactiveURL = getenv(
"DBIWSURLINT");
1899 if (!interactiveURL.empty())
1903 int ncol = this->
NCol();
1905 std::stringstream myss;
1926 if (
fTag !=
"") myss <<
"tag=" <<
fTag <<
"&";
1933 myss <<
"t=" << std::setprecision(12) <<
fMinTSVld;
1938 myss <<
"t0=" << std::setprecision(12) <<
fMinTSVld <<
"&t1=" << std::setprecision(12) <<
fMaxTSVld;
1946 myss <<
"&columns=";
1947 bool firstCol =
true;
1948 for (
int i=0; i<ncol; ++i) {
1950 bool skipCol =
false;
1956 if (skipCol)
continue;
1957 if(!firstCol) myss <<
",";
1996 for (
int i=0; i<this->
NRow(); ++i) {
2003 bool wasInserted=
false;
2004 for (
unsigned int j=0; j<
fChanRowMap[chan].size(); ++j) {
2020 for (; itr != itrEnd; ++itr)
2037 std::vector<nutools::dbi::Row*>& rlist =
fChanRowMap[channel];
2038 if (rlist.empty())
return 0;
2043 for ( ; i<rlist.size(); ++i) {
2044 tv = rlist[i]->VldTime();
2045 if (t >= tv) irow=i;
2048 if (irow>=0)
return rlist[irow];
2055 if (tn !=
"")
fTag = tn;
2057 if (
fTag ==
"")
return false;
2059 std::stringstream myss;
2062 myss <<
"tag=" <<
fTag;
2064 myss <<
"&override=yes";
2069 postHTTPsigned(myss.str().c_str(), pwd.c_str(), NULL, 0, NULL, 0, &status);
2085 catch (std::runtime_error&
e) {
2086 std::cerr << e.what() << std::endl;
2098 std::cerr <<
"Table::WriteToDB: No connection to the database!" << std::endl;
2104 std::cerr <<
"Table::WriteToDB: Table does not exist in database!" 2117 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2118 std::cerr <<
"BEGIN command failed: " << PQerrorMessage(
fConnection) << std::endl;
2126 std::string cmd =
"SET search_path TO " +
fSchema;
2128 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2129 std::cerr <<
"\'" << cmd <<
"\' command failed" << std::endl;
2138 int insertTimeIdx = colMap[
"inserttime"];
2139 int insertUserIdx = colMap[
"insertuser"];
2140 int updateTimeIdx = colMap[
"updatetime"];
2141 int updateUserIdx = colMap[
"updateuser"];
2143 for (
unsigned int i=0; i<
fRow.size(); ++i) {
2145 if (!
fRow[i].InDB()) {
2150 int nrowInsert =
fCol.size();
2151 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2152 if (
fCol[j].
Name() ==
"updatetime")
2154 else if (
fCol[j].
Name() ==
"updateuser")
2156 else if (
fCol[j].Type() ==
"autoincr")
2160 std::ostringstream outs;
2163 outs <<
"INSERT INTO " <<
Schema() <<
"." <<
Name() <<
" (";
2164 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2165 if (
fCol[j].
Name() ==
"updatetime")
continue;
2166 if (
fCol[j].
Name() ==
"updateuser")
continue;
2167 if (
fCol[j].Type() ==
"autoincr")
continue;
2169 outs <<
fCol[j].Name();
2170 if (ic < nrowInsert-1) outs <<
",";
2173 outs <<
") VALUES (";
2176 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2177 if (
fCol[j].
Name() ==
"updatetime")
continue;
2178 if (
fCol[j].
Name() ==
"updateuser")
continue;
2179 if (
fCol[j].Type() ==
"autoincr")
continue;
2183 if (ic < nrowInsert-1) outs <<
",";
2190 std::cerr <<
"Table::WriteToDB: Executing PGSQL command: \n\t" 2191 << outs.str() << std::endl;
2194 std::cout << outs.str() << std::endl;
2197 boost::posix_time::ptime ctt1;
2198 boost::posix_time::ptime ctt2;
2201 ctt1 = boost::posix_time::microsec_clock::local_time();
2206 ctt2 = boost::posix_time::microsec_clock::local_time();
2207 boost::posix_time::time_duration tdiff = ctt2 - ctt1;
2208 std::cerr <<
"Table::WriteToDB(" <<
Name() <<
"): query took " 2209 << tdiff.total_milliseconds() <<
" ms" << std::endl;
2212 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2214 std::cerr <<
"INSERT failed: " << PQerrorMessage(
fConnection)
2226 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2227 if (
fCol[j].Type() ==
"autoincr") {
2229 seqstr = boost::lexical_cast<std::string>(iseq);
2230 fRow[i].Col(j).Set(seqstr,
true);
2242 if (
fRow[i].NModified() > 0 ) {
2246 std::ostringstream outs;
2247 outs <<
"UPDATE " <<
Schema() <<
"." <<
Name() <<
" SET ";
2249 for (
unsigned int j=0; j<
fCol.size() && im < r.
NModified(); ++j) {
2251 outs <<
fCol[j].Name() +
"=";
2260 for (
int j=0; j<nkey; ++j) {
2261 std::string pkey =
fPKeyList[j]->Name();
2262 int pkeyIdx = colMap[pkey];
2263 outs << pkey <<
"=" << r.
Col(pkeyIdx);
2264 if (j < (nkey-1)) outs <<
" and ";
2268 std::cerr <<
"Table::WriteToDB: Executing PGSQL command: \n\t" 2269 << outs.str() << std::endl;
2272 std::cout << outs.str() << std::endl;
2276 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2278 std::cerr <<
"UPDATE failed: " << PQerrorMessage(
fConnection) << std::endl;
2307 int ncol = this->
NCol();
2308 int nrow = this->
NRow();
2310 ss <<
"channel,tv,";
2312 for (
int i=0; i<ncol; ++i) {
2314 if(!first) ss <<
",";
2320 ss <<
"tolerance,,";
2322 for (
int j=0; j<ncol; ++j) {
2325 if(!first) ss <<
",";
2329 if (ctype ==
"double")
2331 else if (ctype ==
"float")
2338 for (
int i=0; i<nrow; ++i) {
2344 for (
int j=0; j<ncol; ++j) {
2345 if(!first) ss <<
",";
2359 std::cerr <<
"Table::Write: Data type mask is not set!" << std::endl;
2364 std::cerr <<
"Table::Write: Web Service URL is not set!" << std::endl;
2369 std::string putURL = getenv(
"DBIWSURLPUT");
2370 if (!putURL.empty())
2374 std::stringstream ss;
2381 std::stringstream typeStr;
2382 typeStr <<
"&type=";
2386 url += typeStr.str();
2391 boost::posix_time::ptime ctt1;
2392 boost::posix_time::ptime ctt2;
2395 ctt1 = boost::posix_time::microsec_clock::local_time();
2398 std::cout <<
"Posting data to: " << url << std::endl;
2400 postHTTPsigned(url.c_str(), pwd.c_str(), NULL, 0,
2401 ss.str().c_str(), ss.str().length(), &status);
2403 ctt2 = boost::posix_time::microsec_clock::local_time();
2404 boost::posix_time::time_duration tdiff = ctt2 - ctt1;
2405 std::cerr <<
"Table::Write(" <<
Name() <<
"): query took " 2406 << tdiff.total_milliseconds() <<
" ms" << std::endl;
2408 return (status == 0);
2419 fout.open(fname.c_str());
2421 fout.open(fname.c_str(),std::ios_base::app);
2424 std::stringstream ss;
2429 if (writeColNames) {
2430 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2431 fout <<
fCol[j].Name();
2432 if (j<
fCol.size()-1) fout <<
",";
2436 for (
unsigned int i=0; i<
fRow.size(); ++i) {
2437 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2438 fout <<
fRow[i].Col(j);
2439 if (j<
fCol.size()-1) fout <<
",";
2467 if (i >=
fCol.size())
return false;
2484 if (i >=
fCol.size())
return false;
2504 if (!c)
return false;
2522 if (!c)
return false;
2539 if (i >=
fCol.size())
return false;
2542 for (
unsigned int j=0; j<
fOrderCol.size(); ++j)
2556 if (i >=
fCol.size())
return false;
2559 for (
unsigned int j=0; j<
fOrderCol.size(); ++j)
2576 if (!c)
return false;
2578 for (
unsigned int j=0; j<
fOrderCol.size(); ++j)
2594 if (!c)
return false;
2596 for (
unsigned int j=0; j<
fOrderCol.size(); ++j)
2608 std::string pwd =
"";
2610 char* pwdFile = getenv(
"DBIWSPWDFILE");
2614 if (!fin.is_open() || !fin.good()) {
2615 std::cerr <<
"Could not open password file " << pwdFile
2616 <<
". Canceling Table::Write()" << std::endl;
code to link reconstructed objects back to the MC truth information
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
map< int, array< map< int, double >, 2 >> Table