diff options
author | John Turner <jturner.usa@gmail.com> | 2025-09-14 00:16:10 -0400 |
---|---|---|
committer | John Turner <jturner.usa@gmail.com> | 2025-09-14 00:16:10 -0400 |
commit | efcea3a80da7c4479d5fe168435ecc9fd06bdc72 (patch) | |
tree | 5cb0177e17b1b00a177f2e830e809f606334571b /src/connection.cpp | |
download | sqlite-kv-bench-efcea3a80da7c4479d5fe168435ecc9fd06bdc72.tar.gz |
Squashed 'subprojects/boost-sqlite/' content from commit 3378e35
git-subtree-dir: subprojects/boost-sqlite
git-subtree-split: 3378e353705271e569cf4ba15c467b840a39798c
Diffstat (limited to 'src/connection.cpp')
-rw-r--r-- | src/connection.cpp | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/src/connection.cpp b/src/connection.cpp new file mode 100644 index 0000000..1ac0ae7 --- /dev/null +++ b/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 + + |