aboutsummaryrefslogtreecommitdiff
path: root/nix/libstore/sqlite.cc
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2016-03-30 15:50:45 +0200
committerLudovic Courtès <ludo@gnu.org>2016-10-28 22:30:17 +0200
commitb1fd0ab73425e682a0a1404da6963f47a033bb34 (patch)
treedac41af9ae5f41713185a4da8710ce54a588e8a2 /nix/libstore/sqlite.cc
parent7bed5d91dea6ecff565d51a021e6f99718d04f86 (diff)
downloadguix-b1fd0ab73425e682a0a1404da6963f47a033bb34.tar
guix-b1fd0ab73425e682a0a1404da6963f47a033bb34.tar.gz
daemon: Improve the SQLite wrapper API.
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 <ludo@gnu.org>
Diffstat (limited to 'nix/libstore/sqlite.cc')
-rw-r--r--nix/libstore/sqlite.cc85
1 files changed, 56 insertions, 29 deletions
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)