forked from finos/legend-pure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test connection execution support for duck db in pure runtime
- use dbtype in testdbconnection to switch between h2 and duckDB test connection in legend-pure connection manager - use singleton connection for duckdb
- Loading branch information
1 parent
96c6bea
commit 0a1fdfe
Showing
10 changed files
with
271 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ime/java/extension/store/relational/shared/connectionManager/DuckDBConnectionWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.pure.runtime.java.extension.store.relational.shared.connectionManager; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
public class DuckDBConnectionWrapper extends ConnectionWrapper | ||
{ | ||
Logger logger = LoggerFactory.getLogger(DuckDBConnectionWrapper.class); | ||
|
||
private int borrowedCounter; | ||
String user; | ||
|
||
public DuckDBConnectionWrapper(Connection connection, String user) | ||
{ | ||
super(connection); | ||
this.user = user; | ||
} | ||
|
||
public void incrementBorrowedCounter() | ||
{ | ||
borrowedCounter++; | ||
} | ||
|
||
private void decrementBorrowedCounter() | ||
{ | ||
borrowedCounter--; | ||
} | ||
|
||
@Override | ||
public void close() throws SQLException | ||
{ | ||
this.decrementBorrowedCounter(); | ||
//never actually close duck db connection and re-use same connection over | ||
// if (borrowedCounter <= 0) | ||
// { | ||
// this.closeConnection(); | ||
// } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...e/java/extension/store/relational/shared/connectionManager/TestDatabaseConnectDuckDB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.pure.runtime.java.extension.store.relational.shared.connectionManager; | ||
|
||
import org.finos.legend.pure.m3.exception.PureExecutionException; | ||
import org.finos.legend.pure.runtime.java.extension.store.relational.shared.ConnectionWithDataSourceInfo; | ||
import org.finos.legend.pure.runtime.java.extension.store.relational.shared.DataSource; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
|
||
public class TestDatabaseConnectDuckDB | ||
{ | ||
private DuckDBConnectionWrapper singletonConnection; // disabled thread-level and user-level segregation, | ||
public static final String TEST_DB_HOST_NAME = "local"; | ||
private static final String TEST_DB_NAME = "pure-duckDB-test-Db"; | ||
private static final DataSource TEST_DATA_SOURCE = new DataSource(TEST_DB_HOST_NAME, -1, TEST_DB_NAME, null); | ||
|
||
public TestDatabaseConnectDuckDB() | ||
{ | ||
try | ||
{ | ||
Class.forName("org.duckdb.DuckDBDriver"); | ||
} | ||
catch (ClassNotFoundException ignore) | ||
{ | ||
// ignore exception about not finding the duckDB driver | ||
} | ||
} | ||
|
||
public ConnectionWithDataSourceInfo getConnectionWithDataSourceInfo(String user) | ||
{ | ||
try | ||
{ | ||
if (this.singletonConnection == null || this.singletonConnection.isClosed()) | ||
{ | ||
Connection connection = DriverManager.getConnection(getConnectionURL()); | ||
//there is no way to configure timezone as jdbc url param or system properties | ||
connection.createStatement().execute("SET TimeZone = 'UTC'"); | ||
|
||
this.singletonConnection = new DuckDBConnectionWrapper(connection, user); | ||
} | ||
} | ||
catch (SQLException ex) | ||
{ | ||
throw new PureExecutionException("Unable to create TestDatabaseConnection of type: DuckDB for user: " + user + ", message: " + ex.getMessage(), ex); | ||
} | ||
this.singletonConnection.incrementBorrowedCounter(); | ||
return new ConnectionWithDataSourceInfo(this.singletonConnection, TEST_DATA_SOURCE, this.getClass().getSimpleName()); | ||
} | ||
|
||
|
||
private static String getConnectionURL() | ||
{ | ||
return "jdbc:duckdb:" + TEST_DB_NAME; | ||
} | ||
|
||
} |
Oops, something went wrong.