[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/emacsql f51bb4bfaf 195/427: Allow foreign key specificatio
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/emacsql f51bb4bfaf 195/427: Allow foreign key specifications within columns. |
Date: |
Tue, 13 Dec 2022 02:59:41 -0500 (EST) |
branch: elpa/emacsql
commit f51bb4bfafbc0d7ef217ef4c6e4a14a485709bf6
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>
Allow foreign key specifications within columns.
---
README.md | 9 +++++----
emacsql-compiler.el | 19 +++++++++++++------
emacsql-tests.el | 9 +++++----
3 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/README.md b/README.md
index 07af67621f..15c4db7b44 100644
--- a/README.md
+++ b/README.md
@@ -75,9 +75,10 @@ allowed) types are `integer`, `float`, and `object`
(default).
Columns constraints include `:primary` (aka `PRIMARY KEY`),
`:autoincrement`, `:unique`, `:non-nil` (aka `NOT NULL`), `:default`,
-and `:check`.
+`:check`, and `:references` (foreign key).
-Table constraints can be `:primary`, `:unique`, `:check`, and `:foreign`.
+Table constraints can be `:primary`, `:unique`, `:check`, and
+`:references`.
```el
;; No constraints schema with four columns:
@@ -96,7 +97,7 @@ or `:on-update` and possible actions are `:set-nil`,
`:set-default`,
`:restrict`, `:cascade`. See [the SQLite documentation][foreign] for
the details on what each of these means.
-`:foreign (<child-keys> <parent-table> <parent-keys> [<actions>])`.
+`:references (<child-keys> <parent-table> <parent-keys> [<actions>])`
```el
;; "subject" table
@@ -104,7 +105,7 @@ the details on what each of these means.
;; "tag" table references subjects
([(subjectid integer) tag]
- :foreign (subjectid subject id :on-delete :cascade))
+ :references (subjectid subject id :on-delete :cascade))
```
Put the keys in a vector if the reference is composite. Also remember
diff --git a/emacsql-compiler.el b/emacsql-compiler.el
index 13296b585f..e97e566adc 100644
--- a/emacsql-compiler.el
+++ b/emacsql-compiler.el
@@ -218,6 +218,8 @@ definitions for return from a `emacsql-defexpander'."
(push (var (pop column) :value) output))
(:check (push "CHECK" output)
(push (format "(%s)" (expr (pop column))) output))
+ (:references
+ (push (combine (emacsql--references (pop column))) output))
((integer float object)
(setf type (cadr (assoc next emacsql-type-map))))
(otherwise
@@ -237,14 +239,13 @@ 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)
+(defun emacsql--references (spec)
+ (emacsql-with-vars "REFERENCES "
+ (cl-destructuring-bind (table parent . actions) (cl-coerce spec 'list)
(mapconcat
#'identity
(cons
- (format "(%s) REFERENCES %s (%s)" (idents child) (var table
:identifier)
- (idents parent))
+ (format "%s (%s)" (var table :identifier) (idents parent))
(cl-loop for (key value) on actions by #'cddr collect
(cl-case key
(:on-update "ON UPDATE")
@@ -259,6 +260,12 @@ definitions for return from a `emacsql-defexpander'."
(otherwise (emacsql-error "Invalid action: %S" key)))))
" "))))
+(defun emacsql--foreign-key (spec)
+ (emacsql-with-vars "FOREIGN KEY "
+ (cl-destructuring-bind (child . references) (cl-coerce spec 'list)
+ (format "(%s) %s" (idents child)
+ (combine (emacsql--references references))))))
+
(defun emacsql--schema-to-string (schema)
(cl-typecase schema
(vector (emacsql--columns-to-string schema))
@@ -273,7 +280,7 @@ definitions for return from a `emacsql-defexpander'."
(:primary (format "PRIMARY KEY (%s)" (idents value)))
(:unique (format "UNIQUE (%s)" (idents value)))
(:check (format "CHECK (%s)" (expr value)))
- (:foreign (combine (emacsql--foreign-key value)))
+ (:references (combine (emacsql--foreign-key value)))
(otherwise
(emacsql-error "Invalid table constraint: %S" key)))))
", ")))
diff --git a/emacsql-tests.el b/emacsql-tests.el
index 31c6567bf6..27ca4731aa 100644
--- a/emacsql-tests.el
+++ b/emacsql-tests.el
@@ -111,10 +111,10 @@
"CREATE TABLE foo (a NONE, b NONE, c NONE, PRIMARY KEY (a, c));")
([:create-table foo ([a b c] :unique [a b c])] '()
"CREATE TABLE foo (a NONE, b NONE, c NONE, UNIQUE (a, b, c));")
- ([:create-table foo ([a b] :check (< a b)) ] '()
+ ([:create-table foo ([a b] :check (< a b))] '()
"CREATE TABLE foo (a NONE, b NONE, CHECK (a < b));")
([:create-table foo
- ([a b c] :foreign ([a b] bar [aa bb] :on-delete :cascade))] '()
+ ([a b c] :references ([a b] bar [aa bb] :on-delete :cascade))] '()
(concat "CREATE TABLE foo (a NONE, b NONE, c NONE, FOREIGN KEY (a, b) "
"REFERENCES bar (aa, bb) ON DELETE CASCADE);"))
;; Drop table
@@ -218,8 +218,9 @@
(emacsql-with-connection (db (emacsql-sqlite nil))
(emacsql-thread db
[:create-table person [(id integer :primary) name]]
- [:create-table likes ([(personid integer) color]
- :foreign (personid person id :on-delete :cascade))]
+ [:create-table likes
+ ([(personid integer) color]
+ :references (personid person id :on-delete :cascade))]
[:replace :into person :values ([0 "Chris"] [1 "Brian"])])
(should (equal (emacsql db [:select * :from person :order-by id])
'((0 "Chris") (1 "Brian"))))
- [nongnu] elpa/emacsql fbda94e6b5 339/427: Improve some doc-strings., (continued)
- [nongnu] elpa/emacsql fbda94e6b5 339/427: Improve some doc-strings., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 6f1e375a2e 367/427: Better escaping of NUL and control characters (#42), ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 3dc6971829 190/427: Add ALTER TABLE stuff., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql ef976b8649 189/427: Add package target for Makefile., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 672963ae53 191/427: Add DISTINCT option to :select., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql b1da2ba9d2 297/427: Change compiler flag order (work around old gcc bug)., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql d21cc86540 188/427: Add lexical binding to tests., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 3d556608ef 394/427: emacsql-mysql: Abort if mysql cannot be found, ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 568e663032 193/427: Fold dashes into underscores in identifiers., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql c85ab1f7b4 194/427: Enable foreign keys in SQLite by default., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql f51bb4bfaf 195/427: Allow foreign key specifications within columns.,
ELPA Syncer <=
- [nongnu] elpa/emacsql c4abd187a3 196/427: Remove junk file., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 3dfd0d4b8c 199/427: Fix missing argument typo., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 7341144e49 201/427: Fix ordering bug in emacsql--var-vars., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql d8d4bf4b98 202/427: Add emacsql-with-transaction macro., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql dc178532e5 203/427: Add emacsql-with-bind., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql cc92c154ee 205/427: Disallow nil as an identifier., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 75c0c4aeb7 204/427: Tweak emacsql-with-bind macro., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 3f38b357c3 206/427: Detect empty SQL vectors (better error messages)., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql ad24b1c508 208/427: Add JOIN syntax., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql fe2eef212c 210/427: Fix header typo., ELPA Syncer, 2022/12/13