Skip to content

Commit

Permalink
Specify :format option to dbi:fetch.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukamachi committed Jun 7, 2024
1 parent 12cf8a6 commit 571ed68
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/core/db.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ Note that DBI:PREPARE-CACHED is added CL-DBI v0.9.5.")
(sxql:limit 1)))))
(with-prepared-query query (conn sql)
(and (dbi:fetch-all
(execute-with-retry query binds))
(execute-with-retry query binds)
:format :plist)
t))))

(defgeneric execute-sql (sql &optional binds)
Expand Down
12 changes: 5 additions & 7 deletions src/core/db/mysql.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
(defun last-insert-id (conn table-name serial-key-name)
(declare (ignore table-name serial-key-name))
(with-prepared-query query (conn "SELECT last_insert_id() AS last_insert_id")
(getf (dbi:fetch
(dbi:execute query))
:|last_insert_id|
0)))
(or (first (dbi:fetch (dbi:execute query) :format :values))
0)))

(defun table-indices (conn table-name)
(with-prepared-query query
Expand All @@ -49,7 +47,7 @@
:columns (mapcar (lambda (column)
(getf column :|column_name|))
column-list))))
(group-by-plist (dbi:fetch-all results)
(group-by-plist (dbi:fetch-all results :format :plist)
:key :|index_name|
:test #'string=)))))

Expand All @@ -64,7 +62,7 @@
(with-prepared-query query (conn sql)
(let* ((results (dbi:execute query))
(definitions
(loop for column = (dbi:fetch results)
(loop for column = (dbi:fetch results :format :plist)
while column
collect (list (getf column :|Field|)
:type (ensure-string (getf column :|Type|))
Expand All @@ -85,7 +83,7 @@
(defun table-view-query (conn table-name)
(with-prepared-query query (conn (format nil "SHOW CREATE VIEW `~A`" table-name))
(let ((results (dbi:execute query)))
(getf (first (dbi:fetch-all results)) :|Create View|))))
(getf (first (dbi:fetch-all results :format :plist)) :|Create View|))))

(defun acquire-advisory-lock (conn id)
;; MySQL accepts -1 to wait forever, while MariaDB doesn't.
Expand Down
31 changes: 12 additions & 19 deletions src/core/db/postgres.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,29 @@
(defun last-insert-id (conn table-name serial-key-name)
(handler-case
(with-prepared-query query
(conn (format nil
"SELECT currval(pg_get_serial_sequence('~A', '~A')) AS last_insert_id"
table-name
serial-key-name))
(getf (dbi:fetch
(dbi:execute query))
:|last_insert_id|
0))
(conn (format nil
"SELECT currval(pg_get_serial_sequence('~A', '~A')) AS last_insert_id"
table-name
serial-key-name))
(or (first (dbi:fetch (dbi:execute query) :format :values)) 0))
(dbi:<dbi-error> () 0)))

(defun get-serial-keys (conn table-name)
(remove-if-not
(lambda (column)
(with-prepared-query query
(conn (format nil "SELECT pg_get_serial_sequence('~A', '~A')" table-name column))
(let ((seq (getf
(first
(let ((seq (first
(dbi:fetch-all
(dbi:execute query)))
:|pg_get_serial_sequence|)))
(dbi:execute query)
:format :values))))
(if (eq seq :null)
nil
seq))))
(with-prepared-query query
(conn (format nil "SELECT column_name FROM information_schema.columns WHERE table_name = '~A'"
table-name))
(mapcar (lambda (row)
(getf row :|column_name|))
(dbi:fetch-all
(dbi:execute query))))))
(mapcar #'car (dbi:fetch-all (dbi:execute query) :format :values)))))

(defun column-definitions (conn table-name)
(let* ((serial-keys (get-serial-keys conn table-name))
Expand Down Expand Up @@ -75,7 +68,7 @@
(let ((definitions
(delete-duplicates
(loop with results = (dbi:execute query)
for column = (dbi:fetch results)
for column = (dbi:fetch results :values :plist)
while column
collect (let ((auto-increment (not (null (member (getf column :|name|)
serial-keys
Expand Down Expand Up @@ -141,7 +134,7 @@
(read-from-string column)
column))
|column_names|))))
(dbi:fetch-all results)))))
(dbi:fetch-all results :values :plist)))))

(defun table-view-query (conn table-name)
(with-prepared-query query (conn (format nil "SELECT pg_get_viewdef('~A'::regclass) AS def" table-name))
Expand All @@ -150,7 +143,7 @@
'(#\Space #\;)
(string-left-trim
'(#\Space)
(getf (first (dbi:fetch-all results)) :|def|))))))
(first (first (dbi:fetch-all results :format :values))))))))

(defun acquire-advisory-lock (conn id)
(dbi:do-sql conn "SELECT pg_advisory_lock(?)" (list id))
Expand Down
15 changes: 8 additions & 7 deletions src/core/db/sqlite3.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
(defun table-info (conn table-name)
(let* ((sql (format nil "PRAGMA table_info(\"~A\")" table-name)))
(with-prepared-query query (conn sql)
(or (dbi:fetch-all (dbi:execute query))
(or (dbi:fetch-all (dbi:execute query) :format :plist)
(error "Table \"~A\" doesn't exist." table-name)))))

(defun last-insert-id (conn table-name)
(declare (ignore table-name))
(with-prepared-query query (conn "SELECT last_insert_rowid() AS last_insert_id")
(getf (dbi:fetch
(dbi:execute query))
:|last_insert_id|
0)))
(or (first (dbi:fetch
(dbi:execute query)
:format :values))
0)))

(defun column-definitions (conn table-name)
(labels ((column-primary-key-p (column)
Expand Down Expand Up @@ -67,14 +67,15 @@
(with-prepared-query query (conn (format nil "PRAGMA index_list(\"~A\")" table-name))
(append
(loop with results = (dbi:execute query)
for index = (dbi:fetch results)
for index = (dbi:fetch results :format :plist)
while index
collect
(let* ((columns (mapcar
(lambda (info) (getf info :|name|))
(dbi:fetch-all
(dbi:execute (dbi:prepare conn (format nil "PRAGMA index_info(\"~A\")"
(getf index :|name|)))))))
(getf index :|name|))))
:format :plist)))
(unique-key (= (getf index :|unique|) 1))
(primary-key (and unique-key
primary-keys
Expand Down

0 comments on commit 571ed68

Please sign in to comment.