1616import java .util .concurrent .ThreadFactory ;
1717import java .util .concurrent .locks .ReentrantLock ;
1818import java .util .logging .Logger ;
19- import java .util .regex .Pattern ;
2019import org .duckdb .io .LimitedInputStream ;
2120
2221public class DuckDBDriver implements java .sql .Driver {
@@ -30,17 +29,10 @@ public class DuckDBDriver implements java.sql.Driver {
3029
3130 static final String DUCKDB_URL_PREFIX = "jdbc:duckdb:" ;
3231 static final String MEMORY_DB = ":memory:" ;
32+ private static final String DUCKLAKE_URL_PREFIX = DUCKDB_URL_PREFIX + "ducklake:" ;
3333
3434 static final ScheduledThreadPoolExecutor scheduler ;
3535
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-
4436 private static final LinkedHashMap <String , ByteBuffer > pinnedDbRefs = new LinkedHashMap <>();
4537 private static final ReentrantLock pinnedDbRefsLock = new ReentrantLock ();
4638 private static boolean pinnedDbRefsShutdownHookRegistered = false ;
@@ -108,35 +100,23 @@ public Connection connect(String url, Properties info) throws SQLException {
108100 // to be established.
109101 props .remove ("path" );
110102
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 )) {
116105 setDefaultOptionValue (props , JDBC_PIN_DB , true );
117106 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 ;
126107 }
127108
128109 // Pin DB option
129110 String pinDbOptStr = removeOption (props , JDBC_PIN_DB );
130111 boolean pinDBOpt = isStringTruish (pinDbOptStr , false );
131112
132113 // Create connection
133- DuckDBConnection conn = DuckDBConnection .newConnection (shortUrl , readOnly , sf .origFileText , props );
114+ DuckDBConnection conn = DuckDBConnection .newConnection (pp . shortUrl , readOnly , sf .origFileText , props );
134115
135116 // Run post-init
136117 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 );
140120 } catch (SQLException e ) {
141121 closeQuietly (conn );
142122 throw e ;
@@ -188,43 +168,6 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
188168 throw new SQLFeatureNotSupportedException ("no logger" );
189169 }
190170
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-
228171 private static ParsedProps parsePropsFromUrl (String url ) throws SQLException {
229172 if (!url .contains (";" )) {
230173 return new ParsedProps (url );
0 commit comments