about summary refs log tree commit diff
path: root/src/libstore/sqlite.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstore/sqlite.cc')
-rw-r--r--src/libstore/sqlite.cc80
1 files changed, 51 insertions, 29 deletions
diff --git a/src/libstore/sqlite.cc b/src/libstore/sqlite.cc
index 8646ff5b12ad..0f1bb4947d34 100644
--- a/src/libstore/sqlite.cc
+++ b/src/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,74 @@ SQLiteStmt::~SQLiteStmt()
     }
 }
 
-void SQLiteStmt::bind(const string & value)
+SQLiteStmt::Use::Use(SQLiteStmt & stmt)
+    : stmt(stmt)
 {
-    if (sqlite3_bind_text(stmt, curArg++, value.c_str(), -1, SQLITE_TRANSIENT) != SQLITE_OK)
-        throwSQLiteError(db, "binding argument");
+    assert(stmt.stmt);
+    /* Note: sqlite3_reset() returns the error code for the most
+       recent call to sqlite3_step().  So ignore it. */
+    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)