-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7029c4a
commit 3721d84
Showing
6 changed files
with
477 additions
and
2 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
239 changes: 239 additions & 0 deletions
239
distribution/lib/Standard/Database/0.0.0-dev/src/JDBC/Generic_JDBC_Connection.enso
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,239 @@ | ||
from Standard.Base import all | ||
from Standard.Table import Table | ||
|
||
import project.Internal.Column_Fetcher as Column_Fetcher_Module | ||
import project.Internal.JDBC_Connection | ||
import project.Internal.SQL_Warning_Helper | ||
import project.Internal.Statement_Setter.Statement_Setter | ||
import project.SQL_Statement.SQL_Statement | ||
from project.Internal.Result_Set import read_column, result_set_to_table | ||
|
||
polyglot java import java.sql.DatabaseMetaData | ||
|
||
type Generic_JDBC_Connection | ||
## PRIVATE | ||
|
||
A wrapper around a JDBC Connection. This can be used to access any | ||
JDBC-compliant database backend, including ones that do not have full | ||
support via `Connection`s. It provides schema information, and the | ||
ability to execute SQL | ||
queries and commands. | ||
|
||
Unlike regular fully-supported `Connection`s, query results are returned | ||
as in-memory `Table`s, rather than as `DB_Table`s. | ||
|
||
The JDBC driver must be available on the JVM classpath (for example, in a | ||
`polyglot/java` folder within the project). | ||
private Value jdbc_connection:JDBC_Connection.JDBC_Connection | ||
|
||
## ADVANCED | ||
GROUP Standard.Base.Input | ||
ICON data_input | ||
|
||
Connect to a database specified by a JDBC URL. | ||
|
||
Arguments: | ||
- url: The JDBC URL describing the database to connect to. | ||
|
||
> Example | ||
Connect to an H2 database instance. | ||
|
||
connection = Generic_JDBC_Connection.connect "jdbc:h2:~/my_database" | ||
connect url:Text -> Generic_JDBC_Connection = | ||
jdbc_connection = JDBC_Connection.create url [] | ||
Generic_JDBC_Connection.Value jdbc_connection | ||
|
||
## ADVANCED | ||
GROUP Standard.Base.Input | ||
ICON data_input | ||
|
||
Close the underlying JDBC connection. | ||
|
||
> Example | ||
Connect to an H2 database instance, then close the connection. | ||
|
||
connection = Generic_JDBC_Connection.connect "jdbc:h2:~/my_database" | ||
connection.close | ||
close self -> Nothing = | ||
self.jdbc_connection.close | ||
|
||
## ALIAS query, insert, update, delete | ||
GROUP Standard.Base.Output | ||
ICON data_output | ||
|
||
Execute a raw SQL query or statement, or an `SQL_Statement`. | ||
|
||
Returns the row count for a SQL DML statement, or 0 for SQL statements | ||
that return nothing. | ||
|
||
Arguments: | ||
- sql: The SQL query or statement or `SQL_Statement` to execute. | ||
|
||
> Example | ||
Create a table. | ||
|
||
connection = Generic_JDBC_Connection.connect "jdbc:h2:~/my_database" | ||
connection.execute "create table foo (a int)" | ||
execute self sql:(Text | SQL_Statement) -> Integer = | ||
self.jdbc_connection.with_prepared_statement sql Statement_Setter.default stmt-> | ||
stmt.executeUpdate | ||
|
||
## ALIAS import, load | ||
GROUP Standard.Base.Input | ||
ICON data_input | ||
|
||
Execute a raw SQL query, or an `SQL_Statement`.Generic_JDBC_Connection. | ||
|
||
Returns the resulting rows. | ||
|
||
Arguments: | ||
- sql: The SQL query or `SQL_Statement` to execute. | ||
|
||
> Example | ||
Query a table. | ||
|
||
connection = Generic_JDBC_Connection.connect "jdbc:h2:~/my_database" | ||
connection.execute "select a, b, c from foo" | ||
read self sql:(Text | SQL_Statement) -> Table = | ||
self.jdbc_connection.with_prepared_statement sql Statement_Setter.default stmt-> | ||
rs = stmt.executeQuery | ||
make_fallback_fetcher _ = | ||
Column_Fetcher_Module.fallback_fetcher | ||
table = result_set_to_table rs make_fallback_fetcher | ||
SQL_Warning_Helper.process_warnings stmt <| | ||
table | ||
|
||
## ALIAS catalog schema database | ||
GROUP Standard.Base.Metadata | ||
ICON metadata | ||
|
||
Returns a `Vector` of the available catalogs. | ||
|
||
> Example | ||
Get the available catalogs. | ||
|
||
connection = Generic_JDBC_Connection.connect "jdbc:h2:~/my_database" | ||
connection.get_catalogs | ||
get_catalogs self -> Vector = | ||
self.with_metadata metadata-> | ||
read_column metadata.getCatalogs "TABLE_CAT" | ||
|
||
## ALIAS catalog schema database | ||
GROUP Standard.Base.Metadata | ||
ICON metadata | ||
|
||
Returns a `Vector` of the available schemas. | ||
|
||
Arguments: | ||
- catalog: A pattern specifying the catalog or catalogs to get schemas | ||
for. If `catalog` is "", returns schemas that do not have a catalog. If | ||
`catalog` is Nothing, return scheams for all catalogs. | ||
- schema_pattern: A pattern specifying the schemas to return. If | ||
`schema_pattern` is Nothing, return all schemas. | ||
|
||
! Patterns | ||
|
||
Patterns can be literal strings, or can include wildcard characters. | ||
The wildcard characters are the same ones that are used in SQL `LIKE` | ||
clauses. | ||
|
||
Examples: | ||
- "A": matches the table "A" | ||
- "A_": matches the tables "AB" and "AC" | ||
- "A%": matches the tables "ABC" and "ADEF" | ||
|
||
> Example | ||
Get the available schemas. | ||
|
||
connection = Generic_JDBC_Connection.connect "jdbc:h2:~/my_database" | ||
connection.get_schemas | ||
get_schemas self catalog:(Text | Nothing)=Nothing schema_pattern:(Text | Nothing)=Nothing -> Vector = | ||
self.with_metadata metadata-> | ||
read_column (metadata.getSchemas catalog schema_pattern) "TABLE_SCHEM" | ||
|
||
## ALIAS catalog schema database table | ||
GROUP Standard.Base.Metadata | ||
ICON metadata | ||
|
||
Returns a `Vector` of the available tables. | ||
|
||
Arguments: | ||
- catalog: A pattern specifying the catalog or catalogs to get tables | ||
for. If `catalog` is "", returns tables that do not have a catalog. If | ||
`catalog` is Nothing, return tables for all catalogs. | ||
- schema_pattern: A pattern specifying the schemas to get tables for. If | ||
`schema_pattern` is Nothing, return tables for all schemas. | ||
- table_name_pattern: A pattern specifying the tables to return. If | ||
`table_name_pattern` is Nothing, return all tables. | ||
- table_types: A `Vector` of table type names to include. If | ||
`table_types` is Nothing, return all types of tables. | ||
|
||
! Patterns | ||
|
||
Patterns can be literal strings, or can include wildcard characters. | ||
The wildcard characters are the same ones that are used in SQL `LIKE` | ||
clauses. | ||
|
||
Examples: | ||
- "A": matches the table "A" | ||
- "A_": matches the tables "AB" and "AC" | ||
- "A%": matches the tables "ABC" and "ADEF" | ||
|
||
> Example | ||
Get the available schemas. | ||
|
||
connection = Generic_JDBC_Connection.connect "jdbc:h2:~/my_database" | ||
connection.get_tables | ||
get_tables self catalog:(Text | Nothing)=Nothing schema_pattern:(Text | Nothing)="" table_name_pattern:(Text | Nothing)=Nothing table_types:(Vector | Nothing)=Nothing -> Vector = | ||
self.with_metadata metadata-> | ||
read_column (metadata.getTables catalog schema_pattern table_name_pattern table_types) "TABLE_NAME" | ||
|
||
## ALIAS catalog schema database table | ||
GROUP Standard.Base.Metadata | ||
ICON metadata | ||
|
||
Returns a `Table` containing metadata about tables. The table contains | ||
columns for table catalog, schema, name, type and more. See | ||
`jdbc.sql.DatabaseMetaData` for a full list of columns. | ||
|
||
Arguments: | ||
- catalog: A pattern specifying the catalog or catalogs to get tables | ||
for. If `catalog` is "", returns tables that do not have a catalog. If | ||
`catalog` is Nothing, return tables for all catalogs. | ||
- schema_pattern: A pattern specifying the schemas to get tables for. If | ||
`schema_pattern` is Nothing, return tables for all schemas. | ||
- table_name_pattern: A pattern specifying the tables to return. If | ||
`table_name_pattern` is Nothing, return all tables. | ||
- table_types: A `Vector` of table type names to include. If | ||
`table_types` is Nothing, return all types of tables. | ||
|
||
! Patterns | ||
|
||
Patterns can be literal strings, or can include wildcard characters. | ||
The wildcard characters are the same ones that are used in SQL `LIKE` | ||
clauses. | ||
|
||
Examples: | ||
- "A": matches the table "A" | ||
- "A_": matches the tables "AB" and "AC" | ||
- "A%": matches the tables "ABC" and "ADEF" | ||
|
||
> Example | ||
Get the available schemas. | ||
|
||
connection = Generic_JDBC_Connection.connect "jdbc:h2:~/my_database" | ||
connection.get_table_info | ||
get_table_info self catalog:(Text | Nothing)=Nothing schema_pattern:(Text | Nothing)="" table_name_pattern:(Text | Nothing)=Nothing table_types:(Text | Nothing)=Nothing -> Table = | ||
rs_to_table_ (self.with_metadata (m-> m.getTables catalog schema_pattern table_name_pattern table_types)) | ||
|
||
## PRIVATE | ||
Execute the function with the database metadata. | ||
with_metadata self f:(DatabaseMetaData -> Any) -> Any = | ||
self.jdbc_connection.with_metadata f | ||
|
||
## PRVIATE | ||
Convert a JDBC ResultSet to a Table using the fallback fetcher. | ||
private rs_to_table_ rs = | ||
make_fallback_fetcher _ = | ||
Column_Fetcher_Module.fallback_fetcher | ||
result_set_to_table rs make_fallback_fetcher |
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
Oops, something went wrong.