[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/emacsql 99a8069389 135/427: Add support for foreign keys.
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/emacsql 99a8069389 135/427: Add support for foreign keys. |
Date: |
Tue, 13 Dec 2022 02:59:35 -0500 (EST) |
branch: elpa/emacsql
commit 99a806938913afcfc30ea85f7b172659257c7d32
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>
Add support for foreign keys.
---
emacsql-tests.el | 4 ++++
emacsql.el | 23 ++++++++++++++++++++++-
2 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/emacsql-tests.el b/emacsql-tests.el
index 5277eb0629..8eb8113805 100644
--- a/emacsql-tests.el
+++ b/emacsql-tests.el
@@ -104,6 +104,10 @@
"CREATE TABLE foo (a, b, c, UNIQUE (a, b, c));")
([:create-table foo ([a b] :check (< a b)) ] '()
"CREATE TABLE foo (a, b, CHECK (a < b));")
+ ([:create-table foo
+ ([a b c] :foreign ([a b] bar [aa bb] :on-delete :cascade))] '()
+ (concat "CREATE TABLE foo (a, b, c, FOREIGN KEY (a, b) "
+ "REFERENCES bar (aa, bb) ON DELETE CASCADE);"))
;; Drop table
([:drop-table $1] '(foo)
"DROP TABLE foo;")))
diff --git a/emacsql.el b/emacsql.el
index 60c65a1a3d..a1986e2e19 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -438,6 +438,26 @@ definitions for return from a `emacsql-defexpander'."
collect (combine (emacsql--column-to-string column)) into parts
finally (cl-return (mapconcat #'identity parts ", ")))))
+(defun emacsql--foreign-key (spec)
+ (emacsql-with-vars "FOREIGN KEY "
+ (cl-destructuring-bind (child table parent . actions) (cl-coerce spec
'list)
+ (mapconcat
+ #'identity
+ (cons
+ (format "(%s) REFERENCES %s (%s)" (idents child) (var table
:identifier)
+ (idents parent))
+ (cl-loop for (key value) on actions by #'cddr collect
+ (cl-ecase key
+ (:on-update "ON UPDATE")
+ (:on-delete "ON DELETE"))
+ collect
+ (cl-ecase value
+ (:restrict "RESTRICT")
+ (:set-nil "SET NULL")
+ (:set-default "SET DEFAULT")
+ (:cascade "CASCADE"))))
+ " "))))
+
(defun emacsql--schema-to-string (schema)
(cl-etypecase schema
(vector (emacsql--columns-to-string schema))
@@ -451,7 +471,8 @@ definitions for return from a `emacsql-defexpander'."
(cl-ecase key
(:primary (format "PRIMARY KEY (%s)" (idents value)))
(:unique (format "UNIQUE (%s)" (idents value)))
- (:check (format "CHECK (%s)" (expr value))))))
+ (:check (format "CHECK (%s)" (expr value)))
+ (:foreign (combine (emacsql--foreign-key value))))))
", ")))))
(defun emacsql--vector (vector)
- [nongnu] elpa/emacsql 7caa63c11e 023/427: Allow any sequence for table spec., (continued)
- [nongnu] elpa/emacsql 7caa63c11e 023/427: Allow any sequence for table spec., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 963f221b82 049/427: Add expr expansion function., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 109664b88b 060/427: Memoize emacsql-expand., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 1fb97f7d4a 092/427: Drop unusable macro emacsql-with-connection., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql add06c47b9 117/427: Bring back :into for :replace (oops!)., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql d7a05db54e 080/427: Add :replace expander., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 0f0840ffbf 128/427: Add :default column constraint., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql bbe3031a50 130/427: Add table constraints., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 53b2e98515 131/427: Fill out README for new schema table constraints., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 364c3cacb6 133/427: More advanced :from sources, again., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 99a8069389 135/427: Add support for foreign keys.,
ELPA Syncer <=
- [nongnu] elpa/emacsql e6ab07e16b 140/427: Switch to a friendlier name., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql bec6af6940 148/427: Add VACUUM., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 03aebdecd1 160/427: Rename :log option to :debug., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql bb221f6134 161/427: Drop .print command flushing., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 72b34f6522 173/427: Add some more autoloading., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 3229cd41af 180/427: Change the requirements for front-end implementations., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 431a3b325f 200/427: Toss "reap" and switch to new finalize with Cask., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql ed86b098ce 197/427: Add prefix arg to emacsql-show-last-sql., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 027c5df94e 209/427: Organize all the keywords in the README., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 1ebbc22411 212/427: Disable undo in process buffers., ELPA Syncer, 2022/12/13