[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/emacsql adfbc7ed83 156/427: Add emacsql-show-last-sql.
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/emacsql adfbc7ed83 156/427: Add emacsql-show-last-sql. |
Date: |
Tue, 13 Dec 2022 02:59:37 -0500 (EST) |
branch: elpa/emacsql
commit adfbc7ed837d5757c5681d5d2e830c3d43b3201a
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>
Add emacsql-show-last-sql.
---
README.md | 4 ++++
emacsql.el | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+)
diff --git a/README.md b/README.md
index 12a8faa210..d1998e8d6c 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,10 @@ shouldn't impact normal use of the database.
;; => (("Jeff" 1000) ("Susan" 1001))
```
+When editing these structured SQL statements, the `M-x
+emacsql-show-last-sql` command (think `eval-last-sexp`) is useful for
+seeing what the actual SQL expression will become when compiled.
+
## Schema
A table schema is a vector of column specifications, or a list
diff --git a/emacsql.el b/emacsql.el
index 64993134b7..2196d282ed 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -711,6 +711,54 @@ definitions for return from a `emacsql-defexpander'."
(emacsql-defexpander :vacuum ()
(list "VACUUM"))
+;; User interaction functions:
+
+(defvar emacsql-show-buffer-name "*emacsql-show*"
+ "Name of the buffer for displaying intermediate SQL.")
+
+(defun emacsql--indent ()
+ "Indent and wrap the SQL expression in the current buffer."
+ (interactive)
+ (save-excursion
+ (setf (point) (point-min))
+ (let ((case-fold-search nil))
+ (while (search-forward-regexp " [A-Z]+" nil :no-error)
+ (when (> (current-column) (* fill-column 0.8))
+ (backward-word)
+ (insert "\n "))))))
+
+(defun emacsql-show-sql (string)
+ "Fontify and display the SQL expression in STRING."
+ (let ((fontified
+ (with-temp-buffer
+ (insert string)
+ (sql-mode)
+ (with-no-warnings ;; autoloaded by previous line
+ (sql-highlight-sqlite-keywords))
+ (font-lock-fontify-buffer)
+ (emacsql--indent)
+ (buffer-string))))
+ (with-current-buffer (get-buffer-create emacsql-show-buffer-name)
+ (if (< (length string) fill-column)
+ (message "%s" fontified)
+ (let ((buffer-read-only nil))
+ (erase-buffer)
+ (insert fontified))
+ (special-mode)
+ (visual-line-mode)
+ (pop-to-buffer (current-buffer))))))
+
+(defun emacsql-flatten-sql (sql)
+ "Convert a structured SQL into a flat string for display."
+ (cl-destructuring-bind (string . vars) (emacsql-expand sql)
+ (apply #'format string (cl-loop for i from 1 to (length vars)
+ collect (intern (format "$%d" i))))))
+
+(defun emacsql-show-last-sql ()
+ "Display the compiled SQL of the structured SQL expression before point."
+ (interactive)
+ (emacsql-show-sql (emacsql-flatten-sql (preceding-sexp))))
+
(provide 'emacsql)
;;; emacsql.el ends here
- [nongnu] elpa/emacsql 829298d5a7 063/427: Add dependency listing., (continued)
- [nongnu] elpa/emacsql 829298d5a7 063/427: Add dependency listing., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 231ec586ca 088/427: Drop forced process coding system., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql eba26bead6 102/427: Add emacsql-compile function for debugging., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql d5cfaee783 158/427: Be more precise about error messages., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql f512300aba 054/427: Fix a few things in emacsql-expr., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql e900f99091 075/427: Update examples., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 1116a0873e 163/427: Turn everything into generic functions., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 47d9476a02 142/427: Add tests for quote operator., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql cd5e856ea6 150/427: Add :autoincrement., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql f31120edb6 155/427: Fix up documentation headers., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql adfbc7ed83 156/427: Add emacsql-show-last-sql.,
ELPA Syncer <=
- [nongnu] elpa/emacsql 60d92d3051 159/427: Change the output mode to list., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql d2f1bcef5d 165/427: Rename add-connection to register., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 9dcfebfba7 030/427: Factor out schema->string code., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 1f6c06bda9 050/427: Use expr expansion in :where expander., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 39aa16906a 034/427: Remove redundant wait., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql cda366379a 085/427: Add Windows note., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql db0159f138 032/427: Update README., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 801bc6239f 222/427: Swap the tuple order., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 9e6ea00f01 243/427: Add note about PostgreSQL., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql ad6c09681e 261/427: Oops, print the tuple., ELPA Syncer, 2022/12/13