Exemple #1
0
  @Override
  protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    for (Record record :
        create()
            .select(
                ReferentialConstraints.CONSTRAINT_SCHEMA,
                ReferentialConstraints.CONSTRAINT_NAME,
                ReferentialConstraints.TABLE_NAME,
                ReferentialConstraints.REFERENCED_TABLE_NAME,
                ReferentialConstraints.UNIQUE_CONSTRAINT_NAME,
                ReferentialConstraints.UNIQUE_CONSTRAINT_SCHEMA,
                KeyColumnUsage.COLUMN_NAME)
            .from(REFERENTIAL_CONSTRAINTS)
            .join(KEY_COLUMN_USAGE)
            .on(ReferentialConstraints.CONSTRAINT_SCHEMA.equal(KeyColumnUsage.CONSTRAINT_SCHEMA))
            .and(ReferentialConstraints.CONSTRAINT_NAME.equal(KeyColumnUsage.CONSTRAINT_NAME))
            .where(ReferentialConstraints.CONSTRAINT_SCHEMA.in(getInputSchemata()))
            .orderBy(
                KeyColumnUsage.CONSTRAINT_SCHEMA.asc(),
                KeyColumnUsage.CONSTRAINT_NAME.asc(),
                KeyColumnUsage.ORDINAL_POSITION.asc())
            .fetch()) {

      SchemaDefinition foreignKeySchema =
          getSchema(record.getValue(ReferentialConstraints.CONSTRAINT_SCHEMA));
      SchemaDefinition uniqueKeySchema =
          getSchema(record.getValue(ReferentialConstraints.UNIQUE_CONSTRAINT_SCHEMA));

      String foreignKey = record.getValue(ReferentialConstraints.CONSTRAINT_NAME);
      String foreignKeyColumn = record.getValue(KeyColumnUsage.COLUMN_NAME);
      String foreignKeyTableName = record.getValue(ReferentialConstraints.TABLE_NAME);
      String referencedKey = record.getValue(ReferentialConstraints.UNIQUE_CONSTRAINT_NAME);
      String referencedTableName = record.getValue(ReferentialConstraints.REFERENCED_TABLE_NAME);

      TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);

      if (foreignKeyTable != null) {
        ColumnDefinition column = foreignKeyTable.getColumn(foreignKeyColumn);

        String key = getKeyName(referencedTableName, referencedKey);
        relations.addForeignKey(foreignKey, key, column, uniqueKeySchema);
      }
    }
  }
Exemple #2
0
 private Result<Record4<String, String, String, String>> fetchKeys(String constraintType) {
   return create()
       .select(
           KeyColumnUsage.TABLE_SCHEMA,
           KeyColumnUsage.CONSTRAINT_NAME,
           KeyColumnUsage.TABLE_NAME,
           KeyColumnUsage.COLUMN_NAME)
       .from(KEY_COLUMN_USAGE)
       .join(TABLE_CONSTRAINTS)
       .on(KeyColumnUsage.TABLE_SCHEMA.equal(TableConstraints.TABLE_SCHEMA))
       .and(KeyColumnUsage.TABLE_NAME.equal(TableConstraints.TABLE_NAME))
       .and(KeyColumnUsage.CONSTRAINT_NAME.equal(TableConstraints.CONSTRAINT_NAME))
       .where(TableConstraints.CONSTRAINT_TYPE.equal(constraintType))
       .and(KeyColumnUsage.TABLE_SCHEMA.in(getInputSchemata()))
       .orderBy(
           KeyColumnUsage.TABLE_SCHEMA.asc(),
           KeyColumnUsage.TABLE_NAME.asc(),
           KeyColumnUsage.ORDINAL_POSITION.asc())
       .fetch();
 }