Skip to content

Commit

Permalink
Merge pull request #92 from mavericksy/dbi_query_as_configurable
Browse files Browse the repository at this point in the history
Hoist dbi sql queries into configurable parameters
  • Loading branch information
fukamachi authored Jul 25, 2024
2 parents 073333b + 938a322 commit 35d3a8e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ is syntactically equal to:
*app*)
```

## Session middleware table column names configuration

If your session table cannot conform to the session middleware expectations, the session middleware column names need to be changed; two &key parameters exist to change them, namely `:data-column-name` and `:id-column-name`,

```common-lisp
(lack:builder
(:session
:data-column-name "my_session_data_column"
:id-column-name "my_id_column")
*app*)
```

## Using Lack in an existing Clack app

Just replace `clack.builder:builder` by `lack:builder`, a superset of `clack.builder:builder`.
Expand Down
32 changes: 22 additions & 10 deletions src/middleware/session/store/dbi.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
:remove-session))
(in-package :lack/middleware/session/store/dbi)


(defmacro with-db-connection (connection store &body body)
`(let ((,connection (funcall (dbi-store-connector ,store))))
(unwind-protect
Expand All @@ -37,13 +38,17 @@
(unmarshal (read-from-string
(utf-8-bytes-to-string (base64-string-to-usb8-array data))))))
(record-timestamps nil :type boolean)
(table-name "sessions"))
(table-name "sessions")
(data-column-name "session_data")
(id-column-name "id"))

(defmethod fetch-session ((store dbi-store) sid)
(with-db-connection conn store
(let* ((query (dbi:prepare conn
(format nil "SELECT session_data FROM ~A WHERE id = ?"
(dbi-store-table-name store))))
(format nil "SELECT ~A FROM ~A WHERE ~A = ?"
(dbi-store-data-column-name store)
(dbi-store-table-name store)
(dbi-store-id-column-name store))))
(result (dbi:fetch (dbi:execute query (list sid)))))
(if result
(handler-case (funcall (dbi-store-deserializer store) (getf result :|session_data|))
Expand All @@ -67,31 +72,38 @@
(let ((serialized-session (funcall (dbi-store-serializer store) session)))
(dbi:with-transaction conn
(let* ((query (dbi:prepare conn
(format nil "SELECT session_data FROM ~A WHERE id = ?"
(dbi-store-table-name store))))
(format nil "SELECT ~A FROM ~A WHERE ~A = ?"
(dbi-store-data-column-name store)
(dbi-store-table-name store)
(dbi-store-id-column-name store))))
(current-session (getf (dbi:fetch (dbi:execute query (list sid))) :|session_data|)))
(cond
;; Session exists but not changed
((equal current-session serialized-session))
;; Session exists and is going to be changed
(current-session
(dbi:do-sql conn
(format nil "UPDATE ~A SET session_data = ?~:[~*~;, updated_at = '~A'~] WHERE id = ?"
(format nil "UPDATE ~A SET ~A = ?~:[~*~;, updated_at = '~A'~] WHERE ~A = ?"
(dbi-store-table-name store)
(dbi-store-data-column-name store)
(dbi-store-record-timestamps store)
(current-timestamp))
(current-timestamp)
(dbi-store-id-column-name store))
(list serialized-session sid)))
;; New session
(t
(dbi:do-sql conn (format nil "INSERT INTO ~A (id, session_data~:[~;, created_at, updated_at~]) VALUES (?, ?~:*~:[~*~;, '~A', ~:*'~A'~])"
(dbi:do-sql conn (format nil "INSERT INTO ~A (~A, ~A~:[~;, created_at, updated_at~]) VALUES (?, ?~:*~:[~*~;, '~A', ~:*'~A'~])"
(dbi-store-table-name store)
(dbi-store-id-column-name store)
(dbi-store-data-column-name store)
(dbi-store-record-timestamps store)
(current-timestamp))
(list sid serialized-session)))))))))

(defmethod remove-session ((store dbi-store) sid)
(with-db-connection conn store
(dbi:do-sql conn
(format nil "DELETE FROM ~A WHERE id = ?"
(dbi-store-table-name store))
(format nil "DELETE FROM ~A WHERE ~A = ?"
(dbi-store-table-name store)
(dbi-store-id-column-name store))
(list sid))))

0 comments on commit 35d3a8e

Please sign in to comment.