summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-17 17:27:59 -0500
committerJune McEnroe <june@causal.agency>2019-12-17 17:27:59 -0500
commit6ef71b37cdcb04f983aeb89f94cbf0b5a7768a80 (patch)
treea83411ece399dc128273e58f8fe01a1c45b4f482
parentHandle ISUPPORT (diff)
downloadlitterbox-6ef71b37cdcb04f983aeb89f94cbf0b5a7768a80.tar.gz
litterbox-6ef71b37cdcb04f983aeb89f94cbf0b5a7768a80.zip
Use parameter names for all binds
-rw-r--r--database.h13
-rw-r--r--unscoop.c34
2 files changed, 25 insertions, 22 deletions
diff --git a/database.h b/database.h
index 6d705db..4ca9c81 100644
--- a/database.h
+++ b/database.h
@@ -108,8 +108,10 @@ dbPrepare(sqlite3 *db, unsigned flags, const char *sql) {
 }
 
 static inline void
-dbBindText(sqlite3_stmt *stmt, int param, const char *text, int len) {
-	int error = sqlite3_bind_text(stmt, param, text, len, NULL);
+dbBindText(sqlite3_stmt *stmt, const char *param, const char *text, int len) {
+	int index = sqlite3_bind_parameter_index(stmt, param);
+	if (!index) errx(EX_SOFTWARE, "no such parameter %s", param);
+	int error = sqlite3_bind_text(stmt, index, text, len, NULL);
 	if (!error) return;
 	errx(
 		EX_SOFTWARE, "sqlite3_bind_text: %s",
@@ -117,8 +119,11 @@ dbBindText(sqlite3_stmt *stmt, int param, const char *text, int len) {
 	);
 }
 
-static inline void dbBindInt(sqlite3_stmt *stmt, int param, int64_t value) {
-	int error = sqlite3_bind_int64(stmt, param, value);
+static inline void
+dbBindInt(sqlite3_stmt *stmt, const char *param, int64_t value) {
+	int index = sqlite3_bind_parameter_index(stmt, param);
+	if (!index) errx(EX_SOFTWARE, "no such parameter %s", param);
+	int error = sqlite3_bind_int64(stmt, index, value);
 	if (!error) return;
 	errx(
 		EX_SOFTWARE, "sqlite3_bind_int64: %s",
diff --git a/unscoop.c b/unscoop.c
index 5aecf19..c4e9d33 100644
--- a/unscoop.c
+++ b/unscoop.c
@@ -206,8 +206,9 @@ static regex_t compile(const char *pattern) {
 	errx(EX_SOFTWARE, "regcomp: %s: %s", buf, pattern);
 }
 
-static void
-bindMatch(sqlite3_stmt *stmt, int param, const char *str, regmatch_t match) {
+static void bindMatch(
+	sqlite3_stmt *stmt, const char *param, const char *str, regmatch_t match
+) {
 	if (match.rm_so < 0) {
 		dbBindText(stmt, param, NULL, -1);
 	} else {
@@ -219,7 +220,6 @@ static sqlite3_stmt *insertName;
 static sqlite3_stmt *insertEvent;
 static int paramNetwork;
 static int paramContext;
-static int paramType;
 
 static void prepareInsert(sqlite3 *db) {
 	const char *InsertName = SQL(
@@ -247,7 +247,6 @@ static void prepareInsert(sqlite3 *db) {
 	insertEvent = dbPrepare(db, SQLITE_PREPARE_PERSISTENT, InsertEvent);
 	paramNetwork = sqlite3_bind_parameter_index(insertEvent, ":network");
 	paramContext = sqlite3_bind_parameter_index(insertEvent, ":context");
-	paramType = sqlite3_bind_parameter_index(insertEvent, ":type");
 }
 
 static void
@@ -263,15 +262,14 @@ matchLine(const struct Format *format, const regex_t *regex, const char *line) {
 			sqlite3_bind_null(insertEvent, i);
 		}
 
-		dbBindInt(insertEvent, paramType, matcher->type);
+		dbBindInt(insertEvent, ":type", matcher->type);
 		for (size_t i = 0; i < ARRAY_LEN(matcher->params); ++i) {
 			const char *param = matcher->params[i];
 			if (!param) continue;
-			int p = sqlite3_bind_parameter_index(insertName, param);
-			if (p) bindMatch(insertName, p, line, match[1 + i]);
-			p = sqlite3_bind_parameter_index(insertEvent, param);
-			if (!p) errx(EX_SOFTWARE, "no such parameter %s", param);
-			bindMatch(insertEvent, p, line, match[1 + i]);
+			if (sqlite3_bind_parameter_index(insertName, param)) {
+				bindMatch(insertName, param, line, match[1 + i]);
+			}
+			bindMatch(insertEvent, param, line, match[1 + i]);
 		}
 
 		dbStep(insertName);
@@ -350,12 +348,12 @@ int main(int argc, char *argv[]) {
 	sqlite3_stmt *insertContext = dbPrepare(
 		db, SQLITE_PREPARE_PERSISTENT, InsertContext
 	);
-	dbBindText(insertContext, 1, network, -1);
-	dbBindText(insertContext, 2, context, -1);
+	dbBindText(insertContext, ":network", network, -1);
+	dbBindText(insertContext, ":context", context, -1);
 
 	prepareInsert(db);
-	dbBindText(insertEvent, paramNetwork, network, -1);
-	dbBindText(insertEvent, paramContext, context, -1);
+	dbBindText(insertEvent, ":network", network, -1);
+	dbBindText(insertEvent, ":context", context, -1);
 
 	size_t sizeTotal = 0;
 	size_t sizeRead = 0;
@@ -388,12 +386,12 @@ int main(int argc, char *argv[]) {
 		regmatch_t pathNetwork = match[i][format->network];
 		regmatch_t pathContext = match[i][format->context];
 		if (!network) {
-			bindMatch(insertContext, 1, argv[i], pathNetwork);
-			bindMatch(insertEvent, paramNetwork, argv[i], pathNetwork);
+			bindMatch(insertContext, ":network", argv[i], pathNetwork);
+			bindMatch(insertEvent, ":network", argv[i], pathNetwork);
 		}
 		if (!context) {
-			bindMatch(insertContext, 2, argv[i], pathContext);
-			bindMatch(insertEvent, paramContext, argv[i], pathContext);
+			bindMatch(insertContext, ":context", argv[i], pathContext);
+			bindMatch(insertEvent, ":context", argv[i], pathContext);
 		}
 		dbStep(insertContext);
 		sqlite3_reset(insertContext);