Search in sources :

Example 1 with InverseForeignKeyData

use of com.querydsl.sql.codegen.support.InverseForeignKeyData 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, " + "superior_id2 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_superior2 FOREIGN KEY (superior_id) REFERENCES employee(id), " + "CONSTRAINT FK_superior1 FOREIGN KEY (superior_id2) 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 sorted in abc
    Map<String, InverseForeignKeyData> exportedKeys = keyDataFactory.getExportedKeys(md, null, null, "EMPLOYEE");
    assertEquals(2, exportedKeys.size());
    Iterator<String> exportedKeysIterator = exportedKeys.keySet().iterator();
    assertEquals("FK_SUPERIOR1", exportedKeysIterator.next());
    assertEquals("FK_SUPERIOR2", exportedKeysIterator.next());
    // foreign keys sorted in abc
    Map<String, ForeignKeyData> importedKeys = keyDataFactory.getImportedKeys(md, null, null, "EMPLOYEE");
    assertEquals(3, importedKeys.size());
    Iterator<String> importedKeysIterator = importedKeys.keySet().iterator();
    assertEquals("FK_SUPERIOR1", importedKeysIterator.next());
    assertEquals("FK_SUPERIOR2", importedKeysIterator.next());
    assertEquals("FK_SURVEY", importedKeysIterator.next());
    // 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 2 with InverseForeignKeyData

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

the class KeyDataFactory method getExportedKeys.

public Map<String, InverseForeignKeyData> getExportedKeys(DatabaseMetaData md, String catalog, String schema, String tableName) throws SQLException {
    try (ResultSet foreignKeys = md.getExportedKeys(catalog, schema, tableName)) {
        Map<String, InverseForeignKeyData> inverseForeignKeyData = new TreeMap<String, InverseForeignKeyData>();
        while (foreignKeys.next()) {
            String name = foreignKeys.getString(FK_NAME);
            String parentColumnName = namingStrategy.normalizeColumnName(foreignKeys.getString(FK_PARENT_COLUMN_NAME));
            String foreignSchemaName = namingStrategy.normalizeSchemaName(foreignKeys.getString(FK_FOREIGN_SCHEMA_NAME));
            String foreignTableName = namingStrategy.normalizeTableName(foreignKeys.getString(FK_FOREIGN_TABLE_NAME));
            String foreignColumn = namingStrategy.normalizeColumnName(foreignKeys.getString(FK_FOREIGN_COLUMN_NAME));
            if (name == null || name.isEmpty()) {
                name = tableName + "_" + foreignTableName + "_IFK";
            }
            InverseForeignKeyData data = inverseForeignKeyData.get(name);
            if (data == null) {
                data = new InverseForeignKeyData(name, foreignSchemaName, foreignTableName, createType(foreignSchemaName, foreignTableName));
                inverseForeignKeyData.put(name, data);
            }
            data.add(parentColumnName, foreignColumn);
        }
        return inverseForeignKeyData;
    }
}
Also used : InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData) ResultSet(java.sql.ResultSet) TreeMap(java.util.TreeMap)

Example 3 with InverseForeignKeyData

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

the class MetaDataSerializer method serializeProperties.

@SuppressWarnings("unchecked")
@Override
protected void serializeProperties(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException {
    Collection<PrimaryKeyData> primaryKeys = (Collection<PrimaryKeyData>) model.getData().get(PrimaryKeyData.class);
    Collection<ForeignKeyData> foreignKeys = (Collection<ForeignKeyData>) model.getData().get(ForeignKeyData.class);
    Collection<InverseForeignKeyData> inverseForeignKeys = (Collection<InverseForeignKeyData>) model.getData().get(InverseForeignKeyData.class);
    if (innerClassesForKeys) {
        Type primaryKeyType = new SimpleType(namingStrategy.getPrimaryKeysClassName());
        Type foreignKeysType = new SimpleType(namingStrategy.getForeignKeysClassName());
        // primary keys
        if (primaryKeys != null) {
            writer.beginClass(primaryKeyType);
            serializePrimaryKeys(model, writer, primaryKeys);
            writer.end();
        }
        // foreign keys
        if (foreignKeys != null || inverseForeignKeys != null) {
            writer.beginClass(foreignKeysType);
            if (foreignKeys != null) {
                serializeForeignKeys(model, writer, foreignKeys, false);
            }
            // inverse foreign keys
            if (inverseForeignKeys != null) {
                serializeForeignKeys(model, writer, inverseForeignKeys, true);
            }
            writer.end();
        }
        super.serializeProperties(model, config, writer);
        if (primaryKeys != null) {
            writer.publicFinal(primaryKeyType, namingStrategy.getPrimaryKeysVariable(model), "new " + primaryKeyType.getSimpleName() + "()");
        }
        if (foreignKeys != null || inverseForeignKeys != null) {
            writer.publicFinal(foreignKeysType, namingStrategy.getForeignKeysVariable(model), "new " + foreignKeysType.getSimpleName() + "()");
        }
    } else {
        super.serializeProperties(model, config, writer);
        // primary keys
        if (primaryKeys != null) {
            serializePrimaryKeys(model, writer, primaryKeys);
        }
        // foreign keys
        if (foreignKeys != null) {
            serializeForeignKeys(model, writer, foreignKeys, false);
        }
        // inverse foreign keys
        if (inverseForeignKeys != null) {
            serializeForeignKeys(model, writer, inverseForeignKeys, true);
        }
    }
}
Also used : PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) SimpleType(com.querydsl.codegen.utils.model.SimpleType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData) Collection(java.util.Collection) ForeignKeyData(com.querydsl.sql.codegen.support.ForeignKeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData)

Example 4 with InverseForeignKeyData

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

the class MetaDataExporter method handleTable.

private void handleTable(DatabaseMetaData md, ResultSet tables) throws SQLException {
    String catalog = tables.getString("TABLE_CAT");
    String schema = tables.getString("TABLE_SCHEM");
    String schemaName = normalize(tables.getString("TABLE_SCHEM"));
    String tableName = normalize(tables.getString("TABLE_NAME"));
    String normalizedSchemaName = namingStrategy.normalizeSchemaName(schemaName);
    String normalizedTableName = namingStrategy.normalizeTableName(tableName);
    SchemaAndTable schemaAndTable = new SchemaAndTable(normalizedSchemaName, normalizedTableName);
    if (!namingStrategy.shouldGenerateClass(schemaAndTable)) {
        return;
    }
    String className = namingStrategy.getClassName(schemaAndTable);
    EntityType classModel = createEntityType(schemaAndTable, className);
    if (exportPrimaryKeys) {
        // collect primary keys
        Map<String, PrimaryKeyData> primaryKeyData = keyDataFactory.getPrimaryKeys(md, catalog, schema, tableName);
        if (!primaryKeyData.isEmpty()) {
            classModel.getData().put(PrimaryKeyData.class, primaryKeyData.values());
        }
    }
    if (exportForeignKeys) {
        if (exportDirectForeignKeys) {
            // collect foreign keys
            Map<String, ForeignKeyData> foreignKeyData = keyDataFactory.getImportedKeys(md, catalog, schema, tableName);
            if (!foreignKeyData.isEmpty()) {
                Collection<ForeignKeyData> foreignKeysToGenerate = new LinkedHashSet<ForeignKeyData>();
                for (ForeignKeyData fkd : foreignKeyData.values()) {
                    if (namingStrategy.shouldGenerateForeignKey(schemaAndTable, fkd)) {
                        foreignKeysToGenerate.add(fkd);
                    }
                }
                if (!foreignKeysToGenerate.isEmpty()) {
                    classModel.getData().put(ForeignKeyData.class, foreignKeysToGenerate);
                }
            }
        }
        if (exportInverseForeignKeys) {
            // collect inverse foreign keys
            Map<String, InverseForeignKeyData> inverseForeignKeyData = keyDataFactory.getExportedKeys(md, catalog, schema, tableName);
            if (!inverseForeignKeyData.isEmpty()) {
                classModel.getData().put(InverseForeignKeyData.class, inverseForeignKeyData.values());
            }
        }
    }
    // collect columns
    try (ResultSet columns = md.getColumns(catalog, schema, tableName.replace("/", "//"), null)) {
        while (columns.next()) {
            handleColumn(classModel, tableName, columns);
        }
    }
    // serialize model
    serialize(classModel, schemaAndTable);
    logger.info("Exported " + tableName + " successfully");
}
Also used : SchemaAndTable(com.querydsl.sql.SchemaAndTable) EntityType(com.querydsl.codegen.EntityType) LinkedHashSet(java.util.LinkedHashSet) PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData) ResultSet(java.sql.ResultSet) ForeignKeyData(com.querydsl.sql.codegen.support.ForeignKeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData)

Example 5 with InverseForeignKeyData

use of com.querydsl.sql.codegen.support.InverseForeignKeyData 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;
                break;
            }
        }
    }
    if (inverseForeignKeys != null) {
        for (InverseForeignKeyData keyData : inverseForeignKeys) {
            if (keyData.getForeignColumns().size() > 1) {
                addJavaUtilImport = true;
                break;
            }
        }
    }
    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) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) ForeignKeyData(com.querydsl.sql.codegen.support.ForeignKeyData) InverseForeignKeyData(com.querydsl.sql.codegen.support.InverseForeignKeyData)

Aggregations

InverseForeignKeyData (com.querydsl.sql.codegen.support.InverseForeignKeyData)5 ForeignKeyData (com.querydsl.sql.codegen.support.ForeignKeyData)4 PrimaryKeyData (com.querydsl.sql.codegen.support.PrimaryKeyData)3 ResultSet (java.sql.ResultSet)2 Collection (java.util.Collection)2 EntityType (com.querydsl.codegen.EntityType)1 ClassType (com.querydsl.codegen.utils.model.ClassType)1 SimpleType (com.querydsl.codegen.utils.model.SimpleType)1 Type (com.querydsl.codegen.utils.model.Type)1 AbstractJDBCTest (com.querydsl.sql.AbstractJDBCTest)1 SchemaAndTable (com.querydsl.sql.SchemaAndTable)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 Test (org.junit.Test)1