5 #include <onlmon/OnlMonBase.h>
58 std::ostringstream
cmd;
59 cmd <<
"SELECT * FROM pg_tables where tablename = '" <<
table <<
"'";
68 std::cout <<
"caught exception Message: " << e.
getMessage() << std::endl;
77 std::cout <<
"need to create table" << std::endl;
80 cmd <<
"CREATE TABLE " << table <<
"(date timestamp(0) with time zone NOT NULL, run int NOT NULL, primary key(date,run))";
87 std::cout <<
"caught exception Message: " << e.
getMessage() << std::endl;
94 std::cout <<
"table " << table <<
" exists" << std::endl;
114 std::ostringstream
cmd;
116 cmd <<
"select * from " <<
table <<
" limit 1";
117 std::map<const std::string, OnlMonDBVar*>::const_iterator iter;
124 std::cout <<
"caught exception Message: " << e.
getMessage() << std::endl;
128 for (iter = varmap.begin(); iter != varmap.end(); ++iter)
133 transform(varname.begin(), varname.end(), varname.begin(), (int (*)(int)) tolower);
143 std::cout << thisvar <<
" is in table" << std::endl;
149 cmd <<
"ALTER TABLE " << table <<
" ADD COLUMN "
150 << thisvar <<
" real";
153 std::cout <<
"executing sql command: " << cmd.str() << std::endl;
164 std::cout <<
"Table " << table <<
" update failed" << std::endl;
165 std::cout <<
"Message: " << e.
getMessage() << std::endl;
187 transform(tablename.begin(), tablename.end(), tablename.begin(), (int (*)(int)) tolower);
191 std::ostringstream
cmd;
192 cmd <<
"DROP TABLE " << tablename;
195 std::cout <<
"iret: " << iret << std::endl;
208 std::ostringstream
cmd;
209 cmd <<
"SELECT * FROM " <<
table;
210 std::cout <<
"Executing " << cmd.str() << std::endl;
224 std::cout <<
"TimeStamp: " << (
id.toString()) <<
" Run #= " << Name << std::endl;
244 std::cout <<
"Exception caught" << std::endl;
245 std::cout <<
"Message: " << e.
getMessage() << std::endl;
265 std::cout <<
"Catalog: " << catalog << std::endl;
267 std::cout <<
"Schema: " << schemapattern << std::endl;
269 std::cout <<
"Table: " << tablenamepattern << std::endl;
270 std::vector<std::string>
types;
274 std::cout <<
"1: " << rs->
getString(1) << std::endl;
275 std::cout <<
"2: " << rs->
getString(2) << std::endl;
276 std::cout <<
"3: " << rs->
getString(3) << std::endl;
277 std::cout <<
"4: " << rs->
getString(4) << std::endl;
278 std::cout <<
"5: " << rs->
getString(5) << std::endl;
282 std::cout <<
"rs1: " << rs1 << std::endl;
286 std::cout <<
"Table: " << rs->
getString(
"TABLE_CAT") << std::endl;
287 std::cout <<
"Table: " << rs->
getString(3) << std::endl;
296 transform(tablename.begin(), tablename.end(), tablename.begin(), (int (*)(int)) tolower);
297 std::ostringstream
cmd;
298 cmd <<
"select * from " << tablename;
305 std::cout <<
"Row: " << rs->
getRow() <<
", id=" <<
id <<
" Name= " << Name << std::endl;
313 std::cout <<
"DB Name: " <<
dbname << std::endl;
314 std::cout <<
"DB Owner: " <<
dbowner << std::endl;
315 std::cout <<
"DB Pwd: " <<
dbpasswd << std::endl;
316 std::cout <<
"DB table: " <<
table << std::endl;
323 if (ticks == 0 || runnumber <= 0)
327 std::map<const std::string, OnlMonDBVar*>::const_iterator iter;
330 std::ostringstream
cmd, cmd1, datestream;
342 mintime.
setTime(ticks - minutesinterval * 60);
343 maxtime.
setTime(ticks + minutesinterval * 60);
346 std::cout <<
"mintime stp: " << mintime.
toString() << std::endl;
347 std::cout <<
"maxtime stp: " << maxtime.
toString() << std::endl;
352 datestream <<
"date > '" << mintime.
toString()
353 <<
"' and date < '" << maxtime.
toString() <<
"'";
354 cmd <<
"SELECT COUNT(*) FROM " <<
table <<
" WHERE run = "
355 << runnumber <<
" and " << datestream.str();
358 std::cout <<
"cmd: " << cmd.str() << std::endl;
369 std::cout <<
"Exception caught" << std::endl;
370 std::cout <<
"Message: " << e.
getMessage() << std::endl;
371 std::cout <<
"sql cmd: " << cmd.str() << std::endl;
381 std::cout <<
"found rows: " << haverow << std::endl;
388 minutesinterval = minutesinterval / 2;
391 else if (haverow == 1)
394 cmd <<
"SELECT * FROM " <<
table <<
" WHERE " << datestream.str();
397 std::cout <<
"command: " << cmd.str() << std::endl;
406 for (iter = varmap.begin(); iter != varmap.end(); ++iter)
408 if (iter->second->wasupdated())
411 float varqual = iter->second->GetVar(2);
412 float sqlvarqual = rs->
getFloat(varqualname);
413 if (varqual > sqlvarqual || rs->
wasNull())
417 for (
unsigned int j = 0;
j < 3;
j++)
420 cmd <<
"UPDATE " <<
table <<
" SET "
422 << iter->second->GetVar(
j) <<
" WHERE "
426 std::cout <<
"Command: " << cmd.str() << std::endl;
432 std::cout << __PRETTY_FUNCTION__ <<
"Update failed please send mail to pinkenburg@bnl.gov"
434 std::cout <<
"And include the macro and the following info" << std::endl;
435 std::cout <<
"TableName: " <<
table << std::endl;
436 std::cout <<
"Variable: " << iter->first <<
addvarname[
j] << std::endl;
437 std::cout <<
"Value: " << iter->second->GetVar(
j) << std::endl;
449 cmd <<
"INSERT INTO " <<
table <<
"(date, run";
452 for (iter = varmap.begin(); iter != varmap.end(); ++iter)
454 if (iter->second->wasupdated())
456 for (
unsigned int j = 0;
j < 3;
j++)
459 cmd1 <<
", " << iter->second->GetVar(
j);
466 printf(
"No updated values\n");
475 std::cout << cmd.str() << std::endl;
486 if (errmsg.find(
"Cannot insert a duplicate key into unique index") != std::string::npos)
489 std::cout <<
"Identical entry already in DB" << std::endl;
495 std::cout << __PRETTY_FUNCTION__ <<
" DB Error in execute stmt: " << e.
getMessage() << std::endl;
497 savesql << cmd.str() << std::endl;
518 std::ostringstream
cmd, datestream;
519 datestream <<
"date > '" << mintime.
toString()
520 <<
"' and date < '" << maxtime.
toString() <<
"'";
521 cmd <<
"SELECT COUNT(*) FROM " <<
table <<
" WHERE " << datestream.str();
524 std::cout <<
"command: " << cmd.str() << std::endl;
534 std::cout <<
"Exception caught, probably your table "
536 <<
" does not exist" << std::endl;
537 std::cout <<
"Message: " << e.
getMessage() << std::endl;
549 timestp.resize(nrows);
550 runnumber.resize(nrows);
552 varerr.resize(nrows);
560 cmd <<
"SELECT date, run, " << varname <<
", " << varnameerr
561 <<
" FROM " <<
table <<
" WHERE "
562 << datestream.str() <<
" ORDER BY date ASC";
565 std::cout <<
"Command: " << cmd.str() << std::endl;
574 std::cout <<
"Exception caught, probably your variable "
575 << varname <<
" or the table " <<
table
576 <<
" does not exist" << std::endl;
577 std::cout <<
"Message: " << e.
getMessage() << std::endl;
584 unsigned int index = 0;
585 int filledrows = nrows;
600 if (filledrows != nrows)
602 varerr.resize(filledrows);
603 var.resize(filledrows);
604 timestp.resize(filledrows);
605 runnumber.resize(filledrows);
622 std::cout << __PRETTY_FUNCTION__
623 <<
" Exception caught during DriverManager::getConnection" << std::endl;
624 std::cout <<
"Message: " << e.
getMessage() << std::endl;
632 printf(
"opened DB connection\n");