16
16
import java .util .concurrent .ThreadFactory ;
17
17
import java .util .concurrent .locks .ReentrantLock ;
18
18
import java .util .logging .Logger ;
19
- import java .util .regex .Pattern ;
20
19
import org .duckdb .io .LimitedInputStream ;
21
20
22
21
public class DuckDBDriver implements java .sql .Driver {
@@ -30,17 +29,10 @@ public class DuckDBDriver implements java.sql.Driver {
30
29
31
30
static final String DUCKDB_URL_PREFIX = "jdbc:duckdb:" ;
32
31
static final String MEMORY_DB = ":memory:" ;
32
+ private static final String DUCKLAKE_URL_PREFIX = DUCKDB_URL_PREFIX + "ducklake:" ;
33
33
34
34
static final ScheduledThreadPoolExecutor scheduler ;
35
35
36
- private static final String DUCKLAKE_OPTION = "ducklake" ;
37
- private static final String DUCKLAKE_ALIAS_OPTION = "ducklake_alias" ;
38
- private static final Pattern DUCKLAKE_ALIAS_OPTION_PATTERN = Pattern .compile ("[a-zA-Z0-9_]+" );
39
- private static final String DUCKLAKE_URL_PREFIX = "ducklake:" ;
40
- private static final String DUCKLAKE_DEFAULT_DBNAME = MEMORY_DB + "ducklakemem" ;
41
- private static final LinkedHashSet <String > ducklakeInstances = new LinkedHashSet <>();
42
- private static final ReentrantLock ducklakeInitLock = new ReentrantLock ();
43
-
44
36
private static final LinkedHashMap <String , ByteBuffer > pinnedDbRefs = new LinkedHashMap <>();
45
37
private static final ReentrantLock pinnedDbRefsLock = new ReentrantLock ();
46
38
private static boolean pinnedDbRefsShutdownHookRegistered = false ;
@@ -108,35 +100,23 @@ public Connection connect(String url, Properties info) throws SQLException {
108
100
// to be established.
109
101
props .remove ("path" );
110
102
111
- // DuckLake options
112
- String ducklake = removeOption (props , DUCKLAKE_OPTION );
113
- String ducklakeAlias = removeOption (props , DUCKLAKE_ALIAS_OPTION , DUCKLAKE_OPTION );
114
- final String shortUrl ;
115
- if (null != ducklake ) {
103
+ // DuckLake connection
104
+ if (pp .shortUrl .startsWith (DUCKLAKE_URL_PREFIX )) {
116
105
setDefaultOptionValue (props , JDBC_PIN_DB , true );
117
106
setDefaultOptionValue (props , JDBC_STREAM_RESULTS , true );
118
- String dbName = dbNameFromUrl (pp .shortUrl );
119
- if (MEMORY_DB .equals (dbName )) {
120
- shortUrl = DUCKDB_URL_PREFIX + DUCKLAKE_DEFAULT_DBNAME ;
121
- } else {
122
- shortUrl = pp .shortUrl ;
123
- }
124
- } else {
125
- shortUrl = pp .shortUrl ;
126
107
}
127
108
128
109
// Pin DB option
129
110
String pinDbOptStr = removeOption (props , JDBC_PIN_DB );
130
111
boolean pinDBOpt = isStringTruish (pinDbOptStr , false );
131
112
132
113
// Create connection
133
- DuckDBConnection conn = DuckDBConnection .newConnection (shortUrl , readOnly , sf .origFileText , props );
114
+ DuckDBConnection conn = DuckDBConnection .newConnection (pp . shortUrl , readOnly , sf .origFileText , props );
134
115
135
116
// Run post-init
136
117
try {
137
- pinDB (pinDBOpt , shortUrl , conn );
138
- runSessionInitSQLFile (conn , url , sf );
139
- initDucklake (conn , shortUrl , ducklake , ducklakeAlias );
118
+ pinDB (pinDBOpt , pp .shortUrl , conn );
119
+ runSessionInitSQLFile (conn , pp .shortUrl , sf );
140
120
} catch (SQLException e ) {
141
121
closeQuietly (conn );
142
122
throw e ;
@@ -188,43 +168,6 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
188
168
throw new SQLFeatureNotSupportedException ("no logger" );
189
169
}
190
170
191
- private static void initDucklake (Connection conn , String url , String ducklake , String ducklakeAlias )
192
- throws SQLException {
193
- if (null == ducklake ) {
194
- return ;
195
- }
196
- ducklakeInitLock .lock ();
197
- try {
198
- String dbName = dbNameFromUrl (url );
199
- String key = dbName + "#" + ducklake ;
200
- if (!ducklakeInstances .contains (key )) {
201
- String attachQuery = createAttachQuery (ducklake , ducklakeAlias );
202
- try (Statement stmt = conn .createStatement ()) {
203
- stmt .execute ("INSTALL ducklake" );
204
- stmt .execute ("LOAD ducklake" );
205
- stmt .execute (attachQuery );
206
- }
207
- ducklakeInstances .add (key );
208
- }
209
- } finally {
210
- ducklakeInitLock .unlock ();
211
- }
212
- try (Statement stmt = conn .createStatement ()) {
213
- stmt .execute ("USE " + ducklakeAlias );
214
- }
215
- }
216
-
217
- private static String createAttachQuery (String ducklake , String ducklakeAlias ) throws SQLException {
218
- ducklake = ducklake .replaceAll ("'" , "''" );
219
- if (!ducklake .startsWith (DUCKLAKE_URL_PREFIX )) {
220
- ducklake = DUCKLAKE_URL_PREFIX + ducklake ;
221
- }
222
- if (!DUCKLAKE_ALIAS_OPTION_PATTERN .matcher (ducklakeAlias ).matches ()) {
223
- throw new SQLException ("Invalid DuckLake alias specified: " + ducklakeAlias );
224
- }
225
- return "ATTACH '" + ducklake + "' AS " + ducklakeAlias ;
226
- }
227
-
228
171
private static ParsedProps parsePropsFromUrl (String url ) throws SQLException {
229
172
if (!url .contains (";" )) {
230
173
return new ParsedProps (url );
0 commit comments