[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/emacsql 364c3cacb6 133/427: More advanced :from sources, a
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/emacsql 364c3cacb6 133/427: More advanced :from sources, again. |
Date: |
Tue, 13 Dec 2022 02:59:35 -0500 (EST) |
branch: elpa/emacsql
commit 364c3cacb6a41345337ac612c666e41a43353c1c
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>
More advanced :from sources, again.
---
README.md | 2 ++
emacsql-tests.el | 10 ++++++++--
emacsql.el | 30 ++++++++++++++++++++----------
3 files changed, 30 insertions(+), 12 deletions(-)
diff --git a/README.md b/README.md
index d936c94f92..65c61f7310 100644
--- a/README.md
+++ b/README.md
@@ -152,7 +152,9 @@ Provides `FROM`.
```el
[... :from employees]
[... :from [employees accounts]]
+[... :from [employees (accounts a)]]
[... :from (:select ...)]
+[... :from [((:select ...) s1) ((:select ...) s2)]]
```
#### :where `<expr>`
diff --git a/emacsql-tests.el b/emacsql-tests.el
index d659dcd804..ac622c12b9 100644
--- a/emacsql-tests.el
+++ b/emacsql-tests.el
@@ -69,7 +69,14 @@
([:select * :from employees :where (< salary 50000)] '()
"SELECT * FROM employees WHERE salary < 50000;")
([:select * :from people :where (in name $1)] '([FOO BAR])
- "SELECT * FROM people WHERE name IN ('FOO', 'BAR');")))
+ "SELECT * FROM people WHERE name IN ('FOO', 'BAR');")
+ ;; Sub queries
+ ([:select name :from (:select * :from $1)] '(people)
+ "SELECT name FROM (SELECT * FROM people);")
+ ([:select name :from [people (accounts a)]] '()
+ "SELECT name FROM people, accounts a;")
+ ([:select p:name :from [((:select * :from people) p)]] '()
+ "SELECT p.name FROM (SELECT * FROM people) p;")))
(ert-deftest emacsql-create-table ()
(emacsql-tests-with-queries
@@ -94,7 +101,6 @@
"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 c38af94b21..af2d0d7aea 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -395,7 +395,8 @@ definitions for return from a `emacsql-defexpander'."
(cl-flet* ((var (thing kind) (emacsql--vars-var thing kind))
(combine (expanded) (emacsql--vars-combine expanded))
(expr (thing) (combine (emacsql--expr thing)))
- (idents (thing) (combine (emacsql--idents thing))))
+ (idents (thing) (combine (emacsql--idents thing)))
+ (subsql (thing) (combine (emacsql-expand thing t))))
(cons (concat ,prefix (progn ,@body)) emacsql--vars))))
(defun emacsql--column-to-string (column)
@@ -504,10 +505,7 @@ definitions for return from a `emacsql-defexpander'."
(1 (error "Wrong number of operands for %s" op))
(2 (format "%s IN %s" (recur 0) (var (nth 1 args) :vector)))
(otherwise
- (let ((subsql (cl-coerce (cdr args) 'vector)))
- (format "%s IN %s"
- (recur 0)
- (combine (emacsql-expand subsql :sub)))))))))))))
+ (format "%s IN %s" (recur 0) (subsql (cdr args))))))))))))
(defun emacsql--idents (idents)
"Read in a vector of IDENTS identifiers, or just an single identifier."
@@ -532,13 +530,25 @@ definitions for return from a `emacsql-defexpander'."
"*"
(idents arg))))
-(emacsql-defexpander :from (table)
+(emacsql-defexpander :from (sources)
"Expands to the FROM keyword."
(emacsql-with-vars "FROM "
- (cl-etypecase table
- (vector (idents table))
- (symbol (var table :identifier))
- (list (combine (emacsql-expand table :subsql-p))))))
+ (cl-etypecase sources
+ (symbol (var sources :identifier))
+ (list (if (eq :select (car sources))
+ (subsql sources)
+ (cl-destructuring-bind (table alias) sources
+ (concat (var table :identifier) " " (var alias :identifier)))))
+ (vector (mapconcat (lambda (source)
+ (cl-etypecase source
+ (symbol (var source :identifier))
+ (list
+ (cl-destructuring-bind (table alias) source
+ (concat (if (symbolp table)
+ (var table :identifier)
+ (subsql table))
+ " " (var alias :identifier))))))
+ sources ", ")))))
(emacsql-defexpander :replace ()
(list "REPLACE"))
- [nongnu] elpa/emacsql c5845884f4 021/427: Rename type to column., (continued)
- [nongnu] elpa/emacsql c5845884f4 021/427: Rename type to column., ELPA Syncer, 2022/12/13
- [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 <=
- [nongnu] elpa/emacsql 99a8069389 135/427: Add support for foreign keys., ELPA Syncer, 2022/12/13
- [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