Skip to content

Commit

Permalink
rebasing by hand
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryTravis committed Jan 23, 2025
1 parent 7029c4a commit 3721d84
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
link.][11926]
- [Reducing helper methods in `Standard.Base.Meta`.][12031]
- [Added Table.Offset][12071]
- [Implemented Generic JDBC connections.][12073]
- [Added Column.Offset][12092]

[11926]: https://github.com/enso-org/enso/pull/11926
[12031]: https://github.com/enso-org/enso/pull/12031
[12071]: https://github.com/enso-org/enso/pull/12071
[12073]: https://github.com/enso-org/enso/pull/12073
[12092]: https://github.com/enso-org/enso/pull/12092

#### Enso Language & Runtime
Expand Down
13 changes: 11 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ val jnaVersion = "5.14.0"
val googleProtobufVersion = "3.25.1"
val shapelessVersion = "2.3.10"
val postgresVersion = "42.4.0"
val h2Version = "2.3.232"

// ============================================================================
// === Utility methods =====================================================
Expand Down Expand Up @@ -4642,7 +4643,8 @@ lazy val `enso-test-java-helpers` = project
Compile / packageBin / artifactPath :=
file("test/Base_Tests/polyglot/java/helpers.jar"),
libraryDependencies ++= Seq(
"org.graalvm.polyglot" % "polyglot" % graalMavenPackagesVersion % "provided"
"org.graalvm.polyglot" % "polyglot" % graalMavenPackagesVersion % "provided",
"com.h2database" % "h2" % h2Version
),
Compile / packageBin := Def.task {
val result = (Compile / packageBin).value
Expand All @@ -4652,7 +4654,14 @@ lazy val `enso-test-java-helpers` = project
)
secondaryLocations.foreach { target =>
IO.copyFile(primaryLocation, target)
}
}
val _ = StdBits
.copyDependencies(
file("test/Table_Tests/polyglot/java/"),
Seq(),
ignoreScalaLibrary = true
)
.value
result
}.value
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ type Subquery_Setup
type Context_Extension
## A recipe for building the extension.

After_Select
After_From
After_Where

Arguments:
- position: Determines where the extension code should be inserted.
The positions of common query parts are following:
Expand Down
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
2 changes: 2 additions & 0 deletions distribution/lib/Standard/Database/0.0.0-dev/src/Main.enso
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export project.Extensions.Upload_In_Memory_Table.delete_rows
export project.Extensions.Upload_In_Memory_Table.select_into_database_table
export project.Extensions.Upload_In_Memory_Table.update_rows

export project.JDBC.Generic_JDBC_Connection.Generic_JDBC_Connection

export project.SQL_Query.SQL_Query

export project.Update_Action.Update_Action
Loading

0 comments on commit 3721d84

Please sign in to comment.