Skip to content

Commit c732b72

Browse files
committed
Support memory DBs in URL without leading colon
Minor change, to allow passing named in-memory connections without double colon prefix. So the following URLs will point to the same in-memory DB: ``` jdbc:duckdb::memory:tag1 jdbc:duckdb:memory:tag1 ``` Testing: new test added that check that DB instance cache works correctly with and without second colon in URL.
1 parent 2007bd0 commit c732b72

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/main/java/org/duckdb/DuckDBConnection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public static DuckDBConnection newConnection(String url, boolean readOnly, Prope
5454
if (db_dir.length() == 0) {
5555
db_dir = ":memory:";
5656
}
57+
if (db_dir.startsWith("memory:")) {
58+
db_dir = ":" + db_dir;
59+
}
5760
ByteBuffer nativeReference = DuckDBNative.duckdb_jdbc_startup(db_dir.getBytes(UTF_8), readOnly, properties);
5861
return new DuckDBConnection(nativeReference, url, readOnly);
5962
}

src/test/java/org/duckdb/TestDuckDBJDBC.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3484,6 +3484,26 @@ public QueryProgress call() throws Exception {
34843484
}
34853485
}
34863486

3487+
public static void test_memory_colon() throws Exception {
3488+
try (Connection conn1 = DriverManager.getConnection("jdbc:duckdb::memory:");
3489+
Statement stmt1 = conn1.createStatement();
3490+
Connection conn2 = DriverManager.getConnection("jdbc:duckdb:memory:");
3491+
Statement stmt2 = conn2.createStatement(); Statement stmt22 = conn2.createStatement()) {
3492+
stmt1.execute("CREATE TABLE tab1(col1 int)");
3493+
assertThrows(() -> { stmt2.execute("DROP TABLE tab1"); }, SQLException.class);
3494+
stmt22.execute("CREATE TABLE tab1(col1 int)");
3495+
}
3496+
try (Connection conn1 = DriverManager.getConnection("jdbc:duckdb::memory:tag1");
3497+
Statement stmt1 = conn1.createStatement(); Statement stmt12 = conn1.createStatement();
3498+
Connection conn2 = DriverManager.getConnection("jdbc:duckdb:memory:tag1");
3499+
Statement stmt2 = conn2.createStatement()) {
3500+
stmt1.execute("CREATE TABLE tab1(col1 int)");
3501+
stmt2.execute("DROP TABLE tab1");
3502+
assertThrows(() -> { stmt1.execute("DROP TABLE tab1"); }, SQLException.class);
3503+
stmt12.execute("CREATE TABLE tab1(col1 int)");
3504+
}
3505+
}
3506+
34873507
public static void main(String[] args) throws Exception {
34883508
String arg1 = args.length > 0 ? args[0] : "";
34893509
final int statusCode;

0 commit comments

Comments
 (0)