summaryrefslogtreecommitdiff
path: root/src/db.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.lisp')
-rw-r--r--src/db.lisp34
1 files changed, 23 insertions, 11 deletions
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 @@
3(defpackage :ukkoclot/src/db 3(defpackage :ukkoclot/src/db
4 (:use :c2cl :sqlite) 4 (:use :c2cl :sqlite)
5 (:import-from :log) 5 (:import-from :log)
6 (:export :get-inline-bot-type :set-inline-bot-type :with-db)) 6 (:import-from :serapeum :-> :defunion)
7 (:export :inline-bot-type :blacklisted :whitelisted :get-inline-bot-type :set-inline-bot-type :with-db))
7(in-package :ukkoclot/src/db) 8(in-package :ukkoclot/src/db)
8 9
9(defconstant +target-version+ 1 10(defconstant +target-version+ 1
10 "Intended DB version") 11 "Intended DB version")
11 12
13(deftype db ()
14 'sqlite-handle)
15
12(defmacro with-db ((name path) &body body) 16(defmacro with-db ((name path) &body body)
13 `(let ((,name (connect ,path))) 17 `(let ((,name (connect ,path)))
14 (unwind-protect (progn (upgrade ,name) ,@body) 18 (unwind-protect (progn (upgrade ,name) ,@body)
15 (disconnect ,name)))) 19 (disconnect ,name))))
16 20
21(defunion inline-bot-type
22 blacklisted
23 whitelisted)
24
25(-> get-inline-bot-type (db integer) (or inline-bot-type null))
17(defun get-inline-bot-type (db id) 26(defun get-inline-bot-type (db id)
18 (let ((type-int (execute-single db "SELECT type FROM inline_bots WHERE id = ?" id))) 27 (let ((type-int (execute-single db "SELECT type FROM inline_bots WHERE id = ?" id)))
19 (when type-int 28 (when type-int
20 (integer->inline-bot-type type-int)))) 29 (integer->inline-bot-type type-int))))
21 30
31(-> set-inline-bot-type (db integer inline-bot-type) (values &optional))
22(defun set-inline-bot-type (db id type) 32(defun set-inline-bot-type (db id type)
23 (execute-non-query db 33 (execute-non-query db
24 "INSERT OR REPLACE INTO inline_bots (id, type) VALUES (?, ?)" 34 "INSERT OR REPLACE INTO inline_bots (id, type) VALUES (?, ?)"
25 id 35 id
26 (inline-bot-type->integer type))) 36 (inline-bot-type->integer type)))
27 37
38(-> inline-bot-type->integer (inline-bot-type) integer)
28(defun inline-bot-type->integer (type) 39(defun inline-bot-type->integer (type)
29 (case type 40 (etypecase type
30 (:blacklisted 0) 41 (blacklisted 0)
31 (:whitelisted 1) 42 (whitelisted 1)))
32 (otherwise (error "Unknown inline bot type ~S" type))))
33 43
44(-> integer->inline-bot-type (integer) inline-bot-type)
34(defun integer->inline-bot-type (num) 45(defun integer->inline-bot-type (num)
35 (case num 46 (ecase num
36 (0 :blacklisted) 47 (0 blacklisted)
37 (1 :whitelisted) 48 (1 whitelisted)))
38 (otherwise (error "Unknown inline bot type value ~S" num))))
39 49
50(-> upgrade (db) (values &optional))
40(defun upgrade (db) 51(defun upgrade (db)
41 (execute-non-query db "CREATE TABLE IF NOT EXISTS version(id INTEGER PRIMARY KEY, version INTEGER)") 52 (execute-non-query db "CREATE TABLE IF NOT EXISTS version(id INTEGER PRIMARY KEY, version INTEGER)")
42 (let ((current-ver (execute-single db "SELECT version FROM version WHERE id = 0"))) 53 (let ((current-ver (execute-single db "SELECT version FROM version WHERE id = 0")))
@@ -62,6 +73,7 @@
62 current-ver))) 73 current-ver)))
63 (log:info "Database updating complete :)"))))) 74 (log:info "Database updating complete :)")))))
64 75
76(-> upgrade-step (db integer) (values &optional))
65(defun upgrade-step (db new-version) 77(defun upgrade-step (db new-version)
66 (case new-version 78 (case new-version
67 (1 79 (1
@@ -73,8 +85,8 @@ CREATE TABLE inline_bots_enum (
73 (execute-non-query db " 85 (execute-non-query db "
74INSERT INTO inline_bots_enum(id, value) 86INSERT INTO inline_bots_enum(id, value)
75VALUES (?, 'blacklisted'), (?, 'whitelisted')" 87VALUES (?, 'blacklisted'), (?, 'whitelisted')"
76 (inline-bot-type->integer :blacklisted) 88 (inline-bot-type->integer blacklisted)
77 (inline-bot-type->integer :whitelisted)) 89 (inline-bot-type->integer whitelisted))
78 90
79 (execute-non-query db "DROP TABLE IF EXISTS inline_bots") 91 (execute-non-query db "DROP TABLE IF EXISTS inline_bots")
80 (execute-non-query db " 92 (execute-non-query db "