Search in sources :

Example 1 with ForeignKeyData

use of com.querydsl.sql.codegen.support.ForeignKeyData in project querydsl by querydsl.

the class KeyDataFactory method getImportedKeys.

public Map<String, ForeignKeyData> getImportedKeys(DatabaseMetaData md, String catalog, String schema, String tableName) throws SQLException {
    ResultSet foreignKeys = md.getImportedKeys(catalog, schema, tableName);
    Map<String, ForeignKeyData> foreignKeyData = new HashMap<String, ForeignKeyData>();
    try {
        while (foreignKeys.next()) {
            String name = foreignKeys.getString(FK_NAME);
            String parentSchemaName = namingStrategy.normalizeSchemaName(foreignKeys.getString(FK_PARENT_SCHEMA_NAME));
            String parentTableName = namingStrategy.normalizeTableName(foreignKeys.getString(FK_PARENT_TABLE_NAME));
            String parentColumnName = namingStrategy.normalizeColumnName(foreignKeys.getString(FK_PARENT_COLUMN_NAME));
            String foreignColumn = namingStrategy.normalizeColumnName(foreignKeys.getString(FK_FOREIGN_COLUMN_NAME));
            if (name == null || name.isEmpty()) {
                name = tableName + "_" + parentTableName + "_FK";
            }
            ForeignKeyData data = foreignKeyData.get(name);
            if (data == null) {
                data = new ForeignKeyData(name, parentSchemaName, parentTableName, createType(parentSchemaName, parentTableName));
                foreignKeyData.put(name, data);
            }
            data.add(foreignColumn, parentColumnName);
        }
        return foreignKeyData;
    } finally {
        foreignKeys.close();
    }
}
Also used : HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) ForeignKeyData(com.querydsl.sql.codegen.support.ForeignKeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData)

Example 2 with ForeignKeyData

use of com.querydsl.sql.codegen.support.ForeignKeyData in project querydsl by querydsl.

the class KeyDataFactoryTest method test.

@Test
public void test() throws SQLException {
    statement.execute("drop table employee if exists");
    statement.execute("drop table survey if exists");
    statement.execute("drop table date_test if exists");
    statement.execute("drop table date_time_test if exists");
    statement.execute("create table survey (id int, name varchar(30), " + "CONSTRAINT PK_survey PRIMARY KEY (id, name))");
    statement.execute("create table employee(" + "id INT, " + "superior_id int, " + "survey_id int, " + "survey_name varchar(30), " + "CONSTRAINT PK_employee PRIMARY KEY (id), " + "CONSTRAINT FK_survey FOREIGN KEY (survey_id, survey_name) REFERENCES survey(id,name), " + "CONSTRAINT FK_superior FOREIGN KEY (superior_id) REFERENCES employee(id))");
    KeyDataFactory keyDataFactory = new KeyDataFactory(new DefaultNamingStrategy(), "Q", "", "test", false);
    DatabaseMetaData md = connection.getMetaData();
    // EMPLOYEE
    // primary key
    Map<String, PrimaryKeyData> primaryKeys = keyDataFactory.getPrimaryKeys(md, null, null, "EMPLOYEE");
    assertFalse(primaryKeys.isEmpty());
    // inverse foreign keys
    Map<String, InverseForeignKeyData> exportedKeys = keyDataFactory.getExportedKeys(md, null, null, "EMPLOYEE");
    assertFalse(exportedKeys.isEmpty());
    assertTrue(exportedKeys.containsKey("FK_SUPERIOR"));
    // foreign keys
    Map<String, ForeignKeyData> importedKeys = keyDataFactory.getImportedKeys(md, null, null, "EMPLOYEE");
    assertFalse(importedKeys.isEmpty());
    assertTrue(importedKeys.containsKey("FK_SUPERIOR"));
    assertTrue(importedKeys.containsKey("FK_SURVEY"));
    // SURVEY
    // primary key
    primaryKeys = keyDataFactory.getPrimaryKeys(md, null, null, "SURVEY");
    assertFalse(primaryKeys.isEmpty());
    // inverse foreign keys
    exportedKeys = keyDataFactory.getExportedKeys(md, null, null, "SURVEY");
    assertFalse(exportedKeys.isEmpty());
    assertTrue(exportedKeys.containsKey("FK_SURVEY"));
    // foreign keys
    importedKeys = keyDataFactory.getImportedKeys(md, null, null, "SURVEY");
    assertTrue(importedKeys.isEmpty());
}
Also used : PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData) DatabaseMetaData(java.sql.DatabaseMetaData) ForeignKeyData(com.querydsl.sql.codegen.support.ForeignKeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData) Test(org.junit.Test) AbstractJDBCTest(com.querydsl.sql.AbstractJDBCTest)

Example 3 with ForeignKeyData

use of com.querydsl.sql.codegen.support.ForeignKeyData in project querydsl by querydsl.

the class MetaDataSerializer method introImports.

@SuppressWarnings("unchecked")
@Override
protected void introImports(CodeWriter writer, SerializerConfig config, EntityType model) throws IOException {
    super.introImports(writer, config, model);
    Collection<ForeignKeyData> foreignKeys = (Collection<ForeignKeyData>) model.getData().get(ForeignKeyData.class);
    Collection<InverseForeignKeyData> inverseForeignKeys = (Collection<InverseForeignKeyData>) model.getData().get(InverseForeignKeyData.class);
    boolean addJavaUtilImport = false;
    if (foreignKeys != null) {
        for (ForeignKeyData keyData : foreignKeys) {
            if (keyData.getForeignColumns().size() > 1) {
                addJavaUtilImport = true;
            }
        }
    }
    if (inverseForeignKeys != null) {
        for (InverseForeignKeyData keyData : inverseForeignKeys) {
            if (keyData.getForeignColumns().size() > 1) {
                addJavaUtilImport = true;
            }
        }
    }
    if (addJavaUtilImport) {
        writer.imports(List.class.getPackage());
    }
    writer.imports(ColumnMetadata.class, java.sql.Types.class);
    if (!entityPathType.getPackage().equals(ColumnMetadata.class.getPackage())) {
        writer.imports(entityPathType);
    }
    writeUserImports(writer);
}
Also used : InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData) ForeignKeyData(com.querydsl.sql.codegen.support.ForeignKeyData)

Aggregations

ForeignKeyData (com.querydsl.sql.codegen.support.ForeignKeyData)3 InverseForeignKeyData (com.querydsl.sql.codegen.support.InverseForeignKeyData)3 AbstractJDBCTest (com.querydsl.sql.AbstractJDBCTest)1 PrimaryKeyData (com.querydsl.sql.codegen.support.PrimaryKeyData)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1