From fec434a4e2d0ff65510581e461d87a945d25759a Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Thu, 23 Oct 2025 10:17:00 +0300 Subject: Use serapeum's -> & defsubst --- src/db.lisp | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'src/db.lisp') diff --git a/src/db.lisp b/src/db.lisp index 60b8115..ea18d16 100644 --- a/src/db.lisp +++ b/src/db.lisp @@ -3,40 +3,51 @@ (defpackage :ukkoclot/src/db (:use :c2cl :sqlite) (:import-from :log) - (:export :get-inline-bot-type :set-inline-bot-type :with-db)) + (:import-from :serapeum :-> :defunion) + (:export :inline-bot-type :blacklisted :whitelisted :get-inline-bot-type :set-inline-bot-type :with-db)) (in-package :ukkoclot/src/db) (defconstant +target-version+ 1 "Intended DB version") +(deftype db () + 'sqlite-handle) + (defmacro with-db ((name path) &body body) `(let ((,name (connect ,path))) (unwind-protect (progn (upgrade ,name) ,@body) (disconnect ,name)))) +(defunion inline-bot-type + blacklisted + whitelisted) + +(-> get-inline-bot-type (db integer) (or inline-bot-type null)) (defun get-inline-bot-type (db id) (let ((type-int (execute-single db "SELECT type FROM inline_bots WHERE id = ?" id))) (when type-int (integer->inline-bot-type type-int)))) +(-> set-inline-bot-type (db integer inline-bot-type) (values &optional)) (defun set-inline-bot-type (db id type) (execute-non-query db "INSERT OR REPLACE INTO inline_bots (id, type) VALUES (?, ?)" id (inline-bot-type->integer type))) +(-> inline-bot-type->integer (inline-bot-type) integer) (defun inline-bot-type->integer (type) - (case type - (:blacklisted 0) - (:whitelisted 1) - (otherwise (error "Unknown inline bot type ~S" type)))) + (etypecase type + (blacklisted 0) + (whitelisted 1))) +(-> integer->inline-bot-type (integer) inline-bot-type) (defun integer->inline-bot-type (num) - (case num - (0 :blacklisted) - (1 :whitelisted) - (otherwise (error "Unknown inline bot type value ~S" num)))) + (ecase num + (0 blacklisted) + (1 whitelisted))) +(-> upgrade (db) (values &optional)) (defun upgrade (db) (execute-non-query db "CREATE TABLE IF NOT EXISTS version(id INTEGER PRIMARY KEY, version INTEGER)") (let ((current-ver (execute-single db "SELECT version FROM version WHERE id = 0"))) @@ -62,6 +73,7 @@ current-ver))) (log:info "Database updating complete :)"))))) +(-> upgrade-step (db integer) (values &optional)) (defun upgrade-step (db new-version) (case new-version (1 @@ -73,8 +85,8 @@ CREATE TABLE inline_bots_enum ( (execute-non-query db " INSERT INTO inline_bots_enum(id, value) VALUES (?, 'blacklisted'), (?, 'whitelisted')" - (inline-bot-type->integer :blacklisted) - (inline-bot-type->integer :whitelisted)) + (inline-bot-type->integer blacklisted) + (inline-bot-type->integer whitelisted)) (execute-non-query db "DROP TABLE IF EXISTS inline_bots") (execute-non-query db " -- cgit v1.2.3