branch: elpa/emacsql commit 3dc697182987717fed84c3046c1b4f8155df135a Author: Christopher Wellons <well...@nullprogram.com> Commit: Christopher Wellons <well...@nullprogram.com>
Add ALTER TABLE stuff. --- README.md | 16 ++++++++++++++++ emacsql-compiler.el | 15 +++++++++++++++ emacsql-tests.el | 9 +++++++++ 3 files changed, 40 insertions(+) diff --git a/README.md b/README.md index 679da8f662..885aa8380c 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,22 @@ Provides `COMMIT` and `ROLLBACK`. [:rollback] ``` +#### :alter-table `<table>`, :rename-to `<table>` + +Provides `ALTER TABLE` and `RENAME TO`. + +```el +[:alter-table prices :rename-to costs] +``` + +### :add-column `<column-spec>` + +Provides `ADD COLUMN`. + +```el +[:alter-table tags :add-column (rating integer :non-nil)] +``` + #### :pragma `<expr>` Provides `PRAGMA`. diff --git a/emacsql-compiler.el b/emacsql-compiler.el index ec572f81ab..db24fbb9f0 100644 --- a/emacsql-compiler.el +++ b/emacsql-compiler.el @@ -507,6 +507,21 @@ definitions for return from a `emacsql-defexpander'." (emacsql-defexpander :rollback () (list "ROLLBACK")) +(emacsql-defexpander :alter-table (table) + (emacsql-with-vars "ALTER TABLE " + (var table :identifier))) + +(emacsql-defexpander :add-column (column) + (emacsql-with-vars "ADD COLUMN " + (cl-typecase column + (symbol (var column :identifier)) + (list (combine (emacsql--column-to-string column))) + (otherwise (emacsql-error "Only one column allowed here: %S" column))))) + +(emacsql-defexpander :rename-to (new-name) + (emacsql-with-vars "RENAME TO " + (var new-name :identifier))) + (emacsql-defexpander :vacuum () (list "VACUUM")) diff --git a/emacsql-tests.el b/emacsql-tests.el index d3dfbd00bb..2ae2ec5fea 100644 --- a/emacsql-tests.el +++ b/emacsql-tests.el @@ -191,6 +191,15 @@ ([:commit] '() "COMMIT;"))) +(ert-deftest emacsql-alter-table () + (emacsql-tests-with-queries + ([:alter-table foo :rename-to bar] '() + "ALTER TABLE foo RENAME TO bar;") + ([:alter-table $1 :rename-to $2] '(alpha beta) + "ALTER TABLE alpha RENAME TO beta;") + ([:alter-table foo :add-column ($1 integer :non-nil)] '(size) + "ALTER TABLE foo ADD COLUMN size INTEGER NOT NULL;"))) + (ert-deftest emacsql-system () "A short test that fully interacts with SQLite." (should-not (emacsql-sqlite3-unavailable-p))