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 @@ -602,7 +602,7 @@ default String createTrigger(String triggerName, Trigger.TriggerTiming timing, T
if (orReplace && supportsCreateOrReplaceTrigger())
sb.append("OR REPLACE ");
sb.append("TRIGGER ");
sb.append(quoteIdentifier(triggerName));
sb.append(triggerName(triggerName, table));
sb.append(' ').append(triggerTimingKeyword(timing)).append(' ').append(event);
sb.append(" ON ").append(qualified(table));
sb.append(' ').append(scope.forEachClause());
Expand All @@ -613,6 +613,14 @@ default String createTrigger(String triggerName, Trigger.TriggerTiming timing, T
return sb.toString();
}

/**
* How a trigger name is written. Unqualified by default; a dialect that
* scopes a trigger to its table's schema has to say so in the name.
*/
default String triggerName(String triggerName, TableReference table) {
return quoteIdentifier(triggerName);
}

default Optional<String> createTriggerProcedure(String procedureName, String schemaName, String body) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,17 @@ public String createTriggerUsingProcedure(String triggerName, String schemaName,
return createTrigger(triggerName, timing, event, table, scope, whenCondition, "CALL " + qualified + "()");
}

/**
* MySQL and MariaDB scope a trigger to its table's schema, and the name has
* to say so. Left unqualified the server creates it in the session's current
* database and then refuses it: <em>Trigger in wrong schema</em>.
*/
@Override
public String triggerName(String triggerName, org.eclipse.daanse.sql.model.schema.TableReference table) {
return table == null || table.schema().isEmpty() ? quoteIdentifier(triggerName)
: quoteIdentifier(table.schema().get().name(), triggerName);
}

/** MySQL/MariaDB: {@code DROP PROCEDURE [IF EXISTS] schema.procedureName}. */
@Override
public Optional<String> dropProcedure(String procedureName, String schemaName, boolean ifExists) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,18 @@ protected StructureInfo getStructureInfo(DatabaseMetaData databaseMetaData) thro
return structureInfo;
}

/**
* The namespace a relation lives in. A driver that reports no schema keeps
* it in the catalog - MySQL and MariaDB call it a database - so the catalog
* name stands in. Without that a consumer grouping relations by schema
* loses every one of them on those servers.
*/
private static Optional<SchemaReference> namespaceOf(Optional<String> catalogName, Optional<String> schemaName) {
Optional<CatalogReference> catalog = catalogName.map(CatalogReference::new);
return schemaName.map(sn -> new SchemaReference(catalog, sn))
.or(() -> catalogName.map(cn -> new SchemaReference(Optional.empty(), cn)));
}

private List<CatalogReference> getCatalogs(DatabaseMetaData databaseMetaData) throws SQLException {

List<CatalogReference> catalogs = new ArrayList<>();
Expand Down Expand Up @@ -500,7 +512,7 @@ private List<TableDefinition> getTableDefinitions(DatabaseMetaData databaseMetaD
final Optional<String> oRefGen = getColumnValue(rs, columnNames, "REF_GENERATION");

Optional<CatalogReference> oCatRef = oCatalogName.map(cn -> new CatalogReference(cn));
Optional<SchemaReference> oSchemaRef = oSchemaName.map(sn -> new SchemaReference(oCatRef, sn));
Optional<SchemaReference> oSchemaRef = namespaceOf(oCatalogName, oSchemaName);

TableReference tableReference = new TableReference(oSchemaRef, tableName, tableType);
TableMetaData tableMetaData = new TableMetaDataRecord(oRemarks, oTypeCat, oTypeSchema, oTypeName,
Expand Down Expand Up @@ -730,7 +742,7 @@ private List<ColumnDefinition> getColumnDefinitions(DatabaseMetaData databaseMet
}

Optional<CatalogReference> oCatRef = oCatalogName.map(cn -> new CatalogReference(cn));
Optional<SchemaReference> oSchemaRef = oSchemaName.map(sn -> new SchemaReference(oCatRef, sn));
Optional<SchemaReference> oSchemaRef = namespaceOf(oCatalogName, oSchemaName);

JDBCType jdbcType;
try {
Expand Down Expand Up @@ -811,13 +823,13 @@ private List<ImportedKey> getImportedKeys(DatabaseMetaData databaseMetaData, Str

// PK
Optional<CatalogReference> oCatRefPk = oCatalogNamePK.map(cn -> new CatalogReference(cn));
Optional<SchemaReference> oSchemaRefPk = oSchemaNamePk.map(sn -> new SchemaReference(oCatRefPk, sn));
Optional<SchemaReference> oSchemaRefPk = namespaceOf(oCatalogNamePK, oSchemaNamePk);
TableReference tableReferencePk = new TableReference(oSchemaRefPk, tableNamePk);
ColumnReference primaryKeyColumn = new ColumnReference(Optional.of(tableReferencePk), columNamePk);

// FK
Optional<CatalogReference> oCatRefFk = oCatalogNameFK.map(cn -> new CatalogReference(cn));
Optional<SchemaReference> oSchemaRefFk = oSchemaNameFk.map(sn -> new SchemaReference(oCatRefFk, sn));
Optional<SchemaReference> oSchemaRefFk = namespaceOf(oCatalogNameFK, oSchemaNameFk);
TableReference tableReferenceFk = new TableReference(oSchemaRefFk, tableNameFk);
ColumnReference foreignKeyColumn = new ColumnReference(Optional.of(tableReferenceFk), columNameFk);

Expand Down Expand Up @@ -927,7 +939,7 @@ private List<Procedure> getProcedures(DatabaseMetaData databaseMetaData, String
final String specificName = rs.getString("SPECIFIC_NAME");

Optional<CatalogReference> oCatRef = oCatalogName.map(CatalogReference::new);
Optional<SchemaReference> oSchemaRef = oSchemaName.map(sn -> new SchemaReference(oCatRef, sn));
Optional<SchemaReference> oSchemaRef = namespaceOf(oCatalogName, oSchemaName);

ProcedureReference reference = new ProcedureReference(oSchemaRef, procedureName, specificName);

Expand Down Expand Up @@ -1012,7 +1024,7 @@ private List<Function> getFunctions(DatabaseMetaData databaseMetaData, String ca
final String specificName = rs.getString("SPECIFIC_NAME");

Optional<CatalogReference> oCatRef = oCatalogName.map(CatalogReference::new);
Optional<SchemaReference> oSchemaRef = oSchemaName.map(sn -> new SchemaReference(oCatRef, sn));
Optional<SchemaReference> oSchemaRef = namespaceOf(oCatalogName, oSchemaName);

FunctionReference reference = new FunctionReference(oSchemaRef, functionName, specificName);

Expand Down Expand Up @@ -1157,13 +1169,13 @@ private ImportedKey readForeignKeyFromResultSet(ResultSet rs) throws SQLExceptio

// PK
Optional<CatalogReference> oCatRefPk = oCatalogNamePK.map(CatalogReference::new);
Optional<SchemaReference> oSchemaRefPk = oSchemaNamePk.map(sn -> new SchemaReference(oCatRefPk, sn));
Optional<SchemaReference> oSchemaRefPk = namespaceOf(oCatalogNamePK, oSchemaNamePk);
TableReference tableReferencePk = new TableReference(oSchemaRefPk, tableNamePk);
ColumnReference primaryKeyColumn = new ColumnReference(Optional.of(tableReferencePk), columNamePk);

// FK
Optional<CatalogReference> oCatRefFk = oCatalogNameFK.map(CatalogReference::new);
Optional<SchemaReference> oSchemaRefFk = oSchemaNameFk.map(sn -> new SchemaReference(oCatRefFk, sn));
Optional<SchemaReference> oSchemaRefFk = namespaceOf(oCatalogNameFK, oSchemaNameFk);
TableReference tableReferenceFk = new TableReference(oSchemaRefFk, tableNameFk);
ColumnReference foreignKeyColumn = new ColumnReference(Optional.of(tableReferenceFk), columNameFk);

Expand Down Expand Up @@ -1208,7 +1220,7 @@ private List<UserDefinedType> getUDTs(DatabaseMetaData databaseMetaData, String
jdbcType = JDBCType.OTHER;
}
Optional<CatalogReference> catRef = oCat.map(CatalogReference::new);
Optional<SchemaReference> schemaRef = oSchema.map(sn -> new SchemaReference(catRef, sn));
Optional<SchemaReference> schemaRef = namespaceOf(oCat, oSchema);
result.add(new UserDefinedTypeRecord(
new UserDefinedTypeReference(schemaRef, typeName),
className, jdbcType, remarks));
Expand Down Expand Up @@ -1297,7 +1309,7 @@ private List<PseudoColumn> getPseudoColumns(DatabaseMetaData databaseMetaData, S
String columnUsage = rs.getString("COLUMN_USAGE");

Optional<CatalogReference> catRef = oCat.map(CatalogReference::new);
Optional<SchemaReference> schemaRef = oSchema.map(sn -> new SchemaReference(catRef, sn));
Optional<SchemaReference> schemaRef = namespaceOf(oCat, oSchema);
TableReference tableRef = new TableReference(schemaRef, tableName);
ColumnReference colRef = new ColumnReference(Optional.of(tableRef), columnName);

Expand All @@ -1322,7 +1334,7 @@ private List<TablePrivilege> getTablePrivileges(DatabaseMetaData databaseMetaDat
Optional<String> isGrantable = Optional.ofNullable(rs.getString("IS_GRANTABLE"));

Optional<CatalogReference> catRef = oCat.map(CatalogReference::new);
Optional<SchemaReference> schemaRef = oSchema.map(sn -> new SchemaReference(catRef, sn));
Optional<SchemaReference> schemaRef = namespaceOf(oCat, oSchema);
TableReference tableRef = new TableReference(schemaRef, tableName);

result.add(new TablePrivilegeRecord(tableRef, grantor, grantee, privilege, isGrantable));
Expand Down Expand Up @@ -1388,7 +1400,7 @@ private List<SuperTable> getSuperTables(DatabaseMetaData databaseMetaData, Strin
String superTableName = rs.getString("SUPERTABLE_NAME");

Optional<CatalogReference> catRef = oCat.map(CatalogReference::new);
Optional<SchemaReference> schemaRef = oSchema.map(sn -> new SchemaReference(catRef, sn));
Optional<SchemaReference> schemaRef = namespaceOf(oCat, oSchema);
TableReference tableRef = new TableReference(schemaRef, tableName);

result.add(new SuperTableRecord(tableRef, superTableName));
Expand Down
Loading