about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-07-12 14:12:53 -0400
committerJune McEnroe <june@causal.agency>2020-07-12 14:12:53 -0400
commit858d2cd9be35a5f6c051b37bfb2a02e920a888b4 (patch)
tree001cc4d79cd13a1f73fd79c9744719303d8b0662
parentAlways show later messages link when using before query (diff)
downloadscooper-858d2cd9be35a5f6c051b37bfb2a02e920a888b4.tar.gz
scooper-858d2cd9be35a5f6c051b37bfb2a02e920a888b4.zip
Use X macro for statements
-rw-r--r--contexts.c10
-rw-r--r--events.c16
-rw-r--r--server.c17
-rw-r--r--server.h40
4 files changed, 40 insertions, 43 deletions
diff --git a/contexts.c b/contexts.c
index 6ed9223..859fc96 100644
--- a/contexts.c
+++ b/contexts.c
@@ -124,20 +124,20 @@ enum kcgi_err contextsPage(struct kreq *req) {
 	error = khtml_closeelem(&html, 1);
 	if (error) return error;
 
-	sqlite3_reset(stmt.contextsMOTD);
-	dbBindText(stmt.contextsMOTD, ":network", scope.network);
+	sqlite3_reset(stmt.motd);
+	dbBindText(stmt.motd, ":network", scope.network);
 
-	result = sqlite3_step(stmt.contextsMOTD);
+	result = sqlite3_step(stmt.motd);
 	if (result == SQLITE_ROW) {
 		error = 0
 			|| khtml_elem(&html, KELEM_H2)
 			|| khtml_puts(&html, "MOTD")
 			|| khtml_closeelem(&html, 1)
 			|| khtml_elem(&html, KELEM_PRE)
-			|| khtml_puts(&html, sqlite3_column_text(stmt.contextsMOTD, 0))
+			|| khtml_puts(&html, sqlite3_column_text(stmt.motd, 0))
 			|| khtml_closeelem(&html, 1);
 		if (error) return error;
-		result = sqlite3_step(stmt.contextsMOTD);
+		result = sqlite3_step(stmt.motd);
 	}
 	if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));
 
diff --git a/events.c b/events.c
index 0604d21..423ce1e 100644
--- a/events.c
+++ b/events.c
@@ -159,13 +159,13 @@ enum kcgi_err eventsPage(struct kreq *req) {
 		|| khtml_elem(&html, KELEM_TABLE);
 	if (error) return error;
 
-	sqlite3_reset(stmt.eventsTopic);
-	dbBindText(stmt.eventsTopic, ":network", scope.network);
-	dbBindText(stmt.eventsTopic, ":context", scope.context);
-	dbBindText(stmt.eventsTopic, ":time", time);
-	dbBindInt(stmt.eventsTopic, ":public", contextsPublic);
+	sqlite3_reset(stmt.topic);
+	dbBindText(stmt.topic, ":network", scope.network);
+	dbBindText(stmt.topic, ":context", scope.context);
+	dbBindText(stmt.topic, ":time", time);
+	dbBindInt(stmt.topic, ":public", contextsPublic);
 
-	int result = sqlite3_step(stmt.eventsTopic);
+	int result = sqlite3_step(stmt.topic);
 	if (result == SQLITE_ROW) {
 		error = 0
 			|| khtml_elem(&html, KELEM_THEAD)
@@ -176,10 +176,10 @@ enum kcgi_err eventsPage(struct kreq *req) {
 				KATTR_COLSPAN, "3",
 				KATTR__MAX
 			)
-			|| khtml_puts(&html, sqlite3_column_text(stmt.eventsTopic, 0))
+			|| khtml_puts(&html, sqlite3_column_text(stmt.topic, 0))
 			|| khtml_closeelem(&html, 3);
 		if (error) return error;
-		result = sqlite3_step(stmt.eventsTopic);
+		result = sqlite3_step(stmt.topic);
 	}
 	if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));
 
diff --git a/server.c b/server.c
index fad9a90..bc05299 100644
--- a/server.c
+++ b/server.c
@@ -26,6 +26,19 @@
 sqlite3 *db;
 struct Statements stmt;
 
+static void prepareAll(void) {
+#define X(name, query) dbPrepare(&stmt.name, query);
+	ENUM_STMTS
+#undef X
+}
+
+static void finalizeAll(void) {
+#define X(name, query) sqlite3_finalize(stmt.name);
+	ENUM_STMTS
+#undef X
+	sqlite3_close(db);
+}
+
 const char *Pages[PagesLen] = {
 #define X(page, path) [page] = path,
 	ENUM_PAGES
@@ -84,7 +97,7 @@ int main(int argc, char *argv[]) {
 
 	int error = sqlite3_open_v2(argv[optind], &db, SQLITE_OPEN_READONLY, NULL);
 	if (error) errx(EX_NOINPUT, "%s: %s", argv[optind], sqlite3_errmsg(db));
-	atexit(dbClose);
+	atexit(finalizeAll);
 
 	sqlite3_stmt *check;
 	error = sqlite3_prepare_v2(
@@ -101,7 +114,7 @@ int main(int argc, char *argv[]) {
 	}
 	sqlite3_finalize(check);
 
-	dbPrepareAll();
+	prepareAll();
 	if (test) return EX_OK;
 	
 	if (khttp_fcgi_test()) {
diff --git a/server.h b/server.h
index 4a18ef0..fd80278 100644
--- a/server.h
+++ b/server.h
@@ -99,14 +99,19 @@ enum kcgi_err searchPage(struct kreq *req);
 
 extern sqlite3 *db;
 
+#define ENUM_STMTS \
+	X(networks, NetworksQuery) \
+	X(contexts, ContextsQuery) \
+	X(motd, ContextsMOTDQuery) \
+	X(topic, EventsTopicQuery) \
+	X(eventsAfter, EventsAfterQuery) \
+	X(eventsBefore, EventsBeforeQuery) \
+	X(search, SearchQuery)
+
 extern struct Statements {
-	sqlite3_stmt *networks;
-	sqlite3_stmt *contexts;
-	sqlite3_stmt *contextsMOTD;
-	sqlite3_stmt *eventsTopic;
-	sqlite3_stmt *eventsAfter;
-	sqlite3_stmt *eventsBefore;
-	sqlite3_stmt *search;
+#define X(name, query) sqlite3_stmt *name;
+	ENUM_STMTS
+#undef X
 } stmt;
 
 static inline void dbPrepare(sqlite3_stmt **stmt, const char *query) {
@@ -116,27 +121,6 @@ static inline void dbPrepare(sqlite3_stmt **stmt, const char *query) {
 	if (error) errx(EX_SOFTWARE, "%s: %s", sqlite3_errmsg(db), query);
 }
 
-static inline void dbPrepareAll(void) {
-	dbPrepare(&stmt.networks, NetworksQuery);
-	dbPrepare(&stmt.contexts, ContextsQuery);
-	dbPrepare(&stmt.contextsMOTD, ContextsMOTDQuery);
-	dbPrepare(&stmt.eventsTopic, EventsTopicQuery);
-	dbPrepare(&stmt.eventsAfter, EventsAfterQuery);
-	dbPrepare(&stmt.eventsBefore, EventsBeforeQuery);
-	dbPrepare(&stmt.search, SearchQuery);
-}
-
-static inline void dbClose(void) {
-	sqlite3_finalize(stmt.networks);
-	sqlite3_finalize(stmt.contexts);
-	sqlite3_finalize(stmt.contextsMOTD);
-	sqlite3_finalize(stmt.eventsTopic);
-	sqlite3_finalize(stmt.eventsAfter);
-	sqlite3_finalize(stmt.eventsBefore);
-	sqlite3_finalize(stmt.search);
-	sqlite3_close(db);
-}
-
 static inline int dbParam(sqlite3_stmt *stmt, const char *param) {
 	int index = sqlite3_bind_parameter_index(stmt, param);
 	if (index) return index;