about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-05 04:32:43 -0500
committerJune McEnroe <june@causal.agency>2019-12-05 04:32:43 -0500
commit13610039539f924639db887a97058986aa203cbc (patch)
tree8e2ed6095fb3086f0b47dab7f1fa3f631009cfd1
parentSet busy timeout (diff)
downloadlitterbox-13610039539f924639db887a97058986aa203cbc.tar.gz
litterbox-13610039539f924639db887a97058986aa203cbc.zip
Add prepare and bind helpers
-rw-r--r--database.h48
1 files changed, 40 insertions, 8 deletions
diff --git a/database.h b/database.h
index 5b46757..3247fee 100644
--- a/database.h
+++ b/database.h
@@ -14,10 +14,12 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#include <assert.h>
 #include <err.h>
 #include <errno.h>
 #include <limits.h>
 #include <sqlite3.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -30,6 +32,7 @@ enum { DatabaseVersion = 0 };
 
 enum Type {
 	Privmsg,
+	Action,
 	Notice,
 	Join,
 	Part,
@@ -91,17 +94,46 @@ static inline sqlite3 *dbFind(int flags) {
 	return NULL;
 }
 
-static inline int dbVersion(sqlite3 *db) {
+static inline sqlite3_stmt *
+dbPrepare(sqlite3 *db, unsigned flags, const char *sql) {
 	sqlite3_stmt *stmt;
-	int error = sqlite3_prepare_v2(db, "PRAGMA user_version;", -1, &stmt, NULL);
-	if (error) errx(EX_SOFTWARE, "sqlite3_prepare_v2: %s", sqlite3_errmsg(db));
+	int error = sqlite3_prepare_v3(db, sql, -1, flags, &stmt, NULL);
+	if (error) errx(EX_SOFTWARE, "sqlite3_prepare_v3: %s", sqlite3_errmsg(db));
+	return stmt;
+}
 
-	error = sqlite3_step(stmt);
-	if (error != SQLITE_ROW) {
-		errx(EX_SOFTWARE, "sqlite3_step: %s", sqlite3_errmsg(db));
-	}
-	int version = sqlite3_column_int(stmt, 0);
+static inline void
+dbBindText(sqlite3_stmt *stmt, int param, const char *text, int len) {
+	int error = sqlite3_bind_text(stmt, param, text, len, NULL);
+	if (!error) return;
+	errx(
+		EX_SOFTWARE, "sqlite3_bind_text: %s",
+		sqlite3_errmsg(sqlite3_db_handle(stmt))
+	);
+}
 
+static inline void dbBindInt(sqlite3_stmt *stmt, int param, int64_t value) {
+	int error = sqlite3_bind_int64(stmt, param, value);
+	if (!error) return;
+	errx(
+		EX_SOFTWARE, "sqlite3_bind_int64: %s",
+		sqlite3_errmsg(sqlite3_db_handle(stmt))
+	);
+}
+
+static inline int dbStep(sqlite3_stmt *stmt) {
+	int error = sqlite3_step(stmt);
+	if (error == SQLITE_ROW || error == SQLITE_DONE) return error;
+	errx(
+		EX_SOFTWARE, "sqlite3_step: %s",
+		sqlite3_errmsg(sqlite3_db_handle(stmt))
+	);
+}
+
+static inline int dbVersion(sqlite3 *db) {
+	sqlite3_stmt *stmt = dbPrepare(db, 0, "PRAGMA user_version;");
+	assert(SQLITE_ROW == dbStep(stmt));
+	int version = sqlite3_column_int(stmt, 0);
 	sqlite3_finalize(stmt);
 	return version;
 }