unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Javier Olaechea <pirata@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 70145@debbugs.gnu.org, jp@neverwas.me
Subject: bug#70145: [PATCH] Add sqlite-execute-batch command
Date: Sat, 13 Apr 2024 22:32:05 -0500	[thread overview]
Message-ID: <CAFVS=zD-vXTBskiS04VkhD-vUfNSPUF3yNjwSe+sqFjDmscPhw@mail.gmail.com> (raw)
In-Reply-To: <86a5lxr8nq.fsf@gnu.org>


[-- Attachment #1.1: Type: text/plain, Size: 469 bytes --]

> Thanks.  This should have a NEWS entry announcing the new command
>

Updated the patch to include the NEWS entry.


> Also, to accept this contribution, we'd need a copyright assignment
> from you.  Would you like to start the paperwork for that at this
> time?  If yes, I will send you the form to fill and the instructions
> to go with it.
>

Yes, please send the form please.

-- 
"I object to doing things that computers can do." — Olin Shivers

[-- Attachment #1.2: Type: text/html, Size: 991 bytes --]

[-- Attachment #2: 0001-Add-sqlite-execute-batch-command.patch --]
[-- Type: text/x-patch, Size: 4247 bytes --]

From 396313cd1caaae71ef215aa9f509c2a4f0e975db Mon Sep 17 00:00:00 2001
From: Javier Olaechea <pirata@gmail.com>
Date: Sun, 31 Mar 2024 23:07:10 -0500
Subject: [PATCH] Add sqlite-execute-batch command

This command is similar to sqlite-execute except that it executes
multiple statements in exchange for not accepting any arguments.

* doc/lispref/text.texi (Database): Document it.
* src/sqlite.c (Fsqlite_execute_batch): Add sqlite_execute_batch
command.  It is similar to sqlite-execute but it executes all the
statements in the query.  Unlike sqlite-execute the command doesn't take
any arguments to pass down to the statements.
* 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    |  8 ++++++++
 etc/NEWS                 |  6 ++++++
 src/sqlite.c             | 12 ++++++++++++
 test/src/sqlite-tests.el | 26 ++++++++++++++++++++++++++
 4 files changed, 52 insertions(+)

diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi
index 0cd4e2c614e..a1580789d58 100644
--- a/doc/lispref/text.texi
+++ b/doc/lispref/text.texi
@@ -5414,6 +5414,14 @@ Database
 
 @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 7a73815179c..c2a43408da2 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -424,6 +424,12 @@ Use 'TAB' in the minibuffer to show or hide the password.  Likewise,
 there is an icon on the mode-line, which toggles the visibility of the
 password when clicking with 'mouse-1'.
 
+** 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
 
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 @@ sqlite-returning
 		        '("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
-- 
2.29.2.154.g7f7ebe054a


  reply	other threads:[~2024-04-14  3:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-02 15:03 bug#70145: [PATCH] Add sqlite-execute-batch command Javier Olaechea
2024-04-02 16:21 ` Eli Zaretskii
2024-04-02 18:52   ` Javier Olaechea
2024-04-02 18:56     ` Eli Zaretskii
2024-04-02 19:10       ` Javier Olaechea
2024-04-03  1:56         ` J.P.
2024-04-03  2:42           ` Javier Olaechea
2024-04-13  8:03             ` Eli Zaretskii
2024-04-14  3:32               ` Javier Olaechea [this message]
2024-04-14  5:52                 ` Eli Zaretskii
2024-06-06  8:00                 ` Javier Olaechea
2024-06-06 10:08                 ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFVS=zD-vXTBskiS04VkhD-vUfNSPUF3yNjwSe+sqFjDmscPhw@mail.gmail.com' \
    --to=pirata@gmail.com \
    --cc=70145@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=jp@neverwas.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).