8#include "daq/slc/database/MonitorDB.h"
10#include <daq/slc/database/DBHandlerException.h>
11#include <daq/slc/system/LogFile.h>
15void MonitorDB::add(
DBInterface& db,
const std::string& table,
16 const std::string& vname,
int val)
19 createTable(db, table);
20 db.execute(
"insert into %s (name, value_i) values ('%s', %d);",
21 table.c_str(), vname.c_str(), val);
23 LogFile::error(e.what());
28void MonitorDB::add(
DBInterface& db,
const std::string& table,
29 const std::string& vname,
float val)
32 createTable(db, table);
33 db.execute(
"insert into %s (name, value_f) values ('%s', %f);",
34 table.c_str(), vname.c_str(), val);
36 LogFile::error(e.what());
41void MonitorDB::add(
DBInterface& db,
const std::string& table,
42 const std::string& vname,
const std::string& val)
45 createTable(db, table);
46 db.execute(
"insert into %s (name, value_t) values ('%s', '%s');",
47 table.c_str(), vname.c_str(), val.c_str());
49 LogFile::error(e.what());
55NSMVarList MonitorDB::get(
DBInterface& db,
const std::string& tablename,
56 const std::string& vname)
58 if (!db.isConnected()) db.connect();
59 db.execute(
"select *, extract(epoch from record_time) rtime "
60 "from %s p where p.name = '%s' order by id;",
61 tablename.c_str(), vname.c_str());
62 return readTable(db, vname);
65NSMVarList MonitorDB::get(
DBInterface& db,
const std::string& tablename,
66 const std::string& vname,
int max)
68 if (!db.isConnected()) db.connect();
69 db.execute(
"select *, extract(epoch from record_time) rtime "
70 "from %s p where p.name = '%s' limit %d order by id;",
71 tablename.c_str(), vname.c_str(), max);
72 return readTable(db, vname);
75NSMVarList MonitorDB::get(
DBInterface& db,
const std::string& tablename,
76 const std::string& vname,
const Date& start,
79 if (!db.isConnected()) db.connect();
80 db.execute(
"select *, extract(epoch from record_time) rtime "
81 "from %s p where p.name = '%s' and record_time > '%s' and "
82 "record_time < '%s'order by id;", tablename.c_str(),
83 vname.c_str(), start.toString(), end.toString());
84 return readTable(db, vname);
87NSMVarList MonitorDB::get(
DBInterface& db,
const std::string& tablename,
88 const std::string& vname,
int max,
91 if (!db.isConnected()) db.connect();
92 db.execute(
"select *, extract(epoch from record_time) rtime "
93 "from %s p where p.name = '%s' and record_time > '%s' and "
94 "record_time < '%s' limit %d order by id;",
95 tablename.c_str(), vname.c_str(),
96 start.toString(), end.toString(), max);
97 return readTable(db, vname);
100NSMVarList MonitorDB::readTable(
DBInterface& db,
const std::string& vname)
102 DBRecordList record_v(db.loadRecords());
104 for (
size_t i = 0; i < record_v.size(); i++) {
107 if (record.hasField(
"value_b")) {
108 var = (int)record.getBool(
"value_b");
109 }
else if (record.hasField(
"value_c")) {
110 var = (int)record.getInt(
"value_c");
111 }
else if (record.hasField(
"value_s")) {
112 var = (int)record.getInt(
"value_s");
113 }
else if (record.hasField(
"value_i")) {
114 var = record.getInt(
"value_i");
115 }
else if (record.hasField(
"value_f")) {
116 var = record.getFloat(
"value_f");
117 }
else if (record.hasField(
"value_d")) {
118 var = record.getFloat(
"value_d");
119 }
else if (record.hasField(
"value_t")) {
120 var = record.get(
"value_t");
122 var.setId(record.getInt(
"id"));
123 var.setDate(record.getInt(
"rtime"));
124 LogFile::debug(
"%d=%s", record.getInt(
"rtime"),
Date(var.getDate()).toString());
130void MonitorDB::createTable(
DBInterface& db,
const std::string& tablename)
132 if (!db.isConnected()) db.connect();
133 if (!db.checkTable(tablename)) {
134 db.execute(
"create table %s \n"
135 "(name varchar(64), \n"
137 "record_time timestamp with time zone default current_timestamp, \n"
138 "value_b boolean default NULL, \n"
139 "value_c char default NULL, \n"
140 "value_s smallint default NULL, \n"
141 "value_i int default NULL, \n"
142 "value_l bigint default NULL, \n"
143 "value_f float default NULL, \n"
144 "value_d double precision default NULL, \n"
145 "value_t text default NULL \n"
146 "); ", tablename.c_str());
147 db.execute(
"create index %s_id_index on %s(id);",
148 tablename.c_str(), tablename.c_str());
Abstract base class for different kinds of events.