diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi index 07fb730f0f1..7f2123d06f9 100644 --- a/doc/lispref/text.texi +++ b/doc/lispref/text.texi @@ -5414,6 +5414,14 @@ called @var{gif}, you have to mark it specially to let @end defun +@defun sqlite-execute-batch db statements +Execute the @acronym{SQL} @var{statements}. @var{statements} is a +string containing 0 or more @acronym{SQL} statements. This command +might be useful when we want to execute multiple @acronym{DDL} +statements. + +@end defun + @defun sqlite-select db query &optional values return-type Select some data from @var{db} and return them. For instance: diff --git a/etc/NEWS b/etc/NEWS index 922721f143c..b53136e3ba2 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -484,6 +484,13 @@ When visiting a script that invokes 'env -S INTERPRETER ARGS...' in its shebang line, Emacs will now skip over 'env -S' and deduce the major mode based on the interpreter after 'env -S'. ++++ +** New command 'sqlite-execute-batch'. +This command lets the user execute multiple SQL commands in one +command. It is useful when the user wants to evaluate an entire SQL +file. + ++++ * Editing Changes in Emacs 30.1 diff --git a/src/sqlite.c b/src/sqlite.c index 261080da673..c606fa5f831 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -646,6 +646,17 @@ sqlite_exec (sqlite3 *sdb, const char *query) return Qt; } +DEFUN ("sqlite-execute-batch", Fsqlite_execute_batch, Ssqlite_execute_batch, 2, 2, 0, + doc: /* Execute multiple SQL statements in DB. +Query is a string containing 0 or more SQL statements. */) + (Lisp_Object db, Lisp_Object query) +{ + check_sqlite (db, false); + CHECK_STRING (query); + Lisp_Object encoded = encode_string(query); + return sqlite_exec (XSQLITE (db)->db, SSDATA (encoded)); +} + DEFUN ("sqlite-transaction", Fsqlite_transaction, Ssqlite_transaction, 1, 1, 0, doc: /* Start a transaction in DB. */) (Lisp_Object db) @@ -866,6 +877,7 @@ syms_of_sqlite (void) defsubr (&Ssqlite_close); defsubr (&Ssqlite_execute); defsubr (&Ssqlite_select); + defsubr (&Ssqlite_execute_batch); defsubr (&Ssqlite_transaction); defsubr (&Ssqlite_commit); defsubr (&Ssqlite_rollback); diff --git a/test/src/sqlite-tests.el b/test/src/sqlite-tests.el index a10dca9a0c9..e87a5fc77b1 100644 --- a/test/src/sqlite-tests.el +++ b/test/src/sqlite-tests.el @@ -261,4 +261,30 @@ '("Joe" "Doe")) '((1 "Joe"))))))) +(ert-deftest sqlite-multiple-statements () + (skip-unless (sqlite-available-p)) + (let ((db (sqlite-open nil)) + (query (with-temp-buffer + (insert "-- -*- sql-product: sqlite -*- + +-- I 💘 emojis + +CREATE TABLE settings ( + name TEXT NOT NULL, + value TEXT, + section TEXT NOT NULL, + PRIMARY KEY (section, name) +); + +CREATE TABLE tags📎 ( + name TEXT PRIMARY KEY NOT NULL +); + +-- CREATE TABLE todo_states (id INTEGER PRIMARY KEY, name TEXT NOT NULL); +") + (buffer-string)))) + (sqlite-execute-batch db query) + (should (equal '(("settings") ("tags📎")) + (sqlite-select db "select name from sqlite_master where type = 'table' and name not like 'sqlite_%' order by name"))))) + ;;; sqlite-tests.el ends here