diff options
Diffstat (limited to 'src/db.lisp')
| -rw-r--r-- | src/db.lisp | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/db.lisp b/src/db.lisp index ea18d16..db8f7c1 100644 --- a/src/db.lisp +++ b/src/db.lisp | |||
| @@ -3,8 +3,8 @@ | |||
| 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 | (:import-from :serapeum :-> :defunion) | 6 | (:import-from :serapeum :-> :defunion :with-thunk) |
| 7 | (:export :inline-bot-type :blacklisted :whitelisted :get-inline-bot-type :set-inline-bot-type :with-db)) | 7 | (:export :inline-bot-type :blacklisted :whitelisted :get-inline-bot-type :set-inline-bot-type :call-with-db :with-db)) |
| 8 | (in-package :ukkoclot/src/db) | 8 | (in-package :ukkoclot/src/db) |
| 9 | 9 | ||
| 10 | (defconstant +target-version+ 1 | 10 | (defconstant +target-version+ 1 |
| @@ -13,10 +13,19 @@ | |||
| 13 | (deftype db () | 13 | (deftype db () |
| 14 | 'sqlite-handle) | 14 | 'sqlite-handle) |
| 15 | 15 | ||
| 16 | (-> call-with-db (pathname (function (db) t)) t) | ||
| 17 | (defun call-with-db (path fn) | ||
| 18 | "Similar to `with-db', but instead of binding the database in a macro body, | ||
| 19 | calls the function `fn' with it as an argument." | ||
| 20 | (let ((db (connect path))) | ||
| 21 | (unwind-protect (progn (upgrade db) | ||
| 22 | (funcall fn db)) | ||
| 23 | (disconnect db)))) | ||
| 24 | |||
| 16 | (defmacro with-db ((name path) &body body) | 25 | (defmacro with-db ((name path) &body body) |
| 17 | `(let ((,name (connect ,path))) | 26 | "Open database specified by `path' and bind it to `name' for the duration of the `body'." |
| 18 | (unwind-protect (progn (upgrade ,name) ,@body) | 27 | (with-thunk (body name) |
| 19 | (disconnect ,name)))) | 28 | `(call-with-db ,path ,body))) |
| 20 | 29 | ||
| 21 | (defunion inline-bot-type | 30 | (defunion inline-bot-type |
| 22 | blacklisted | 31 | blacklisted |