LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
SQLite3Wrapper.cc
Go to the documentation of this file.
2 
7 
8 #include <cassert>
9 #include <cstddef>
10 
11 namespace {
12 
13  enum DBTYPE { SQLITE3_TKEYDB, SQLITE3_MEMDB, SQLITE3_FILEDB, SQLITE3_TMPDB };
14 
15  std::string
16  dbTypeAsString(DBTYPE type)
17  {
18  std::string result;
19  switch (type) {
20  case SQLITE3_TKEYDB:
21  result = "tkeyvfs";
22  break;
23  case SQLITE3_MEMDB:
24  result = "in-memory";
25  break;
26  case SQLITE3_FILEDB:
27  result = "file";
28  break;
29  case SQLITE3_TMPDB:
30  result = "tmp";
31  break;
32  default:
33  result = "UNKNOWN";
34  break;
35  }
36  return result;
37  }
38 
39  void
40  traceit(void* string_ptr, const char* zSQL)
41  {
42  assert(string_ptr);
43  mf::LogAbsolute("SQLTrace")
44  << "+ SQLTrace (" << *reinterpret_cast<std::string*>(string_ptr)
45  << "): " << zSQL;
46  }
47 }
48 
50 
51 art::SQLite3Wrapper::SQLite3Wrapper(std::string const& key, int flags)
52  : db_(0), key_(key)
53 {
54  initDB(flags);
55 }
56 
58  std::string const& key,
59  int flags)
60  : db_(0), key_(key)
61 {
62  initDB(flags, tfile);
63 }
64 
65 bool
67 {
68  static char const* s_debug = getenv("ART_DEBUG_SQL");
69  return s_debug;
70 }
71 
72 void
74 {
76  swap(tmp);
77 }
78 
79 void
80 art::SQLite3Wrapper::reset(std::string const& key, int flags)
81 {
82  SQLite3Wrapper tmp{key, flags};
83  swap(tmp);
84 }
85 
86 void
87 art::SQLite3Wrapper::reset(TFile* tfile, std::string const& key, int flags)
88 {
89  SQLite3Wrapper tmp{tfile, key, flags};
90  swap(tmp);
91 }
92 
94 {
95  if (db_) {
96  sqlite3_close(db_);
97  }
98 }
99 
100 void
102 {
103  using std::swap;
104  swap(db_, other.db_);
105  swap(key_, other.key_);
106 }
107 
108 void
109 art::SQLite3Wrapper::initDB(int flags, TFile* tfile)
110 {
111  int err = 0;
112  DBTYPE type = tfile ? SQLITE3_TKEYDB :
113  key_.size() ?
114  (key_ == ":memory:" ? SQLITE3_MEMDB : SQLITE3_FILEDB) :
115  SQLITE3_TMPDB;
116  switch (type) {
117  case SQLITE3_TKEYDB:
118  if (!key_.size()) {
120  << "Failed to open TKEYVFS DB due to empty key spec.\n";
121  }
122  err = tkeyvfs_open_v2(key_.c_str(), &db_, flags, tfile);
123  break;
124  case SQLITE3_MEMDB:
125  err = sqlite3_open(key_.c_str(), &db_);
126  break;
127  case SQLITE3_FILEDB:
128  err = sqlite3_open_v2(key_.c_str(), &db_, flags, nullptr);
129  break;
130  case SQLITE3_TMPDB:
131  err = sqlite3_open(key_.c_str(), &db_);
132  break;
133  }
134  if (err) {
136  << "Failed to open requested DB, \"" << key_ << "\" of type, \""
137  << dbTypeAsString(type) << "\" -- " << sqlite3_errmsg(db_) << "\n";
138  }
139  maybeTrace();
140 }
141 
142 void
144 {
145  if (wantTracing()) {
146  // Cast away constness for C interface.
147  sqlite3_trace(db_, traceit, const_cast<std::string*>(&key_));
148  }
149 }
DBTYPE
void swap(SQLite3Wrapper &other)
static bool wantTracing()
void initDB(int flags, TFile *tfile=0)
Float_t tmp
Definition: plot.C:37
int tkeyvfs_open_v2(const char *filename, sqlite3 **ppDb, int flags, TFile *rootFile )
Definition: tkeyvfs.cc:1837
std::string const & key() const
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void maybeTrace() const
MaybeLogger_< ELseverityLevel::ELsev_severe, true > LogAbsolute