libdballe  8.3
sqlite.h
Go to the documentation of this file.
1 
4 #ifndef DBALLE_SQL_SQLITE_H
5 #define DBALLE_SQL_SQLITE_H
6 
7 #include <dballe/core/error.h>
8 #include <dballe/sql/sql.h>
9 #include <sqlite3.h>
10 #include <vector>
11 #include <functional>
12 
13 namespace dballe {
14 namespace sql {
15 struct SQLiteStatement;
16 
21 {
22  std::string msg;
23 
24  error_sqlite(sqlite3* db, const std::string& msg);
25  error_sqlite(const std::string& dbmsg, const std::string& msg);
26  ~error_sqlite() noexcept {}
27 
28  const char* what() const noexcept override { return msg.c_str(); }
29 
30  static void throwf(sqlite3* db, const char* fmt, ...) WREPORT_THROWF_ATTRS(2, 3);
31 };
32 
35 {
36 protected:
38  sqlite3* db = nullptr;
39 
40  void init_after_connect();
41  static void on_sqlite3_profile(void* arg, const char* query, sqlite3_uint64 usecs);
42 
43 public:
45  SQLiteConnection(const SQLiteConnection&) = delete;
46  SQLiteConnection(const SQLiteConnection&&) = delete;
48 
49  SQLiteConnection& operator=(const SQLiteConnection&) = delete;
50 
51  operator sqlite3*() { return db; }
52 
53  void open_file(const std::string& pathname, int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
54  void open_memory(int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
55  void open_private_file(int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
56 
57  std::unique_ptr<Transaction> transaction(bool readonly=false) override;
58  std::unique_ptr<SQLiteStatement> sqlitestatement(const std::string& query);
59 
60  bool has_table(const std::string& name) override;
61  std::string get_setting(const std::string& key) override;
62  void set_setting(const std::string& key, const std::string& value) override;
63  void drop_settings() override;
64  void execute(const std::string& query) override;
65  void explain(const std::string& query, FILE* out) override;
66 
70  void drop_table_if_exists(const char* name);
71 
78  int get_last_insert_id();
79 
81  int changes();
82 
84  void exec(const std::string& query);
85  void exec_nothrow(const std::string& query) noexcept;
86 
87 #if SQLITE_VERSION_NUMBER >= 3014000
88 
93  void trace(unsigned mask=SQLITE_TRACE_STMT);
94 #endif
95 };
96 
99 {
100  SQLiteConnection& conn;
101  sqlite3_stmt *stm = nullptr;
102 
103  SQLiteStatement(SQLiteConnection& conn, const std::string& query);
104  SQLiteStatement(const SQLiteStatement&) = delete;
105  SQLiteStatement(const SQLiteStatement&&) = delete;
106  ~SQLiteStatement();
107  SQLiteStatement& operator=(const SQLiteStatement&) = delete;
108 
116  template<typename... Args> void bind(const Args& ...args)
117  {
118  bindn<sizeof...(args)>(args...);
119  }
120 
121  void bind_null_val(int idx);
122  void bind_val(int idx, int val);
123  void bind_val(int idx, unsigned val);
124  void bind_val(int idx, unsigned short val);
125  void bind_val(int idx, const Datetime& val);
126  void bind_val(int idx, const char* val); // Warning: SQLITE_STATIC is used
127  void bind_val(int idx, const std::string& val); // Warning: SQLITE_STATIC is used
128  void bind_val(int idx, const std::vector<uint8_t>& val); // Warning: SQLITE_STATIC is used
129 
131  void execute();
132 
139  void execute(std::function<void()> on_row);
140 
145  void execute_one(std::function<void()> on_row);
146 
148  int column_int(int col) { return sqlite3_column_int(stm, col); }
149 
151  sqlite3_int64 column_int64(int col) { return sqlite3_column_int64(stm, col); }
152 
154  double column_double(int col) { return sqlite3_column_double(stm, col); }
155 
157  const char* column_string(int col) { return (const char*)sqlite3_column_text(stm, col); }
158 
160  std::vector<uint8_t> column_blob(int col) {
161  int size = sqlite3_column_bytes(stm, col);
162  const uint8_t* val = (const uint8_t*)sqlite3_column_blob(stm, col);
163  return std::vector<uint8_t>(val, val + size);
164  }
165 
167  Datetime column_datetime(int col);
168 
170  bool column_isnull(int col) { return sqlite3_column_type(stm, col) == SQLITE_NULL; }
171 
172  void wrap_sqlite3_reset();
173  void wrap_sqlite3_reset_nothrow() noexcept;
178  [[noreturn]] void reset_and_throw(const std::string& errmsg);
179 
180  operator sqlite3_stmt*() { return stm; }
181 #if 0
182  int execute();
185  int exec_direct(const char* query);
187  int exec_direct(const char* query, int qlen);
188 
190  int execute_and_close();
192  int exec_direct_and_close(const char* query);
194  int exec_direct_and_close(const char* query, int qlen);
195 
200  int columns_count();
201  bool fetch();
202  bool fetch_expecting_one();
203  void close_cursor();
204  void close_cursor_if_needed();
206  size_t select_rowcount();
208  size_t rowcount();
209 #endif
210 
211 private:
212  // Implementation of variadic bind: terminating condition
213  template<size_t total> void bindn() {}
214  // Implementation of variadic bind: recursive iteration over the parameter pack
215  template<size_t total, typename ...Args, typename T> void bindn(const T& first, const Args& ...args)
216  {
217  bind_val(total - sizeof...(args), first);
218  bindn<total>(args...);
219  }
220 };
221 
222 }
223 }
224 #endif
225 
const char * column_string(int col)
Read the string value of a column in the result set (0-based)
Definition: sqlite.h:157
sqlite3_int64 column_int64(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite.h:151
void bind(const Args &...args)
Bind all the arguments in a single invocation.
Definition: sqlite.h:116
int column_int(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite.h:148
bool column_isnull(int col)
Check if a column has a NULL value (0-based)
Definition: sqlite.h:170
Definition: cmdline.h:18
double column_double(int col)
Read the double value of a column in the result set (0-based)
Definition: sqlite.h:154
Definition: sql.h:59
Common infrastructure for talking with SQL databases.
Error in case of failed database operations.
Definition: error.h:21
Report an SQLite error.
Definition: sqlite.h:20
Date and time.
Definition: types.h:164
#define WREPORT_THROWF_ATTRS(a, b)
SQLite statement.
Definition: sqlite.h:98
std::vector< uint8_t > column_blob(int col)
Read the string value of a column in the result set (0-based)
Definition: sqlite.h:160
Database connection.
Definition: sqlite.h:34