17 #include <boost/algorithm/string.hpp> 18 #include <boost/algorithm/string/case_conv.hpp> 19 #include <boost/date_time/posix_time/posix_time.hpp> 21 #include <nutools/IFDatabase/Table.h> 22 #include <nutools/IFDatabase/Util.h> 32 fMaxTSVld(0),fMinTSVld(0),
69 char* tmpStr =
getenv(
"DBITIMEOUT");
71 int tmpTO = atoi(tmpStr);
78 const char* wsHost =
getenv(
"DBIWSURL");
82 const char* ucondbHost =
getenv(
"DBIUCONDBURL");
86 const char* qeHost =
getenv(
"DBIQEURL");
90 tmpStr =
getenv(
"DBIVERB");
119 char* tmpStr =
getenv(
"DBIVERB");
129 tmpStr =
getenv(
"DBITIMEOUT");
131 int tmpTO = atoi(tmpStr);
152 if (!dbuser.empty())
SetUser(dbuser);
162 errStr =
"Table::Table(): table \'" + stName +
"\' not found in database!";
163 throw std::runtime_error(errStr);
175 std::vector<std::string> pkeyList;
177 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";
181 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
187 throw std::runtime_error(errStr);
190 int nRow = PQntuples(res);
192 errStr =
std::string(
"Table::Table(): no primary keys defined for table \'") + tableName +
std::string(
"\', unable to proceed.");
196 throw std::runtime_error(errStr);
199 for (
int i=0; i<nRow; ++i) {
201 pkeyList.push_back(key);
213 tmpStr =
getenv(
"DBICACHEDIR");
241 std::cerr <<
"Table::GetColsFromDB() currently disabled for unstructured conditions tables." <<
std::endl;
255 cmd +=
"\' and table_schema=\'" +
fSchema +
"\'";
259 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
266 throw std::runtime_error(errStr);
269 int nRow = PQntuples(res);
271 for (
int i=0; i<nRow; ++i) {
276 if (cname ==
"__snapshot_id")
continue;
277 if (cname ==
"__tr")
continue;
278 if (cname ==
"__channel")
continue;
279 if (cname ==
"__tv")
continue;
282 if (ctype ==
"smallint") ctype=
"short";
283 else if (ctype ==
"double precision") ctype=
"double";
284 else if (ctype ==
"boolean") ctype=
"bool";
285 else if (ctype ==
"timestamp without time zone") ctype=
"timestamp";
286 else if (ctype.substr(0,7) ==
"varchar" || ctype ==
"text")
292 cmd =
"SELECT pg_get_serial_sequence(\'" + stName +
293 "\',\'" + cname +
"\')";
295 int nRow2 = PQntuples(res2);
296 for (
int j=0; j<nRow2; ++j) {
297 std::string tStr =
std::string(PQgetvalue(res2,j,0));
298 if (tStr !=
"") ctype =
"auto_incr";
306 if (find(pkeyList.begin(),pkeyList.end(),cname) != pkeyList.end()) {
328 for (
size_t i=0; i<
fCol.size(); ++i) {
330 std::cerr <<
"Table::AddCol: column \'" << cname <<
"\' already exists! Fatal, aborting..." <<
std::endl;
337 fCol.push_back(cdef);
344 return fCol.size()-1;
355 for (
unsigned int i=0; i<
fCol.size(); ++i) {
356 if (
fCol[i].
Name() ==
"inserttime" ||
357 fCol[i].Name() ==
"insertuser" ||
358 fCol[i].Name() ==
"updatetime" ||
359 fCol[i].Name() ==
"updateuser" )
continue;
360 if (!
fCol[i].CanBeNull())
387 if (i < 0)
return false;
391 if (j >=
fRow.size())
return false;
394 for (
unsigned int k=0;
k<kEnd; ++
k)
408 if (i >= 0 && i < (
int)
fRow.size())
420 for (
unsigned int i=0; i<
fNullList.size(); ++i)
422 std::cerr <<
fCol[
fNullList[i].second].Name() <<
" is NULL in row " 518 boost::to_lower(tname);
525 boost::to_lower(tnameStr);
573 for ( ; i <
fCol.size(); ++i)
576 if (i >=
fCol.size())
return 0;
584 for (
unsigned int i=0; i<
fCol.size(); ++i)
585 if (
fCol[i].
Name() == cname)
return (
int)i;
586 std::cerr <<
"No such column \"" << cname <<
"\". Returning -1" <<
std::endl;
593 std::map<std::string,int> tmap;
594 for (
unsigned int i=0; i<
fCol.size(); ++i) {
603 std::vector<std::string> nameList;
605 for (
unsigned int i=0; i<
fCol.size(); ++i)
606 nameList.push_back(
fCol[i].Name());
615 for ( ; i <
fCol.size(); ++i)
618 if (i >=
fCol.size())
return;
620 fCol[i].SetTolerance(t);
628 for ( ; i <
fCol.size(); ++i)
631 if (i >=
fCol.size())
return 0.;
633 return fCol[i].Tolerance();
642 std::vector<int> len(0);
646 for ( ; i<this->
NCol(); ++i) {
648 if ((
int)this->
GetCol(i)->
Type().length() > tlen)
657 while (i < this->
NCol()) {
658 for ( ; i<this->
NCol() && nsp<78; ++i)
661 for (
int k=0;
k<nsp; ++
k) {
671 for (
int k=0;
k<nsp; ++
k) {
681 for (
int k=0;
k<nsp; ++
k) {
700 tmpStr =
getenv(
"DBIHOST");
705 sprintf(hname,
"DBIHOST%d",ntry);
708 std::cerr <<
"Switching to " << tmpStr <<
std::endl;
715 tmpStr =
getenv(
"DBINAME");
718 tmpStr =
getenv(
"DBIPORT");
721 tmpStr =
getenv(
"DBIUSER");
730 std::cerr <<
"Table::GetConnectionInfo: DB User undefined. Setting to \"" 734 throw std::runtime_error(
"Table::GetConnectionInfo: DB USER undefined.");
739 throw std::runtime_error(
"Table::GetConnectionInfo: DB HOST undefined.");
742 throw std::runtime_error(
"Table::GetConnectionInfo: DB NAME undefined.");
756 bool gotConnInfo =
false;
760 catch (std::runtime_error&
e) {
765 if (!gotConnInfo)
return false;
770 char* tmpStr =
getenv(
"DBIGRIDPWDFILE");
778 char* tmpStr =
getenv(
"DBIPWDFILE");
803 std::cerr <<
"Connection to " <<
fDBHost <<
":" 808 sleepTime = 1 + ((double)
random()/(double)RAND_MAX)*(1 << nTry++);
816 std::cerr <<
"Too many attempts to connect to the database, " 824 std::cout <<
"Got new connection" <<
std::endl;
835 std::cout <<
"Closed connection" <<
std::endl;
857 char* tmpStr =
getenv(
"DBIPWDFILE");
861 std::cerr <<
"DBIPWDFILE env. variable is not set, disabling " 862 <<
"password-access to the dB." <<
std::endl;
871 fin.open(fNameStr.c_str());
872 if (!fin.is_open() || !fin.good()) {
873 std::cerr <<
"Could not open password file " << fNameStr
874 <<
". Disabling password-access to the dB." <<
std::endl;
902 std::string cmd =
"SELECT tablename FROM pg_tables WHERE schemaname=\'" +
907 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
909 std::cerr <<
"Table::ExistsInDB command failed: " 918 int nRow = PQntuples(res);
921 std::vector<std::string> tList;
923 tList.push_back(tname+
std::string(
"_snapshot_data"));
925 tList.push_back(tname+
std::string(
"_tag_snapshot"));
928 for (
int i=0; i<nRow; ++i) {
939 if (
std::string(PQgetvalue(res,i,0)) == tList[0] ||
977 cmd +=
"_" + col +
"_seq";
980 std::cerr <<
"Table::GetCurrSeqVal: Executing PGSQL command: \n\t" 984 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
992 if (PQnfields(res) != 1) {
998 int nRow = PQntuples(res);
1005 if (! PQgetisnull(res,0,0)) {
1008 iseq = boost::lexical_cast<
long>(vstr);
1010 catch (boost::bad_lexical_cast &) {
1034 std::cerr <<
"Table::ExecuteSQL: No connection to the database!" <<
std::endl;
1038 if (cmd ==
"")
return false;
1041 std::cerr <<
"Executing SQL query: " << cmd <<
std::endl;
1043 boost::posix_time::ptime ctt1;
1044 boost::posix_time::ptime ctt2;
1047 ctt1 = boost::posix_time::microsec_clock::local_time();
1052 ctt2 = boost::posix_time::microsec_clock::local_time();
1053 boost::posix_time::time_duration
tdiff = ctt2 - ctt1;
1054 std::cerr <<
"Table::ExecuteSQL(" << cmd <<
"): query took " 1055 << tdiff.total_milliseconds() <<
" ms" <<
std::endl;
1070 std::cerr <<
"Table::LoadFromDB: Detector not set! Table::SetDetector()" 1071 <<
" must be called first!" <<
std::endl;
1085 std::cerr <<
"Table::LoadFromDB: No connection to the database!" <<
std::endl;
1090 std::cerr <<
"Table::LoadFromDB: Table \"" <<
Name()
1091 <<
"\" not found in database!" <<
std::endl;
1100 std::ostringstream outs;
1103 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1113 outs <<
"DECLARE myportal CURSOR FOR SELECT ";
1115 outs <<
"DISTINCT ON (";
1130 if (fValiditySQL !=
"" && !
fValidityStart.empty()) outs <<
" and ";
1134 bool needsQuotes=
false;
1146 if (needsQuotes) outs <<
"'";
1148 if (needsQuotes) outs <<
"'";
1153 if (needsQuotes) outs <<
"'";
1155 if (needsQuotes) outs <<
"'";
1163 outs <<
" ORDER BY ";
1173 for (
unsigned int i=0; i<
fOrderCol.size(); ++i) {
1175 if (i<(
fOrderCol.size()-1)) outs <<
", ";
1194 std::cerr <<
"Table::LoadFromDB: Executing PGSQL command: \n\t" << outs.str() <<
std::endl;
1197 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1206 boost::posix_time::ptime ctt1;
1207 boost::posix_time::ptime ctt2;
1210 ctt1 = boost::posix_time::microsec_clock::local_time();
1213 res = PQexec(
fConnection,
"FETCH ALL in myportal");
1215 ctt2 = boost::posix_time::microsec_clock::local_time();
1216 boost::posix_time::time_duration
tdiff = ctt2 - ctt1;
1217 std::cerr <<
"Table::LoadFromDB(" <<
Name() <<
"): query took " 1218 << tdiff.total_milliseconds() <<
" ms" <<
std::endl;
1221 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
1229 int nRow = PQntuples(res);
1231 std::cerr <<
"Table::LoadFromDB(" <<
Name() <<
"): got " << nRow
1235 ctt1 = boost::posix_time::microsec_clock::local_time();
1238 std::vector<int> colMap(
fCol.size());
1240 for (
unsigned int i=0; i<
fCol.size(); ++i) {
1241 colMap[i] = PQfnumber(res,
fCol[i].
Name().c_str());
1246 unsigned int ioff =
fRow.size();
1249 for (
int i=0; i < nRow; i++) {
1250 for (
unsigned int j=0; j <
fCol.size(); j++) {
1253 if (! PQgetisnull(res,i,k)) {
1255 fRow[ioff+i].Col(j).FastSet(vstr);
1261 fRow[ioff+i].SetInDB();
1266 ctt2 = boost::posix_time::microsec_clock::local_time();
1267 boost::posix_time::time_duration
tdiff = ctt2 - ctt1;
1268 std::cerr <<
"Table::LoadFromDB(" <<
Name() <<
"): parsing took " 1269 << tdiff.total_milliseconds() <<
" ms" <<
std::endl;
1293 std::cout <<
"Reading " << fname <<
std::endl;
1296 fin.open(fname.c_str());
1297 if (!fin.is_open()) {
1298 std::cerr <<
"Could not open " << fname <<
std::endl;
1302 std::cerr <<
"Stream not good " << fname <<
std::endl;
1311 std::vector<int> colMap(
fCol.size());
1312 for (
unsigned int i=0; i<
fCol.size(); ++i) {
1316 bool hasColNames =
true;
1317 bool hasTols =
true;
1324 std::getline(fin,s);
1331 for (
int j=0; ic<s.length(); ++j) {
1334 while (s[ic++] !=
',' && ic<s.length())
1335 buff[k++] = s[ic-1];
1337 if (ic==s.length()) buff[k++] = s[s.length()-1];
1342 if (value ==
"channel") { chanIdx=j; ++joff;}
1343 else if (value ==
"tv") { tvIdx=j; ++joff;}
1344 else if (value ==
"tvend") { tvEndIdx=j; ++joff;}
1346 for (
unsigned int jc=0; jc<
fCol.size(); ++jc)
1348 colMap[j-joff] = jc;
1356 hasColNames =
false;
1362 std::getline(fin,s);
1368 for (
int j=0; ic<s.length(); ++j) {
1370 while (s[ic] !=
',' && ic<s.length())
1371 buff[k++] = s[ic++];
1374 if (ic < s.length())
1380 if (value.length() > 0) {
1381 if (j==chanIdx || j==tvIdx || j==tvEndIdx)
1384 fCol[colMap[j-joff]].SetTolerance(atof(buff));
1397 unsigned int ioff=
fRow.size();
1398 unsigned int irow=0;
1401 while (std::getline(fin,s))
1407 std::getline(fin,s);
1411 std::getline(fin,s);
1415 std::cout <<
"Table::LoadFromCSV() found no rows in " 1421 std::cout <<
"Added " << nRow <<
" empty rows" <<
std::endl;
1423 for (
int jrow=0; jrow<nRow; ++jrow) {
1424 std::getline(fin,s);
1430 for (
int j=0; ic<s.length(); ++j) {
1433 while (s[ic++] !=
',' && ic<s.length()) {
1434 buff[k++] = s[ic-1];
1435 if (buff[k-1] ==
'x') hasX=
true;
1437 if (ic==s.length()) buff[k++] = s[s.length()-1];
1442 fRow[ioff+irow].SetChannel(strtoull(buff,NULL,10));
1445 else if (j==tvIdx) {
1446 fRow[ioff+irow].SetVldTime(strtoull(buff,NULL,10));
1449 else if (j==tvEndIdx) {
1450 fRow[ioff+irow].SetVldTimeEnd(strtoull(buff,NULL,10));
1455 if (
fCol[j-joff].
Type() ==
"bigint" ||
1458 std::istringstream iss(value);
1462 value = boost::lexical_cast<
std::string>(longValue);
1468 else if (
fCol[j-joff].
Type() ==
"int") {
1470 std::istringstream iss(value);
1474 value = boost::lexical_cast<
std::string>(intValue);
1480 else if (
fCol[j-joff].
Type() ==
"short") {
1482 std::istringstream iss(value);
1486 value = boost::lexical_cast<
std::string>(shortValue);
1493 if (
fCol[j-joff].
Type() ==
"text") {
1495 if ((value[0] ==
'"' && value[value.length()-1] ==
'"') ||
1496 (value[0] ==
'\'' && value[value.length()-1] ==
'\''))
1497 value = value.substr(1,value.length()-2);
1499 fRow[ioff+irow].Col(colMap[j-joff]).FastSet(value);
1503 fRow[ioff+irow].SetInDB();
1517 int i = strlen(line);
1518 while (*line < '0' || *line >
'9') line++;
1528 FILE* file = fopen(
"/proc/self/status",
"r");
1533 while (fgets(line, 128, file) != NULL){
1534 if (strncmp(line,
"VmSize:", 7) == 0){
1540 std::cerr <<
Schema() <<
"." <<
Name() <<
": this process using " 1541 << result/1024 <<
" MB of VirtualMemory" <<
std::endl;
1547 FILE* file = fopen(
"/proc/self/status",
"r");
1552 while (fgets(line, 128, file) != NULL){
1553 if (strncmp(line,
"VmRSS:", 6) == 0){
1559 std::cerr <<
Schema() <<
"." <<
Name() <<
": this process using " 1560 << result/1024 <<
" MB of PhysicalMemory" <<
std::endl;
1571 std::vector<int> colMap(
fCol.size());
1572 std::vector<bool> isString(
fCol.size());
1573 std::vector<bool> isKnownField(
fCol.size());
1575 const char* uagent = NULL;
1578 std::cout <<
"DBWeb query: " << myss <<
std::endl;
1580 boost::posix_time::ptime ctt1;
1581 boost::posix_time::ptime ctt2;
1584 ctt1 = boost::posix_time::microsec_clock::local_time();
1587 ds = getDataWithTimeout(myss.c_str(), uagent,
1591 ctt2 = boost::posix_time::microsec_clock::local_time();
1592 boost::posix_time::time_duration
tdiff = ctt2 - ctt1;
1593 std::cerr <<
"Table::Load(" <<
Name() <<
"): query took " 1594 << tdiff.total_milliseconds() <<
" ms" <<
std::endl;
1597 int httpStatus = getHTTPstatus(ds);
1599 if (httpStatus == 504) {
1605 while (httpStatus == 504 && ((t1-t0) < fConnectionTimeout) ) {
1606 sleepTime = 1 + ((double)
random()/(double)RAND_MAX)*(1 << nTry++);
1608 std::cerr <<
"Table::Load() for " <<
Name()
1609 <<
" failed with error 504, retrying in " << sleepTime
1615 ctt1 = boost::posix_time::microsec_clock::local_time();
1617 ds = getDataWithTimeout(myss.c_str(), uagent,
1621 ctt2 = boost::posix_time::microsec_clock::local_time();
1622 boost::posix_time::time_duration
tdiff = ctt2 - ctt1;
1623 std::cerr <<
"Table::Load(" <<
Name() <<
"): query took " 1624 << tdiff.total_milliseconds() <<
" ms" <<
std::endl;
1626 httpStatus = getHTTPstatus(ds);
1630 if (httpStatus != 200) {
1631 std::cerr <<
"Table::Load: Web Service returned HTTP status " 1632 << httpStatus <<
": " << getHTTPmessage(ds) <<
std::endl;
1637 ctt1 = boost::posix_time::microsec_clock::local_time();
1639 int ntup = getNtuples(ds);
1644 std::cout <<
"Got zero rows from database. Is that expected?" <<
std::endl;
1652 std::cout <<
"Got " << ntup-1 <<
" rows from database" <<
std::endl;
1654 int ioff=
fRow.size();
1658 tu = getFirstTuple(ds);
1660 std::cerr <<
"Table::Load(" <<
Name() <<
") has NULL first tuple!" 1664 int ncol2 = getNfields(tu);
1671 for (
int i=0; i<ncol2; ++i) {
1672 getStringValue(tu,i,ss,
sizeof(ss),&err);
1673 if (chanStr == ss) { chanIdx=i;
continue;}
1674 if (tvStr == ss) { tvIdx=i;
continue;}
1675 if (tvEndStr == ss) { tvEndIdx=i;
continue;}
1677 bool foundMatch=
false;
1678 for (
unsigned int icol=0; icol<
fCol.size(); ++icol) {
1681 isString[i] =
false;
1682 if (
fCol[icol].
Type() ==
"string" ||
fCol[icol].Type() ==
"text")
1690 isKnownField[i] =
false;
1692 isKnownField[i] =
true;
1696 tu = getNextTuple(ds);
1698 while (tu != NULL) {
1699 for (
int i=0; i<ncol2; ++i) {
1700 getStringValue(tu,i,ss,
sizeof(ss),&err);
1702 uint64_t chan = strtoull(ss,NULL,10);
1703 fRow[ioff+irow].SetChannel(chan);
1706 else if (i == tvIdx) {
1707 float t1 = strtof(ss,NULL);
1708 fRow[ioff+irow].SetVldTime(t1);
1710 else if (i == tvEndIdx) {
1711 float t1 = strtof(ss,NULL);
1712 fRow[ioff+irow].SetVldTimeEnd(t1);
1715 if (isKnownField[i]) {
1716 if (isString[i] && (ss[0]==
'\'' || ss[0]==
'\"')) {
1718 strncpy(ss2,&ss[1],k-2);
1720 fRow[ioff+irow].Col(colMap[i]).FastSet(ss2);
1723 fRow[ioff+irow].Col(colMap[i]).FastSet(ss);
1728 tu = getNextTuple(ds);
1733 ctt2 = boost::posix_time::microsec_clock::local_time();
1734 boost::posix_time::time_duration
tdiff = ctt2 - ctt1;
1735 std::cerr <<
"Table::Load(" <<
Name() <<
"): parsing took " 1736 << tdiff.total_milliseconds() <<
" ms" <<
std::endl;
1742 while(
int(
fRow.size()) > irow)
fRow.pop_back();
1754 std::cerr <<
"Table::LoadNonConditionsTable: Query Engine URL is not set! using Table::LoadFromDB() instead." <<
std::endl;
1759 std::cerr <<
"Table::LoadNonConditionsTable: pure SQL statements are not supported, using Table::LoadFromDB() instead." <<
std::endl;
1763 std::stringstream myss;
1767 int ncol = this->
NCol();
1771 for (
int i=0; i<ncol; ++i) {
1773 bool skipCol =
false;
1780 if (skipCol)
continue;
1794 std::cerr <<
"Table::LoadNonConditionsTable: validity strings are not supported, using Table::LoadFromDB() instead." <<
std::endl;
1818 for (
unsigned int i=0; i<
fOrderCol.size(); ++i) {
1820 if (i<(
fOrderCol.size()-1)) myss <<
", ";
1846 std::cerr <<
"Table::LoadUnstructuredConditionsTable: No validity time is set!" <<
std::endl;
1851 std::cerr <<
"Table::LoadConditionsTable: Web Service URL is not set!" <<
std::endl;
1857 if (!interactiveURL.empty())
1863 std::stringstream myss;
1876 std::cerr <<
"Table::LoadConditionsTable: Data type mask is not set!" <<
std::endl;
1881 std::cerr <<
"Table::LoadConditionsTable: No validity time is set!" <<
std::endl;
1886 std::cerr <<
"Table::LoadConditionsTable: Web Service URL is not set!" <<
std::endl;
1892 if (!interactiveURL.empty())
1896 int ncol = this->
NCol();
1898 std::stringstream myss;
1919 if (
fTag !=
"") myss <<
"tag=" <<
fTag <<
"&";
1939 myss <<
"&columns=";
1940 bool firstCol =
true;
1941 for (
int i=0; i<ncol; ++i) {
1943 bool skipCol =
false;
1949 if (skipCol)
continue;
1950 if(!firstCol) myss <<
",";
1989 for (
int i=0; i<this->
NRow(); ++i) {
1996 bool wasInserted=
false;
1997 for (
unsigned int j=0; j<
fChanRowMap[chan].size(); ++j) {
2013 for (; itr != itrEnd; ++itr)
2031 if (rlist.empty())
return 0;
2036 for ( ; i<rlist.size(); ++i) {
2037 tv = rlist[i]->VldTime();
2038 if (t >= tv) irow=i;
2041 if (irow>=0)
return rlist[irow];
2048 if (tn !=
"")
fTag = tn;
2050 if (
fTag ==
"")
return false;
2052 std::stringstream myss;
2055 myss <<
"tag=" <<
fTag;
2057 myss <<
"&override=yes";
2062 postHTTPsigned(myss.str().c_str(), pwd.c_str(), NULL, 0, NULL, 0, &status);
2078 catch (std::runtime_error&
e) {
2091 std::cerr <<
"Table::WriteToDB: No connection to the database!" <<
std::endl;
2097 std::cerr <<
"Table::WriteToDB: Table does not exist in database!" 2110 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2121 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2122 std::cerr <<
"\'" << cmd <<
"\' command failed" <<
std::endl;
2131 int insertTimeIdx = colMap[
"inserttime"];
2132 int insertUserIdx = colMap[
"insertuser"];
2133 int updateTimeIdx = colMap[
"updatetime"];
2134 int updateUserIdx = colMap[
"updateuser"];
2136 for (
unsigned int i=0; i<
fRow.size(); ++i) {
2138 if (!
fRow[i].InDB()) {
2143 int nrowInsert =
fCol.size();
2144 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2145 if (
fCol[j].
Name() ==
"updatetime")
2147 else if (
fCol[j].
Name() ==
"updateuser")
2149 else if (
fCol[j].
Type() ==
"autoincr")
2153 std::ostringstream outs;
2156 outs <<
"INSERT INTO " <<
Schema() <<
"." <<
Name() <<
" (";
2157 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2158 if (
fCol[j].
Name() ==
"updatetime")
continue;
2159 if (
fCol[j].
Name() ==
"updateuser")
continue;
2160 if (
fCol[j].
Type() ==
"autoincr")
continue;
2162 outs <<
fCol[j].Name();
2163 if (ic < nrowInsert-1) outs <<
",";
2166 outs <<
") VALUES (";
2169 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2170 if (
fCol[j].
Name() ==
"updatetime")
continue;
2171 if (
fCol[j].
Name() ==
"updateuser")
continue;
2172 if (
fCol[j].
Type() ==
"autoincr")
continue;
2176 if (ic < nrowInsert-1) outs <<
",";
2183 std::cerr <<
"Table::WriteToDB: Executing PGSQL command: \n\t" 2190 boost::posix_time::ptime ctt1;
2191 boost::posix_time::ptime ctt2;
2194 ctt1 = boost::posix_time::microsec_clock::local_time();
2199 ctt2 = boost::posix_time::microsec_clock::local_time();
2200 boost::posix_time::time_duration
tdiff = ctt2 - ctt1;
2201 std::cerr <<
"Table::WriteToDB(" <<
Name() <<
"): query took " 2202 << tdiff.total_milliseconds() <<
" ms" <<
std::endl;
2205 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2207 std::cerr <<
"INSERT failed: " << PQerrorMessage(
fConnection)
2219 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2220 if (
fCol[j].
Type() ==
"autoincr") {
2223 fRow[i].Col(j).Set(seqstr,
true);
2235 if (
fRow[i].NModified() > 0 ) {
2239 std::ostringstream outs;
2240 outs <<
"UPDATE " <<
Schema() <<
"." <<
Name() <<
" SET ";
2242 for (
unsigned int j=0; j<
fCol.size() && im < r.
NModified(); ++j) {
2244 outs <<
fCol[j].Name() +
"=";
2253 for (
int j=0; j<nkey; ++j) {
2255 int pkeyIdx = colMap[pkey];
2256 outs << pkey <<
"=" << r.
Col(pkeyIdx);
2257 if (j < (nkey-1)) outs <<
" and ";
2261 std::cerr <<
"Table::WriteToDB: Executing PGSQL command: \n\t" 2269 if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2300 int ncol = this->
NCol();
2301 int nrow = this->
NRow();
2303 ss <<
"channel,tv,";
2305 for (
int i=0; i<ncol; ++i) {
2307 if(!first) ss <<
",";
2313 ss <<
"tolerance,,";
2315 for (
int j=0; j<ncol; ++j) {
2318 if(!first) ss <<
",";
2322 if (ctype ==
"double")
2324 else if (ctype ==
"float")
2331 for (
int i=0; i<nrow; ++i) {
2337 for (
int j=0; j<ncol; ++j) {
2338 if(!first) ss <<
",";
2352 std::cerr <<
"Table::Write: Data type mask is not set!" <<
std::endl;
2357 std::cerr <<
"Table::Write: Web Service URL is not set!" <<
std::endl;
2363 if (!putURL.empty())
2367 std::stringstream ss;
2374 std::stringstream typeStr;
2375 typeStr <<
"&type=";
2379 url += typeStr.str();
2384 boost::posix_time::ptime ctt1;
2385 boost::posix_time::ptime ctt2;
2388 ctt1 = boost::posix_time::microsec_clock::local_time();
2391 std::cout <<
"Posting data to: " << url <<
std::endl;
2393 postHTTPsigned(url.c_str(), pwd.c_str(), NULL, 0,
2394 ss.str().c_str(), ss.str().length(), &status);
2396 ctt2 = boost::posix_time::microsec_clock::local_time();
2397 boost::posix_time::time_duration
tdiff = ctt2 - ctt1;
2398 std::cerr <<
"Table::Write(" <<
Name() <<
"): query took " 2399 << tdiff.total_milliseconds() <<
" ms" <<
std::endl;
2401 return (status == 0);
2412 fout.open(fname.c_str());
2414 fout.open(fname.c_str(),std::ios_base::app);
2417 std::stringstream ss;
2422 if (writeColNames) {
2423 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2424 fout <<
fCol[j].Name();
2425 if (j<
fCol.size()-1) fout <<
",";
2429 for (
unsigned int i=0; i<
fRow.size(); ++i) {
2430 for (
unsigned int j=0; j<
fCol.size(); ++j) {
2431 fout <<
fRow[i].Col(j);
2432 if (j<
fCol.size()-1) fout <<
",";
2460 if (i >=
fCol.size())
return false;
2477 if (i >=
fCol.size())
return false;
2497 if (!c)
return false;
2515 if (!c)
return false;
2532 if (i >=
fCol.size())
return false;
2535 for (
unsigned int j=0; j<
fOrderCol.size(); ++j)
2549 if (i >=
fCol.size())
return false;
2552 for (
unsigned int j=0; j<
fOrderCol.size(); ++j)
2569 if (!c)
return false;
2571 for (
unsigned int j=0; j<
fOrderCol.size(); ++j)
2587 if (!c)
return false;
2589 for (
unsigned int j=0; j<
fOrderCol.size(); ++j)
2603 char* pwdFile =
getenv(
"DBIWSPWDFILE");
2607 if (!fin.is_open() || !fin.good()) {
2608 std::cerr <<
"Could not open password file " << pwdFile
2609 <<
". Canceling Table::Write()" <<
std::endl;
code to link reconstructed objects back to the MC truth information
def line(rflist, normalization=13700 *units.eplus)
static std::string trim(const std::string &str, const std::string &whitespace=" \t")
QTextStream & hex(QTextStream &s)
Q_EXPORT QTSManip setprecision(int p)
std::string getenv(std::string const &name)
unsigned __int64 uint64_t
void err(const char *fmt,...)
Q_EXPORT QTSManip setw(int w)
const GenericPointer< typename T::ValueType > T2 value
static double tdiff(const art::Timestamp &ts1, const art::Timestamp &ts2)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
QTextStream & endl(QTextStream &s)