|
@@ -26,18 +26,20 @@
|
|
|
"sqlite connection lock") "Database connection lock")
|
|
"sqlite connection lock") "Database connection lock")
|
|
|
(defmacro with-db ((db) &body body)
|
|
(defmacro with-db ((db) &body body)
|
|
|
`(bordeaux-threads:with-recursive-lock-held (*db-lock*)
|
|
`(bordeaux-threads:with-recursive-lock-held (*db-lock*)
|
|
|
- (if *current-db*
|
|
|
|
|
- (let ((,db *current-db*))
|
|
|
|
|
- (declare (ignorable ,db))
|
|
|
|
|
- ,@body)
|
|
|
|
|
- (sqlite:with-open-database (,db (db-path) :busy-timeout 30)
|
|
|
|
|
- (sqlite:execute-non-query ,db "PRAGMA foreign_keys = ON")
|
|
|
|
|
- (let ((*current-db* ,db))
|
|
|
|
|
- (handler-case (progn ,@body)
|
|
|
|
|
- (sqlite:sqlite-error (e)
|
|
|
|
|
- (log:error (sqlite:sqlite-error-code e)
|
|
|
|
|
- (sqlite:sqlite-error-message e))
|
|
|
|
|
- (error e))))))))
|
|
|
|
|
|
|
+ (unless *current-db*
|
|
|
|
|
+ (let ((path (db-path)))
|
|
|
|
|
+ (log:info "Connecting to ~A" path)
|
|
|
|
|
+ (setf *current-db* (sqlite:connect path))
|
|
|
|
|
+ (sqlite:execute-non-query *current-db* "PRAGMA foreign_keys = ON")))
|
|
|
|
|
+ (let ((,db *current-db*))
|
|
|
|
|
+ (declare (ignorable ,db))
|
|
|
|
|
+ (handler-case (progn ,@body)
|
|
|
|
|
+ (sqlite:sqlite-error (e)
|
|
|
|
|
+ (log:error (sqlite:sqlite-error-code e)
|
|
|
|
|
+ (sqlite:sqlite-error-message e))
|
|
|
|
|
+ (ignore-errors (sqlite:disconnect *current-db*))
|
|
|
|
|
+ (setf *current-db* nil)
|
|
|
|
|
+ (error e))))))
|
|
|
|
|
|
|
|
(defmacro db-transaction (&body body)
|
|
(defmacro db-transaction (&body body)
|
|
|
(let ((db (gensym "DB-")))
|
|
(let ((db (gensym "DB-")))
|