Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
9591d7d
#882 jdp-2025-06: Schema Support
mrotteveel Jun 18, 2025
9865b6b
#882 Fix tests failing due to Firebird 6 schemas
mrotteveel Jun 18, 2025
a3c5979
#882 Implement to DatabaseMetaData.getSchemas return schemas
mrotteveel Jun 19, 2025
7deb6ed
#882 Implement DatabaseMetaData informational methods to report schem…
mrotteveel Jun 19, 2025
740ce4d
Use isNullOrEmpty
mrotteveel Jun 19, 2025
286eea0
#882 Schema support for DatabaseMetaData.getProcedures
mrotteveel Jun 19, 2025
44cccc2
#882 Use SYSTEM unquoted due to dialect 1 support
mrotteveel Jun 19, 2025
d34af61
#882 Schema support for getBestRowIdentifier
mrotteveel Jun 21, 2025
05edd2d
#882 Schema support for getColumnPrivileges
mrotteveel Jun 21, 2025
7699234
#882 Schema support for getColumns
mrotteveel Jun 21, 2025
1d30a6d
#882 Schema support for getImportedKeys/getExportedKeys/getCrossRefer…
mrotteveel Jun 21, 2025
69effeb
#882 Schema support for getFunctionColumns
mrotteveel Jun 22, 2025
a5d8434
#882 Schema support for getFunctions
mrotteveel Jun 22, 2025
c08df8b
#882 Schema support for getIndexInfo
mrotteveel Jun 22, 2025
a1655eb
#882 Schema support for getPrimaryKeys
mrotteveel Jun 22, 2025
6b55727
#882 Schema support for getProcedureColumns
mrotteveel Jun 22, 2025
584e2cc
#882 Schema support for getPseudoColumns
mrotteveel Jun 22, 2025
6bdb4bc
#882 Schema support for getTablePrivileges
mrotteveel Jun 23, 2025
73ebef5
#882 Schema support for getTables
mrotteveel Jun 23, 2025
90ce855
#882 Schema support for getCatalogs
mrotteveel Jun 23, 2025
11349a7
Mark metadata classes sealed or final
mrotteveel Jun 23, 2025
ea0a273
#882 Schema support for getXXXSourceCode
mrotteveel Jun 23, 2025
b60c7d6
Add ifSchemaElse also to FirebirdSupportInfo
mrotteveel Jun 23, 2025
a154551
#882 Client props, xid detection
mrotteveel Jun 24, 2025
18a5228
#882 Retrieve schema name in column information
mrotteveel Jun 24, 2025
bed0828
#882 Incomplete change to selectable procedure detection
mrotteveel Jun 24, 2025
ce3abcc
#882 Misc wording of jdp-2025-06
mrotteveel Jun 24, 2025
d365d92
#882 Define connection property searchPath
mrotteveel Jun 24, 2025
276d8bc
Add supportInfoFor(FirebirdConnection) to avoid wrapper check
mrotteveel Jun 25, 2025
501eb78
Tighten up StoredProcedureMetaData, use support info
mrotteveel Jun 25, 2025
ca102d2
#882 Implement Connection.get/setSchema
mrotteveel Jul 9, 2025
6a7b308
#882 Additional schema tests for FBResultSetMetaData
mrotteveel Jul 12, 2025
aa3d585
#882 Modify FBRowUpdater to search for schema
mrotteveel Jul 13, 2025
1f839f0
Deduplicate strings in StatementInfoProcessor
mrotteveel Jul 13, 2025
fbff0a6
Add some todo for later
mrotteveel Jul 14, 2025
10ed654
#882 Improve schema test coverage
mrotteveel Jul 17, 2025
3662432
#882 Improve schema test coverage (getFunctions)
mrotteveel Jul 17, 2025
b83f275
Replace QualifiedName with ObjectReference
mrotteveel Jul 18, 2025
2cb017e
Add schema support assumption to test
mrotteveel Jul 18, 2025
b8769ad
#882 Improve schema test coverage (getProcedures/getProcedureColumns)
mrotteveel Jul 22, 2025
099a4b2
#882 Improve schema test coverage (getIndexInfo/getPrimaryKeys)
mrotteveel Jul 22, 2025
6a9febc
#882 Improve schema test coverage (getPseudoColumns/getTables)
mrotteveel Jul 22, 2025
48f4b7c
#882 Improve schema test coverage (getTablePrivileges)
mrotteveel Jul 22, 2025
24478cf
#882 schema support for generated keys
mrotteveel Jul 29, 2025
d251d84
#882 Add schema support for FBCallableStatement
mrotteveel Oct 9, 2025
82aa541
Fix test failure on Firebird 5 and older
mrotteveel Oct 9, 2025
a7542c0
#882 Document decision not to backport schema support to Jaybird 6
mrotteveel Oct 11, 2025
04523d7
Misc. copy editing and fixing typos
mrotteveel Oct 11, 2025
381b792
#882 Add JB_PROCEDURE_SOURCE column to getProcedures
mrotteveel Oct 13, 2025
102a856
#882 Reject idea to add column with schemas to getCatalogs
mrotteveel Oct 13, 2025
567cdea
Remove unnecessary caching from BasicVersion
mrotteveel Oct 17, 2025
e2e4e87
#882 Schema support for StatisticsManager.getTableStatistics
mrotteveel Oct 17, 2025
2353b24
#882 Schema support for FBTableStatisticsManager
mrotteveel Oct 18, 2025
c2320ed
Fixed wrong name
mrotteveel Oct 19, 2025
180999c
#882 Added setters to FirebirdConnection to set search path
mrotteveel Oct 19, 2025
d934271
Document compatibility issue with schema(Pattern) in dbmd
mrotteveel Oct 25, 2025
a35a3a0
Correct documentation of getXXXSourceCode methods
mrotteveel Oct 30, 2025
48514d5
Misc review changes
mrotteveel Oct 30, 2025
13e38c3
Use Collection instead of List, use common code path
mrotteveel Nov 3, 2025
4a83dec
Simplify version string creation in GDSServerVersion
mrotteveel Nov 3, 2025
ec1056e
SPECIFIC_NAME should not include the schema
mrotteveel Nov 3, 2025
0c9efec
Update error codes and messages from 6.0.0.1338
mrotteveel Nov 4, 2025
e37afa1
Ignore drop schema failure, refactor to single code path
mrotteveel Nov 4, 2025
588a017
Finalize jdp-2025-06
mrotteveel Nov 4, 2025
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
Prev Previous commit
Next Next commit
#882 Schema support for getBestRowIdentifier
  • Loading branch information
mrotteveel committed Oct 25, 2025
commit d34af6183c90a2b322eaed8135b15c1efa68ea1b
9 changes: 7 additions & 2 deletions src/main/org/firebirdsql/jdbc/FBDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1372,14 +1372,19 @@ public ResultSet getTablePrivileges(String catalog, String schemaPattern, String
/**
* {@inheritDoc}
* <p>
* Jaybird considers the primary key (scoped as {@code bestRowSession} as the best identifier for all scopes.
* Pseudo column {@code RDB$DB_KEY} (scoped as {@code bestRowTransaction} is considered the second-best alternative
* Jaybird considers the primary key (scoped as {@code bestRowSession}) as the best identifier for all scopes.
* Pseudo column {@code RDB$DB_KEY} (scoped as {@code bestRowTransaction}) is considered the second-best alternative
* for scopes {@code bestRowTemporary} and {@code bestRowTransaction} if {@code table} has no primary key.
* </p>
* <p>
* Jaybird currently considers {@code RDB$DB_KEY} to be {@link DatabaseMetaData#bestRowTransaction} even if the
* dbkey_scope is set to 1 (session). This may change in the future. See also {@link #getRowIdLifetime()}.
* </p>
* <p>
* On Firebird 6.0 and higher, passing {@code null} for {@code schema} will return columns of <strong>all</strong>
* tables with name {@code name}. It is recommended to <em>always</em> specify a non-{@code null} {@code schema} on
* Firebird 6.0 and higher.
* </p>
*/
@Override
public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable)
Expand Down
5 changes: 3 additions & 2 deletions src/main/org/firebirdsql/jdbc/FBRowUpdater.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-FileCopyrightText: Copyright 2005-2011 Roman Rokytskyy
// SPDX-FileCopyrightText: Copyright 2012-2024 Mark Rotteveel
// SPDX-FileCopyrightText: Copyright 2012-2025 Mark Rotteveel
// SPDX-License-Identifier: LGPL-2.1-or-later
package org.firebirdsql.jdbc;

Expand Down Expand Up @@ -250,8 +250,9 @@ private static List<FieldDescriptor> deriveKeyColumns(String tableName, RowDescr
*/
private static List<FieldDescriptor> keyColumnsOfBestRowIdentifier(String tableName, RowDescriptor rowDescriptor,
DatabaseMetaData dbmd) throws SQLException {
// TODO Add schema support
try (ResultSet bestRowIdentifier = dbmd
.getBestRowIdentifier("", "", tableName, DatabaseMetaData.bestRowTransaction, true)) {
.getBestRowIdentifier("", null, tableName, DatabaseMetaData.bestRowTransaction, true)) {
int bestRowIdentifierColumnCount = 0;
List<FieldDescriptor> keyColumns = new ArrayList<>();
while (bestRowIdentifier.next()) {
Expand Down
170 changes: 129 additions & 41 deletions src/main/org/firebirdsql/jdbc/metadata/GetBestRowIdentifier.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-FileCopyrightText: Copyright 2001-2024 Firebird development team and individual contributors
// SPDX-FileCopyrightText: Copyright 2022-2024 Mark Rotteveel
// SPDX-FileCopyrightText: Copyright 2001-2025 Firebird development team and individual contributors
// SPDX-FileCopyrightText: Copyright 2022-2025 Mark Rotteveel
// SPDX-License-Identifier: LGPL-2.1-or-later
package org.firebirdsql.jdbc.metadata;

Expand Down Expand Up @@ -41,7 +41,7 @@
* @author Mark Rotteveel
* @since 5
*/
public final class GetBestRowIdentifier extends AbstractMetadataMethod {
public abstract class GetBestRowIdentifier extends AbstractMetadataMethod {

private static final String ROWIDENTIFIER = "ROWIDENTIFIER";

Expand All @@ -56,31 +56,6 @@ public final class GetBestRowIdentifier extends AbstractMetadataMethod {
.at(7).simple(SQL_SHORT, 0, "PSEUDO_COLUMN", ROWIDENTIFIER).addField()
.toRowDescriptor();

//@formatter:off
private static final String GET_BEST_ROW_IDENT_START =
"select\n"
+ " RF.RDB$FIELD_NAME as COLUMN_NAME,\n"
+ " F.RDB$FIELD_TYPE as " + FIELD_TYPE + ",\n"
+ " F.RDB$FIELD_SUB_TYPE as " + FIELD_SUB_TYPE + ",\n"
+ " F.RDB$FIELD_PRECISION as " + FIELD_PRECISION + ",\n"
+ " F.RDB$FIELD_SCALE as " + FIELD_SCALE + ",\n"
+ " F.RDB$FIELD_LENGTH as " + FIELD_LENGTH + ",\n"
+ " F.RDB$CHARACTER_LENGTH as " + CHAR_LEN + ",\n"
+ " F.RDB$CHARACTER_SET_ID as " + CHARSET_ID + "\n"
+ "from RDB$RELATION_CONSTRAINTS RC\n"
+ "inner join RDB$INDEX_SEGMENTS IDX\n"
+ " on IDX.RDB$INDEX_NAME = RC.RDB$INDEX_NAME\n"
+ "inner join RDB$RELATION_FIELDS RF\n"
+ " on RF.RDB$FIELD_NAME = IDX.RDB$FIELD_NAME and RF.RDB$RELATION_NAME = RC.RDB$RELATION_NAME\n"
+ "inner join RDB$FIELDS F\n"
+ " on F.RDB$FIELD_NAME = RF.RDB$FIELD_SOURCE\n"
+ "where RC.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'\n"
+ "and ";

private static final String GET_BEST_ROW_IDENT_END =
"\norder by IDX.RDB$FIELD_POSITION";
//@formatter:on

private GetBestRowIdentifier(DbMetadataMediator mediator) {
super(ROW_DESCRIPTOR, mediator);
}
Expand All @@ -93,7 +68,7 @@ public ResultSet getBestRowIdentifier(String catalog, String schema, String tabl
}

RowValueBuilder valueBuilder = new RowValueBuilder(ROW_DESCRIPTOR);
List<RowValue> rows = getPrimaryKeyIdentifier(table, valueBuilder);
List<RowValue> rows = getPrimaryKeyIdentifier(schema, table, valueBuilder);

// if no primary key exists, add RDB$DB_KEY as pseudo-column
if (rows.isEmpty()) {
Expand All @@ -105,8 +80,8 @@ public ResultSet getBestRowIdentifier(String catalog, String schema, String tabl
return createEmpty();
}

try (ResultSet pseudoColumns =
dbmd.getPseudoColumns(catalog, schema, escapeWildcards(table), "RDB$DB\\_KEY")) {
try (ResultSet pseudoColumns = dbmd.getPseudoColumns(
catalog, escapeWildcards(schema), escapeWildcards(table), "RDB$DB\\_KEY")) {
if (!pseudoColumns.next()) {
return createEmpty();
}
Expand All @@ -118,7 +93,7 @@ public ResultSet getBestRowIdentifier(String catalog, String schema, String tabl
.at(1).setString("RDB$DB_KEY")
.at(2).setInt(Types.ROWID)
.at(3).setString(getDataTypeName(char_type, 0, CS_BINARY))
.at(4).setInt(pseudoColumns.getInt(8))
.at(4).setInt(pseudoColumns.getInt(6))
.at(5).set(null)
.at(6).set(null)
.at(7).setShort(DatabaseMetaData.bestRowPseudo)
Expand All @@ -132,22 +107,20 @@ public ResultSet getBestRowIdentifier(String catalog, String schema, String tabl
/**
* Get primary key of the table as best row identifier.
*
* @param schema
* name of the schema
* @param table
* name of the table.
* name of the table
* @param valueBuilder
* builder for row values
* @return list of result set values, when empty, no primary key has been defined for a table or the table does not
* exist. The returned list can be modified by caller if needed.
* @throws SQLException
* if something went wrong.
*/
private List<RowValue> getPrimaryKeyIdentifier(String table, RowValueBuilder valueBuilder) throws SQLException {
Clause tableClause = Clause.equalsClause("RC.RDB$RELATION_NAME", table);
String sql = GET_BEST_ROW_IDENT_START
+ tableClause.getCondition(false)
+ GET_BEST_ROW_IDENT_END;

MetadataQuery metadataQuery = new MetadataQuery(sql, Clause.parameters(tableClause));
private List<RowValue> getPrimaryKeyIdentifier(String schema, String table, RowValueBuilder valueBuilder)
throws SQLException {
MetadataQuery metadataQuery = createGetPrimaryKeyIdentifierQuery(schema, table);
try (ResultSet rs = mediator.performMetaDataQuery(metadataQuery)) {
List<RowValue> rows = new ArrayList<>();
while (rs.next()) {
Expand All @@ -157,6 +130,8 @@ private List<RowValue> getPrimaryKeyIdentifier(String table, RowValueBuilder val
}
}

abstract MetadataQuery createGetPrimaryKeyIdentifierQuery(String schema, String table);

@Override
RowValue createMetadataRow(ResultSet rs, RowValueBuilder valueBuilder) throws SQLException {
TypeMetadata typeMetadata = TypeMetadata.builder(mediator.getFirebirdSupportInfo())
Expand All @@ -176,6 +151,119 @@ RowValue createMetadataRow(ResultSet rs, RowValueBuilder valueBuilder) throws SQ
}

public static GetBestRowIdentifier create(DbMetadataMediator mediator) {
return new GetBestRowIdentifier(mediator);
// NOTE: Indirection through static method prevents unnecessary classloading
if (mediator.getFirebirdSupportInfo().isVersionEqualOrAbove(6)) {
return FB6.createInstance(mediator);
} else {
return FB5.createInstance(mediator);
}
}

/**
* Implementation for Firebird 5.0 and older.
*/
private static final class FB5 extends GetBestRowIdentifier {

//@formatter:off
private static final String GET_BEST_ROW_IDENT_START = """
select
RF.RDB$FIELD_NAME as COLUMN_NAME,
""" +
" F.RDB$FIELD_TYPE as " + FIELD_TYPE + ",\n" +
" F.RDB$FIELD_SUB_TYPE as " + FIELD_SUB_TYPE + ",\n" +
" F.RDB$FIELD_PRECISION as " + FIELD_PRECISION + ",\n" +
" F.RDB$FIELD_SCALE as " + FIELD_SCALE + ",\n" +
" F.RDB$FIELD_LENGTH as " + FIELD_LENGTH + ",\n" +
" F.RDB$CHARACTER_LENGTH as " + CHAR_LEN + ",\n" +
" F.RDB$CHARACTER_SET_ID as " + CHARSET_ID + "\n" + """
from RDB$RELATION_CONSTRAINTS RC
inner join RDB$INDEX_SEGMENTS IDX
on IDX.RDB$INDEX_NAME = RC.RDB$INDEX_NAME
inner join RDB$RELATION_FIELDS RF
on RF.RDB$FIELD_NAME = IDX.RDB$FIELD_NAME and RF.RDB$RELATION_NAME = RC.RDB$RELATION_NAME
inner join RDB$FIELDS F
on F.RDB$FIELD_NAME = RF.RDB$FIELD_SOURCE
where RC.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'
and\s""";
//@formatter:on

private static final String GET_BEST_ROW_IDENT_END = "\norder by IDX.RDB$FIELD_POSITION";


private FB5(DbMetadataMediator mediator) {
super(mediator);
}

private static GetBestRowIdentifier createInstance(DbMetadataMediator mediator) {
return new FB5(mediator);
}

@Override
MetadataQuery createGetPrimaryKeyIdentifierQuery(String schema, String table) {
Clause tableClause = Clause.equalsClause("RC.RDB$RELATION_NAME", table);
String sql = GET_BEST_ROW_IDENT_START
+ tableClause.getCondition(false)
+ GET_BEST_ROW_IDENT_END;

return new MetadataQuery(sql, Clause.parameters(tableClause));
}
}

/**
* Implementation for Firebird 6.0 and higher.
*/
private static final class FB6 extends GetBestRowIdentifier {

//@formatter:off
private static final String GET_BEST_ROW_IDENT_START_6 = """
select
RF.RDB$FIELD_NAME as COLUMN_NAME,
""" +
" F.RDB$FIELD_TYPE as " + FIELD_TYPE + ",\n" +
" F.RDB$FIELD_SUB_TYPE as " + FIELD_SUB_TYPE + ",\n" +
" F.RDB$FIELD_PRECISION as " + FIELD_PRECISION + ",\n" +
" F.RDB$FIELD_SCALE as " + FIELD_SCALE + ",\n" +
" F.RDB$FIELD_LENGTH as " + FIELD_LENGTH + ",\n" +
" F.RDB$CHARACTER_LENGTH as " + CHAR_LEN + ",\n" +
" F.RDB$CHARACTER_SET_ID as " + CHARSET_ID + "\n" + """
from SYSTEM.RDB$RELATION_CONSTRAINTS RC
inner join SYSTEM.RDB$INDEX_SEGMENTS IDX
on IDX.RDB$SCHEMA_NAME = RC.RDB$SCHEMA_NAME and IDX.RDB$INDEX_NAME = RC.RDB$INDEX_NAME
inner join SYSTEM.RDB$RELATION_FIELDS RF
on RF.RDB$FIELD_NAME = IDX.RDB$FIELD_NAME and RF.RDB$SCHEMA_NAME = RC.RDB$SCHEMA_NAME and RF.RDB$RELATION_NAME = RC.RDB$RELATION_NAME
inner join SYSTEM.RDB$FIELDS F
on F.RDB$SCHEMA_NAME = RF.RDB$FIELD_SOURCE_SCHEMA_NAME and F.RDB$FIELD_NAME = RF.RDB$FIELD_SOURCE
where RC.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'
and\s""";
//@formatter:on

// The order by schema name is to ensure a consistent order when this is called with schema = null, as that will
// not narrow the search by schema, so can return columns of multiple same named tables in different schemas.
private static final String GET_BEST_ROW_IDENT_END_6 = "\norder by RC.RDB$SCHEMA_NAME, IDX.RDB$FIELD_POSITION";

private FB6(DbMetadataMediator mediator) {
super(mediator);
}

private static GetBestRowIdentifier createInstance(DbMetadataMediator mediator) {
return new FB6(mediator);
}

@Override
MetadataQuery createGetPrimaryKeyIdentifierQuery(String schema, String table) {
List<Clause> clauses = new ArrayList<>(2);
if (schema != null) {
// NOTE: empty string will return no rows as required ("" retrieves those without a schema)
clauses.add(Clause.equalsClause("RC.RDB$SCHEMA_NAME", schema));
}
clauses.add(Clause.equalsClause("RC.RDB$RELATION_NAME", table));
String sql = GET_BEST_ROW_IDENT_START_6
+ Clause.conjunction(clauses)
+ GET_BEST_ROW_IDENT_END_6;

return new MetadataQuery(sql, Clause.parameters(clauses));
}

}

}
13 changes: 13 additions & 0 deletions src/test/org/firebirdsql/common/FBTestProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,19 @@ public static void defaultDatabaseTearDown(FBManager fbManager) throws Exception
}
}

/**
* If schema support is available, returns {@code forSchema}, otherwise returns {@code withoutSchema}.
*
* @param forSchema
* value to return when schema support is available
* @param withoutSchema
* value to return when schema support is not available
* @return {@code forSchema} if schema support is available, otherwise {@code withoutSchema}
*/
public static <T> T ifSchemaElse(T forSchema, T withoutSchema) {
return getDefaultSupportInfo().supportsSchemas() ? forSchema : withoutSchema;
}

private FBTestProperties() {
// No instantiation
}
Expand Down
Loading