4 #ifndef DBALLE_SQL_SQLITE_H
5 #define DBALLE_SQL_SQLITE_H
7 #include <dballe/core/error.h>
15 struct SQLiteStatement;
25 error_sqlite(
const std::string& dbmsg,
const std::string& msg);
28 const char* what()
const noexcept
override {
return msg.c_str(); }
38 sqlite3*
db =
nullptr;
40 void init_after_connect();
41 static void on_sqlite3_profile(
void* arg,
const char* query, sqlite3_uint64 usecs);
51 operator sqlite3*() {
return db; }
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);
57 std::unique_ptr<Transaction>
transaction()
override;
58 std::unique_ptr<SQLiteStatement> sqlitestatement(
const std::string& query);
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;
64 void execute(
const std::string& query)
override;
65 void explain(
const std::string& query, FILE* out)
override;
84 void exec(
const std::string& query);
85 void exec_nothrow(
const std::string& query) noexcept;
92 sqlite3_stmt *stm =
nullptr;
107 template<
typename... Args>
void bind(
const Args& ...args)
109 bindn<
sizeof...(args)>(args...);
112 void bind_null_val(
int idx);
113 void bind_val(
int idx,
int val);
114 void bind_val(
int idx,
unsigned val);
115 void bind_val(
int idx,
unsigned short val);
116 void bind_val(
int idx,
const Datetime& val);
117 void bind_val(
int idx,
const char* val);
118 void bind_val(
int idx,
const std::string& val);
119 void bind_val(
int idx,
const std::vector<uint8_t>& val);
130 void execute(std::function<
void()> on_row);
139 int column_int(
int col) {
return sqlite3_column_int(stm, col); }
142 sqlite3_int64
column_int64(
int col) {
return sqlite3_column_int64(stm, col); }
148 const char*
column_string(
int col) {
return (
const char*)sqlite3_column_text(stm, col); }
152 int size = sqlite3_column_bytes(stm, col);
153 const uint8_t* val = (
const uint8_t*)sqlite3_column_blob(stm, col);
154 return std::vector<uint8_t>(val, val + size);
161 bool column_isnull(
int col) {
return sqlite3_column_type(stm, col) == SQLITE_NULL; }
163 void wrap_sqlite3_reset();
164 void wrap_sqlite3_reset_nothrow() noexcept;
171 operator sqlite3_stmt*() {
return stm; }
176 int exec_direct(
const char* query);
178 int exec_direct(
const char* query,
int qlen);
181 int execute_and_close();
183 int exec_direct_and_close(
const char* query);
185 int exec_direct_and_close(
const char* query,
int qlen);
193 bool fetch_expecting_one();
195 void close_cursor_if_needed();
197 size_t select_rowcount();
204 template<
size_t total>
void bindn() {}
206 template<
size_t total,
typename ...Args,
typename T>
void bindn(
const T& first,
const Args& ...args)
208 bind_val(total -
sizeof...(args), first);
209 bindn<total>(args...);
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:148
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.
std::unique_ptr< Transaction > transaction() override
Begin a transaction.
sqlite3_int64 column_int64(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite.h:142
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:107
int column_int(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite.h:139
bool column_isnull(int col)
Check if a column has a NULL value (0-based)
Definition: sqlite.h:161
int get_last_insert_id()
Return LAST_INSERT_ID or LAST_INSER_ROWID or whatever is appropriate for the current database...
double column_double(int col)
Read the double value of a column in the result set (0-based)
Definition: sqlite.h:145
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.
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:38
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
Date and time.
Definition: types.h:158
void execute()
Run the query, ignoring all results.
#define WREPORT_THROWF_ATTRS(a, b)
SQLite statement.
Definition: sqlite.h:89
std::vector< uint8_t > column_blob(int col)
Read the string value of a column in the result set (0-based)
Definition: sqlite.h:151
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.