Commits

Grace Batumbya committed e3091a4

Added support for URI filenames.
Fixes #42: Using shared in-memory with URI filename.

  • Participants
  • Parent commits d1715e3
  • Tags 3.7.15-SNAPSHOT-1-win-linux

Comments (0)

Files changed (5)

 Work in progress:
+* Added support for URI filenames
+    1. <http://www.sqlite.org/uri.html> examples <http://www.sqlite.org/c3ref/open.html#urifilenameexamples>
+* Issue 42 - Using shared in-memory with URI filename
 * Issue 47 - MetaData.getExportedKeys() returns empty string for a named foreign key
 * Issue 46 - org.sqlite.MetaData.getExportedKeys constructs incorrect SQL
 * Issue 44 - getExportedKeys throws Null Exception for foreign key without explicit primary column name

src/main/java/org/sqlite/Conn.java

         this.openModeFlags = config.getOpenModeFlags();
         open(openModeFlags);
 
-        boolean enableSharedCache = config.isEnabledSharedCache();
-        boolean enableLoadExtension = config.isEnabledLoadExtension();
-        db.shared_cache(enableSharedCache);
-        db.enable_load_extension(enableLoadExtension);
+        if (fileName.startsWith("file:") && !fileName.contains("cache="))
+        {   // URI cache overrides flags
+            db.shared_cache(config.isEnabledSharedCache());
+        }
+        db.enable_load_extension(config.isEnabledLoadExtension());
 
         this.transactionMode = config.getTransactionMode();
 
      */
     private void open(int openModeFlags) throws SQLException {
         // check the path to the file exists
-        if (!":memory:".equals(fileName)) {
+        if (!":memory:".equals(fileName) && !fileName.startsWith("file:") && !fileName.contains("mode=memory")) {
             if (fileName.startsWith(RESOURCE_NAME_PREFIX)) {
                 String resourceName = fileName.substring(RESOURCE_NAME_PREFIX.length());
 

src/main/java/org/sqlite/SQLiteConfig.java

             setOpenMode(SQLiteOpenMode.READWRITE);
             setOpenMode(SQLiteOpenMode.CREATE);
         }
+        openMode = pragmaTable.getProperty(Pragma.SHARED_CACHE.pragmaName);
+        setOpenMode(SQLiteOpenMode.OPEN_URI); // Enable URI filenames
+
         transactionMode = TransactionMode.getMode(
             prop.getProperty(Pragma.TRANSACTION_MODE.pragmaName, TransactionMode.DEFFERED.name()));
     }

src/main/java/org/sqlite/SQLiteOpenMode.java

     CREATE(0x00000004), /* Ok for int SQLITE3_open_v2() */
     DELETEONCLOSE(0x00000008), /* VFS only */
     EXCLUSIVE(0x00000010), /* VFS only */
+    OPEN_URI(0x00000040), /* Ok for sqlite3_open_v2() */
+    OPEN_MEMORY(0x00000080), /* Ok for sqlite3_open_v2() */
     MAIN_DB(0x00000100), /* VFS only */
     TEMP_DB(0x00000200), /* VFS only */
     TRANSIENT_DB(0x00000400), /* VFS only */

src/test/java/org/sqlite/ConnectionTest.java

         return tmp;
     }
 
+    @Test
+    public void URIFilenames() throws SQLException {
+        Connection conn1 = DriverManager.getConnection("jdbc:sqlite:file:memdb1?mode=memory&cache=shared");
+        Statement stmt1 = conn1.createStatement();
+        stmt1.executeUpdate("create table tbl (col int)");
+        stmt1.executeUpdate("insert into tbl values(100)");
+        stmt1.close();
+
+        Connection conn2 = DriverManager.getConnection("jdbc:sqlite:file:memdb1?mode=memory&cache=shared");
+        Statement stmt2 = conn2.createStatement();
+        ResultSet rs = stmt2.executeQuery("select * from tbl");
+        assertTrue(rs.next());
+        assertEquals(100, rs.getInt(1));
+        stmt2.close();
+
+        Connection conn3 = DriverManager.getConnection("jdbc:sqlite:file::memory:?cache=shared");
+        Statement stmt3 = conn3.createStatement();
+        stmt3.executeUpdate("attach 'file:memdb1?mode=memory&cache=shared' as memdb1");
+        rs = stmt3.executeQuery("select * from memdb1.tbl");
+        assertTrue(rs.next());
+        assertEquals(100, rs.getInt(1));
+        stmt3.executeUpdate("create table tbl2(col int)");
+        stmt3.executeUpdate("insert into tbl2 values(200)");
+        stmt3.close();
+
+        Connection conn4 = DriverManager.getConnection("jdbc:sqlite:file::memory:?cache=shared");
+        Statement stmt4 = conn4.createStatement();
+        rs = stmt4.executeQuery("select * from tbl2");
+        assertTrue(rs.next());
+        assertEquals(200, rs.getInt(1));
+        
+    }
 }