8#include "daq/slc/database/RunNumberTable.h"
10#include <daq/slc/database/DBHandlerException.h>
11#include <daq/slc/database/DBInterface.h>
13#include <daq/slc/system/LogFile.h>
17RunNumber RunNumberTable::add(
const std::string& node,
18 const std::string& runtype,
21 if (runtype.size() == 0 || node.size() == 0) {
22 LogFile::fatal(
"Empty node name : node='%s' runtype='%s'", node.c_str(), runtype.c_str());
25 if (runtype.size() == 0 || node.size() == 0) {
26 LogFile::fatal(
"Empty runtype : node='%s' runtype='%s'", node.c_str(), runtype.c_str());
29 if (node ==
"pxd" || node ==
"svd" || node ==
"cdc" || node ==
"top" ||
30 node ==
"arich" || node ==
"ecl" || node ==
"klm" || node ==
"trg" || node ==
"b4test") {
31 if (node != runtype) {
32 LogFile::fatal(
"wrong node name or runtype : node='%s' != runtype='%s'", node.c_str(), runtype.c_str());
35 }
else if (node ==
"global") {
36 if (runtype !=
"physics" && runtype !=
"cosmic" && runtype !=
"beam" &&
37 runtype !=
"null" && runtype !=
"debug" && runtype !=
"hlttest") {
38 LogFile::fatal(
"wrong node name or runtype : node='%s' runtype='%s'!=physics/cosmic/beam/debug/hlttest/null", node.c_str(),
43 LogFile::fatal(
"wrong node name : node='%s'!=global/pxd/svd/cdc/top/arich/ecl/klm/trg/b4test runtype='%s'", node.c_str(),
47 int expno_tmp = getExpNumber(
"");
48 if (node !=
"global" || expno < expno_tmp) expno = expno_tmp;
49 int runno_tmp = getRunNumber(node, expno);
50 if (runno < runno_tmp) runno = runno_tmp;
51 m_db.execute(
"insert into runnumber_new (node, runtype, expno, runno) "
52 "values ('%s', '%s', %d, %d) "
53 "returning id, extract(epoch from record_time) as tstart;",
54 node.c_str(), runtype.c_str(), expno, runno);
55 DBRecordList record_v(m_db.loadRecords());
56 if (record_v.size() > 0) {
57 int id = record_v[0].getInt(
"id");
58 int record_time = record_v[0].getInt(
"tstart");
59 return RunNumber(node, runtype, expno, runno,
id, record_time);
62 LogFile::error(
"DB access error : %s", e.what());
69 return add(
rn.getNode(),
rn.getRunType(),
rn.getExpNumber(),
rn.getRunNumber());
72int RunNumberTable::getRunNumber(
const std::string& ,
int expno)
74 if (expno <= 0)
return -1;
79 m_db.execute(
"select max(runno)+1 as runno from runnumber_new "
82 m_db.execute(
"select max(runno)+1 as runno from runnumber_new "
83 "where expno = %d limit 1;", expno);
97 DBRecordList record_v(m_db.loadRecords());
98 if (record_v.size() > 0) {
99 runno = record_v[0].getInt(
"runno");
100 if (runno <= 0) runno = 1;
103 LogFile::error(
"DB access error : %s", e.what());
109int RunNumberTable::getExpNumber(
const std::string& node)
113 if (node.size() == 0) {
114 m_db.execute(
"select max(expno) as expno from runnumber_new limit 1;");
116 m_db.execute(
"select max(expno) as expno from runnumber_new "
117 "where node = '%s' limit 1;", node.c_str());
119 DBRecordList record_v(m_db.loadRecords());
120 if (record_v.size() > 0) {
121 expno = record_v[0].getInt(
"expno");
122 if (expno <= 0) expno = 1;
125 LogFile::error(
"DB access error : %s", e.what());
131RunNumberList RunNumberTable::get(
const std::string& node,
int expno,
int runno_min,
int runno_max)
135 const char* cmd =
"select node, runtype, expno, runno, id, "
136 "extract(epoch from record_time) as record_time from runnumber_new";
137 if (node.size() == 0) {
140 m_db.execute(
"%s where runno >= %d and runno <= %d order by id", cmd, runno_min, runno_max);
142 m_db.execute(
"%s where runno >= %d order by id", cmd, runno_min);
146 m_db.execute(
"%s where expno = %d and runno >= %d and runno <= %d order by id",
147 cmd, expno, runno_min, runno_max);
149 m_db.execute(
"%s where expno = %d and runno >= %d order by id", cmd, expno, runno_min);
155 m_db.execute(
"%s where node = '%s' and runno >= %d and runno <= %d order by id",
156 cmd, node.c_str(), runno_min, runno_max);
158 m_db.execute(
"%s where node = '%s' and runno >= %d order by id", cmd, node.c_str(), runno_min);
162 m_db.execute(
"%s where node = '%s' and expno = %d and runno >= %d and runno <= %d order by id",
163 cmd, node.c_str(), expno, runno_min, runno_max);
165 m_db.execute(
"%s where node = '%s' and expno = %d and runno >= %d order by id",
166 cmd, node.c_str(), expno, runno_min);
170 const DBRecordList record_v(m_db.loadRecords());
171 for (DBRecordList::const_iterator it = record_v.begin();
172 it != record_v.end(); ++it) {
174 list.push_back(
RunNumber(record.get(
"node"), record.get(
"runtype"),
175 record.getInt(
"expno"), record.getInt(
"runno"),
176 record.getInt(
"id"), record.getInt(
"record_time")));
179 LogFile::error(
"DB access error : %s", e.what());
184RunNumberList RunNumberTable::get(
int expno,
int runno_min,
int runno_max)
186 return get(
"", expno, runno_min, runno_max);
189void RunNumberTable::create()
191 if (!m_db.checkTable(
"runnumber_new")) {
192 m_db.execute(
"create table runnumber_new ("
193 "record_time timestamp with time zone "
194 "not null default current_timestamp, "
195 "id serial not null primary key, "
196 "node text not null, "
197 "runtype text not null, "
198 "expno int not null, "
199 "runno int not null, "
200 "unique (node, expno, runno));");
201 m_db.execute(
"create index runnumber_new_id_index on runnumber_new(id);");
TString rn()
Get random string.
Abstract base class for different kinds of events.