Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.daanse.sql.dialect.db.testsupport.IndexRoundTrip;
import org.eclipse.daanse.sql.dialect.db.testsupport.RoutineRoundTrip;
import org.eclipse.daanse.sql.dialect.db.testsupport.UserRoundTrip;
import org.eclipse.daanse.sql.jdbc.api.MetadataScope;
import org.eclipse.daanse.sql.jdbc.api.meta.StructureInfo;
import org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal;
import org.eclipse.daanse.sql.jdbc.api.schema.ImportedKey;
Expand All @@ -51,6 +52,11 @@
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

/**
* The statements of the Oracle dialect, run in a container and read back. The container's user
* {@code rt} writes and reads its own schema with plain rights, so its provider reads in
* {@link MetadataScope#USER}; SYSTEM reads the whole database.
*/
@Testcontainers
@EnabledIfSystemProperty(named = "integration.docker", matches = "true")
@TestInstance(Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -156,7 +162,7 @@ void materialized_view_is_created_read_back_and_dropped() throws SQLException {
TableReference view = new TableReference(Optional.of(new SchemaReference(Optional.empty(), schema)),
"MV_USERS", TableReference.TYPE_VIEW);
String query = "SELECT id, name FROM \"" + schema + "\".\"USERS\" WHERE id <= 5";
OracleMetadataProvider provider = new OracleMetadataProvider();
OracleMetadataProvider provider = new OracleMetadataProvider(MetadataScope.USER);
String create = dialect.ddlGenerator().createMaterializedView(view, query, false).orElseThrow();
String createIfNotExists = dialect.ddlGenerator().createMaterializedView(view, query, true).orElseThrow();
String drop = dialect.ddlGenerator().dropMaterializedView(view, false).orElseThrow();
Expand All @@ -183,8 +189,8 @@ void materialized_view_is_created_read_back_and_dropped() throws SQLException {
/** An index the dialect writes with a descending column is read back with the order of each column. */
@Test
void descending_index_column_is_created_and_read_back() throws SQLException {
IndexRoundTrip.assertDescendingIndexRoundTrip(conn, dialect.ddlGenerator(), new OracleMetadataProvider(), users,
"IX_USERS_DESC", "NAME", "ID");
IndexRoundTrip.assertDescendingIndexRoundTrip(conn, dialect.ddlGenerator(),
new OracleMetadataProvider(MetadataScope.USER), users, "IX_USERS_DESC", "NAME", "ID");
}

/**
Expand All @@ -210,7 +216,7 @@ void deferrable_foreign_key_is_checked_at_commit_and_read_back() throws SQLExcep
} finally {
conn.setAutoCommit(true);
}
assertThat(new OracleMetadataProvider().getAllImportedKeys(conn, null, schema).orElseThrow())
assertThat(new OracleMetadataProvider(MetadataScope.USER).getAllImportedKeys(conn, null, schema).orElseThrow())
.filteredOn(key -> "FK_DC_DP".equals(key.name())).singleElement()
.satisfies(key -> assertThat(key.deferrability()).isEqualTo(ImportedKey.Deferrability.INITIALLY_DEFERRED));
try (Statement s = conn.createStatement()) {
Expand All @@ -229,7 +235,7 @@ void view_comments_are_written_and_read_back() throws SQLException {
TableReference view = new TableReference(Optional.of(new SchemaReference(Optional.empty(), schema)),
"V_NOTED", TableReference.TYPE_VIEW);
DatabaseServiceImpl service = new DatabaseServiceImpl();
OracleMetadataProvider provider = new OracleMetadataProvider();
OracleMetadataProvider provider = new OracleMetadataProvider(MetadataScope.USER);
try (Statement s = conn.createStatement()) {
s.execute("CREATE VIEW \"" + schema + "\".\"V_NOTED\" AS SELECT ID, NAME FROM \"" + schema + "\".\"USERS\"");
s.execute(dialect.ddlGenerator().commentOnTable(view, "the view's comment").orElseThrow());
Expand Down Expand Up @@ -261,9 +267,10 @@ void routines_are_created_called_read_back_and_dropped() throws SQLException {
List.of(RoutineParameter.in("N", "NUMBER"), new RoutineParameter("TOTAL", ParameterMode.INOUT,
"NUMBER")),
null, "plsql", "BEGIN TOTAL := TOTAL + N; END;");
RoutineRoundTrip.assertRoutineRoundTrip(conn, dialect.ddlGenerator(), new OracleMetadataProvider(), function,
procedure, "SELECT \"" + schema + "\".\"RT_ADD\"(2, 3) FROM DUAL", 5,
List.of("n IN", "total INOUT"), "RETURN A + B", "TOTAL := TOTAL + N");
RoutineRoundTrip.assertRoutineRoundTrip(conn, dialect.ddlGenerator(),
new OracleMetadataProvider(MetadataScope.USER), function, procedure,
"SELECT \"" + schema + "\".\"RT_ADD\"(2, 3) FROM DUAL", 5, List.of("n IN", "total INOUT"),
"RETURN A + B", "TOTAL := TOTAL + N");
}

/** A role the dialect writes is created, again where it can say so, read back and dropped. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void measure() throws SQLException {

private void runMetaInfo(DataSource ds) throws SQLException {
try (Connection c = ds.getConnection()) {
service.createMetaInfo(c, new org.eclipse.daanse.sql.jdbc.metadata.OracleMetadataProvider());
service.createMetaInfo(c, new org.eclipse.daanse.sql.jdbc.metadata.OracleMetadataProvider(
org.eclipse.daanse.sql.jdbc.api.MetadataScope.USER));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@
import org.eclipse.daanse.sql.jdbc.api.schema.VersionColumn;
import org.eclipse.daanse.sql.jdbc.api.schema.ViewDefinition;

/**
* Reads the metadata of one database engine from its system catalog, where JDBC's
* {@link java.sql.DatabaseMetaData} reads too little or too slowly. A method that returns
* {@link Optional#empty()} leaves the reading to JDBC.
* <p>
* A provider reads in one {@link MetadataScope}: the whole database ({@link MetadataScope#DATABASE},
* the default) or what the connected user may see ({@link MetadataScope#USER}). The scope is a
* property of the provider, not of a method: {@link #withScope(MetadataScope)} gives the provider for
* another scope, and every method of it reads in that scope. In {@link MetadataScope#DATABASE} a
* method may need rights the connection lacks; {@link #effectiveScope(Connection)} says beforehand
* whether it has them.
*/
public interface MetadataProvider {

/**
Expand All @@ -57,6 +69,51 @@ public interface MetadataProvider {
MetadataProvider EMPTY = new MetadataProvider() {
};

/**
* This provider reading in {@code scope}. A provider that reads one way only, or is in
* {@code scope} already, returns itself.
*
* @param scope the scope to read in
* @return the provider for {@code scope}
*/
default MetadataProvider withScope(MetadataScope scope) {
return this;
}

/**
* The scope this provider was asked to read in.
*
* @return the scope {@link #withScope(MetadataScope)} gave it; {@link MetadataScope#DATABASE} by
* default and for a provider that reads one way only
*/
default MetadataScope scope() {
return MetadataScope.DATABASE;
}

/**
* The scope this provider reads in on {@code connection}: the one it was asked for, or
* {@link MetadataScope#USER} where {@link MetadataScope#DATABASE} was asked for and the
* connection may not read what that needs ({@link #scopeRequirement()}). Asked once before a
* snapshot, which is then read in the scope given, as a whole.
*
* @param connection the connection to read through
* @return the scope the connection may read in
* @throws SQLException on database access error
*/
default MetadataScope effectiveScope(Connection connection) throws SQLException {
return scope();
}

/**
* The right a connection needs to read in {@link MetadataScope#DATABASE}, in words, as a message
* names it: {@code "none"} where every connection may.
*
* @return the right, such as {@code "SELECT_CATALOG_ROLE or SELECT ANY DICTIONARY"}
*/
default String scopeRequirement() {
return "none";
}

/**
* @param catalog the catalog name, or null
* @return the index info list, or Optional.empty() to fall back to standard
Expand Down Expand Up @@ -258,7 +315,11 @@ default Optional<List<CatalogReference>> getAllCatalogs(Connection connection) t

/**
* Bulk alternative to
* {@link java.sql.DatabaseMetaData#getSchemas(String, String)}.
* {@link java.sql.DatabaseMetaData#getSchemas(String, String)}: the schemas of the provider's
* scope, in a database that keeps its schemas as catalogs (MySQL, MariaDB) these catalogs. A
* snapshot reads the tables, routines and grants of these schemas; empty leaves the list to JDBC.
*
* @param catalog the catalog name, or {@code null} for the database of the connection
*/
default Optional<List<SchemaReference>> getAllSchemas(Connection connection, String catalog) throws SQLException {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,16 @@ public interface MetadataProviderFactory {
*/
boolean supports(String databaseProductName);

/** @return the provider, reading in {@link MetadataScope#DATABASE} */
MetadataProvider createProvider();

/**
* The provider for {@code scope}, as {@link MetadataProvider#withScope(MetadataScope)} gives it.
*
* @param scope the scope to read in
* @return the provider for {@code scope}
*/
default MetadataProvider createProvider(MetadataScope scope) {
return createProvider().withScope(scope);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SmartCity Jena - initial
* Stefan Bischof (bipolis.org) - initial
*/
package org.eclipse.daanse.sql.jdbc.api;

/**
* From whose point of view a database is read. A {@link MetadataProvider} reads in one scope; a
* snapshot is taken in one scope as a whole, never part in one and part in the other.
*/
public enum MetadataScope {

/**
* The whole database of the connection, as its administrator sees it: every schema with every
* table, view, key, constraint, index, trigger and routine, every user and role, every
* membership and every grant. The connection needs the right to read the whole catalog
* ({@link MetadataProvider#scopeRequirement()}); where it lacks it, the provider reads in
* {@link #USER} instead.
*/
DATABASE,

/**
* What the connected user may see: the schemas and objects it has a right on, also in schemas
* of others, the roles it holds, its memberships and the grants that concern it. What it may
* not see is left out, without an error.
*/
USER
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SmartCity Jena - initial
* Stefan Bischof (bipolis.org) - initial
*/
package org.eclipse.daanse.sql.jdbc.api;

import java.io.Serial;
import java.sql.SQLException;

/**
* A snapshot was asked for in a scope the connection may not read, and the caller wanted no
* fallback: thrown before anything is read. Names the database product, the scope asked for, the
* scope the connection may read and the right it lacks.
*/
public class MetadataScopeException extends SQLException {

@Serial
private static final long serialVersionUID = 1L;

private final String product;
private final MetadataScope requested;
private final MetadataScope available;
private final String requirement;

/**
* @param product the database product, as the driver names it
* @param requested the scope asked for
* @param available the scope the connection may read
* @param requirement the right the connection lacks for {@code requested}, in words
*/
public MetadataScopeException(String product, MetadataScope requested, MetadataScope available,
String requirement) {
super(message(product, requested, available, requirement));
this.product = product;
this.requested = requested;
this.available = available;
this.requirement = requirement;
}

private static String message(String product, MetadataScope requested, MetadataScope available,
String requirement) {
return "The connection to " + product + " may not read the metadata in scope " + requested
+ ", only in scope " + available + "; scope " + requested + " needs " + requirement;
}

/** @return the database product, as the driver names it */
public String product() {
return product;
}

/** @return the scope asked for */
public MetadataScope requested() {
return requested;
}

/** @return the scope the connection may read */
public MetadataScope available() {
return available;
}

/** @return the right the connection lacks for the scope asked for, in words */
public String requirement() {
return requirement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,51 @@ default MetaInfo createMetaInfo(DataSource dataSource, MetadataProvider metadata
default MetaInfo createMetaInfo(Connection connection, MetadataProvider metadataProvider) throws SQLException {
return createMetaInfo(connection);
}

/**
* A snapshot in the scope of {@code metadataProvider} ({@link MetadataProvider#scope()}). Where
* the connection may not read in that scope ({@link MetadataProvider#effectiveScope(Connection)}),
* the snapshot is read as a whole in the scope the connection may read, and
* {@link org.eclipse.daanse.sql.jdbc.api.meta.StructureInfo#requestedScope()} and
* {@link org.eclipse.daanse.sql.jdbc.api.meta.StructureInfo#scope()} tell the two apart; with
* {@code strict}, a {@link MetadataScopeException} is thrown instead, before anything is read.
* The default implementation checks the scope and delegates to
* {@link #createMetaInfo(Connection, MetadataProvider)}.
*
* @param connection caller-managed connection (not closed by this method)
* @param metadataProvider dialect-specific provider, in the scope to read in
* @param strict whether to fail instead of reading in another scope
* @return MetaInfo snapshot
* @throws MetadataScopeException where {@code strict} and the connection may not read in the
* scope of {@code metadataProvider}
* @throws SQLException on database access error
*/
default MetaInfo createMetaInfo(Connection connection, MetadataProvider metadataProvider, boolean strict)
throws SQLException {
if (strict) {
MetadataScope effective = metadataProvider.effectiveScope(connection);
if (effective != metadataProvider.scope()) {
throw new MetadataScopeException(connection.getMetaData().getDatabaseProductName(),
metadataProvider.scope(), effective, metadataProvider.scopeRequirement());
}
}
return createMetaInfo(connection, metadataProvider);
}

/**
* As {@link #createMetaInfo(Connection, MetadataProvider, boolean)}, on a connection borrowed
* from {@code dataSource} and released at once.
*
* @param dataSource pooled or unpooled source
* @param metadataProvider dialect-specific provider, in the scope to read in
* @param strict whether to fail instead of reading in another scope
* @return MetaInfo snapshot
* @throws SQLException on database access error
*/
default MetaInfo createMetaInfo(DataSource dataSource, MetadataProvider metadataProvider, boolean strict)
throws SQLException {
try (Connection connection = dataSource.getConnection()) {
return createMetaInfo(connection, metadataProvider, strict);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.List;

import org.eclipse.daanse.sql.jdbc.api.MetadataScope;
import org.eclipse.daanse.sql.model.schema.CatalogReference;
import org.eclipse.daanse.sql.jdbc.api.schema.CheckConstraint;
import org.eclipse.daanse.sql.model.schema.ColumnDefinition;
Expand Down Expand Up @@ -126,4 +127,25 @@ default List<org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal> principal
return List.of();
}

/**
* The scope the snapshot was asked for.
*
* @return the scope asked for; {@link MetadataScope#DATABASE} where none was, as for a snapshot
* read through JDBC alone, whose driver decides what it shows
*/
default MetadataScope requestedScope() {
return MetadataScope.DATABASE;
}

/**
* The scope the snapshot was read in: the one asked for, or {@link MetadataScope#USER} where
* {@link MetadataScope#DATABASE} was asked for and the connection may not read the whole
* database.
*
* @return the scope read in; {@link MetadataScope#DATABASE} where none was asked for
*/
default MetadataScope scope() {
return MetadataScope.DATABASE;
}

}
Loading
Loading