Skip to content

Commit

Permalink
Rename delete-by-pk -> delete-by-values and allows relational key-val…
Browse files Browse the repository at this point in the history
…ues.
  • Loading branch information
fukamachi committed Jun 27, 2017
1 parent 8c9c332 commit 254f57f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/core.lisp
Expand Up @@ -29,7 +29,7 @@
#:update-dao
#:create-dao
#:delete-dao
#:delete-by-pk
#:delete-by-values
#:save-dao
#:select-dao
#:includes
Expand Down
24 changes: 8 additions & 16 deletions src/core/dao.lisp
Expand Up @@ -43,7 +43,7 @@
#:update-dao
#:create-dao
#:delete-dao
#:delete-by-pk
#:delete-by-values
#:save-dao
#:select-dao
#:includes
Expand Down Expand Up @@ -151,21 +151,13 @@
(setf (dao-synced obj) nil)))
(values)))

(defgeneric delete-by-pk (class &rest pkeys)
(:method ((class symbol) &rest pkeys)
(apply #'delete-by-pk (find-class class) pkeys))
(:method ((class dao-table-class) &rest pkeys)
(let ((primary-key (table-primary-key class)))
(unless primary-key
(error 'no-primary-keys :table (table-name class)))

(execute-sql
(sxql:delete-from (sxql:make-sql-symbol (table-name class))
(sxql:where
`(:and ,@(mapcar (lambda (key value)
`(:= ,(unlispify key) ,value))
primary-key
pkeys))))))
(defgeneric delete-by-values (class &rest fields-and-values)
(:method ((class symbol) &rest fields-and-values)
(apply #'delete-by-values (find-class class) fields-and-values))
(:method ((class dao-table-class) &rest fields-and-values)
(execute-sql
(sxql:delete-from (sxql:make-sql-symbol (table-name class))
(where-and fields-and-values class)))
(values)))

(defgeneric save-dao (obj)
Expand Down
2 changes: 1 addition & 1 deletion t/dao.lisp
Expand Up @@ -167,7 +167,7 @@
(ok tweet)
(mito:delete-dao tweet)
(is (mito:count-dao 'tweet) 1)
(mito:delete-by-pk 'tweet 2)
(mito:delete-by-values 'tweet :id 2)
(is (mito:count-dao 'tweet) 0))

(disconnect-toplevel))
Expand Down

0 comments on commit 254f57f

Please sign in to comment.