emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/emacsql bbe3031a50 130/427: Add table constraints.


From: ELPA Syncer
Subject: [nongnu] elpa/emacsql bbe3031a50 130/427: Add table constraints.
Date: Tue, 13 Dec 2022 02:59:35 -0500 (EST)

branch: elpa/emacsql
commit bbe3031a5010e3ba821ace910481cd74611b9f3a
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>

    Add table constraints.
---
 emacsql-tests.el |  8 ++++++++
 emacsql.el       | 22 +++++++++++++++++++---
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/emacsql-tests.el b/emacsql-tests.el
index fd514fe205..d659dcd804 100644
--- a/emacsql-tests.el
+++ b/emacsql-tests.el
@@ -87,6 +87,14 @@
      "CREATE TABLE foo (a, b, c PRIMARY KEY);")
     ([:create-table foo [a b (c :default $1)]] '("FOO")
      "CREATE TABLE foo (a, b, c DEFAULT '\"FOO\"');")
+    ;; Table constraints
+    ([:create-table foo ([a b c] :primary [a c])] '()
+     "CREATE TABLE foo (a, b, c, PRIMARY KEY (a, c));")
+    ([:create-table foo ([a b c] :unique [a b c])] '()
+     "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));")
+
     ([:drop-table $1] '(foo)
      "DROP TABLE foo;")))
 
diff --git a/emacsql.el b/emacsql.el
index 0683b37fee..91808ae202 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -427,16 +427,32 @@ definitions for return from a `emacsql-defexpander'."
       (push name output)
       (mapconcat #'identity output " "))))
 
-(defun emacsql--schema-to-string (schema)
-  "Convert SCHEMA into a SQL-consumable string."
+(defun emacsql--columns-to-string (columns)
+  "Convert COLUMNS into a SQL-consumable string."
   (emacsql-with-vars ""
-    (cl-loop for column across schema
+    (cl-loop for column across columns
              when (symbolp column)
              collect (var column :identifier) into parts
              else
              collect (combine (emacsql--column-to-string column)) into parts
              finally (cl-return (mapconcat #'identity parts ", ")))))
 
+(defun emacsql--schema-to-string (schema)
+  (cl-etypecase schema
+    (vector (emacsql--columns-to-string schema))
+    (list
+     (emacsql-with-vars ""
+       (mapconcat
+        #'identity
+        (cons
+         (combine (emacsql--columns-to-string (pop schema)))
+         (cl-loop for (key value) on schema by #'cddr collect
+                  (cl-ecase key
+                    (:primary (format "PRIMARY KEY (%s)" (idents value)))
+                    (:unique (format "UNIQUE (%s)" (idents value)))
+                    (:check (format "CHECK (%s)" (expr value))))))
+        ", ")))))
+
 (defun emacsql--vector (vector)
   "Expand VECTOR, making variables as needed."
   (emacsql-with-vars ""



reply via email to

[Prev in Thread] Current Thread [Next in Thread]