summary refs log tree commit diff
path: root/database.h
diff options
context:
space:
mode:
Diffstat (limited to 'database.h')
-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;
 }
light'> 2018-09-13Never send PRIVMSG to TagStatus or TagVerboseJune McEnroe 2018-09-13Move color selection to format.cJune McEnroe 2018-09-13Fix len for format->split at end of stringJune McEnroe 2018-09-13Avoid uninitialized x in uiReadJune McEnroe 2018-09-13Add IRCDefault to colors enumJune McEnroe 2018-09-13Return a format->split even at the end of the stringJune McEnroe 2018-09-13Fix weird tab-complete after commaJune McEnroe I have no idea why I did this. 2018-09-13Rewrite UI againJune McEnroe The persistent topic is gone and the status line is now at the top. The status formatting still needs to be reworked. I also want to try showing the nick in the input window so it really looks like your next message. 2018-09-12Add note about C-oJune McEnroe Why are there so few well usable ctrl key bindings? 2018-09-12Use formatParse split to position input cursorJune McEnroe 2018-09-12Factor out IRC formatting parsingJune McEnroe 2018-09-11Add /help equivalent to /manJune McEnroe 2018-09-11Don't render every PM as a pingJune McEnroe 2018-09-11Add urlOpenMatchJune McEnroe 2018-09-10Depend on man.sh for chroot.tar targetJune McEnroe 2018-09-10Set LESSSECURE=1 in man.shJune McEnroe Ridiculous. 2018-09-10Add /man commandJune McEnroe 2018-09-10Install man page in chrootJune McEnroe 2018-09-10Install man pageJune McEnroe 2018-09-10Split keys into subsections and document colorsJune McEnroe 2018-09-10Add "blank" lines to chatte.1June McEnroe 2018-09-10Document key bindings in chatte.1June McEnroe 2018-09-08Document slash commands in chatte.1June McEnroe