264{
265 std::string tablename_date = tablename +
"_" +
Date().toString(
"%Y");
266 std::string tablename_id = tablename + "_id";
267 try {
268 if (!db.isConnected()) db.connect();
270 LogFile::error(e.what());
271 return false;
272 }
273 try {
274 if (obj.getName().size() == 0) {
275 LogFile::error("Configname is null. createDB canceled.");
276 return false;
277 }
278 if (!db.checkTable("daqconfig")) {
279 db.execute("create table daqconfig \n"
280 "(name text not null, \n"
281 "id bigserial, lastupdate timestamp, \n"
282 "UNIQUE(name));");
283 db.execute("create index daqconfig_id_index on daqconfig(id);",
284 tablename.c_str(), tablename.c_str());
285 }
286 if (!db.checkTable(tablename_id)) {
287 try {
288 db.execute("insert into daqconfig (name, lastupdate) values "
289 "('%s', current_timestamp);", tablename_id.c_str());
290 } catch (const std::exception& e) {
291 db.execute("update daqconfig set lastupdate = current_timestamp where name = '%s';",
292 tablename_id.c_str());
293 }
294 db.execute("create table %s \n"
295 "(name text not null \n"
296 "check (replace(name, '.', '') = name) not null, \n"
297 "id bigserial, \n"
298 "content text, \n"
299 "record_time timestamp with time zone default current_timestamp, \n"
300 "UNIQUE (name)); ", tablename_id.c_str());
301 }
302 if (!db.checkTable(tablename_date)) {
303 try {
304 db.execute("insert into daqconfig (name, lastupdate) values "
305 "('%s', current_timestamp);", tablename_date.c_str());
306 } catch (const std::exception& e) {
307 db.execute("update daqconfig set lastupdate = current_timestamp where name = '%s';",
308 tablename_date.c_str());
309 }
310 db.execute("create table %s \n"
311 "(name text not null,\n"
312 "id bigserial, \n"
313 "pid bigint, \n"
314 "value_b boolean default NULL, \n"
315 "value_i int default NULL, \n"
316 "value_f float default NULL, \n"
317 "value_t text default NULL \n"
318 "); ", tablename_date.c_str());
319 db.execute("create index %s_index on %s(id);",
320 tablename_date.c_str(), tablename_date.c_str());
321 }
323 LogFile::warning(e.what());
324 }
325 try {
326 db.execute("begin;");
327 bool failed = true;
328 int id = 1;
329 try {
330 db.execute("insert into %s (name, content) values ('%s','%s') returning id;",
331 tablename_id.c_str(), obj.getName().c_str(), tablename_date.c_str());
332 failed = false;
333 DBRecordList record(db.loadRecords());
334 if (record.size() > 0) {
335 id = record[0].getInt("id");
336 }
338 LogFile::error(e.what());
339 }
340 db.execute("commit;");
341 if (failed) return false;
342 std::string s = obj.printSQL(tablename_date, id);
343 db.execute(s);
345 LogFile::error(e.what());
346 return false;
347 }
348 return true;
349}