helpers.cc
Go to the documentation of this file.
4 
5 #include "sqlite3.h"
6 
9 {
10  // Arbitrary decision: don't allow users to specify a URI since
11  // they may (unintentionally) remove the 'nolock' parameter, thus
12  // potentially causing issues with NFS.
13  if (filename.substr(0, 5) == "file:") {
15  << "art does not allow an SQLite database filename that starts with "
16  "'file:'.\n"
17  << "Please contact artists@fnal.gov if you believe this is an error.";
18  }
19  return "file:" + filename + "?nolock=1";
20 }
21 
22 //=================================================================
23 // hasTableWithSchema(db, name, expectedSchema) returns true if the db
24 // has a table name 'name' with a schema that matches the expected
25 // one. It returns false if there is no table of that name, and
26 // throws an exception if there is a table of the given name but it
27 // does not match both the given column names and column types.
28 bool
30  std::string const& name,
31  std::string expectedSchema)
32 {
34  res << select("sql")
35  .from(db, "sqlite_master")
36  .where("type=\"table\" and name=\""s + name + '"');
37 
38  if (res.empty()) {
39  return false;
40  }
41 
42  // This is a somewhat fragile way of validating schemas. A better
43  // way would be to rely on sqlite3's insertion facilities to
44  // determine if an insert of in-memory data would be compatible with
45  // the on-disk schema. This would require creating a temporary
46  // table (so as to avoid inserting then deleting a dummy row into
47  // the desired table)according to the on-disk schema, and inserting
48  // some default values according to the requested schema.
49  std::string retrievedSchema{unique_value(res)};
50  detail::normalize_statement(retrievedSchema);
51  detail::normalize_statement(expectedSchema);
52  if (retrievedSchema == expectedSchema) {
53  return true;
54  }
55 
57  << "Existing database table name does not match the expected schema:\n"
58  << " Schema on disk : " << retrievedSchema << '\n'
59  << " Expected schema: " << expectedSchema << '\n';
60 }
61 
62 void
63 cet::sqlite::delete_from(sqlite3* const db, std::string const& tablename)
64 {
65  exec(db, "delete from "s + tablename + ';');
66 }
67 
68 void
69 cet::sqlite::drop_table(sqlite3* const db, std::string const& tablename)
70 {
71  exec(db, "drop table "s + tablename + ';');
72 }
73 
74 void
76  std::string const& tablename)
77 {
78  exec(db, "drop table if exists "s + tablename + ';');
79 }
80 
81 unsigned
82 cet::sqlite::nrows(sqlite3* db, std::string const& tablename)
83 {
85  r << select("count(*)").from(db, tablename);
86  return unique_value(r);
87 }
static QCString name
Definition: declinfo.cpp:673
bool hasTableWithSchema(sqlite3 *db, std::string const &tablename, std::string expectedSchema)
Definition: helpers.cc:29
void delete_from(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:63
T unique_value(query_result< T > const &r)
Definition: query_result.h:94
std::string string
Definition: nybbler.cc:12
void normalize_statement(std::string &to_replace)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:27
string filename
Definition: train.py:213
std::string assembleNoLockURI(std::string const &filename)
Definition: helpers.cc:8
void drop_table_if_exists(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:75
auto select(T const &...t)
Definition: select.h:146
struct sqlite3 sqlite3
void drop_table(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:69
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
unsigned nrows(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:82
void exec(sqlite3 *db, std::string const &ddl)
Definition: exec.cc:5
static QCString * s
Definition: config.cpp:1042