boost_sqlite 1
A sqlite C++ library
Loading...
Searching...
No Matches
connection.hpp
1// Copyright (c) 2022 Klemens D. Morgenstern
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5#ifndef BOOST_SQLITE_CONNECTION_HPP
6#define BOOST_SQLITE_CONNECTION_HPP
7
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>
12#include <memory>
13#include <boost/system/system_error.hpp>
14
15BOOST_SQLITE_BEGIN_NAMESPACE
16
17template<typename T>
18struct static_resultset;
19
32{
34 using handle_type = sqlite3*;
36 handle_type handle() const { return impl_.get(); }
38 handle_type release() && { return impl_.release(); }
39
41 connection() = default;
43 explicit connection(handle_type handle, bool take_ownership = true) : impl_(handle, take_ownership) {}
45 connection(connection && ) = default;
48
51 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) { connect(filename, flags); }
52
53
56 BOOST_SQLITE_DECL void connect(cstring_ref filename, int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
57 BOOST_SQLITE_DECL void connect(cstring_ref filename, int flags, system::error_code & ec);
59
62 BOOST_SQLITE_DECL void close();
63 BOOST_SQLITE_DECL void close(system::error_code & ec, error_info & ei);
65
67 bool valid() const {return impl_ != nullptr;}
68
69
72 BOOST_SQLITE_DECL resultset query(
73 core::string_view q,
74 system::error_code & ec,
75 error_info & ei);
76
77 BOOST_SQLITE_DECL resultset query(core::string_view q);
78
79 template<typename T>
80 static_resultset<T> query(
81 core::string_view q,
82 system::error_code & ec,
83 error_info & ei)
84 {
85 static_resultset<T> tmp = query(q, ec, ei);
86 if (ec)
87 return {};
88 tmp.check_columns_(ec, ei);
89 if (ec)
90 return {};
91
92 return tmp;
93 }
94
95 template<typename T>
96 static_resultset<T> query(core::string_view q)
97 {
98 system::error_code ec;
99 error_info ei;
100 auto tmp = query<T>(q, ec, ei);
101 if (ec)
102 throw_exception(system::system_error(ec, ei.message()));
103 return tmp;
104 }
106
109 BOOST_SQLITE_DECL void execute(
110 cstring_ref q,
111 system::error_code & ec,
112 error_info & ei);
113
114 BOOST_SQLITE_DECL void execute(cstring_ref q);
115
116 template<typename StringLike,
117 typename = decltype(std::declval<const StringLike&>().c_str())>
119 const StringLike& q,
120 system::error_code & ec,
121 error_info & ei)
122 {
123 execute(q.c_str(), ec, ei);
124 }
125 template<typename StringLike,
126 typename = decltype(std::declval<const StringLike&>().c_str())>
127 void execute(const StringLike & q) { execute(q.c_str());}
129
132 BOOST_SQLITE_DECL statement prepare(
133 core::string_view q,
134 system::error_code & ec,
135 error_info & ei);
136
137 BOOST_SQLITE_DECL statement prepare(core::string_view q);
139
142 cstring_ref table,
143 cstring_ref db_name = "main") const
144 {
145 return sqlite3_table_column_metadata(impl_.get(), db_name.c_str(), table.c_str(),
146 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr)
147 == SQLITE_OK;
148 }
149
152 cstring_ref table,
153 cstring_ref column,
154 cstring_ref db_name = "main") const
155 {
156 return sqlite3_table_column_metadata(impl_.get(), db_name.c_str(), table.c_str(), column.c_str(),
157 nullptr, nullptr, nullptr, nullptr, nullptr)
158 == SQLITE_OK;
159 }
160 private:
161 struct deleter_
162 {
163 deleter_(bool owned = true) : owned_(owned) {}
164 bool owned_ = true;
165 void operator()(sqlite3 *impl)
166 {
167 if (owned_)
168 sqlite3_close_v2(impl);
169 }
170 };
171
172 std::unique_ptr<sqlite3, deleter_> impl_{nullptr, deleter_{}};
173};
174
175BOOST_SQLITE_END_NAMESPACE
176
177
178#endif //BOOST_SQLITE_CONNECTION_HPP
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.
Definition error.hpp:33
Representation of a result from a database.
Definition resultset.hpp:41
A statement used for a prepared-statement.