libdballe  7.21
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 
12 namespace dballe {
13 namespace sql {
14 struct SQLiteStatement;
15 
20 {
21  std::string msg;
22 
23  error_sqlite(sqlite3* db, const std::string& msg);
24  error_sqlite(const std::string& dbmsg, const std::string& msg);
25  ~error_sqlite() noexcept {}
26 
27  const char* what() const noexcept override { return msg.c_str(); }
28 
29  static void throwf(sqlite3* db, const char* fmt, ...) WREPORT_THROWF_ATTRS(2, 3);
30 };
31 
34 {
35 protected:
37  sqlite3* db = nullptr;
38 
39  void init_after_connect();
40  static void on_sqlite3_profile(void* arg, const char* query, sqlite3_uint64 usecs);
41 
42 public:
44  SQLiteConnection(const SQLiteConnection&) = delete;
45  SQLiteConnection(const SQLiteConnection&&) = delete;
47 
48  SQLiteConnection& operator=(const SQLiteConnection&) = delete;
49 
50  operator sqlite3*() { return db; }
51 
52  void open_file(const std::string& pathname, int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
53  void open_memory(int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
54  void open_private_file(int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
55 
56  std::unique_ptr<Transaction> transaction() override;
57  std::unique_ptr<SQLiteStatement> sqlitestatement(const std::string& query);
58 
59  bool has_table(const std::string& name) override;
60  std::string get_setting(const std::string& key) override;
61  void set_setting(const std::string& key, const std::string& value) override;
62  void drop_settings() override;
63  void execute(const std::string& query) override;
64  void explain(const std::string& query, FILE* out) override;
65 
69  void drop_table_if_exists(const char* name);
70 
77  int get_last_insert_id();
78 
80  int changes();
81 
83  void exec(const std::string& query);
84  void exec_nothrow(const std::string& query) noexcept;
85 };
86 
89 {
90  SQLiteConnection& conn;
91  sqlite3_stmt *stm = nullptr;
92 
93  SQLiteStatement(SQLiteConnection& conn, const std::string& query);
94  SQLiteStatement(const SQLiteStatement&) = delete;
95  SQLiteStatement(const SQLiteStatement&&) = delete;
96  ~SQLiteStatement();
97  SQLiteStatement& operator=(const SQLiteStatement&) = delete;
98 
106  template<typename... Args> void bind(const Args& ...args)
107  {
108  bindn<sizeof...(args)>(args...);
109  }
110 
111  void bind_null_val(int idx);
112  void bind_val(int idx, int val);
113  void bind_val(int idx, unsigned val);
114  void bind_val(int idx, unsigned short val);
115  void bind_val(int idx, const Datetime& val);
116  void bind_val(int idx, const char* val); // Warning: SQLITE_STATIC is used
117  void bind_val(int idx, const std::string& val); // Warning: SQLITE_STATIC is used
118  void bind_val(int idx, const std::vector<uint8_t>& val); // Warning: SQLITE_STATIC is used
119 
121  void execute();
122 
129  void execute(std::function<void()> on_row);
130 
135  void execute_one(std::function<void()> on_row);
136 
138  int column_int(int col) { return sqlite3_column_int(stm, col); }
139 
141  sqlite3_int64 column_int64(int col) { return sqlite3_column_int64(stm, col); }
142 
144  double column_double(int col) { return sqlite3_column_double(stm, col); }
145 
147  const char* column_string(int col) { return (const char*)sqlite3_column_text(stm, col); }
148 
150  std::vector<uint8_t> column_blob(int col) {
151  int size = sqlite3_column_bytes(stm, col);
152  const uint8_t* val = (const uint8_t*)sqlite3_column_blob(stm, col);
153  return std::vector<uint8_t>(val, val + size);
154  }
155 
157  Datetime column_datetime(int col);
158 
160  bool column_isnull(int col) { return sqlite3_column_type(stm, col) == SQLITE_NULL; }
161 
162  void wrap_sqlite3_reset();
163  void wrap_sqlite3_reset_nothrow() noexcept;
168  [[noreturn]] void reset_and_throw(const std::string& errmsg);
169 
170  operator sqlite3_stmt*() { return stm; }
171 #if 0
172  int execute();
175  int exec_direct(const char* query);
177  int exec_direct(const char* query, int qlen);
178 
180  int execute_and_close();
182  int exec_direct_and_close(const char* query);
184  int exec_direct_and_close(const char* query, int qlen);
185 
190  int columns_count();
191  bool fetch();
192  bool fetch_expecting_one();
193  void close_cursor();
194  void close_cursor_if_needed();
196  size_t select_rowcount();
198  size_t rowcount();
199 #endif
200 
201 private:
202  // Implementation of variadic bind: terminating condition
203  template<size_t total> void bindn() {}
204  // Implementation of variadic bind: recursive iteration over the parameter pack
205  template<size_t total, typename ...Args, typename T> void bindn(const T& first, const Args& ...args)
206  {
207  bind_val(total - sizeof...(args), first);
208  bindn<total>(args...);
209  }
210 };
211 
212 }
213 }
214 #endif
215 
const char * column_string(int col)
Read the string value of a column in the result set (0-based)
Definition: sqlite.h:147
sqlite3_int64 column_int64(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite.h:141
void bind(const Args &...args)
Bind all the arguments in a single invocation.
Definition: sqlite.h:106
int column_int(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite.h:138
bool column_isnull(int col)
Check if a column has a NULL value (0-based)
Definition: sqlite.h:160
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
double column_double(int col)
Read the double value of a column in the result set (0-based)
Definition: sqlite.h:144
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:19
Date and time.
Definition: types.h:158
#define WREPORT_THROWF_ATTRS(a, b)
SQLite statement.
Definition: sqlite.h:88
std::vector< uint8_t > column_blob(int col)
Read the string value of a column in the result set (0-based)
Definition: sqlite.h:150
Database connection.
Definition: sqlite.h:33