[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/emacsql 9dcfebfba7 030/427: Factor out schema->string code
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/emacsql 9dcfebfba7 030/427: Factor out schema->string code. |
Date: |
Tue, 13 Dec 2022 02:59:25 -0500 (EST) |
branch: elpa/emacsql
commit 9dcfebfba7b3ce50dd56e777a404f900c6f0b23c
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>
Factor out schema->string code.
---
emacsql.el | 47 ++++++++++++++++++++++++++++++++++++-----------
1 file changed, 36 insertions(+), 11 deletions(-)
diff --git a/emacsql.el b/emacsql.el
index 69711df2ae..fd043d7f2f 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -218,20 +218,45 @@ If NAMED is non-nil, don't include column names."
,@body
(emacsql--check-error ,conn)))
+(defun emacsql--column-to-string (column)
+ "Convert COLUMN schema into a SQL string."
+ (let ((name (emacsql-escape (pop column)))
+ (output ())
+ (type nil))
+ (while column
+ (let ((next (pop column)))
+ (case next
+ (:primary (push "PRIMARY KEY" output))
+ (:non-nil (push "NOT NULL" output))
+ (:unique (push "UNIQUE" output))
+ (integer (setf type "INTEGER"))
+ (float (setf type "REAL"))
+ (object (setf type "TEXT"))
+ (otherwise
+ (if (keywordp next)
+ (error "Unknown schema contraint %s" next)
+ (error "Invalid type %s: %s" next
+ "must be 'integer', 'float', or 'object'"))))))
+ (mapconcat #'identity
+ (nconc (if type (list name type) (list name)) (nreverse output))
+ " ")))
+
+(defun emacsql--schema-to-string (schema)
+ "Convert SCHEMA into a SQL-consumable string."
+ (cl-loop for column being the elements of schema
+ when (symbolp column) collect (emacsql-escape column) into parts
+ else collect (emacsql--column-to-string column) into parts
+ finally (return (mapconcat #'identity parts ", "))))
+
(defun emacsql-create (conn table schema &optional if-not-exists)
"Create TABLE in CONN with SCHEMA."
(emacsql-with-errors conn
- (cl-loop for column being the elements of schema
- when (consp column)
- collect (mapconcat #'emacsql-escape column " ")
- into parts
- else collect (format "%s" column) into parts
- finally (emacsql--send
- conn
- (format "CREATE TABLE %s%s(%s);"
- (if if-not-exists "IF NOT EXISTS " "")
- (emacsql-escape table)
- (mapconcat #'identity parts ", "))))))
+ (emacsql--send
+ conn
+ (format "CREATE TABLE %s%s(%s);"
+ (if if-not-exists "IF NOT EXISTS " "")
+ (emacsql-escape table)
+ (emacsql--schema-to-string schema)))))
(defun emacsql-drop (conn table)
"Drop TABLE from CONN."
- [nongnu] elpa/emacsql d5cfaee783 158/427: Be more precise about error messages., (continued)
- [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, 2022/12/13
- [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 <=
- [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
- [nongnu] elpa/emacsql ff59ca9336 265/427: Clear out binaries in distclean, too., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 00bb81ff2b 266/427: Fix Makefile typo., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql c8de0ff1fd 293/427: Adjust the EmacSQL protocol., ELPA Syncer, 2022/12/13