342 IOVTimeStamp begin_ts(0, 0);
343 IOVTimeStamp end_ts(0, 0);
344 std::vector<std::string> column_names;
345 std::vector<std::string> column_types;
346 std::vector<DBChannelID_t>
channels;
347 std::vector<DBDataset::value_type>
values;
359 if(rc != SQLITE_OK) {
368 std::ostringstream sql;
369 sql <<
"SELECT " << table_iovs <<
".iov_id," << table_iovs <<
".begin_time" 370 <<
" FROM " << table_tag_iovs <<
"," << table_iovs
371 <<
" WHERE " << table_tag_iovs <<
".tag='" <<
fTag <<
"'" 372 <<
" AND " << table_tag_iovs <<
".iov_id=" << table_iovs <<
".iov_id" 373 <<
" AND " << table_iovs <<
".begin_time <= " <<
t 374 <<
" ORDER BY " << table_iovs <<
".begin_time desc";
380 rc = sqlite3_prepare_v2(db, sql.str().c_str(), -1, &stmt, 0);
381 if(rc != SQLITE_OK) {
383 log <<
"sqlite3_prepare_v2 failed." <<
fSQLitePath <<
"\n";
384 log <<
"Failed sql = " << sql.str() <<
"\n";
392 rc = sqlite3_step(stmt);
395 if(rc == SQLITE_ROW) {
397 begin_time = sqlite3_column_int(stmt, 1);
403 mf::LogError(
"DBFolder") <<
"sqlite3_step returned error result = " << rc <<
"\n";
409 sqlite3_finalize(stmt);
414 sql <<
"SELECT " << table_iovs <<
".begin_time" 415 <<
" FROM " << table_tag_iovs <<
"," << table_iovs
416 <<
" WHERE " << table_tag_iovs <<
".tag='" <<
fTag <<
"'" 417 <<
" AND " << table_tag_iovs <<
".iov_id=" << table_iovs <<
".iov_id" 418 <<
" AND " << table_iovs <<
".begin_time > " <<
t 419 <<
" ORDER BY " << table_iovs <<
".begin_time";
424 rc = sqlite3_prepare_v2(db, sql.str().c_str(), -1, &stmt, 0);
425 if(rc != SQLITE_OK) {
427 log <<
"sqlite3_prepare_v2 failed." << fSQLitePath <<
"\n";
428 log <<
"Failed sql = " << sql.str() <<
"\n";
436 rc = sqlite3_step(stmt);
438 if(rc == SQLITE_ROW) {
439 end_time = sqlite3_column_int(stmt, 0);
442 else if(rc != SQLITE_DONE) {
443 mf::LogError(
"DBFolder") <<
"sqlite3_step returned error result = " << rc <<
"\n";
449 sqlite3_finalize(stmt);
456 sql <<
"SELECT COUNT(DISTINCT channel)" 457 <<
" FROM " << table_data <<
"," << table_iovs <<
"," << table_tag_iovs
458 <<
" WHERE " << table_tag_iovs <<
".tag='" <<
fTag <<
"'" 459 <<
" AND " << table_iovs <<
".iov_id=" << table_tag_iovs <<
".iov_id" 460 <<
" AND " << table_data <<
".__iov_id=" << table_tag_iovs <<
".iov_id" 461 <<
" AND " << table_iovs <<
".begin_time <= " <<
t;
466 rc = sqlite3_prepare_v2(db, sql.str().c_str(), -1, &stmt, 0);
467 if(rc != SQLITE_OK) {
469 log <<
"sqlite3_prepare_v2 failed." << fSQLitePath <<
"\n";
470 log <<
"Failed sql = " << sql.str() <<
"\n";
478 rc = sqlite3_step(stmt);
479 unsigned int nrows = 0;
480 if(rc == SQLITE_ROW) {
481 nrows = sqlite3_column_int(stmt, 0);
485 mf::LogError(
"DBFolder") <<
"sqlite3_step returned error result = " << rc <<
"\n";
491 channels.reserve(nrows);
495 sqlite3_finalize(stmt);
499 begin_ts = IOVTimeStamp(begin_time, 0);
506 end_ts = IOVTimeStamp(end_time, 0);
511 sql <<
"SELECT " << table_data <<
".*,MAX(begin_time)" 512 <<
" FROM " << table_data <<
"," << table_iovs <<
"," << table_tag_iovs
513 <<
" WHERE " << table_tag_iovs <<
".tag='" <<
fTag <<
"'" 514 <<
" AND " << table_iovs <<
".iov_id=" << table_tag_iovs <<
".iov_id" 515 <<
" AND " << table_data <<
".__iov_id=" << table_tag_iovs <<
".iov_id" 516 <<
" AND " << table_iovs <<
".begin_time <= " << t
517 <<
" GROUP BY channel" 518 <<
" ORDER BY channel";
523 rc = sqlite3_prepare_v2(db, sql.str().c_str(), -1, &stmt, 0);
524 if(rc != SQLITE_OK) {
526 log <<
"sqlite3_prepare_v2 failed." << fSQLitePath <<
"\n";
527 log <<
"Failed sql = " << sql.str() <<
"\n";
534 int ncols = sqlite3_column_count(stmt);
535 column_names.reserve(ncols);
536 column_types.reserve(ncols);
538 rc = sqlite3_step(stmt);
539 if(rc == SQLITE_ROW) {
543 for(
int col = 0; col < ncols; ++col) {
544 std::string colname = sqlite3_column_name(stmt, col);
549 if(colname[0] !=
'_' && colname.substr(0,3) !=
"MAX") {
550 column_names.push_back(colname);
551 int dtype = sqlite3_column_type(stmt, col);
552 if(dtype == SQLITE_INTEGER)
553 column_types.push_back(
"integer");
554 else if(dtype == SQLITE_FLOAT)
555 column_types.push_back(
"real");
556 else if(dtype == SQLITE_TEXT)
557 column_types.push_back(
"text");
558 else if(dtype == SQLITE_NULL)
559 column_types.push_back(
"NULL");
561 mf::LogError(
"DBFolder") <<
"Unknown type " << dtype <<
"\n";
577 size_t nrelcols = column_names.size();
578 values.reserve(nrows * nrelcols);
583 rc = sqlite3_reset(stmt);
584 if(rc != SQLITE_OK) {
585 mf::LogError(
"DBFolder") <<
"sqlite3_reset failed." <<
"\n";
589 while(rc != SQLITE_DONE) {
590 rc = sqlite3_step(stmt);
591 if(rc == SQLITE_ROW) {
595 mf::LogError(
"DBFolder") <<
"Too many data rows " << irow <<
"\n";
596 throw cet::exception(
"DBFolder") <<
"Too many data rows " << irow;
603 bool firstcol =
true;
604 for(
int col = 0; col < ncols; ++col) {
605 std::string colname = sqlite3_column_name(stmt, col);
610 if(colname[0] !=
'_' && colname.substr(0,3) !=
"MAX") {
611 int dtype = sqlite3_column_type(stmt, col);
613 if(dtype == SQLITE_INTEGER) {
614 long value = sqlite3_column_int(stmt, col);
618 channels.push_back(value);
620 else if(dtype == SQLITE_FLOAT) {
621 double value = sqlite3_column_double(stmt, col);
625 mf::LogError(
"DBFolder") <<
"First column has wrong type float." <<
"\n";
626 throw cet::exception(
"DBFolder") <<
"First column has wrong type float.";
629 else if(dtype == SQLITE_TEXT) {
630 const char*
s = (
const char*)sqlite3_column_text(stmt, col);
632 values.emplace_back(std::make_unique<std::string>(s));
634 mf::LogError(
"DBFolder") <<
"First column has wrong type text." <<
"\n";
635 throw cet::exception(
"DBFolder") <<
"First column has wrong type text.";
638 else if(dtype == SQLITE_NULL) {
642 mf::LogError(
"DBFolder") <<
"First column has wrong type null." <<
"\n";
643 throw cet::exception(
"DBFolder") <<
"First column has wrong type null.";
647 mf::LogError(
"DBFolder") <<
"Unrecognized sqlite data type" <<
"\n";
648 throw cet::exception(
"DBFolder") <<
"Unrecognized sqlite data type.";
654 else if(rc != SQLITE_DONE) {
655 mf::LogError(
"DBFolder") <<
"sqlite3_step returned error result = " << rc <<
"\n";
660 mf::LogError(
"DBFolder") <<
"Wrong number of data rows " << irow <<
"," << nrows <<
"\n";
661 throw cet::exception(
"DBFolder") <<
"Wrong number of data rows " << irow <<
"," << nrows <<
"\n";
663 if(values.size() != nrows * nrelcols) {
665 << values.size() <<
"," << nrows <<
"," << nrelcols <<
"\n";
667 << values.size() <<
"," << nrows <<
"," << nrelcols <<
"\n";
672 sqlite3_finalize(stmt);
680 data = DBDataset(begin_ts, end_ts,
std::variant< long, double, std::unique_ptr< std::string > > value_type
struct sqlite3_stmt sqlite3_stmt
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
static IOVTimeStamp MaxTimeStamp()
unsigned nrows(sqlite3 *db, std::string const &tablename)
cet::coded_exception< error, detail::translate > exception