]> git.eshelyaron.com Git - emacs.git/commitdiff
Add 'sqlite-execute-batch' command
authorJavier Olaechea <pirata@gmail.com>
Mon, 1 Apr 2024 04:07:10 +0000 (23:07 -0500)
committerEshel Yaron <me@eshelyaron.com>
Thu, 6 Jun 2024 10:26:59 +0000 (12:26 +0200)
This command is similar to 'sqlite-execute' except that it
executes multiple statements in exchange for not accepting
any arguments.  (Bug#70145)
* src/sqlite.c (Fsqlite_execute_batch): New function.

* test/src/sqlite-tests.el (sqlite-multiple-statements): Add
smoke test for 'sqlite-execute-batch'.

* etc/NEWS: Mention new command 'sqlite-execute-batch'.
* doc/lispref/text.texi (Database): Document the new command.

(cherry picked from commit 23ef989935d38fe5c2c105933ae5f4d692656c72)

doc/lispref/text.texi
etc/NEWS
src/sqlite.c
test/src/sqlite-tests.el

index 28c2e8e78189ef0f262db3058af4d5278b4e5fa7..3d4ce2575118fa359d45b912f4b16cf533e8432e 100644 (file)
@@ -5422,6 +5422,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:
 
index 3c65d51eab190695164736fea966d3c0ea6c5bef..9b5aa27632930a45e6d50a0429145a9d7fa63bed 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -471,6 +471,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.
+
++++
 \f
 * Editing Changes in Emacs 30.1
 
index 261080da673bea1f3f9b0362c927c043a6415a8c..c606fa5f831b113c708fce947ec7f7e39704dfe3 100644 (file)
@@ -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);
index a10dca9a0c93afa34246b95bb0a6258cbb138233..e87a5fc77b1165360a82252703bc8f1fcad4760e 100644 (file)
                        '("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