3 #include "cetlib/sqlite/Transaction.h" 4 #include "cetlib/sqlite/create_table.h" 5 #include "cetlib/sqlite/exec.h" 6 #include "cetlib/sqlite/select.h" 17 sqlite3* result =
nullptr;
18 sqlite3_open(
":memory:", &result);
21 Transaction txn{result};
24 column<std::string, primary_key>{
"ID"},
25 column<std::string>{
"PSetBlob"});
34 std::string
const msgString{msg ? msg :
""};
39 auto errcode = sqlite3_errcode(db);
40 if (errcode != SQLITE_OK) {
44 << sqlite3_errstr(errcode) <<
" (" << errcode
45 <<
"): " << sqlite3_errmsg(db)
46 << (msgString.empty() ?
"" : (std::string(
". ") + msgString));
52 sqlite3_finalize(stmt_);
57 std::cerr << e.what() <<
'\n';
63 retcode = sqlite3_close(primaryDB_);
64 }
while (retcode == SQLITE_BUSY);
71 std::lock_guard<decltype(mutex_)> lock{mutex_};
75 sqlite3_stmt* oStmt =
nullptr;
76 sqlite3* primaryDB = instance_().primaryDB_;
81 "INSERT OR IGNORE INTO ParameterSets(ID, PSetBlob) VALUES(?, ?);",
88 query_result<std::string, std::string> inputPSes;
89 inputPSes << select(
"*").from(db,
"ParameterSets");
91 for (
auto const& row : inputPSes) {
94 std::tie(idString, psBlob) = row;
97 oStmt, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
100 oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
102 switch (sqlite3_step(oStmt)) {
108 sqlite3_reset(oStmt);
111 sqlite3_finalize(oStmt);
119 std::lock_guard<decltype(mutex_)> lock{mutex_};
121 cet::sqlite::Transaction txn{db};
122 cet::sqlite::exec(db,
123 "DROP TABLE IF EXISTS ParameterSets;" 124 "CREATE TABLE ParameterSets(ID PRIMARY KEY, PSetBlob);");
127 sqlite3_stmt* oStmt =
nullptr;
130 "INSERT OR IGNORE INTO ParameterSets(ID, PSetBlob) VALUES(?, ?);",
135 for (
auto const& p : instance_().registry_) {
136 std::string id(p.first.to_string());
137 std::string psBlob(p.second.to_compact_string());
138 sqlite3_bind_text(oStmt, 1,
id.c_str(),
id.size() + 1, SQLITE_STATIC);
141 oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
143 switch (sqlite3_step(oStmt)) {
145 sqlite3_reset(oStmt);
153 sqlite3*
const primaryDB{instance_().primaryDB_};
155 query_result<std::string, std::string> regPSes;
156 regPSes << select(
"*").from(primaryDB,
"ParameterSets");
158 for (
auto const& row : regPSes) {
159 std::string idString;
161 std::tie(idString, psBlob) = row;
163 oStmt, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
166 oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
168 switch (sqlite3_step(oStmt)) {
170 sqlite3_reset(oStmt);
177 sqlite3_finalize(oStmt);
184 std::lock_guard<decltype(mutex_)> lock{mutex_};
186 sqlite3* primaryDB = instance_().primaryDB_;
187 auto& registry = instance_().registry_;
189 query_result<std::string, std::string> entriesToStageIn;
190 entriesToStageIn << select(
"*").from(primaryDB,
"ParameterSets");
192 cet::transform_all(entriesToStageIn,
193 std::inserter(registry,
std::begin(registry)),
194 [](
auto const& row) {
195 std::string idString;
197 std::tie(idString, psBlob) = row;
205 : primaryDB_{openPrimaryDB()}
217 if (
stmt_ ==
nullptr) {
219 "SELECT PSetBlob FROM ParameterSets WHERE ID = ?;",
225 auto idString =
id.to_string();
226 auto result = sqlite3_bind_text(
227 stmt_, 1, idString.c_str(), idString.size() + 1, SQLITE_STATIC);
229 result = sqlite3_step(
stmt_);
235 reinterpret_cast<char const*>(sqlite3_column_text(
stmt_, 0)), pset);
244 sqlite3_reset(
stmt_);
void make_ParameterSet(intermediate_table const &tbl, ParameterSet &ps)
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
collection_type registry_
static std::recursive_mutex mutex_
typename collection_type::const_iterator const_iterator
const_iterator find_(ParameterSetID const &id)
void throwOnSQLiteFailure(sqlite3 *db, char *msg=nullptr)
cet::coded_exception< error, detail::translate > exception
static void exportTo(sqlite3 *db)
static void importFrom(sqlite3 *db)