Sqlitepp  1.2.3
A thin sqlite C++ wrapper
sqlite.hpp
Go to the documentation of this file.
1 
4 // Copyright 2019 Matthew Chandler
5 
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 
24 #ifndef SQLITE_HPP
25 #define SQLITE_HPP
26 
27 #include <string>
28 
29 #include <sqlitepp/sqlite3.h>
30 
32 
35 namespace sqlite
36 {
38 
42  class Connection final
43  {
44  public:
45  class Stmt;
46 
48 
52  explicit Connection(const std::string & filename);
53  ~Connection();
54 
55  // non-copyable
56  Connection(const Connection &) = delete;
57  Connection & operator=(const Connection &) = delete;
58 
59  // movable
61  Connection & operator=(Connection &&);
62 
64 
69  Stmt create_statement(const std::string & sql);
70 
72 
82  void exec(const std::string & sql, int (*callback)(void *, int, char **, char **) = nullptr,
83  void * arg = nullptr);
84 
86 
88  void begin_transaction();
89 
91 
93  void commit();
94 
96 
98  void rollback();
99 
101 
103  void interrupt();
104 
106 
109  sqlite3_int64 last_insert_rowid();
110 
112 
118  int total_changes();
119 
120 
122 
125  {
126  std::string type;
127  std::string collation;
128  bool not_null = false;
129  bool primary_key = false;
130  bool auto_inc = false;
131  };
132 
134 
141  Column_metadata table_column_metadata(const std::string & table_name, const std::string & column_name,
142  const std::string & db_name = "main");
143 
145 
147  const sqlite3 * get_c_obj() const;
148 
150 
152  sqlite3 * get_c_obj();
153 
154  private:
156  sqlite3 * db_ = nullptr;
157  };
158 
160 
167  class Connection::Stmt final
168  {
169  public:
171 
177  Stmt(const std::string & sql, Connection & db);
178  ~Stmt();
179 
180  // non-copyable
181  Stmt(const Stmt &) = delete;
182  Stmt & operator=(const Stmt &) = delete;
183 
184  // movable
185  Stmt(Stmt &&);
186  Stmt & operator=(Stmt &&);
187 
191 
193 
199  void bind(const int index, const double val);
200 
202  void bind(const int index, const int val);
203 
205  void bind(const int index, const sqlite3_int64 val);
206 
208  void bind(const int index, const std::string & val);
209 
211  void bind(const int index, const char * val);
212 
214 
219  void bind_null(const int index);
220 
222  void bind(const int index);
223 
225 
230  void bind(const std::string & name, const double val);
231 
233  void bind(const std::string & name, const int val);
234 
236  void bind(const std::string & name, const sqlite3_int64 val);
237 
239  void bind(const std::string & name, const std::string & val);
240 
242  void bind(const std::string & name, const char * val);
243 
245 
249  void bind_null(const std::string & name);
250 
252  void bind(const std::string & name);
253 
255 
257 
262  std::string bind_parameter_name(const int index);
263 
265 
270  int bind_parameter_index(const std::string & name);
271 
273 
276  int bind_parameter_count();
277 
279 
285  bool step();
286 
288 
295  template<typename T>
296  T get_col(const int column);
297 
299 
303  void reset();
304 
306 
308  void clear_bindings();
309 
311 
314  bool busy();
315 
317 
320  bool readonly();
321 
323 
325  const sqlite3_stmt * get_c_obj() const;
326 
328 
330  sqlite3_stmt * get_c_obj();
331 
332  private:
334  sqlite3_stmt * stmt_ = nullptr;
336  sqlite3 * db_ = nullptr;
337  };
338 };
339 
340 # endif // SQLITE_HPP
Column metadata info.
Definition: sqlite.hpp:124
std::string type
Column&#39;s declared data type.
Definition: sqlite.hpp:126
Sqlite C++ wrapper and associated types.
Definition: database.hpp:30
Prepared statement obj - usually created by Connection::create_statement.
Definition: sqlite.hpp:167
Sqlite database connection.
Definition: sqlite.hpp:42
bool not_null
true if the column has a NOT NULL constraint
Definition: sqlite.hpp:128
void commit()
End a transaction & commit.
Definition: sqlite.cpp:89
void rollback()
End a transaction & rollback.
Definition: sqlite.cpp:94
void exec(const std::string &sql, int(*callback)(void *, int, char **, char **)=nullptr, void *arg=nullptr)
Execute SQL statement(s)
Definition: sqlite.cpp:67
sqlite3_int64 last_insert_rowid()
Get last INSERTed Row ID.
Definition: sqlite.cpp:104
void begin_transaction()
Start a transaction.
Definition: sqlite.cpp:84
int total_changes()
Get total number of rows modified.
Definition: sqlite.cpp:109
bool primary_key
true if column is part of the PRIMARY KEY
Definition: sqlite.hpp:129
bool auto_inc
true True if column is AUTOINCREMENT
Definition: sqlite.hpp:130
Connection(const std::string &filename)
Open / create new DB.
Definition: sqlite.cpp:29
Stmt create_statement(const std::string &sql)
Create a new prepared statement.
Definition: sqlite.cpp:62
const sqlite3 * get_c_obj() const
Get wrapped C sqlite3 object (for use with the sqlite C API)
Definition: sqlite.cpp:141
Column_metadata table_column_metadata(const std::string &table_name, const std::string &column_name, const std::string &db_name="main")
Get column metadata.
Definition: sqlite.cpp:114
std::string collation
Name of default collation sequence.
Definition: sqlite.hpp:127
void interrupt()
Interrupt a long-running query.
Definition: sqlite.cpp:99