about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-23 21:08:32 -0500
committerJune McEnroe <june@causal.agency>2019-12-23 21:08:32 -0500
commitde5f00d71614a5018a6468d5fd7e3cc039c41ec6 (patch)
tree4d4bf449f922b8e1e6031afca650adf91a03744b
parentHandle NICK (diff)
downloadlitterbox-de5f00d71614a5018a6468d5fd7e3cc039c41ec6.tar.gz
litterbox-de5f00d71614a5018a6468d5fd7e3cc039c41ec6.zip
Factour out verbose, dbStep+reset
-rw-r--r--database.h12
-rw-r--r--litterbox.c38
-rw-r--r--unscoop.16
-rw-r--r--unscoop.c12
4 files changed, 29 insertions, 39 deletions
diff --git a/database.h b/database.h
index 6efe9d0..1dcc71b 100644
--- a/database.h
+++ b/database.h
@@ -44,6 +44,8 @@ enum Type {
 	Topic,
 };
 
+static bool verbose;
+
 static inline void dbExec(sqlite3 *db, const char *sql) {
 	int error = sqlite3_exec(db, sql, NULL, NULL, NULL);
 	if (error) errx(EX_SOFTWARE, "%s: %s", sqlite3_errmsg(db), sql);
@@ -173,6 +175,16 @@ static inline int dbStep(sqlite3_stmt *stmt) {
 	);
 }
 
+static inline void dbRun(sqlite3_stmt *stmt) {
+	dbStep(stmt);
+	if (verbose && sqlite3_changes(sqlite3_db_handle(stmt))) {
+		char *sql = sqlite3_expanded_sql(stmt);
+		if (sql) fprintf(stderr, "%s\n", sql);
+		sqlite3_free(sql);
+	}
+	sqlite3_reset(stmt);
+}
+
 static inline int dbVersion(sqlite3 *db) {
 	sqlite3_stmt *stmt = dbPrepare(db, false, SQL(PRAGMA user_version;));
 	dbStep(stmt);
diff --git a/litterbox.c b/litterbox.c
index b28398b..178f790 100644
--- a/litterbox.c
+++ b/litterbox.c
@@ -28,16 +28,6 @@
 
 #include "database.h"
 
-static bool verbose;
-
-static void printSQL(sqlite3_stmt *stmt) {
-	if (!verbose) return;
-	char *sql = sqlite3_expanded_sql(stmt);
-	if (!sql) return;
-	fprintf(stderr, "%s\n", sql);
-	sqlite3_free(sql);
-}
-
 static sqlite3 *db;
 
 static const char *CreateJoins = SQL(
@@ -124,25 +114,21 @@ static void bindNetwork(const char *network) {
 static void insertContext(const char *context, bool query) {
 	dbBindText(stmts[InsertContext], ":context", context);
 	dbBindInt(stmts[InsertContext], ":query", query);
-	dbStep(stmts[InsertContext]);
+	dbRun(stmts[InsertContext]);
 	dbBindText(stmts[InsertEvent], ":context", context);
-	if (sqlite3_changes(db)) printSQL(stmts[InsertContext]);
-	sqlite3_reset(stmts[InsertContext]);
 }
 
 static void insertName(const char *nick, const char *user, const char *host) {
 	dbBindText(stmts[InsertName], ":nick", nick);
 	dbBindText(stmts[InsertName], ":user", user);
 	dbBindText(stmts[InsertName], ":host", host);
-	dbStep(stmts[InsertName]);
+	dbRun(stmts[InsertName]);
 	dbBindText(stmts[InsertEvent], ":nick", nick);
 	dbBindText(stmts[InsertEvent], ":user", user);
 	dbBindText(stmts[InsertEvent], ":host", host);
 	dbBindText(stmts[InsertEvents], ":nick", nick);
 	dbBindText(stmts[InsertEvents], ":user", user);
 	dbBindText(stmts[InsertEvents], ":host", host);
-	if (sqlite3_changes(db)) printSQL(stmts[InsertName]);
-	sqlite3_reset(stmts[InsertName]);
 }
 
 static void insertEvent(
@@ -152,9 +138,7 @@ static void insertEvent(
 	dbBindInt(stmts[InsertEvent], ":type", type);
 	dbBindText(stmts[InsertEvent], ":target", target);
 	dbBindText(stmts[InsertEvent], ":message", message);
-	dbStep(stmts[InsertEvent]);
-	printSQL(stmts[InsertEvent]);
-	sqlite3_reset(stmts[InsertEvent]);
+	dbRun(stmts[InsertEvent]);
 }
 
 static void insertEvents(
@@ -164,33 +148,25 @@ static void insertEvents(
 	dbBindInt(stmts[InsertEvents], ":type", type);
 	dbBindText(stmts[InsertEvents], ":target", target);
 	dbBindText(stmts[InsertEvents], ":message", message);
-	dbStep(stmts[InsertEvents]);
-	printSQL(stmts[InsertEvents]);
-	sqlite3_reset(stmts[InsertEvents]);
+	dbRun(stmts[InsertEvents]);
 }
 
 static void insertJoin(const char *nick, const char *channel) {
 	dbBindText(stmts[InsertJoin], ":nick", nick);
 	dbBindText(stmts[InsertJoin], ":channel", channel);
-	dbStep(stmts[InsertJoin]);
-	printSQL(stmts[InsertJoin]);
-	sqlite3_reset(stmts[InsertJoin]);
+	dbRun(stmts[InsertJoin]);
 }
 
 static void deleteJoin(const char *nick, const char *channel) {
 	dbBindText(stmts[DeleteJoin], ":nick", nick);
 	dbBindText(stmts[DeleteJoin], ":channel", channel);
-	dbStep(stmts[DeleteJoin]);
-	printSQL(stmts[DeleteJoin]);
-	sqlite3_reset(stmts[DeleteJoin]);
+	dbRun(stmts[DeleteJoin]);
 }
 
 static void updateJoin(const char *old, const char *new) {
 	dbBindText(stmts[UpdateJoin], ":old", old);
 	dbBindText(stmts[UpdateJoin], ":new", new);
-	dbStep(stmts[UpdateJoin]);
-	printSQL(stmts[UpdateJoin]);
-	sqlite3_reset(stmts[UpdateJoin]);
+	dbRun(stmts[UpdateJoin]);
 }
 
 static struct tls *client;
diff --git a/unscoop.1 b/unscoop.1
index ac8a625..6418733 100644
--- a/unscoop.1
+++ b/unscoop.1
@@ -1,4 +1,4 @@
-.Dd December 12, 2019
+.Dd December 23, 2019
 .Dt UNSCOOP 1
 .Os
 .
@@ -8,6 +8,7 @@
 .
 .Sh SYNOPSIS
 .Nm
+.Op Fl v
 .Op Fl C Ar context
 .Op Fl N Ar network
 .Op Fl d Ar path
@@ -80,6 +81,9 @@ and
 .It Cm textual
 Logs from the Textual IRC client.
 .El
+.
+.It Fl v
+Log SQL to standard error.
 .El
 .
 .Sh EXAMPLES
diff --git a/unscoop.c b/unscoop.c
index d9015a6..996b929 100644
--- a/unscoop.c
+++ b/unscoop.c
@@ -272,10 +272,8 @@ matchLine(const struct Format *format, const regex_t *regex, const char *line) {
 			bindMatch(insertEvent, param, line, match[1 + i]);
 		}
 
-		dbStep(insertName);
-		dbStep(insertEvent);
-		sqlite3_reset(insertName);
-		sqlite3_reset(insertEvent);
+		dbRun(insertName);
+		dbRun(insertEvent);
 		break;
 	}
 }
@@ -307,13 +305,14 @@ int main(int argc, char *argv[]) {
 	const struct Format *format = &Formats[0];
 
 	int opt;
-	while (0 < (opt = getopt(argc, argv, "C:DN:d:f:"))) {
+	while (0 < (opt = getopt(argc, argv, "C:DN:d:f:v"))) {
 		switch (opt) {
 			break; case 'C': context = optarg;
 			break; case 'D': dedup = true;
 			break; case 'N': network = optarg;
 			break; case 'd': path = optarg;
 			break; case 'f': format = formatParse(optarg);
+			break; case 'v': verbose = true;
 			break; default:  return EX_USAGE;
 		}
 	}
@@ -391,8 +390,7 @@ int main(int argc, char *argv[]) {
 			bindMatch(insertContext, ":context", argv[i], pathContext);
 			bindMatch(insertEvent, ":context", argv[i], pathContext);
 		}
-		dbStep(insertContext);
-		sqlite3_reset(insertContext);
+		dbRun(insertContext);
 
 		ssize_t len;
 		while (0 < (len = getline(&line, &cap, file))) {