3 #ifndef __SQLITEXX_SQLITE_DBCONNECTION_H__
4 #define __SQLITEXX_SQLITE_DBCONNECTION_H__
49 const std::string& filename,
51 const std::chrono::milliseconds timeout = DEFAULT_TIMEOUT);
57 dbconnection(
const std::string& filename,
const std::chrono::milliseconds timeout);
63 dbconnection(
const std::u16string& filename,
const std::chrono::milliseconds timeout = DEFAULT_TIMEOUT);
98 explicit operator bool()
const noexcept;
102 sqlite3*
handle()
const noexcept;
114 void open(
const std::u16string& filename);
120 long long row_id()
const noexcept;
130 template <
typename F>
132 const std::string& name,
134 int is_deterministic =
false,
135 const textencoding encoding = textencoding::utf8,
138 using FunctionType =
typename SQLiteFunctionTraits<F>::f_type;
139 FunctionType *userFunction =
new FunctionType(
function);
141 int flags =
static_cast<int>(encoding);
142 if (is_deterministic) {
143 flags |= SQLITE_DETERMINISTIC;
146 int errorcode = sqlite3_create_function_v2(
150 static_cast<int>(flags),
152 &internal_general_scalar_function<FunctionType>,
155 &internal_delete<FunctionType>);
157 throw_error_code(errorcode,
"");
167 template <
typename F>
169 const std::string& name,
171 bool is_deterministic =
false,
172 const textencoding encoding = textencoding::utf8)
174 using FunctionType =
typename function_traits<F>::f_type;
175 FunctionType *userFunction =
new FunctionType(
function);
177 int flags =
static_cast<int>(encoding);
178 if (is_deterministic) {
179 flags |= SQLITE_DETERMINISTIC;
182 int errorcode = sqlite3_create_function_v2(
185 function_traits<F>::nargs,
188 &internal_scalar_function<FunctionType>,
191 &internal_delete<FunctionType>);
193 throw_error_code(errorcode,
"");
202 template <
typename A>
204 const std::string& name,
205 bool is_deterministic =
false,
206 const textencoding encoding = textencoding::utf8)
208 using StepFunctionType = function_traits<decltype(&A::step)>;
209 aggregate_wrapper<A> *wrapper =
new aggregate_wrapper<A>;
211 int flags =
static_cast<int>(encoding);
212 if (is_deterministic) {
213 flags |= SQLITE_DETERMINISTIC;
216 int errorcode = sqlite3_create_function_v2(
219 StepFunctionType::nargs,
220 static_cast<int>(flags),
223 &internal_step<aggregate_wrapper<A> >,
224 &internal_final<aggregate_wrapper<A> >,
225 &internal_dispose<aggregate_wrapper<A> >);
227 throw_error_code(errorcode,
"");
238 template <
typename F>
240 const std::string& name,
242 const textencoding encoding = textencoding::utf8)
244 using CollationType =
typename collation_traits<F>::f_type;
245 CollationType *userFunction =
new CollationType(
function);
247 int flags =
static_cast<int>(encoding);
249 int errorcode = sqlite3_create_collation_v2(
254 &internal_collation_function<CollationType>,
255 &internal_delete<CollationType>);
257 throw_error_code(errorcode,
"");
261 template <
typename F>
262 void profile(F&& callback,
void*
const context =
nullptr)
264 sqlite3_profile(
handle(), callback, context);
268 using connection_handle = std::shared_ptr<sqlite3>;
269 connection_handle m_handle;
271 static const std::chrono::minutes DEFAULT_TIMEOUT;
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. ...
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...
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.
openmode
Different ways to open a dbconnection.
database will be created if it does not already exist
dbconnection() noexcept
Default constructor.
Helps with serializing access to a database connection.
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.
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.
void open(const std::string &filename, openmode mode=openmode::read_write|openmode::create)
Open an SQLite database file as specified by the filename argument.