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
Diffstat (limited to '')
-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;
 }
Use hilex for bin HTMLJune McEnroe 2021-01-12Don't output a pre in hilex by defaultJune McEnroe 2021-01-12Move hilex out of hilex directoryJune McEnroe 2021-01-12Consolidate hilex formatters into hilex.cJune McEnroe 2021-01-12Remove hacky tagging from hilexJune McEnroe God that makes the lexers so much simpler. 2021-01-12Add htagml -iJune McEnroe 2021-01-12Render tag index in HTMLJune McEnroe 2021-01-12Add htagml -xJune McEnroe 2021-01-12Prevent matching the same tag twiceJune McEnroe 2021-01-12Process htagml file line by lineJune McEnroe This simplifies some things, adds support for line number tag definitions, and should enable combining htagml with other preprocessors in the future. 2021-01-12Split fields by tab onlyJune McEnroe Also don't fail hard on non-forward-search definitions. 2021-01-12List both Makefile and html.sh under README.7June McEnroe 2021-01-12Add htagml exampleJune McEnroe 2021-01-12Use mandoc and htagml for bin htmlJune McEnroe 2021-01-12Add htagmlJune McEnroe 2021-01-12Replace causal.agency with a simple mdoc pageJune McEnroe 2021-01-11Publish "Using vi"June McEnroe 2021-01-11Enable diff.colorMovedJune McEnroe 2021-01-10Set less search case-insensitiveJune McEnroe 2021-01-10Set EXINITJune McEnroe neovim is laggy as hell in my OpenBSD VM, so I switched to vi so I could type without getting frustrated. 2021-01-09Add c -t flag to print expression typeJune McEnroe Also add missing float case. 2021-01-05Update taglineJune McEnroe