1 #include "daq/slc/database/RunNumberTable.h"
3 #include <daq/slc/database/DBHandlerException.h>
4 #include <daq/slc/database/DBInterface.h>
6 #include <daq/slc/system/LogFile.h>
10 RunNumber RunNumberTable::add(
const std::string& node,
11 const std::string& runtype,
14 if (runtype.size() == 0 || node.size() == 0) {
15 LogFile::fatal(
"Empty node name : node='%s' runtype='%s'", node.c_str(), runtype.c_str());
18 if (runtype.size() == 0 || node.size() == 0) {
19 LogFile::fatal(
"Empty runtype : node='%s' runtype='%s'", node.c_str(), runtype.c_str());
22 if (node ==
"pxd" || node ==
"svd" || node ==
"cdc" || node ==
"top" ||
23 node ==
"arich" || node ==
"ecl" || node ==
"klm" || node ==
"trg" || node ==
"b4test") {
24 if (node != runtype) {
25 LogFile::fatal(
"wrong node name or runtype : node='%s' != runtype='%s'", node.c_str(), runtype.c_str());
28 }
else if (node ==
"global") {
29 if (runtype !=
"physics" && runtype !=
"cosmic" &&
30 runtype !=
"beam" && runtype !=
"null" && runtype !=
"debug") {
31 LogFile::fatal(
"wrong node name or runtype : node='%s' runtype='%s'!=physics/cosmic/beam/debug/null", node.c_str(),
36 LogFile::fatal(
"wrong node name : node='%s'!=global/pxd/svd/cdc/top/arich/ecl/klm/trg/b4test runtype='%s'", node.c_str(),
40 int expno_tmp = getExpNumber(
"");
41 if (node !=
"global" || expno < expno_tmp) expno = expno_tmp;
42 int runno_tmp = getRunNumber(node, expno);
43 if (runno < runno_tmp) runno = runno_tmp;
44 m_db.execute(
"insert into runnumber_new (node, runtype, expno, runno) "
45 "values ('%s', '%s', %d, %d) "
46 "returning id, extract(epoch from record_time) as tstart;",
47 node.c_str(), runtype.c_str(), expno, runno);
48 DBRecordList record_v(m_db.loadRecords());
49 if (record_v.size() > 0) {
50 int id = record_v[0].getInt(
"id");
51 int record_time = record_v[0].getInt(
"tstart");
52 return RunNumber(node, runtype, expno, runno,
id, record_time);
55 LogFile::error(
"DB access error : %s",
e.what());
62 return add(
rn.getNode(),
rn.getRunType(),
rn.getExpNumber(),
rn.getRunNumber());
65 int RunNumberTable::getRunNumber(
const std::string& node,
int expno)
67 if (expno <= 0)
return -1;
72 m_db.execute(
"select max(runno)+1 as runno from runnumber_new "
75 m_db.execute(
"select max(runno)+1 as runno from runnumber_new "
76 "where expno = %d limit 1;", expno);
90 DBRecordList record_v(m_db.loadRecords());
91 if (record_v.size() > 0) {
92 runno = record_v[0].getInt(
"runno");
93 if (runno <= 0) runno = 1;
96 LogFile::error(
"DB access error : %s",
e.what());
102 int RunNumberTable::getExpNumber(
const std::string& node)
106 if (node.size() == 0) {
107 m_db.execute(
"select max(expno) as expno from runnumber_new limit 1;");
109 m_db.execute(
"select max(expno) as expno from runnumber_new "
110 "where node = '%s' limit 1;", node.c_str());
112 DBRecordList record_v(m_db.loadRecords());
113 if (record_v.size() > 0) {
114 expno = record_v[0].getInt(
"expno");
115 if (expno <= 0) expno = 1;
118 LogFile::error(
"DB access error : %s",
e.what());
124 RunNumberList RunNumberTable::get(
const std::string& node,
int expno,
int runno_min,
int runno_max)
128 const char* cmd =
"select node, runtype, expno, runno, id, "
129 "extract(epoch from record_time) as record_time from runnumber_new";
130 if (node.size() == 0) {
133 m_db.execute(
"%s where runno >= %d and runno <= %d order by id", cmd, runno_min, runno_max);
135 m_db.execute(
"%s where runno >= %d order by id", cmd, runno_min);
139 m_db.execute(
"%s where expno = %d and runno >= %d and runno <= %d order by id",
140 cmd, expno, runno_min, runno_max);
142 m_db.execute(
"%s where expno = %d and runno >= %d order by id", cmd, expno, runno_min);
148 m_db.execute(
"%s where node = '%s' and runno >= %d and runno <= %d order by id",
149 cmd, node.c_str(), runno_min, runno_max);
151 m_db.execute(
"%s where node = '%s' and runno >= %d order by id", cmd, node.c_str(), runno_min);
155 m_db.execute(
"%s where node = '%s' and expno = %d and runno >= %d and runno <= %d order by id",
156 cmd, node.c_str(), expno, runno_min, runno_max);
158 m_db.execute(
"%s where node = '%s' and expno = %d and runno >= %d order by id",
159 cmd, node.c_str(), expno, runno_min);
163 const DBRecordList record_v(m_db.loadRecords());
164 for (DBRecordList::const_iterator it = record_v.begin();
165 it != record_v.end(); it++) {
167 list.push_back(
RunNumber(record.get(
"node"), record.get(
"runtype"),
168 record.getInt(
"expno"), record.getInt(
"runno"),
169 record.getInt(
"id"), record.getInt(
"record_time")));
172 LogFile::error(
"DB access error : %s",
e.what());
177 RunNumberList RunNumberTable::get(
int expno,
int runno_min,
int runno_max)
179 return get(
"", expno, runno_min, runno_max);
182 void RunNumberTable::create()
184 if (!m_db.checkTable(
"runnumber_new")) {
185 m_db.execute(
"create table runnumber_new ("
186 "record_time timestamp with time zone "
187 "not null default current_timestamp, "
188 "id serial not null primary key, "
189 "node text not null, "
190 "runtype text not null, "
191 "expno int not null, "
192 "runno int not null, "
193 "unique (node, expno, runno));");
194 m_db.execute(
"create index runnumber_new_id_index on runnumber_new(id);");