[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/emacsql 4206b17f39 134/427: Add support for AS with CREATE
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/emacsql 4206b17f39 134/427: Add support for AS with CREATE TABLE. |
Date: |
Tue, 13 Dec 2022 02:59:35 -0500 (EST) |
branch: elpa/emacsql
commit 4206b17f3949dc4ebb80d77d2a943c6047996955
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>
Add support for AS with CREATE TABLE.
---
README.md | 6 ++++--
emacsql-tests.el | 4 ++++
emacsql.el | 11 +++++++----
3 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
index 65c61f7310..9a9c7764be 100644
--- a/README.md
+++ b/README.md
@@ -119,13 +119,15 @@ When multiple keywords appear in sequence, Emacsql will
generally
concatenate them with a dash, e.g. `CREATE TABLE` becomes
`:create-table`.
-#### :create-table `<table>` `<schema>`
+#### :create-table `<table>` `<schema|select>`
-Provides `CREATE TABLE`.
+Provides `CREATE TABLE`. A selection can be used in place of a schema,
+which will create a `CREATE TABLE ... AS` statement.
```el
[:create-table employees [name (id integer :primary) (salary float)]]
[:create-table (:temporary :if-not-exists employees) ...]
+[:create-table names [:select name :from employees]]
```
#### :drop-table `<table>`
diff --git a/emacsql-tests.el b/emacsql-tests.el
index ac622c12b9..5277eb0629 100644
--- a/emacsql-tests.el
+++ b/emacsql-tests.el
@@ -94,6 +94,9 @@
"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\"');")
+ ;; From select
+ ([:create-table $1 [:select name :from $2]] '(names people)
+ "CREATE TABLE names AS (SELECT name FROM people);")
;; Table constraints
([:create-table foo ([a b c] :primary [a c])] '()
"CREATE TABLE foo (a, b, c, PRIMARY KEY (a, c));")
@@ -101,6 +104,7 @@
"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
([:drop-table $1] '(foo)
"DROP TABLE foo;")))
diff --git a/emacsql.el b/emacsql.el
index af2d0d7aea..60c65a1a3d 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -605,10 +605,13 @@ definitions for return from a `emacsql-defexpander'."
(otherwise (setf name (var item :identifier)))))
(let* ((items (list temporary "TABLE" if-not-exists name))
(spec (cl-remove-if-not #'identity items)))
- (format "%s (%s)" (mapconcat #'identity spec " ")
- (if (symbolp schema)
- (var schema :schema)
- (combine (emacsql--schema-to-string schema))))))))
+ (format "%s %s" (mapconcat #'identity spec " ")
+ (cond ((symbolp schema)
+ (format "(%s)" (var schema :schema)))
+ ((eq :select (elt schema 0))
+ (concat "AS " (subsql schema)))
+ ((let ((compiled (emacsql--schema-to-string schema)))
+ (format "(%s)" (combine compiled))))))))))
(emacsql-defexpander :drop-table (table)
(emacsql-with-vars "DROP TABLE "
- [nongnu] elpa/emacsql 364bf466ea 086/427: Linkify the issue in the README., (continued)
- [nongnu] elpa/emacsql 364bf466ea 086/427: Linkify the issue in the README., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql dd3f153406 089/427: Drop old with-errors macro., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 72c1954517 096/427: Block on connection until the output has flushed., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 1e9857ccc4 103/427: Allow variables inside vectors., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 8a66563af6 105/427: Combine :insert and :into., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql d04ef64ce0 109/427: Add IF NOT EXISTS and TEMPORARY to CREATE TABLE., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 58641b9827 115/427: Fill out keywords in README., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql fc941d7c75 118/427: Capture a common identifier vector pattern., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql a09911a205 125/427: Add LIMIT and OFFSET expanders., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 8945af0dd8 129/427: Add :check and allow schemas to have variables., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 4206b17f39 134/427: Add support for AS with CREATE TABLE.,
ELPA Syncer <=
- [nongnu] elpa/emacsql 6c32f02139 137/427: Add emacsql-thread macro., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 54c180017e 138/427: Add :pragma and note in README., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 5e2ab842f7 139/427: Add foreign key test., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 42c2fd7569 145/427: Tweak limitation wording., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql fe0c7004fc 146/427: Add :begin, :commit, and :rollback., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 2550196816 162/427: Drop SQLite version requirements., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 9a1b4209ba 164/427: Add PostgreSQL front-end., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 5f4c2ed5a7 166/427: Add emacsql-psql-unavailable-p., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 1d0ef4011c 167/427: Make emacsql-connection abstract., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 64c3a03d62 169/427: Flesh out some more README., ELPA Syncer, 2022/12/13