libdballe  7.7
sqlite/internals.h
1 /*
2  * db/sqlite/internals - Implementation infrastructure for the SQLite DB connection
3  *
4  * Copyright (C) 2014 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBALLE_DB_SQLITE_INTERNALS_H
23 #define DBALLE_DB_SQLITE_INTERNALS_H
24 
25 #include <dballe/db/db.h>
26 #include <dballe/db/sql.h>
27 #include <sqlite3.h>
28 
29 namespace dballe {
30 namespace db {
31 struct SQLiteStatement;
32 
36 struct error_sqlite : public db::error
37 {
38  std::string msg;
39 
44  error_sqlite(sqlite3* db, const std::string& msg);
45  error_sqlite(const std::string& dbmsg, const std::string& msg);
46  ~error_sqlite() throw () {}
47 
48  wreport::ErrorCode code() const throw () { return wreport::WR_ERR_ODBC; }
49 
50  virtual const char* what() const throw () { return msg.c_str(); }
51 
52  static void throwf(sqlite3* db, const char* fmt, ...) WREPORT_THROWF_ATTRS(2, 3);
53 };
54 
57 {
58 protected:
60  sqlite3* db = nullptr;
61 
62 protected:
63  void init_after_connect();
64 
65 public:
67  SQLiteConnection(const SQLiteConnection&) = delete;
68  SQLiteConnection(const SQLiteConnection&&) = delete;
70 
71  SQLiteConnection& operator=(const SQLiteConnection&) = delete;
72 
73  operator sqlite3*() { return db; }
74 
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);
78 
79  std::unique_ptr<Transaction> transaction() override;
80  std::unique_ptr<SQLiteStatement> sqlitestatement(const std::string& query);
81 
83  bool has_table(const std::string& name) override;
84 
90  std::string get_setting(const std::string& key) override;
91 
97  void set_setting(const std::string& key, const std::string& value) override;
98 
100  void drop_settings() override;
101 
105  void drop_table_if_exists(const char* name);
106 
113  int get_last_insert_id();
114 
116  int changes();
117 
119  void exec(const std::string& query);
120  void exec_nothrow(const std::string& query) noexcept;
121 };
122 
125 {
126  SQLiteConnection& conn;
127  sqlite3_stmt *stm = nullptr;
128 
129  SQLiteStatement(SQLiteConnection& conn, const std::string& query);
130  SQLiteStatement(const SQLiteStatement&) = delete;
131  SQLiteStatement(const SQLiteStatement&&) = delete;
132  ~SQLiteStatement();
133  SQLiteStatement& operator=(const SQLiteStatement&) = delete;
134 
142  template<typename... Args> void bind(const Args& ...args)
143  {
144  bindn<sizeof...(args)>(args...);
145  }
146 
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); // Warning: SQLITE_STATIC is used
153  void bind_val(int idx, const std::string& val); // Warning: SQLITE_STATIC is used
154 
156  void execute();
157 
164  void execute(std::function<void()> on_row);
165 
170  void execute_one(std::function<void()> on_row);
171 
173  int column_int(int col) { return sqlite3_column_int(stm, col); }
174 
176  sqlite3_int64 column_int64(int col) { return sqlite3_column_int64(stm, col); }
177 
179  double column_double(int col) { return sqlite3_column_double(stm, col); }
180 
182  const char* column_string(int col) { return (const char*)sqlite3_column_text(stm, col); }
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 
error_sqlite(sqlite3 *db, const std::string &msg)
Copy informations from the ODBC diagnostic record to the dba error report.
void bind(const Args &...args)
Bind all the arguments in a single invocation.
Definition: sqlite/internals.h:142
SQLite statement.
Definition: sqlite/internals.h:124
Base exception for database errors.
Definition: db/defs.h:54
Database connection.
Definition: sqlite/internals.h:56
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
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.
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
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
sqlite3_int64 column_int64(int col)
Read the int value of a column in the result set (0-based)
Definition: sqlite/internals.h:176
Definition: sql.h:69