summaryrefslogtreecommitdiff
path: root/subprojects/boost-sqlite/src/connection.cpp
diff options
context:
space:
mode:
authorJohn Turner <jturner.usa@gmail.com>2025-09-14 00:16:10 -0400
committerJohn Turner <jturner.usa@gmail.com>2025-09-14 00:16:10 -0400
commit13e0821fd783a1d5083d825af53cf20e8dcbfd76 (patch)
tree1ea363b0f13b3e87d177100e6ae6b9f30a2de1b8 /subprojects/boost-sqlite/src/connection.cpp
parentaa55cb93036a89c64c08e08f4e1de4fa1fd5a775 (diff)
parentefcea3a80da7c4479d5fe168435ecc9fd06bdc72 (diff)
downloadsqlite-kv-bench-13e0821fd783a1d5083d825af53cf20e8dcbfd76.tar.gz
Merge commit 'efcea3a80da7c4479d5fe168435ecc9fd06bdc72' as 'subprojects/boost-sqlite'
Diffstat (limited to 'subprojects/boost-sqlite/src/connection.cpp')
-rw-r--r--subprojects/boost-sqlite/src/connection.cpp158
1 files changed, 158 insertions, 0 deletions
diff --git a/subprojects/boost-sqlite/src/connection.cpp b/subprojects/boost-sqlite/src/connection.cpp
new file mode 100644
index 0000000..1ac0ae7
--- /dev/null
+++ b/subprojects/boost-sqlite/src/connection.cpp
@@ -0,0 +1,158 @@
+//
+// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+
+#include <boost/sqlite/connection.hpp>
+#include <boost/sqlite/statement.hpp>
+
+BOOST_SQLITE_BEGIN_NAMESPACE
+
+
+
+void connection::connect(cstring_ref filename, int flags)
+{
+ system::error_code ec;
+ connect(filename, flags, ec);
+ if (ec)
+ throw_exception(system::system_error(ec, "connect"));
+}
+
+void connection::connect(cstring_ref filename, int flags, system::error_code & ec)
+{
+ sqlite3 * res;
+ auto r = sqlite3_open_v2(filename.c_str(), &res, flags,
+ nullptr);
+ if (r != SQLITE_OK)
+ BOOST_SQLITE_ASSIGN_EC(ec, r);
+ else
+ {
+ impl_.reset(res);
+ impl_.get_deleter().owned_ = true;
+ }
+ sqlite3_extended_result_codes(impl_.get(), true);
+}
+
+void connection::close()
+{
+ system::error_code ec;
+ error_info ei;
+ close(ec, ei);
+ if (ec)
+ throw_exception(system::system_error(ec, ei.message()));
+}
+
+void connection::close(system::error_code & ec,
+ error_info & ei)
+{
+ if (impl_)
+ {
+ auto tmp = impl_.release();
+ auto cc = sqlite3_close(tmp);
+ if (SQLITE_OK != cc)
+ {
+ impl_.reset(tmp);
+ BOOST_SQLITE_ASSIGN_EC(ec, cc);
+ ei.set_message(sqlite3_errmsg(impl_.get()));
+ }
+ }
+}
+
+
+resultset connection::query(
+ core::string_view q,
+ system::error_code & ec,
+ error_info & ei)
+{
+ resultset res;
+ sqlite3_stmt * ss;
+ const auto cc = sqlite3_prepare_v2(impl_.get(),
+ q.data(), static_cast<int>(q.size()),
+ &ss, nullptr);
+
+ if (cc != SQLITE_OK)
+ {
+ BOOST_SQLITE_ASSIGN_EC(ec, cc);
+ ei.set_message(sqlite3_errmsg(impl_.get()));
+ }
+ else
+ {
+ res.impl_.reset(ss);
+ if (!ec)
+ res.read_next(ec, ei);
+ }
+ return res;
+}
+
+resultset connection::query(core::string_view q)
+{
+ system::error_code ec;
+ error_info ei;
+ auto tmp = query(q, ec, ei);
+ if (ec)
+ throw_exception(system::system_error(ec, ei.message()));
+ return tmp;
+}
+
+statement connection::prepare(
+ core::string_view q,
+ system::error_code & ec,
+ error_info & ei)
+{
+ statement res;
+ sqlite3_stmt * ss;
+ const auto cc = sqlite3_prepare_v2(impl_.get(),
+ q.data(), static_cast<int>(q.size()),
+ &ss, nullptr);
+
+ if (cc != SQLITE_OK)
+ {
+ BOOST_SQLITE_ASSIGN_EC(ec, cc);
+ ei.set_message(sqlite3_errmsg(impl_.get()));
+ }
+ else
+ res.impl_.reset(ss);
+ return res;
+}
+
+statement connection::prepare(core::string_view q)
+{
+ system::error_code ec;
+ error_info ei;
+ auto tmp = prepare(q, ec, ei);
+ if (ec)
+ throw_exception(system::system_error(ec, ei.message()));
+ return tmp;
+}
+
+void connection::execute(
+ cstring_ref q,
+ system::error_code & ec,
+ error_info & ei)
+{
+ char * msg = nullptr;
+
+ auto res = sqlite3_exec(impl_.get(), q.c_str(), nullptr, nullptr, &msg);
+ if (res != SQLITE_OK)
+ {
+ BOOST_SQLITE_ASSIGN_EC(ec, res);
+ if (msg != nullptr)
+ ei.set_message(msg);
+ }
+}
+
+void connection::execute(cstring_ref q)
+{
+ system::error_code ec;
+ error_info ei;
+ execute(q, ec, ei);
+ if (ec)
+ throw_exception(system::system_error(ec, ei.message()));
+}
+
+BOOST_SQLITE_END_NAMESPACE
+
+