emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

scratch/sqlite 75b43c5: Add support for more in-memory instances, and pr


From: Lars Ingebrigtsen
Subject: scratch/sqlite 75b43c5: Add support for more in-memory instances, and print better
Date: Tue, 7 Dec 2021 01:13:34 -0500 (EST)

branch: scratch/sqlite
commit 75b43c5a2056d0b552fb51c9b5e2e7a6726a3891
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Add support for more in-memory instances, and print better
---
 src/lisp.h               |  1 +
 src/print.c              |  4 +++-
 src/sqlite.c             | 23 ++++++++++++++++++-----
 test/src/sqlite-tests.el | 14 ++++++++++++++
 4 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/src/lisp.h b/src/lisp.h
index 0ce1b09..92ab05b 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -2576,6 +2576,7 @@ struct Lisp_Sqlite
   union vectorlike_header header;
   void *db;
   void *stmt;
+  char *name;
   void (*finalizer) (void *);
   bool eof;
   bool is_statement;
diff --git a/src/print.c b/src/print.c
index b32580c..63c91b7 100644
--- a/src/print.c
+++ b/src/print.c
@@ -1878,7 +1878,9 @@ print_vectorlike (Lisp_Object obj, Lisp_Object 
printcharfun, bool escapeflag,
     case PVEC_SQLITE:
       {
        print_c_string ("#<sqlite ", printcharfun);
-       int i = sprintf (buf, "ptr=%p", XSQLITE (obj)->db);
+       int i = sprintf (buf, "db=%p", XSQLITE (obj)->db);
+       strout (buf, i, i, printcharfun);
+       i = sprintf (buf, " name=%s", XSQLITE (obj)->name);
        strout (buf, i, i, printcharfun);
        printchar ('>', printcharfun);
       }
diff --git a/src/sqlite.c b/src/sqlite.c
index abffa12..5000eb2 100644
--- a/src/sqlite.c
+++ b/src/sqlite.c
@@ -38,47 +38,60 @@ sqlite_free (void *arg)
     sqlite3_finalize (ptr->stmt);
   else if (ptr->db)
     sqlite3_close (ptr->db);
+  xfree (ptr->name);
   xfree (ptr);
 }
 
 static Lisp_Object
-make_sqlite (bool is_statement, void *db, void *stmt)
+make_sqlite (bool is_statement, void *db, void *stmt, char *name)
 {
   struct Lisp_Sqlite *ptr
     = ALLOCATE_PLAIN_PSEUDOVECTOR (struct Lisp_Sqlite, PVEC_SQLITE);
   ptr->is_statement = is_statement;
   ptr->finalizer = sqlite_free;
   ptr->db = db;
+  ptr->name = name;
   ptr->stmt = stmt;
   ptr->eof = false;
   return make_lisp_ptr (ptr, Lisp_Vectorlike);
 }
 
+static int db_count = 0;
+
 DEFUN ("sqlite-open", Fsqlite_open, Ssqlite_open, 0, 1, 0,
        doc: /* Open FILE as an sqlite database.
 If FILE is nil, an in-memory database will be opened instead.  */)
   (Lisp_Object file)
 {
+  char *name;
+
   if (!NILP (file))
     {
       CHECK_STRING (file);
-      file = Fexpand_file_name (file, Qnil);
+      name = xstrdup (SSDATA (Fexpand_file_name (file, Qnil)));
+    }
+  else
+    {
+      name = xstrdup (SSDATA (CALLN (Fformat, build_string (":memory:%d"),
+                                    make_int (++db_count))));
     }
 
   sqlite3 *sdb;
-  int ret = sqlite3_open_v2 (NILP (file) ? ":memory:" : SSDATA (file),
+  int ret = sqlite3_open_v2 (name,
                             &sdb,
                             SQLITE_OPEN_FULLMUTEX
                             | SQLITE_OPEN_READWRITE
                             | SQLITE_OPEN_CREATE
+                            | (NILP (file) ? SQLITE_OPEN_MEMORY : 0)
 #ifdef SQLITE_OPEN_URI
                             | SQLITE_OPEN_URI
 #endif
                             | 0, NULL);
+
   if (ret != SQLITE_OK)
     return Qnil;
 
-  return make_sqlite (false, sdb, NULL);
+  return make_sqlite (false, sdb, NULL, name);
 }
 
 /* Bind values in a statement like
@@ -323,7 +336,7 @@ which means that we return a set object that can be queried 
with
 
   if (EQ (return_type, Qset))
     {
-      retval = make_sqlite (true, db, stmt);
+      retval = make_sqlite (true, db, stmt, XSQLITE (db)->name);
       goto exit;
     }
 
diff --git a/test/src/sqlite-tests.el b/test/src/sqlite-tests.el
index eaae159..ae08710 100644
--- a/test/src/sqlite-tests.el
+++ b/test/src/sqlite-tests.el
@@ -159,4 +159,18 @@
                   (sqlite-select db "select col1 from test5 where col2 = 2"))))
         (should (equal out string))))))
 
+(ert-deftest sqlite-different-dbs ()
+  (skip-unless (sqlite-available-p))
+  (let (db1 db2)
+    (setq db1 (sqlite-open))
+    (setq db2 (sqlite-open))
+    (sqlite-execute
+     db1 "create table if not exists test6 (col1 text, col2 number)")
+    (sqlite-execute
+     db2 "create table if not exists test6 (col1 text, col2 number)")
+    (sqlite-execute
+     db1 "insert into test6 values (?, ?)" '("foo" 2))
+    (should (sqlite-select db1 "select * from test6"))
+    (should-not (sqlite-select db2 "select * from test6"))))
+
 ;;; sqlite-tests.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]