libdballe  9.11
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  std::string pathname;
40  int flags = 0;
42  sqlite3* db = nullptr;
44  bool forked = false;
45 
46  void init_after_connect();
47  static void on_sqlite3_profile(void* arg, const char* query, sqlite3_uint64 usecs);
48 
50 
51  void fork_prepare() override;
52  void fork_parent() override;
53  void fork_child() override;
54 
55  void check_connection();
56 
57  void reopen();
58 
59 public:
60  SQLiteConnection(const SQLiteConnection&) = delete;
61  SQLiteConnection(const SQLiteConnection&&) = delete;
63 
64  static std::shared_ptr<SQLiteConnection> create();
65 
66  SQLiteConnection& operator=(const SQLiteConnection&) = delete;
67 
68  operator sqlite3*() { return db; }
69 
70  void open_file(const std::string& pathname, int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
71  void open_memory(int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
72  void open_private_file(int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
73 
74  std::unique_ptr<Transaction> transaction(bool readonly=false) override;
75  std::unique_ptr<SQLiteStatement> sqlitestatement(const std::string& query);
76 
77  bool has_table(const std::string& name) override;
78  std::string get_setting(const std::string& key) override;
79  void set_setting(const std::string& key, const std::string& value) override;
80  void drop_settings() override;
81  void execute(const std::string& query) override;
82  void explain(const std::string& query, FILE* out) override;
83 
87  void drop_table_if_exists(const char* name);
88 
95  int get_last_insert_id();
96 
98  int changes();
99 
101  void exec(const std::string& query);
102  void exec_nothrow(const std::string& query) noexcept;
103 
104 #if SQLITE_VERSION_NUMBER >= 3014000
105 
110  void trace(unsigned mask=SQLITE_TRACE_STMT);
111 #endif
112 };
113 
116 {
117  SQLiteConnection& conn;
118  std::string query;
119  sqlite3_stmt *stm = nullptr;
120 
121  SQLiteStatement(SQLiteConnection& conn, const std::string& query);
122  SQLiteStatement(const SQLiteStatement&) = delete;
123  SQLiteStatement(const SQLiteStatement&&) = delete;
124  ~SQLiteStatement();
125  SQLiteStatement& operator=(const SQLiteStatement&) = delete;
126 
134  template<typename... Args> void bind(const Args& ...args)
135  {
136  bindn<sizeof...(args)>(args...);
137  }
138 
139  void bind_null_val(int idx);
140  void bind_val(int idx, int val);
141  void bind_val(int idx, unsigned val);
142  void bind_val(int idx, unsigned short val);
143  void bind_val(int idx, const Datetime& val);
144  void bind_val(int idx, const char* val); // Warning: SQLITE_STATIC is used
145  void bind_val(int idx, const std::string& val); // Warning: SQLITE_STATIC is used
146  void bind_val(int idx, const std::vector<uint8_t>& val); // Warning: SQLITE_STATIC is used
147 
149  void execute();
150 
157  void execute(std::function<void()> on_row);
158 
163  void execute_one(std::function<void()> on_row);
164 
166  int column_int(int col) { return sqlite3_column_int(stm, col); }
167 
169  sqlite3_int64 column_int64(int col) { return sqlite3_column_int64(stm, col); }
170 
172  double column_double(int col) { return sqlite3_column_double(stm, col); }
173 
175  const char* column_string(int col) { return (const char*)sqlite3_column_text(stm, col); }
176 
178  std::vector<uint8_t> column_blob(int col) {
179  int size = sqlite3_column_bytes(stm, col);
180  const uint8_t* val = (const uint8_t*)sqlite3_column_blob(stm, col);
181  return std::vector<uint8_t>(val, val + size);
182  }
183 
185  Datetime column_datetime(int col);
186 
188  bool column_isnull(int col) { return sqlite3_column_type(stm, col) == SQLITE_NULL; }
189 
190  void wrap_sqlite3_reset();
191  void wrap_sqlite3_reset_nothrow() noexcept;
196  [[noreturn]] void reset_and_throw(const std::string& errmsg);
197 
198  operator sqlite3_stmt*() { return stm; }
199 #if 0
200  int execute();
203  int exec_direct(const char* query);
205  int exec_direct(const char* query, int qlen);
206 
208  int execute_and_close();
210  int exec_direct_and_close(const char* query);
212  int exec_direct_and_close(const char* query, int qlen);
213 
218  int columns_count();
219  bool fetch();
220  bool fetch_expecting_one();
221  void close_cursor();
222  void close_cursor_if_needed();
224  size_t select_rowcount();
226  size_t rowcount();
227 #endif
228 
229 private:
230  // Implementation of variadic bind: terminating condition
231  template<size_t total> void bindn() {}
232  // Implementation of variadic bind: recursive iteration over the parameter pack
233  template<size_t total, typename ...Args, typename T> void bindn(const T& first, const Args& ...args)
234  {
235  bind_val(total - sizeof...(args), first);
236  bindn<total>(args...);
237  }
238 };
239 
240 }
241 }
242 #endif
243 
int changes()
Count the number of rows modified by the last query that was run.
const char * column_string(int col)
Read the string value of a column in the result set (0-based)
Definition: sqlite.h:175
void execute(const std::string &query) override
Execute a query without reading its results.
void drop_table_if_exists(const char *name)
Delete a table in the database if it exists, otherwise do nothing.
void reset_and_throw(const std::string &errmsg)
Get the current error message, reset the statement and throw error_sqlite.
sqlite3_int64 column_int64(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite.h:169
void exec(const std::string &query)
Wrap sqlite3_exec, without a callback.
void bind(const Args &...args)
Bind all the arguments in a single invocation.
Definition: sqlite.h:134
int column_int(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite.h:166
bool column_isnull(int col)
Check if a column has a NULL value (0-based)
Definition: sqlite.h:188
Definition: utils.h:31
int get_last_insert_id()
Return LAST_INSERT_ID or LAST_INSER_ROWID or whatever is appropriate for the current database...
bool forked
Marker to catch attempts to reuse connections in forked processes.
Definition: sqlite.h:44
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:172
void drop_settings() override
Drop the settings table.
void execute_one(std::function< void()> on_row)
Run the query, raising an error if there is more than one row in the result.
Definition: sql.h:52
std::unique_ptr< Transaction > transaction(bool readonly=false) override
Begin a transaction.
void explain(const std::string &query, FILE *out) override
Format and print the EXPLAIN output for the query to the given file.
sqlite3 * db
Database connection.
Definition: sqlite.h:42
Common infrastructure for talking with SQL databases.
Error in case of failed database operations.
Definition: error.h:21
std::string get_setting(const std::string &key) override
Get a value from the settings table.
Datetime column_datetime(int col)
Read the string value of a column and parse it as a Datetime.
Report an SQLite error.
Definition: sqlite.h:20
int flags
Connection flags.
Definition: sqlite.h:40
Date and time.
Definition: types.h:164
void execute()
Run the query, ignoring all results.
#define WREPORT_THROWF_ATTRS(a, b)
SQLite statement.
Definition: sqlite.h:115
std::string pathname
Connection pathname.
Definition: sqlite.h:38
std::vector< uint8_t > column_blob(int col)
Read the string value of a column in the result set (0-based)
Definition: sqlite.h:178
bool has_table(const std::string &name) override
Check if the database contains a table.
Database connection.
Definition: sqlite.h:34
void set_setting(const std::string &key, const std::string &value) override
Set a value in the settings table.