Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null safety for sqlite::Connection::fromRaw #16

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/podrm/sqlite/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Result {

class Connection {
public:
static Connection fromRaw(sqlite3 *connection);
static Connection fromRaw(sqlite3 &connection);

static Connection inMemory(const char *name);

Expand All @@ -81,7 +81,7 @@ class Connection {
private:
std::unique_ptr<sqlite3, int (*)(sqlite3 *)> connection;

explicit Connection(sqlite3 *connection);
explicit Connection(sqlite3 &connection);
};

} // namespace podrm::sqlite
20 changes: 10 additions & 10 deletions lib/sqlite/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ using Statement =
sqlite3_finalize(statement);
})>;

Statement createStatement(sqlite3 *const connection,
Statement createStatement(sqlite3 &connection,
const std::string_view statement) {
sqlite3_stmt *stmt = nullptr;
const int result =
sqlite3_prepare_v3(connection, statement.data(),
sqlite3_prepare_v3(&connection, statement.data(),
static_cast<int>(statement.size()), 0, &stmt, nullptr);
if (result != SQLITE_OK) {
throw std::runtime_error{sqlite3_errmsg(connection)};
throw std::runtime_error{sqlite3_errmsg(&connection)};
}

return Statement{stmt};
Expand Down Expand Up @@ -87,10 +87,10 @@ bool Result::nextRow() {
return true;
}

Connection::Connection(sqlite3 *const connection)
: connection(connection, &sqlite3_close_v2) {}
Connection::Connection(sqlite3 &connection)
: connection(&connection, &sqlite3_close_v2) {}

Connection Connection::fromRaw(sqlite3 *const connection) {
Connection Connection::fromRaw(sqlite3 &connection) {
return Connection{connection};
}

Expand All @@ -103,7 +103,7 @@ Connection Connection::inMemory(const char *const name) {
throw std::runtime_error{sqlite3_errstr(result)};
}

return Connection{connection};
return Connection{*connection};
}

Connection Connection::inFile(const std::filesystem::path &path) {
Expand All @@ -115,11 +115,11 @@ Connection Connection::inFile(const std::filesystem::path &path) {
throw std::runtime_error{sqlite3_errstr(result)};
}

return Connection{connection};
return Connection{*connection};
}

void Connection::execute(const std::string_view statement) {
Statement stmt = createStatement(this->connection.get(), statement);
Statement stmt = createStatement(*this->connection, statement);

const int executeResult = sqlite3_step(stmt.get());
if (executeResult != SQLITE_DONE) {
Expand All @@ -128,7 +128,7 @@ void Connection::execute(const std::string_view statement) {
}

Result Connection::query(const std::string_view statement) {
Statement stmt = createStatement(this->connection.get(), statement);
Statement stmt = createStatement(*this->connection, statement);

return Result{{stmt.release(), &sqlite3_finalize}};
}
Expand Down
Loading