22 #ifndef DBALLE_DB_SQLITE_INTERNALS_H
23 #define DBALLE_DB_SQLITE_INTERNALS_H
26 #include <dballe/db/sql.h>
31 struct SQLiteStatement;
45 error_sqlite(
const std::string& dbmsg,
const std::string& msg);
48 wreport::ErrorCode code()
const throw () {
return wreport::WR_ERR_ODBC; }
50 virtual const char* what()
const throw () {
return msg.c_str(); }
52 static void throwf(sqlite3* db,
const char* fmt, ...) WREPORT_THROWF_ATTRS(2, 3);
60 sqlite3*
db =
nullptr;
63 void init_after_connect();
73 operator sqlite3*() {
return db; }
75 void open_file(
const std::string& pathname,
int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
76 void open_memory(
int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
77 void open_private_file(
int flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
79 std::unique_ptr<Transaction>
transaction()
override;
80 std::unique_ptr<SQLiteStatement> sqlitestatement(
const std::string& query);
83 bool has_table(
const std::string& name)
override;
90 std::string
get_setting(
const std::string& key)
override;
97 void set_setting(
const std::string& key,
const std::string& value)
override;
119 void exec(
const std::string& query);
120 void exec_nothrow(
const std::string& query) noexcept;
127 sqlite3_stmt *stm =
nullptr;
142 template<
typename... Args>
void bind(
const Args& ...args)
144 bindn<
sizeof...(args)>(args...);
147 void bind_null_val(
int idx);
148 void bind_val(
int idx,
int val);
149 void bind_val(
int idx,
unsigned val);
150 void bind_val(
int idx,
unsigned short val);
151 void bind_val(
int idx,
const Datetime& val);
152 void bind_val(
int idx,
const char* val);
153 void bind_val(
int idx,
const std::string& val);
164 void execute(std::function<
void()> on_row);
173 int column_int(
int col) {
return sqlite3_column_int(stm, col); }
176 sqlite3_int64
column_int64(
int col) {
return sqlite3_column_int64(stm, col); }
182 const char*
column_string(
int col) {
return (
const char*)sqlite3_column_text(stm, col); }
188 bool column_isnull(
int col) {
return sqlite3_column_type(stm, col) == SQLITE_NULL; }
190 void wrap_sqlite3_reset();
191 void wrap_sqlite3_reset_nothrow() noexcept;
198 operator sqlite3_stmt*() {
return stm; }
203 int exec_direct(
const char* query);
205 int exec_direct(
const char* query,
int qlen);
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);
220 bool fetch_expecting_one();
222 void close_cursor_if_needed();
224 size_t select_rowcount();
231 template<
size_t total>
void bindn() {}
233 template<
size_t total,
typename ...Args,
typename T>
void bindn(
const T& first,
const Args& ...args)
235 bind_val(total -
sizeof...(args), first);
236 bindn<total>(args...);
error_sqlite(sqlite3 *db, const std::string &msg)
Copy informations from the ODBC diagnostic record to the dba error report.
void reset_and_throw(const std::string &errmsg)
Get the current error message, reset the statement and throw error_sqlite.
void bind(const Args &...args)
Bind all the arguments in a single invocation.
Definition: sqlite/internals.h:142
void drop_table_if_exists(const char *name)
Delete a table in the database if it exists, otherwise do nothing.
sqlite3 * db
Database connection.
Definition: sqlite/internals.h:60
SQLite statement.
Definition: sqlite/internals.h:124
Base exception for database errors.
Definition: db/defs.h:54
void drop_settings() override
Drop the settings table.
bool has_table(const std::string &name) override
Check if the database contains a table.
Database connection.
Definition: sqlite/internals.h:56
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
int get_last_insert_id()
Return LAST_INSERT_ID or LAST_INSER_ROWID or whatever is appropriate for the current database...
bool column_isnull(int col)
Check if a column has a NULL value (0-based)
Definition: sqlite/internals.h:188
Functions used to connect to DB-All.e and insert, query and delete data.
std::unique_ptr< Transaction > transaction() override
Begin a transaction.
double column_double(int col)
Read the double value of a column in the result set (0-based)
Definition: sqlite/internals.h:179
int column_int(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite/internals.h:173
Report an SQLite error.
Definition: sqlite/internals.h:36
void set_setting(const std::string &key, const std::string &value) override
Set a value in the settings table.
void exec(const std::string &query)
Wrap sqlite3_exec, without a callback.
void execute_one(std::function< void()> on_row)
Run the query, raising an error if there is more than one row in the result.
Date and time.
Definition: types.h:147
const char * column_string(int col)
Read the string value of a column in the result set (0-based)
Definition: sqlite/internals.h:182
int changes()
Count the number of rows modified by the last query that was run.
void execute()
Run the query, ignoring all results.
Datetime column_datetime(int col)
Read the string value of a column and parse it as a Datetime.
sqlite3_int64 column_int64(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite/internals.h:176
std::string get_setting(const std::string &key) override
Get a value from the settings table.