Use std::unique_ptr

This commit is contained in:
Dr. Patrick Urbanke
2025-04-05 07:22:16 +02:00
parent 0a3d965944
commit ad667ca751
2 changed files with 8 additions and 28 deletions

View File

@@ -17,19 +17,17 @@
namespace sqlgen::sqlite {
class Connection : public sqlgen::Connection {
using ConnPtr = std::unique_ptr<sqlite3, decltype(&sqlite3_close)>;
public:
Connection(const std::string& _fname) : conn_(make_conn(_fname)) {}
Connection(const Connection& _other) = delete;
Connection(Connection&& _other) : conn_(_other.conn_) {
_other.conn_ = nullptr;
}
static rfl::Result<Ref<sqlgen::Connection>> make(
const std::string& _fname) noexcept;
~Connection();
~Connection() = default;
Result<Nothing> commit() final { return execute("COMMIT;"); }
@@ -37,8 +35,6 @@ class Connection : public sqlgen::Connection {
Connection& operator=(const Connection& _other) = delete;
Connection& operator=(Connection&& _other);
Result<Ref<Iterator>> read(const dynamic::SelectFrom& _query) final {
return error("TODO");
}
@@ -68,7 +64,7 @@ class Connection : public sqlgen::Connection {
std::string insert_to_sql(const dynamic::Insert& _stmt) noexcept;
/// Generates the underlying connection.
static sqlite3* make_conn(const std::string& _fname);
static ConnPtr make_conn(const std::string& _fname);
/// Expresses the properies as SQL.
std::string properties_to_sql(const dynamic::types::Properties& _p) noexcept;
@@ -78,7 +74,7 @@ class Connection : public sqlgen::Connection {
private:
/// The underlying sqlite3 connection.
sqlite3* conn_;
ConnPtr conn_;
};
} // namespace sqlgen::sqlite

View File

@@ -9,12 +9,6 @@
namespace sqlgen::sqlite {
Connection::~Connection() {
if (conn_) {
sqlite3_close(conn_);
}
}
std::string Connection::column_to_sql_definition(
const dynamic::Column& _col) noexcept {
return "\"" + _col.name + "\"" + " " + type_to_sql(_col.type) +
@@ -59,7 +53,7 @@ rfl::Result<Ref<sqlgen::Connection>> Connection::make(
Result<Nothing> Connection::execute(const std::string& _sql) noexcept {
char* errmsg = nullptr;
sqlite3_exec(conn_, _sql.c_str(), nullptr, nullptr, &errmsg);
sqlite3_exec(conn_.get(), _sql.c_str(), nullptr, nullptr, &errmsg);
if (errmsg) {
const auto err = error(errmsg);
sqlite3_free(errmsg);
@@ -100,14 +94,14 @@ std::string Connection::insert_to_sql(const dynamic::Insert& _stmt) noexcept {
return stream.str();
}
sqlite3* Connection::make_conn(const std::string& _fname) {
typename Connection::ConnPtr Connection::make_conn(const std::string& _fname) {
sqlite3* conn = nullptr;
const auto err = sqlite3_open(_fname.c_str(), &conn);
if (err) {
throw std::runtime_error("Can't open database: " +
std::string(sqlite3_errmsg(conn)));
}
return conn;
return ConnPtr(conn, &sqlite3_close);
}
std::string Connection::properties_to_sql(
@@ -116,16 +110,6 @@ std::string Connection::properties_to_sql(
std::string(_p.nullable ? "" : " NOT NULL");
}
Connection& Connection::operator=(Connection&& _other) {
if (this == &_other) {
return *this;
}
sqlite3_close(conn_);
conn_ = _other.conn_;
_other.conn_ = nullptr;
return *this;
}
std::string Connection::to_sql(const dynamic::Statement& _stmt) noexcept {
return _stmt.visit([&](const auto& _s) -> std::string {
using S = std::remove_cvref_t<decltype(_s)>;