resolveRangeSet.cc
Go to the documentation of this file.
2 #include "sqlite3.h"
3 
4 namespace {
5 
6  void
7  successful_prepare(int const rc,
8  std::string const& filename,
9  std::string const& statement)
10  {
11  if (rc == SQLITE_OK)
12  return;
14  << "Error in preparing statement for getting contributors.\n"
15  << "File: " << filename << '\n'
16  << "Preparation statement: " << statement << '\n';
17  }
18 
19  void
20  successful_step(int const rc,
21  std::string const& sqlErrMsg,
22  std::string const& filename)
23  {
24  if (rc == SQLITE_DONE)
25  return;
27  << "Unexpected status from table read: " << sqlErrMsg << " (" << rc
28  << ").\n"
29  << "File: " << filename << '\n';
30  }
31 
32  void
33  successful_finalize(int const rc,
34  std::string const& sqlErrMsg,
35  std::string const& filename)
36  {
37  if (rc == SQLITE_OK)
38  return;
40  << "Unexpected status from DB status cleanup: " << sqlErrMsg << " (" << rc
41  << ").\n"
42  << "File: " << filename << '\n';
43  }
44 
45 } // namespace
46 
47 using art::EventRange;
48 using art::RangeSet;
49 
52  std::string const& filename,
53  BranchType const bt,
54  unsigned const rangeSetID,
55  bool const compact)
56 {
57  // Invalid rangeSetID check
58  if (rangeSetID == std::numeric_limits<unsigned>::max())
59  return RangeSetInfo::invalid();
60 
61  sqlite3_stmt* stmt{nullptr};
62  std::string const run_ddl{
63  "SELECT Run FROM " + BranchTypeToString(bt) +
64  "RangeSets WHERE rowid==" + std::to_string(rangeSetID) + ";"};
65  auto rc = sqlite3_prepare_v2(db, run_ddl.c_str(), -1, &stmt, nullptr);
66  successful_prepare(rc, filename, run_ddl);
67 
68  rc = sqlite3_step(stmt);
69  auto const r = static_cast<RunNumber_t>(sqlite3_column_int(stmt, 0));
70  rc = sqlite3_finalize(stmt);
71  successful_finalize(rc, sqlite3_errmsg(db), filename);
72 
73  std::string const result_column_begin{compact ? "min(begin)" : "begin"};
74  std::string const result_column_end{compact ? "max(end)" : "end"};
75  std::string const maybe_suffix{compact ? " GROUP BY SubRun" : ""};
76  std::string const ddl{
77  "SELECT SubRun," + result_column_begin + ',' + result_column_end +
78  " FROM EventRanges WHERE rowid IN"
79  "(SELECT EventRangesID FROM " +
80  BranchTypeToString(bt) + "RangeSets_EventRanges WHERE RangeSetsID==" +
81  std::to_string(rangeSetID) + ')' + maybe_suffix + ';'};
82  rc = sqlite3_prepare_v2(db, ddl.c_str(), -1, &stmt, nullptr);
83  successful_prepare(rc, filename, ddl);
84 
85  std::vector<EventRange> ranges;
86  while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
87  ranges.emplace_back(sqlite3_column_int(stmt, 0),
88  sqlite3_column_int(stmt, 1),
89  sqlite3_column_int(stmt, 2));
90  }
91  successful_step(rc, sqlite3_errmsg(db), filename);
92 
93  rc = sqlite3_finalize(stmt);
94  successful_finalize(rc, sqlite3_errmsg(db), filename);
95 
96  return RangeSetInfo{r, std::move(ranges)};
97 }
98 
101 {
102  if (rsi.is_invalid()) {
103  return RangeSet::invalid();
104  }
105  return RangeSet{rsi.run, rsi.ranges};
106 }
107 
110  std::string const& filename,
111  BranchType const bt,
112  unsigned const rangeSetID,
113  bool const compact)
114 {
115  auto const& rsInfo =
116  resolveRangeSetInfo(db, filename, bt, rangeSetID, compact);
117  return resolveRangeSet(rsInfo);
118 }
RangeSetInfo resolveRangeSetInfo(sqlite3 *, std::string const &filename, BranchType, unsigned RangeSetID, bool compact)
std::vector< EventRange > ranges
Definition: RangeSetInfo.h:35
std::string string
Definition: nybbler.cc:12
RangeSet resolveRangeSet(RangeSetInfo const &rs)
std::string const & BranchTypeToString(BranchType const bt)
Definition: BranchType.cc:65
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
BranchType
Definition: BranchType.h:18
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
IDNumber_t< Level::Run > RunNumber_t
Definition: IDNumber.h:120