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 getPrimaryKeys
  • Loading branch information
mrotteveel committed Oct 25, 2025
commit a1655eb99b489f1229aa377704f0a6176688cb24
2 changes: 1 addition & 1 deletion src/main/org/firebirdsql/jdbc/FBDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ public ResultSet getVersionColumns(String catalog, String schema, String table)
*/
@Override
public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException {
return GetPrimaryKeys.create(getDbMetadataMediator()).getPrimaryKeys(table);
return GetPrimaryKeys.create(getDbMetadataMediator()).getPrimaryKeys(schema, table);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private static GetBestRowIdentifier createInstance(DbMetadataMediator mediator)

@Override
MetadataQuery createGetPrimaryKeyIdentifierQuery(String schema, String table) {
List<Clause> clauses = new ArrayList<>(2);
var clauses = new ArrayList<Clause>(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));
Expand Down
129 changes: 103 additions & 26 deletions src/main/org/firebirdsql/jdbc/metadata/GetPrimaryKeys.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 All @@ -10,6 +10,7 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import static org.firebirdsql.gds.ISCConstants.SQL_SHORT;
import static org.firebirdsql.gds.ISCConstants.SQL_VARYING;
Expand All @@ -22,7 +23,7 @@
* @author Mark Rotteveel
* @since 5
*/
public final class GetPrimaryKeys extends AbstractMetadataMethod {
public abstract class GetPrimaryKeys extends AbstractMetadataMethod {

private static final String COLUMNINFO = "COLUMNINFO";

Expand All @@ -36,42 +37,25 @@ public final class GetPrimaryKeys extends AbstractMetadataMethod {
.at(6).simple(SQL_VARYING, OBJECT_NAME_LENGTH, "JB_PK_INDEX_NAME", COLUMNINFO).addField()
.toRowDescriptor();

private static final String GET_PRIMARY_KEYS_START = """
select
RC.RDB$RELATION_NAME as TABLE_NAME,
ISGMT.RDB$FIELD_NAME as COLUMN_NAME,
ISGMT.RDB$FIELD_POSITION + 1 as KEY_SEQ,
RC.RDB$CONSTRAINT_NAME as PK_NAME,
RC.RDB$INDEX_NAME as JB_PK_INDEX_NAME
from RDB$RELATION_CONSTRAINTS RC
inner join RDB$INDEX_SEGMENTS ISGMT
on RC.RDB$INDEX_NAME = ISGMT.RDB$INDEX_NAME
where RC.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'
and\s""";

private static final String GET_PRIMARY_KEYS_END = "\norder by ISGMT.RDB$FIELD_NAME ";

private GetPrimaryKeys(DbMetadataMediator mediator) {
super(ROW_DESCRIPTOR, mediator);
}

public ResultSet getPrimaryKeys(String table) throws SQLException {
public final ResultSet getPrimaryKeys(String schema, String table) throws SQLException {
if (isNullOrEmpty(table)) {
return createEmpty();
}
Clause tableClause = Clause.equalsClause("RC.RDB$RELATION_NAME", table);
String sql = GET_PRIMARY_KEYS_START
+ tableClause.getCondition(false)
+ GET_PRIMARY_KEYS_END;
MetadataQuery metadataQuery = new MetadataQuery(sql, Clause.parameters(tableClause));
MetadataQuery metadataQuery = createGetPrimaryKeysQuery(schema, table);
return createMetaDataResultSet(metadataQuery);
}

abstract MetadataQuery createGetPrimaryKeysQuery(String schema, String table);

@Override
RowValue createMetadataRow(ResultSet rs, RowValueBuilder valueBuilder) throws SQLException {
return valueBuilder
.at(0).set(null)
.at(1).set(null)
.at(1).setString(rs.getString("TABLE_SCHEM"))
.at(2).setString(rs.getString("TABLE_NAME"))
.at(3).setString(rs.getString("COLUMN_NAME"))
.at(4).setShort(rs.getShort("KEY_SEQ"))
Expand All @@ -81,6 +65,99 @@ RowValue createMetadataRow(ResultSet rs, RowValueBuilder valueBuilder) throws SQ
}

public static GetPrimaryKeys create(DbMetadataMediator mediator) {
return new GetPrimaryKeys(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 GetPrimaryKeys {

private static final String GET_PRIMARY_KEYS_START_5 = """
select
cast(null as char(1)) as TABLE_SCHEM,
RC.RDB$RELATION_NAME as TABLE_NAME,
ISGMT.RDB$FIELD_NAME as COLUMN_NAME,
ISGMT.RDB$FIELD_POSITION + 1 as KEY_SEQ,
RC.RDB$CONSTRAINT_NAME as PK_NAME,
RC.RDB$INDEX_NAME as JB_PK_INDEX_NAME
from RDB$RELATION_CONSTRAINTS RC
inner join RDB$INDEX_SEGMENTS ISGMT
on RC.RDB$INDEX_NAME = ISGMT.RDB$INDEX_NAME
where RC.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'
and\s""";

private static final String GET_PRIMARY_KEYS_END_5 = "\norder by ISGMT.RDB$FIELD_NAME";

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

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

@Override
MetadataQuery createGetPrimaryKeysQuery(String schema, String table) {
Clause tableClause = Clause.equalsClause("RC.RDB$RELATION_NAME", table);
String sql = GET_PRIMARY_KEYS_START_5
+ tableClause.getCondition(false)
+ GET_PRIMARY_KEYS_END_5;
return new MetadataQuery(sql, Clause.parameters(tableClause));
}

}

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

private static final String GET_PRIMARY_KEYS_START_6 = """
select
trim(trailing from RC.RDB$SCHEMA_NAME) as TABLE_SCHEM,
trim(trailing from RC.RDB$RELATION_NAME) as TABLE_NAME,
trim(trailing from ISGMT.RDB$FIELD_NAME) as COLUMN_NAME,
ISGMT.RDB$FIELD_POSITION + 1 as KEY_SEQ,
trim(trailing from RC.RDB$CONSTRAINT_NAME) as PK_NAME,
trim(trailing from RC.RDB$INDEX_NAME) as JB_PK_INDEX_NAME
from SYSTEM.RDB$RELATION_CONSTRAINTS RC
inner join SYSTEM.RDB$INDEX_SEGMENTS ISGMT
on RC.RDB$SCHEMA_NAME = ISGMT.RDB$SCHEMA_NAME and RC.RDB$INDEX_NAME = ISGMT.RDB$INDEX_NAME
where RC.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'
and\s""";

// For consistent order (e.g. for tests), we're also sorting on schema name.
// JDBC specifies that the result set is sorted on COLUMN_NAME, so we can't sort on schema first
private static final String GET_PRIMARY_KEYS_END_6 = "\norder by ISGMT.RDB$FIELD_NAME, ISGMT.RDB$SCHEMA_NAME";

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

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

@Override
MetadataQuery createGetPrimaryKeysQuery(String schema, String table) {
var clauses = new ArrayList<Clause>(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_PRIMARY_KEYS_START_6
+ Clause.conjunction(clauses)
+ GET_PRIMARY_KEYS_END_6;
return new MetadataQuery(sql, Clause.parameters(clauses));
}

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright 2024 Mark Rotteveel
// SPDX-FileCopyrightText: Copyright 2024-2025 Mark Rotteveel
// SPDX-License-Identifier: LGPL-2.1-or-later
package org.firebirdsql.jdbc;

Expand All @@ -20,6 +20,7 @@

import static java.util.Collections.unmodifiableMap;
import static org.firebirdsql.common.FBTestProperties.getConnectionViaDriverManager;
import static org.firebirdsql.common.FBTestProperties.ifSchemaElse;
import static org.firebirdsql.common.assertions.ResultSetAssertions.assertNextRow;
import static org.firebirdsql.common.assertions.ResultSetAssertions.assertNoNextRow;

Expand All @@ -30,6 +31,8 @@
*/
class FBDatabaseMetaDataPrimaryKeysTest {

// TODO Add schema support: tests involving other schema

private static final String UNNAMED_CONSTRAINT_PREFIX = "INTEG_";
private static final String UNNAMED_PK_INDEX_PREFIX = "RDB$PRIMARY";

Expand Down Expand Up @@ -170,6 +173,7 @@ private void validateExpectedPrimaryKeys(String tableName, List<Map<PrimaryKeysM
static {
var defaults = new EnumMap<>(PrimaryKeysMetaData.class);
Arrays.stream(PrimaryKeysMetaData.values()).forEach(key -> defaults.put(key, null));
defaults.put(PrimaryKeysMetaData.TABLE_SCHEM, ifSchemaElse("PUBLIC", null));
DEFAULT_COLUMN_VALUES = unmodifiableMap(defaults);
}

Expand Down