Namespaces | Classes | Typedefs | Functions
cet::sqlite Namespace Reference

Namespaces

 detail
 
 errors
 
 ExceptionDetail
 

Classes

struct  column
 
struct  column< double, Constraints... >
 
struct  column< float, Constraints... >
 
struct  column< int, Constraints... >
 
struct  column< long long, Constraints... >
 
struct  column< long, Constraints... >
 
struct  column< std::string, Constraints... >
 
struct  column< unsigned int, Constraints... >
 
struct  column< unsigned long long, Constraints... >
 
struct  column< unsigned long, Constraints... >
 
class  column_base
 
class  Connection
 
class  ConnectionFactory
 
struct  IncompleteInsert
 
struct  IncompleteSelectStmt
 
class  Ntuple
 
struct  permissive_column
 
struct  permissive_column< column< T, Constraints... > >
 
struct  primary_key
 
struct  query_result
 
struct  SelectStmt
 
class  Transaction
 

Typedefs

template<size_t N>
using name_array = std::array< std::string, N >
 
using Exception = cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate >
 

Functions

template<typename... Cols>
void create_table (sqlite3 *const db, std::string const &tablename, Cols const &...cols)
 
void create_table_as (std::string const &tablename, SelectStmt const &stmt)
 
void exec (sqlite3 *db, std::string const &ddl)
 
std::string assembleNoLockURI (std::string const &filename)
 
bool hasTableWithSchema (sqlite3 *db, std::string const &tablename, std::string expectedSchema)
 
unsigned nrows (sqlite3 *db, std::string const &tablename)
 
void delete_from (sqlite3 *db, std::string const &tablename)
 
void drop_table (sqlite3 *db, std::string const &tablename)
 
void drop_table_if_exists (sqlite3 *db, std::string const &tablename)
 
template<typename... Args>
void createTableIfNeeded (sqlite3 *db, bool const delete_contents, std::string const &tablename, permissive_column< Args > const &...cols)
 
auto insert_into (sqlite3 *const db, std::string const &tablename)
 
template<typename T >
unique_value (query_result< T > const &r)
 
template<typename... Args>
std::ostream & operator<< (std::ostream &os, query_result< Args... > const &res)
 
template<typename... Args>
query_result< Args... > query (sqlite3 *db, std::string const &ddl)
 
template<typename... T>
auto select (T const &...t)
 
template<typename... T>
auto select_distinct (T const &...t)
 
template<typename... Args>
void operator<< (query_result< Args... > &r, SelectStmt const &cq)
 
template<typename T = double>
min (sqlite3 *const db, std::string const &table_name, std::string const &column_name)
 
template<typename T = double>
max (sqlite3 *const db, std::string const &table_name, std::string const &column_name)
 
double mean (sqlite3 *db, std::string const &table_name, std::string const &column_name)
 
double median (sqlite3 *db, std::string const &table_name, std::string const &column_name)
 
double rms (sqlite3 *db, std::string const &table_name, std::string const &column_name)
 

Typedef Documentation

Definition at line 27 of file Exception.h.

template<size_t N>
using cet::sqlite::name_array = typedef std::array<std::string, N>

Definition at line 40 of file column.h.

Function Documentation

std::string cet::sqlite::assembleNoLockURI ( std::string const &  filename)

Definition at line 8 of file helpers.cc.

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 }
string filename
Definition: train.py:213
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
template<typename... Cols>
void cet::sqlite::create_table ( sqlite3 *const  db,
std::string const &  tablename,
Cols const &...  cols 
)

Definition at line 118 of file create_table.h.

121  {
122  auto const& ddl = detail::create_table_ddl(tablename, cols...);
123  sqlite::exec(db, ddl);
124  }
std::string create_table_ddl(std::string const &tablename, Cols const &...cols)
Definition: create_table.h:96
void exec(sqlite3 *db, std::string const &ddl)
Definition: exec.cc:5
void cet::sqlite::create_table_as ( std::string const &  tablename,
SelectStmt const &  stmt 
)
inline

Definition at line 127 of file create_table.h.

128  {
129  std::string ddl{detail::create_table_as_ddl(tablename, stmt)};
130  sqlite::exec(stmt.db_, ddl);
131  }
std::string string
Definition: nybbler.cc:12
void exec(sqlite3 *db, std::string const &ddl)
Definition: exec.cc:5
std::string create_table_as_ddl(std::string const &tablename, SelectStmt const &stmt)
Definition: create_table.h:106
template<typename... Args>
void cet::sqlite::createTableIfNeeded ( sqlite3 db,
bool const  delete_contents,
std::string const &  tablename,
permissive_column< Args > const &...  cols 
)

Definition at line 46 of file helpers.h.

50 {
51  auto const& sqlddl = detail::create_table_ddl(tablename, cols...);
52  if (hasTableWithSchema(db, tablename, sqlddl)) {
53  if (delete_contents) {
54  delete_from(db, tablename); // Prefer drop_table, but failure-to-prepare
55  // exception ends up being thrown.
56  }
57  } else {
58  exec(db, sqlddl);
59  }
60 }
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
std::string create_table_ddl(std::string const &tablename, Cols const &...cols)
Definition: create_table.h:96
void exec(sqlite3 *db, std::string const &ddl)
Definition: exec.cc:5
void cet::sqlite::delete_from ( sqlite3 db,
std::string const &  tablename 
)

Definition at line 63 of file helpers.cc.

64 {
65  exec(db, "delete from "s + tablename + ';');
66 }
void exec(sqlite3 *db, std::string const &ddl)
Definition: exec.cc:5
static QCString * s
Definition: config.cpp:1042
void cet::sqlite::drop_table ( sqlite3 db,
std::string const &  tablename 
)

Definition at line 69 of file helpers.cc.

70 {
71  exec(db, "drop table "s + tablename + ';');
72 }
void exec(sqlite3 *db, std::string const &ddl)
Definition: exec.cc:5
static QCString * s
Definition: config.cpp:1042
void cet::sqlite::drop_table_if_exists ( sqlite3 db,
std::string const &  tablename 
)

Definition at line 75 of file helpers.cc.

77 {
78  exec(db, "drop table if exists "s + tablename + ';');
79 }
void exec(sqlite3 *db, std::string const &ddl)
Definition: exec.cc:5
static QCString * s
Definition: config.cpp:1042
void cet::sqlite::exec ( sqlite3 db,
std::string const &  ddl 
)

Definition at line 5 of file exec.cc.

6 {
7  char* errmsg{nullptr};
8  if (sqlite3_exec(db, ddl.c_str(), nullptr, nullptr, &errmsg) != SQLITE_OK) {
9  std::string msg{errmsg};
10  sqlite3_free(errmsg);
12  }
13 }
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool cet::sqlite::hasTableWithSchema ( sqlite3 db,
std::string const &  tablename,
std::string  expectedSchema 
)

Definition at line 29 of file helpers.cc.

32 {
33  query_result<std::string> res;
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 }
static QCString name
Definition: declinfo.cpp:673
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)
auto select(T const &...t)
Definition: select.h:146
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
static QCString * s
Definition: config.cpp:1042
auto cet::sqlite::insert_into ( sqlite3 *const  db,
std::string const &  tablename 
)
inline

Definition at line 110 of file insert.h.

111  {
112  std::string result{"insert into " + tablename};
113  return IncompleteInsert{db, std::move(result)};
114  }
static QCString result
std::string string
Definition: nybbler.cc:12
def move(depos, offset)
Definition: depos.py:107
template<typename T = double>
T cet::sqlite::max ( sqlite3 *const  db,
std::string const &  table_name,
std::string const &  column_name 
)

Definition at line 66 of file statistics.h.

69 {
70  query_result<T> r;
71  r << select("max(" + column_name + ")").from(db, table_name);
72  return unique_value(r);
73 }
T unique_value(query_result< T > const &r)
Definition: query_result.h:94
auto select(T const &...t)
Definition: select.h:146
double cet::sqlite::mean ( sqlite3 db,
std::string const &  table_name,
std::string const &  column_name 
)

Definition at line 16 of file statistics.cc.

19 {
20  query_result<double> r;
21  r << select("avg(" + column_name + ")").from(db, table_name);
22  return unique_value(r);
23 }
T unique_value(query_result< T > const &r)
Definition: query_result.h:94
auto select(T const &...t)
Definition: select.h:146
double cet::sqlite::median ( sqlite3 db,
std::string const &  table_name,
std::string const &  column_name 
)

Definition at line 26 of file statistics.cc.

29 {
30  auto r = query<double>(
31  db,
32  "select avg("s + column_name + ")" + " from (select " + column_name +
33  " from " + table_name + " order by " + column_name +
34  " limit 2 - (select count(*) from " + table_name + ") % 2" +
35  " offset (select (count(*) - 1) / 2" + " from " + table_name + "))");
36  return unique_value(r);
37 }
T unique_value(query_result< T > const &r)
Definition: query_result.h:94
static QCString * s
Definition: config.cpp:1042
template<typename T = double>
T cet::sqlite::min ( sqlite3 *const  db,
std::string const &  table_name,
std::string const &  column_name 
)

Definition at line 55 of file statistics.h.

58 {
59  query_result<T> r;
60  r << select("min(" + column_name + ")").from(db, table_name);
61  return unique_value(r);
62 }
T unique_value(query_result< T > const &r)
Definition: query_result.h:94
auto select(T const &...t)
Definition: select.h:146
unsigned cet::sqlite::nrows ( sqlite3 db,
std::string const &  tablename 
)

Definition at line 82 of file helpers.cc.

83 {
84  query_result<unsigned> r;
85  r << select("count(*)").from(db, tablename);
86  return unique_value(r);
87 }
T unique_value(query_result< T > const &r)
Definition: query_result.h:94
auto select(T const &...t)
Definition: select.h:146
template<typename... Args>
std::ostream& cet::sqlite::operator<< ( std::ostream &  os,
query_result< Args... > const &  res 
)

Definition at line 105 of file query_result.h.

106  {
107  using size_t = decltype(res.columns.size());
108  auto const ncolumns = res.columns.size();
109  for (size_t i{}; i != ncolumns; ++i) {
110  os << res.columns[i] << ' ';
111  }
112  os << "\n--------------------------------\n";
113  for (auto const& row : res.data) {
114  os << row.str() << '\n';
115  }
116  return os;
117  }
template<typename... Args>
void cet::sqlite::operator<< ( query_result< Args... > &  r,
SelectStmt const &  cq 
)

Definition at line 162 of file select.h.

163  {
164  r = query<Args...>(cq.db_, cq.ddl_ + ";");
165  }
query_result< Args... > query(sqlite3 *db, std::string const &ddl)
Definition: select.h:75
template<typename... Args>
query_result<Args...> cet::sqlite::query ( sqlite3 db,
std::string const &  ddl 
)

Definition at line 75 of file select.h.

76  {
77  query_result<Args...> res;
78  char* errmsg{nullptr};
79  if (sqlite3_exec(
80  db, ddl.c_str(), detail::get_result<Args...>, &res, &errmsg) !=
81  SQLITE_OK) {
82  std::string msg{errmsg};
83  sqlite3_free(errmsg);
85  }
86  return res;
87  }
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
int get_result(void *data, int ncols, char **results, char **cnames)
Definition: get_result.h:34
double cet::sqlite::rms ( sqlite3 db,
std::string const &  table_name,
std::string const &  column_name 
)

Definition at line 40 of file statistics.cc.

43 {
44  auto r = query<double>(db,
45  "select sum("s + "(" + column_name + "-(select avg(" +
46  column_name + ") from " + table_name + "))" + "*" +
47  "(" + column_name + "-(select avg(" + column_name +
48  ") from " + table_name + "))" + " ) /" + "(count(" +
49  column_name + ")) from " + table_name);
50  return std::sqrt(unique_value(r));
51 }
T unique_value(query_result< T > const &r)
Definition: query_result.h:94
static QCString * s
Definition: config.cpp:1042
template<typename... T>
auto cet::sqlite::select ( T const &...  t)

Definition at line 146 of file select.h.

147  {
148  std::string result{"select " + detail::concatenate(t...)};
149  return IncompleteSelectStmt{std::move(result)};
150  }
static QCString result
std::string string
Definition: nybbler.cc:12
std::string concatenate(H const &h, T const &...t)
Definition: select.h:138
def move(depos, offset)
Definition: depos.py:107
template<typename... T>
auto cet::sqlite::select_distinct ( T const &...  t)

Definition at line 154 of file select.h.

155  {
156  std::string result{"select distinct " + detail::concatenate(t...)};
157  return IncompleteSelectStmt{std::move(result)};
158  }
static QCString result
std::string string
Definition: nybbler.cc:12
std::string concatenate(H const &h, T const &...t)
Definition: select.h:138
def move(depos, offset)
Definition: depos.py:107
template<typename T >
T cet::sqlite::unique_value ( query_result< T > const &  r)
inline

Definition at line 94 of file query_result.h.

95  {
96  if (r.data.size() != 1ull) {
98  << "unique_value expected of non-unique query.";
99  }
100  return std::get<T>(r.data[0]);
101  }
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66