Search in sources :

Example 1 with PrimaryKeyData

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

the class KeyDataFactory method getPrimaryKeys.

public Map<String, PrimaryKeyData> getPrimaryKeys(DatabaseMetaData md, String catalog, String schema, String tableName) throws SQLException {
    ResultSet primaryKeys = md.getPrimaryKeys(catalog, schema, tableName);
    Map<String, PrimaryKeyData> primaryKeyData = new HashMap<String, PrimaryKeyData>();
    try {
        while (primaryKeys.next()) {
            String name = primaryKeys.getString(PK_NAME);
            String columnName = primaryKeys.getString(PK_COLUMN_NAME);
            if (name == null || name.isEmpty()) {
                name = tableName + "_PK";
            }
            PrimaryKeyData data = primaryKeyData.get(name);
            if (data == null) {
                data = new PrimaryKeyData(name);
                primaryKeyData.put(name, data);
            }
            data.add(columnName);
        }
        return primaryKeyData;
    } finally {
        primaryKeys.close();
    }
}
Also used : PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet)

Example 2 with PrimaryKeyData

use of com.querydsl.sql.codegen.support.PrimaryKeyData 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 PrimaryKeyData

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

the class ExtendedBeanSerializer method bodyEnd.

@SuppressWarnings("unchecked")
@Override
protected void bodyEnd(EntityType model, CodeWriter writer) throws IOException {
    Collection<PrimaryKeyData> primaryKeys = (Collection<PrimaryKeyData>) model.getData().get(PrimaryKeyData.class);
    if (primaryKeys == null || primaryKeys.isEmpty()) {
        return;
    }
    Map<String, Property> columnToProperty = new HashMap<String, Property>();
    for (Property property : model.getProperties()) {
        columnToProperty.put(property.getAnnotation(Column.class).value(), property);
    }
    StringBuilder anyColumnIsNull = new StringBuilder();
    StringBuilder columnEquals = new StringBuilder();
    StringBuilder toString = new StringBuilder();
    List<String> properties = new ArrayList<String>();
    for (PrimaryKeyData pk : primaryKeys) {
        for (String column : pk.getColumns()) {
            Property property = columnToProperty.get(column);
            String propName = property.getEscapedName();
            if (anyColumnIsNull.length() > 0) {
                anyColumnIsNull.append(" || ");
                columnEquals.append(" && ");
                toString.append("+ \";\" + ");
            } else {
                toString.append("\"" + model.getSimpleName() + "#\" + ");
            }
            anyColumnIsNull.append(propName + " == null");
            columnEquals.append(propName + ".equals(obj." + propName + ")");
            toString.append(propName);
            properties.add(propName);
        }
    }
    // equals
    writer.annotation(Override.class);
    writer.beginPublicMethod(Types.BOOLEAN_P, "equals", o);
    writer.line("if (", anyColumnIsNull + ") {");
    writer.line("    return super.equals(o);");
    writer.line("}");
    writer.line("if (!(o instanceof ", model.getSimpleName(), ")) {");
    writer.line("    return false;");
    writer.line("}");
    writer.line(model.getSimpleName(), " obj = (", model.getSimpleName(), ") o;");
    writer.line("return ", columnEquals + ";");
    writer.end();
    // hashCode
    writer.annotation(Override.class);
    writer.beginPublicMethod(Types.INT, "hashCode");
    writer.line("if (", anyColumnIsNull + ") {");
    writer.line("    return super.hashCode();");
    writer.line("}");
    writer.line("final int prime = 31;");
    writer.line("int result = 1;");
    for (String property : properties) {
        writer.line("result = prime * result + ", property, ".hashCode();");
    }
    writer.line("return result;");
    writer.end();
    // toString
    writer.annotation(Override.class);
    writer.beginPublicMethod(Types.STRING, "toString");
    //        writer.line("if (", anyColumnIsNull + ") {");
    //        writer.line("    return super.toString();");
    //        writer.line("}");
    writer.line("return ", toString + ";");
    writer.end();
}
Also used : PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) Property(com.querydsl.codegen.Property)

Example 4 with PrimaryKeyData

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

the class MetaDataSerializer method serializePrimaryKeys.

protected void serializePrimaryKeys(EntityType model, CodeWriter writer, Collection<PrimaryKeyData> primaryKeys) throws IOException {
    for (PrimaryKeyData primaryKey : primaryKeys) {
        String fieldName = namingStrategy.getPropertyNameForPrimaryKey(primaryKey.getName(), model);
        StringBuilder value = new StringBuilder("createPrimaryKey(");
        boolean first = true;
        for (String column : primaryKey.getColumns()) {
            if (!first) {
                value.append(", ");
            }
            value.append(namingStrategy.getPropertyName(column, model));
            first = false;
        }
        value.append(")");
        Type type = new ClassType(PrimaryKey.class, model);
        writer.publicFinal(type, fieldName, value.toString());
    }
}
Also used : PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData)

Aggregations

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