1 #include "daq/slc/database/MonitorDB.h"
3 #include <daq/slc/database/DBHandlerException.h>
4 #include <daq/slc/system/LogFile.h>
8 void MonitorDB::add(
DBInterface& db,
const std::string& table,
9 const std::string& vname,
int val)
12 createTable(db, table);
13 db.execute(
"insert into %s (name, value_i) values ('%s', %d);",
14 table.c_str(), vname.c_str(), val);
16 LogFile::error(
e.what());
21 void MonitorDB::add(
DBInterface& db,
const std::string& table,
22 const std::string& vname,
float val)
25 createTable(db, table);
26 db.execute(
"insert into %s (name, value_f) values ('%s', %f);",
27 table.c_str(), vname.c_str(), val);
29 LogFile::error(
e.what());
34 void MonitorDB::add(
DBInterface& db,
const std::string& table,
35 const std::string& vname,
const std::string& val)
38 createTable(db, table);
39 db.execute(
"insert into %s (name, value_t) values ('%s', '%s');",
40 table.c_str(), vname.c_str(), val.c_str());
42 LogFile::error(
e.what());
48 NSMVarList MonitorDB::get(
DBInterface& db,
const std::string& tablename,
49 const std::string& vname)
51 if (!db.isConnected()) db.connect();
52 db.execute(
"select *, extract(epoch from record_time) rtime "
53 "from %s p where p.name = '%s' order by id;",
54 tablename.c_str(), vname.c_str());
55 return readTable(db, vname);
58 NSMVarList MonitorDB::get(
DBInterface& db,
const std::string& tablename,
59 const std::string& vname,
int max)
61 if (!db.isConnected()) db.connect();
62 db.execute(
"select *, extract(epoch from record_time) rtime "
63 "from %s p where p.name = '%s' limit %d order by id;",
64 tablename.c_str(), vname.c_str(), max);
65 return readTable(db, vname);
68 NSMVarList MonitorDB::get(
DBInterface& db,
const std::string& tablename,
69 const std::string& vname,
const Date& start,
72 if (!db.isConnected()) db.connect();
73 db.execute(
"select *, extract(epoch from record_time) rtime "
74 "from %s p where p.name = '%s' and record_time > '%s' and "
75 "record_time < '%s'order by id;", tablename.c_str(),
76 vname.c_str(), start.toString(), end.toString());
77 return readTable(db, vname);
80 NSMVarList MonitorDB::get(
DBInterface& db,
const std::string& tablename,
81 const std::string& vname,
int max,
84 if (!db.isConnected()) db.connect();
85 db.execute(
"select *, extract(epoch from record_time) rtime "
86 "from %s p where p.name = '%s' and record_time > '%s' and "
87 "record_time < '%s' limit %d order by id;",
88 tablename.c_str(), vname.c_str(),
89 start.toString(), end.toString(), max);
90 return readTable(db, vname);
93 NSMVarList MonitorDB::readTable(
DBInterface& db,
const std::string& vname)
95 DBRecordList record_v(db.loadRecords());
97 for (
size_t i = 0; i < record_v.size(); i++) {
100 if (record.hasField(
"value_b")) {
101 var = (int)record.getBool(
"value_b");
102 }
else if (record.hasField(
"value_c")) {
103 var = (int)record.getInt(
"value_c");
104 }
else if (record.hasField(
"value_s")) {
105 var = (int)record.getInt(
"value_s");
106 }
else if (record.hasField(
"value_i")) {
107 var = record.getInt(
"value_i");
108 }
else if (record.hasField(
"value_f")) {
109 var = record.getFloat(
"value_f");
110 }
else if (record.hasField(
"value_d")) {
111 var = record.getFloat(
"value_d");
112 }
else if (record.hasField(
"value_t")) {
113 var = record.get(
"value_t");
115 var.setId(record.getInt(
"id"));
116 var.setDate(record.getInt(
"rtime"));
117 LogFile::debug(
"%d=%s", record.getInt(
"rtime"),
Date(var.getDate()).toString());
123 void MonitorDB::createTable(
DBInterface& db,
const std::string& tablename)
125 if (!db.isConnected()) db.connect();
126 if (!db.checkTable(tablename)) {
127 db.execute(
"create table %s \n"
128 "(name varchar(64), \n"
130 "record_time timestamp with time zone default current_timestamp, \n"
131 "value_b boolean default NULL, \n"
132 "value_c char default NULL, \n"
133 "value_s smallint default NULL, \n"
134 "value_i int default NULL, \n"
135 "value_l bigint default NULL, \n"
136 "value_f float default NULL, \n"
137 "value_d double precision default NULL, \n"
138 "value_t text default NULL \n"
139 "); ", tablename.c_str());
140 db.execute(
"create index %s_id_index on %s(id);",
141 tablename.c_str(), tablename.c_str());