branch: elpa/emacsql
commit 76bf0c34dce3efb24702635a341b51b7f2e754ef
Author: Christopher Wellons <[email protected]>
Commit: Christopher Wellons <[email protected]>
Add rudimentary :where expander.
---
README.md | 5 ++---
emacsql.el | 15 +++++++++++++++
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index f288c265ee..f323cf7f88 100644
--- a/README.md
+++ b/README.md
@@ -28,9 +28,8 @@ Requires Emacs 24 or later.
(emacsql-insert db 'employees ["Jeff" 1000 60000.0]
["Susan" 1001 64000.0])
-;; The high-level SELECT interface is a work in progress.
-(emacsql-select-raw db (concat "SELECT name, id FROM employees "
- "WHERE salary > 60000;"))
+;; Query the database for results:
+(emacsql db [:select [name id] :from employees :where (> salary 60000)])
;; => (("Susan" 1001))
```
diff --git a/emacsql.el b/emacsql.el
index ebb62d31b8..ef6a7a0188 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -386,6 +386,21 @@ KIND should be :value or :identifier."
(list "FROM %s" (cons (emacsql-var table) :identifier))
(list (concat "FROM " (emacsql-escape-format table :identifier)))))
+(emacsql-defexpander :where (expr)
+ (let ((vars ()))
+ (cl-flet* ((collect (thing kind)
+ (push (cons (emacsql-var thing) kind) vars) "%s")
+ (handle (v)
+ (cond ((emacsql-var v) (collect v))
+ ((symbolp v) (emacsql-escape-format v :identifier))
+ ((emacsql-escape-value v)))))
+ (cl-destructuring-bind (op a b) expr
+ (cons (format "WHERE %s %s %s"
+ (handle a)
+ op
+ (handle b))
+ vars)))))
+
(provide 'emacsql)
;;; emacsql.el ends here