Use shared_ptrs for the connection

This commit is contained in:
Dr. Patrick Urbanke
2025-04-08 06:17:25 +02:00
parent 43ff5cee3f
commit 8a99272eca
2 changed files with 4 additions and 8 deletions

View File

@@ -18,14 +18,12 @@
namespace sqlgen::sqlite {
class Connection : public sqlgen::Connection {
using ConnPtr = std::unique_ptr<sqlite3, decltype(&sqlite3_close)>;
using StmtPtr = std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)>;
using ConnPtr = Ref<sqlite3>;
using StmtPtr = std::shared_ptr<sqlite3_stmt>;
public:
Connection(const std::string& _fname)
: stmt_(StmtPtr(nullptr, &sqlite3_finalize)), conn_(make_conn(_fname)) {}
Connection(const Connection& _other) = delete;
: stmt_(nullptr), conn_(make_conn(_fname)) {}
static rfl::Result<Ref<sqlgen::Connection>> make(
const std::string& _fname) noexcept;
@@ -36,8 +34,6 @@ class Connection : public sqlgen::Connection {
Result<Nothing> execute(const std::string& _sql) noexcept final;
Connection& operator=(const Connection& _other) = delete;
Result<Ref<IteratorBase>> read(const dynamic::SelectFrom& _query) final {
return error("TODO");
}

View File

@@ -103,7 +103,7 @@ typename Connection::ConnPtr Connection::make_conn(const std::string& _fname) {
throw std::runtime_error("Can't open database: " +
std::string(sqlite3_errmsg(conn)));
}
return ConnPtr(conn, &sqlite3_close);
return ConnPtr::make(std::shared_ptr<sqlite3>(conn, &sqlite3_close)).value();
}
std::string Connection::properties_to_sql(