Beispiel #1
0
  @Override
  protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
    for (Record record :
        create()
            .select(
                concat(SYSTABLE.TABLE_NAME, val("__"), SYSIDX.INDEX_NAME).as("indexName"),
                SYSTABLE.TABLE_NAME,
                SYSTABCOL.COLUMN_NAME)
            .from(SYSIDX)
            .join(SYSIDXCOL)
            .on(SYSIDX.TABLE_ID.equal(SYSIDXCOL.TABLE_ID))
            .and(SYSIDX.INDEX_ID.equal(SYSIDXCOL.INDEX_ID))
            .join(SYSTABLE)
            .on(SYSIDXCOL.TABLE_ID.equal(SYSTABLE.TABLE_ID))
            .join(SYSTABCOL)
            .on(SYSIDXCOL.TABLE_ID.equal(SYSTABCOL.TABLE_ID))
            .and(SYSIDXCOL.COLUMN_ID.equal(SYSTABCOL.COLUMN_ID))
            .where(SYSIDX.INDEX_CATEGORY.equal((byte) 1))
            .orderBy(SYSIDXCOL.SEQUENCE)
            .fetch()) {

      String key = record.getValue("indexName", String.class);
      String tableName = record.getValue(SYSTABLE.TABLE_NAME);
      String columnName = record.getValue(SYSTABCOL.COLUMN_NAME);

      TableDefinition table = getTable(getSchema(), tableName);
      if (table != null) {
        relations.addPrimaryKey(key, table.getColumn(columnName));
      }
    }
  }
Beispiel #2
0
  @Override
  protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    Sysidx fkIndex = SYSIDX.as("fkIndex");
    Sysidx ukIndex = SYSIDX.as("ukIndex");

    Systable fkTable = SYSTABLE.as("fkTable");
    Systable ukTable = SYSTABLE.as("ukTable");

    for (Record record :
        create()
            .select(
                concat(fkTable.TABLE_NAME, val("__"), fkIndex.INDEX_NAME).as("fkIndexName"),
                fkTable.TABLE_NAME,
                SYSTABCOL.COLUMN_NAME,
                concat(ukTable.TABLE_NAME, val("__"), ukIndex.INDEX_NAME).as("ukIndexName"))
            .from(SYSFKEY)
            .join(fkIndex)
            .on(SYSFKEY.FOREIGN_INDEX_ID.equal(fkIndex.INDEX_ID))
            .and(SYSFKEY.FOREIGN_TABLE_ID.equal(fkIndex.TABLE_ID))
            .join(SYSIDXCOL)
            .on(fkIndex.INDEX_ID.equal(SYSIDXCOL.INDEX_ID))
            .and(fkIndex.TABLE_ID.equal(SYSIDXCOL.TABLE_ID))
            .join(fkTable)
            .on(SYSFKEY.FOREIGN_TABLE_ID.equal(fkTable.TABLE_ID))
            .join(SYSTABCOL)
            .on(SYSIDXCOL.TABLE_ID.equal(SYSTABCOL.TABLE_ID))
            .and(SYSIDXCOL.COLUMN_ID.equal(SYSTABCOL.COLUMN_ID))
            .join(ukIndex)
            .on(SYSFKEY.PRIMARY_INDEX_ID.equal(ukIndex.INDEX_ID))
            .and(SYSFKEY.PRIMARY_TABLE_ID.equal(ukIndex.TABLE_ID))
            .join(ukTable)
            .on(SYSFKEY.PRIMARY_TABLE_ID.equal(ukTable.TABLE_ID))
            .orderBy(fkTable.TABLE_NAME.asc(), fkIndex.INDEX_NAME.asc(), SYSIDXCOL.SEQUENCE.asc())
            .fetch()) {

      String foreignKey = record.getValue("fkIndexName", String.class);
      String foreignKeyTableName = record.getValue(SYSTABLE.TABLE_NAME);
      String foreignKeyColumn = record.getValue(SYSTABCOL.COLUMN_NAME);
      String referencedKey = record.getValue("ukIndexName", String.class);

      TableDefinition foreignKeyTable = getTable(getSchema(), foreignKeyTableName);

      if (foreignKeyTable != null) {
        ColumnDefinition referencingColumn = foreignKeyTable.getColumn(foreignKeyColumn);
        relations.addForeignKey(foreignKey, referencedKey, referencingColumn, getSchema());
      }
    }
  }
Beispiel #3
0
  @Override
  protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys("UNIQUE")) {
      SchemaDefinition schema = getSchema(record.getValue(KeyColumnUsage.TABLE_SCHEMA));
      String constraintName = record.getValue(KeyColumnUsage.CONSTRAINT_NAME);
      String tableName = record.getValue(KeyColumnUsage.TABLE_NAME);
      String columnName = record.getValue(KeyColumnUsage.COLUMN_NAME);

      String key = getKeyName(tableName, constraintName);
      TableDefinition table = getTable(schema, tableName);

      if (table != null) {
        relations.addUniqueKey(key, table.getColumn(columnName));
      }
    }
  }
Beispiel #4
0
  @Override
  protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys(false)) {
      SchemaDefinition schema = getSchema(record.get(Statistics.TABLE_SCHEMA));
      String constraintName = record.get(Statistics.INDEX_NAME);
      String tableName = record.get(Statistics.TABLE_NAME);
      String columnName = record.get(Statistics.COLUMN_NAME);

      String key = getKeyName(tableName, constraintName);
      TableDefinition table = getTable(schema, tableName);

      if (table != null) {
        relations.addUniqueKey(key, table.getColumn(columnName));
      }
    }
  }
Beispiel #5
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);
      }
    }
  }