From b1fd0ab73425e682a0a1404da6963f47a033bb34 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 30 Mar 2016 15:50:45 +0200 Subject: daemon: Improve the SQLite wrapper API. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In particular, this eliminates a bunch of boilerplate code. Also integrates these Nix commits: 80da7a6 Probably fix SQLITE_BUSY errors 37a337b throwSQLiteError(): Check for SIGINT so we don't loop forever Co-authored-by: Ludovic Courtès --- nix/libstore/sqlite.cc | 85 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 29 deletions(-) (limited to 'nix/libstore/sqlite.cc') diff --git a/nix/libstore/sqlite.cc b/nix/libstore/sqlite.cc index 8646ff5b12..e08c67f40e 100644 --- a/nix/libstore/sqlite.cc +++ b/nix/libstore/sqlite.cc @@ -53,15 +53,6 @@ void SQLiteStmt::create(sqlite3 * db, const string & s) this->db = db; } -void SQLiteStmt::reset() -{ - assert(stmt); - /* Note: sqlite3_reset() returns the error code for the most - recent call to sqlite3_step(). So ignore it. */ - sqlite3_reset(stmt); - curArg = 1; -} - SQLiteStmt::~SQLiteStmt() { try { @@ -72,43 +63,79 @@ SQLiteStmt::~SQLiteStmt() } } -void SQLiteStmt::bind(const string & value) +SQLiteStmt::Use::Use(SQLiteStmt & stmt) + : stmt(stmt) +{ + assert(stmt.stmt); + /* Note: sqlite3_reset() returns the error code for the most + recent call to sqlite3_step(). So ignore it. */ + sqlite3_reset(stmt); +} + +SQLiteStmt::Use::~Use() { - if (sqlite3_bind_text(stmt, curArg++, value.c_str(), -1, SQLITE_TRANSIENT) != SQLITE_OK) - throwSQLiteError(db, "binding argument"); + sqlite3_reset(stmt); } -void SQLiteStmt::bind(int value) +SQLiteStmt::Use & SQLiteStmt::Use::operator () (const std::string & value, bool notNull) { - if (sqlite3_bind_int(stmt, curArg++, value) != SQLITE_OK) - throwSQLiteError(db, "binding argument"); + if (notNull) { + if (sqlite3_bind_text(stmt, curArg++, value.c_str(), -1, SQLITE_TRANSIENT) != SQLITE_OK) + throwSQLiteError(stmt.db, "binding argument"); + } else + bind(); + return *this; } -void SQLiteStmt::bind64(long long value) +SQLiteStmt::Use & SQLiteStmt::Use::operator () (int64_t value, bool notNull) { - if (sqlite3_bind_int64(stmt, curArg++, value) != SQLITE_OK) - throwSQLiteError(db, "binding argument"); + if (notNull) { + if (sqlite3_bind_int64(stmt, curArg++, value) != SQLITE_OK) + throwSQLiteError(stmt.db, "binding argument"); + } else + bind(); + return *this; } -void SQLiteStmt::bind() +SQLiteStmt::Use & SQLiteStmt::Use::bind() { if (sqlite3_bind_null(stmt, curArg++) != SQLITE_OK) - throwSQLiteError(db, "binding argument"); + throwSQLiteError(stmt.db, "binding argument"); + return *this; } -SQLiteStmtUse::SQLiteStmtUse(SQLiteStmt & stmt) - : stmt(stmt) +int SQLiteStmt::Use::step() { - stmt.reset(); + return sqlite3_step(stmt); } -SQLiteStmtUse::~SQLiteStmtUse() +void SQLiteStmt::Use::exec() { - try { - stmt.reset(); - } catch (...) { - ignoreException(); - } + int r = step(); + assert(r != SQLITE_ROW); + if (r != SQLITE_DONE) + throwSQLiteError(stmt.db, "executing SQLite statement"); +} + +bool SQLiteStmt::Use::next() +{ + int r = step(); + if (r != SQLITE_DONE && r != SQLITE_ROW) + throwSQLiteError(stmt.db, "executing SQLite query"); + return r == SQLITE_ROW; +} + +std::string SQLiteStmt::Use::getStr(int col) +{ + auto s = (const char *) sqlite3_column_text(stmt, col); + assert(s); + return s; +} + +int64_t SQLiteStmt::Use::getInt(int col) +{ + // FIXME: detect nulls? + return sqlite3_column_int64(stmt, col); } SQLiteTxn::SQLiteTxn(sqlite3 * db) -- cgit v1.2.3