SQLiteXX  0.1.0
 All Classes Namespaces Files Functions Enumerations Enumerator
DBConnection.h
Go to the documentation of this file.
1 
3 #ifndef __SQLITEXX_SQLITE_DBCONNECTION_H__
4 #define __SQLITEXX_SQLITE_DBCONNECTION_H__
5 
6 #include "Exception.h"
7 #include "Functions.h"
8 #include "Mutex.h"
9 #include "Open.h"
10 
11 #include <sqlite3.h>
12 
13 #include <cassert>
14 #include <chrono>
15 #include <memory>
16 #include <thread>
17 
18 
19 namespace sqlite
20 {
25  {
26  public:
27 
30  dbconnection() noexcept;
31 
35  dbconnection(const dbconnection& other) noexcept;
36 
41  dbconnection(dbconnection&& other) noexcept;
42 
49  const std::string& filename,
51  const std::chrono::milliseconds timeout = DEFAULT_TIMEOUT);
52 
57  dbconnection(const std::string& filename, const std::chrono::milliseconds timeout);
58 
63  dbconnection(const std::u16string& filename, const std::chrono::milliseconds timeout = DEFAULT_TIMEOUT);
64 
70  dbconnection& operator=(const dbconnection& other) noexcept;
71 
77  dbconnection& operator=(dbconnection&& other) noexcept;
78 
82  static dbconnection memory();
83 
87  static dbconnection wide_memory();
88 
93 
98  explicit operator bool() const noexcept;
99 
102  sqlite3* handle() const noexcept;
103 
108  void open(const std::string& filename, openmode mode = openmode::read_write | openmode::create);
109 
114  void open(const std::u16string& filename);
115 
120  long long row_id() const noexcept;
121 
130  template <typename F>
132  const std::string& name,
133  F&& function,
134  int is_deterministic = false,
135  const textencoding encoding = textencoding::utf8,
136  int nargs = -1)
137  {
138  using FunctionType = typename SQLiteFunctionTraits<F>::f_type;
139  FunctionType *userFunction = new FunctionType(function);
140 
141  int flags = static_cast<int>(encoding);
142  if (is_deterministic) {
143  flags |= SQLITE_DETERMINISTIC;
144  }
145 
146  int errorcode = sqlite3_create_function_v2(
147  handle(),
148  name.c_str(),
149  nargs,
150  static_cast<int>(flags),
151  (void*)userFunction,
152  &internal_general_scalar_function<FunctionType>,
153  nullptr,
154  nullptr,
155  &internal_delete<FunctionType>);
156 
157  throw_error_code(errorcode, "");
158  }
159 
167  template <typename F>
169  const std::string& name,
170  F&& function,
171  bool is_deterministic = false,
172  const textencoding encoding = textencoding::utf8)
173  {
174  using FunctionType = typename function_traits<F>::f_type;
175  FunctionType *userFunction = new FunctionType(function);
176 
177  int flags = static_cast<int>(encoding);
178  if (is_deterministic) {
179  flags |= SQLITE_DETERMINISTIC;
180  }
181 
182  int errorcode = sqlite3_create_function_v2(
183  handle(),
184  name.c_str(),
185  function_traits<F>::nargs,
186  flags,
187  (void*)userFunction,
188  &internal_scalar_function<FunctionType>,
189  nullptr,
190  nullptr,
191  &internal_delete<FunctionType>);
192 
193  throw_error_code(errorcode, "");
194  }
195 
202  template <typename A>
204  const std::string& name,
205  bool is_deterministic = false,
206  const textencoding encoding = textencoding::utf8)
207  {
208  using StepFunctionType = function_traits<decltype(&A::step)>;
209  aggregate_wrapper<A> *wrapper = new aggregate_wrapper<A>;
210 
211  int flags = static_cast<int>(encoding);
212  if (is_deterministic) {
213  flags |= SQLITE_DETERMINISTIC;
214  }
215 
216  int errorcode = sqlite3_create_function_v2(
217  handle(),
218  name.c_str(),
219  StepFunctionType::nargs,
220  static_cast<int>(flags),
221  (void*)wrapper,
222  nullptr,
223  &internal_step<aggregate_wrapper<A> >,
224  &internal_final<aggregate_wrapper<A> >,
225  &internal_dispose<aggregate_wrapper<A> >);
226 
227  throw_error_code(errorcode, "");
228  }
229 
238  template <typename F>
240  const std::string& name,
241  F&& function,
242  const textencoding encoding = textencoding::utf8)
243  {
244  using CollationType = typename collation_traits<F>::f_type;
245  CollationType *userFunction = new CollationType(function);
246 
247  int flags = static_cast<int>(encoding);
248 
249  int errorcode = sqlite3_create_collation_v2(
250  handle(),
251  name.c_str(),
252  flags,
253  (void*)userFunction,
254  &internal_collation_function<CollationType>,
255  &internal_delete<CollationType>);
256 
257  throw_error_code(errorcode, "");
258  }
259 
260 
261  template <typename F>
262  void profile(F&& callback, void* const context = nullptr)
263  {
264  sqlite3_profile(handle(), callback, context);
265  }
266 
267  private:
268  using connection_handle = std::shared_ptr<sqlite3>;
269  connection_handle m_handle;
270 
271  static const std::chrono::minutes DEFAULT_TIMEOUT;
272  };
273 }
274 
275 #endif
dbconnection & operator=(const dbconnection &other) noexcept
Copy assignment operator.
sqlite3 * handle() const noexcept
Returns pointer to the underlying "sqlite3" object.
void create_collation(const std::string &name, F &&function, const textencoding encoding=textencoding::utf8)
Used to add an SQL collation or redefine the behavior of existing SQL collations. ...
Definition: DBConnection.h:239
void create_aggregate(const std::string &name, bool is_deterministic=false, const textencoding encoding=textencoding::utf8)
Used to add SQL aggregate functions or redefine the behavior of existing SQL aggregate functions...
Definition: DBConnection.h:203
void create_function(const std::string &name, F &&function, bool is_deterministic=false, const textencoding encoding=textencoding::utf8)
Used to add SQL functions or redefine the behavior of existing SQL functions.
Definition: DBConnection.h:168
openmode
Different ways to open a dbconnection.
Definition: Open.h:14
database will be created if it does not already exist
dbconnection() noexcept
Default constructor.
Definition: DBConnection.cpp:7
Helps with serializing access to a database connection.
Definition: Mutex.h:19
long long row_id() const noexcept
Returns the rowid of the most recent successful "INSERT" into a rowid table or virtual table on datab...
static dbconnection wide_memory()
Create a purely in memory database with UTF-16 as the native byte order.
void create_general_function(const std::string &name, F &&function, int is_deterministic=false, const textencoding encoding=textencoding::utf8, int nargs=-1)
Used to add SQL functions or redefine the behavior of existing SQL functions.
Definition: DBConnection.h:131
sqlite::mutex mutex()
Returns a mutex that serializes access to the database.
opened for reading and writing if possible, or reading only if the file is write protected by the ope...
static dbconnection memory()
Create a purely in memory database.
Class that represents a connection to a database.
Definition: DBConnection.h:24
void open(const std::string &filename, openmode mode=openmode::read_write|openmode::create)
Open an SQLite database file as specified by the filename argument.