5#ifndef BOOST_SQLITE_CONNECTION_HPP
6#define BOOST_SQLITE_CONNECTION_HPP
8#include <boost/sqlite/detail/config.hpp>
9#include <boost/sqlite/error.hpp>
10#include <boost/sqlite/resultset.hpp>
11#include <boost/sqlite/statement.hpp>
13#include <boost/system/system_error.hpp>
15BOOST_SQLITE_BEGIN_NAMESPACE
18struct static_resultset;
51 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) { connect(filename, flags); }
56 BOOST_SQLITE_DECL
void connect(
cstring_ref filename,
int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
67 bool valid()
const {
return impl_ !=
nullptr;}
74 system::error_code & ec,
82 system::error_code & ec,
85 static_resultset<T> tmp = query(q, ec, ei);
88 tmp.check_columns_(ec, ei);
96 static_resultset<T>
query(core::string_view q)
98 system::error_code ec;
100 auto tmp = query<T>(q, ec, ei);
102 throw_exception(system::system_error(ec, ei.message()));
111 system::error_code & ec,
116 template<
typename StringLike,
117 typename =
decltype(std::declval<const StringLike&>().c_str())>
120 system::error_code & ec,
123 execute(q.c_str(), ec, ei);
125 template<
typename StringLike,
126 typename =
decltype(std::declval<const StringLike&>().c_str())>
134 system::error_code & ec,
145 return sqlite3_table_column_metadata(impl_.get(), db_name.c_str(), table.c_str(),
146 nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr)
156 return sqlite3_table_column_metadata(impl_.get(), db_name.c_str(), table.c_str(), column.c_str(),
157 nullptr,
nullptr,
nullptr,
nullptr,
nullptr)
163 deleter_(
bool owned =
true) : owned_(owned) {}
165 void operator()(sqlite3 *impl)
168 sqlite3_close_v2(impl);
172 std::unique_ptr<sqlite3, deleter_> impl_{
nullptr, deleter_{}};
175BOOST_SQLITE_END_NAMESPACE
main object for a connection to a database.
void execute(const StringLike &q, system::error_code &ec, error_info &ei)
void close(system::error_code &ec, error_info &ei)
connection & operator=(connection &&)=default
Move assign operator.
resultset query(core::string_view q)
connection(cstring_ref filename, int flags=SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE)
Construct a connection and connect it to filename.. flags is set by SQLITE_OPEN_* flags.
bool has_table(cstring_ref table, cstring_ref db_name="main") const
Check if the database has the table.
connection(connection &&)=default
Move constructor.
void execute(cstring_ref q)
void execute(const StringLike &q)
bool has_column(cstring_ref table, cstring_ref column, cstring_ref db_name="main") const
Check if the database has the table.
resultset query(core::string_view q, system::error_code &ec, error_info &ei)
bool valid() const
Check if the database holds a valid handle.
void execute(cstring_ref q, system::error_code &ec, error_info &ei)
static_resultset< T > query(core::string_view q)
connection(handle_type handle, bool take_ownership=true)
Construct the connection from a handle.
sqlite3 * handle_type
The handle of the connection.
connection()=default
Default constructor.
static_resultset< T > query(core::string_view q, system::error_code &ec, error_info &ei)
void connect(cstring_ref filename, int flags=SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE)
handle_type release() &&
Release the owned handle.
void connect(cstring_ref filename, int flags, system::error_code &ec)
statement prepare(core::string_view q)
handle_type handle() const
Returns the handle.
statement prepare(core::string_view q, system::error_code &ec, error_info &ei)
Small wrapper for a null-terminated string that can be directly passed to C APIS.
Additional information about error conditions stored in an sqlite-allocate string.
Representation of a result from a database.
A statement used for a prepared-statement.