Search in sources :

Example 1 with ColumnImpl

use of com.querydsl.sql.ColumnImpl in project querydsl by querydsl.

the class ExtendedBeanSerializerTest method equals_hashcode_tostring.

@Test
public void equals_hashcode_tostring() throws Exception {
    Property idCol = new Property(type, "id", new ClassType(Integer.class));
    idCol.addAnnotation(new ColumnImpl("ID"));
    Property subIdCol = new Property(type, "sub_id", new ClassType(String.class));
    subIdCol.addAnnotation(new ColumnImpl("SUB_ID"));
    Property nameCol = new Property(type, "name", new ClassType(String.class));
    nameCol.addAnnotation(new ColumnImpl("NAME"));
    type.addProperty(idCol);
    type.addProperty(subIdCol);
    type.addProperty(nameCol);
    type.getData().put(PrimaryKeyData.class, Arrays.asList(new PrimaryKeyData("PK", new String[] { "ID", "SUB_ID" })));
    ExtendedBeanSerializer extendedBeanSerializer = new ExtendedBeanSerializer();
    extendedBeanSerializer.setAddToString(true);
    FileWriter fw = new FileWriter(srcFile);
    extendedBeanSerializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(fw));
    fw.close();
    URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { compileFolder.getRoot().toURI().toURL() });
    int retCode = new SimpleCompiler().run(null, System.out, System.err, srcFile.getAbsolutePath());
    assertEquals("The generated source should compile", 0, retCode);
    Class<?> cls = Class.forName(FULL_NAME, true, classLoader);
    ReflectionHelper reflection = new ReflectionHelper(cls);
    Object obj1 = cls.newInstance();
    Object obj1a = cls.newInstance();
    Object obj2 = cls.newInstance();
    reflection.setValues(obj1, 1, "##", "X");
    reflection.setValues(obj1a, 1, "##", null);
    reflection.setValues(obj2, 2, "--", "Y");
    assertEquals(obj1, obj1a);
    assertEquals(obj1.hashCode(), obj1a.hashCode());
    assertNotEquals(obj1, obj2);
    assertEquals(obj1.toString(), "DomainClass#1;##");
}
Also used : SimpleCompiler(com.querydsl.codegen.utils.SimpleCompiler) FileWriter(java.io.FileWriter) JavaWriter(com.querydsl.codegen.utils.JavaWriter) ClassType(com.querydsl.codegen.utils.model.ClassType) PrimaryKeyData(com.querydsl.sql.codegen.support.PrimaryKeyData) URLClassLoader(java.net.URLClassLoader) ColumnImpl(com.querydsl.sql.ColumnImpl) Property(com.querydsl.codegen.Property) Test(org.junit.Test)

Example 2 with ColumnImpl

use of com.querydsl.sql.ColumnImpl in project querydsl by querydsl.

the class MetaDataExporter method handleColumn.

private void handleColumn(EntityType classModel, String tableName, ResultSet columns) throws SQLException {
    String columnName = normalize(columns.getString("COLUMN_NAME"));
    String normalizedColumnName = namingStrategy.normalizeColumnName(columnName);
    int columnType = columns.getInt("DATA_TYPE");
    String typeName = columns.getString("TYPE_NAME");
    Number columnSize = (Number) columns.getObject("COLUMN_SIZE");
    Number columnDigits = (Number) columns.getObject("DECIMAL_DIGITS");
    int columnIndex = columns.getInt("ORDINAL_POSITION");
    int nullable = columns.getInt("NULLABLE");
    String columnDefaultValue = columns.getString("COLUMN_DEF");
    String propertyName = namingStrategy.getPropertyName(normalizedColumnName, classModel);
    Class<?> clazz = configuration.getJavaType(columnType, typeName, columnSize != null ? columnSize.intValue() : 0, columnDigits != null ? columnDigits.intValue() : 0, tableName, columnName);
    if (clazz == null) {
        clazz = Object.class;
    }
    TypeCategory fieldType = TypeCategory.get(clazz.getName());
    if (Number.class.isAssignableFrom(clazz)) {
        fieldType = TypeCategory.NUMERIC;
    } else if (Enum.class.isAssignableFrom(clazz)) {
        fieldType = TypeCategory.ENUM;
    }
    Type typeModel = new ClassType(fieldType, clazz);
    Property property = createProperty(classModel, normalizedColumnName, propertyName, typeModel);
    ColumnMetadata column = ColumnMetadata.named(normalizedColumnName).ofType(columnType).withIndex(columnIndex);
    if (nullable == DatabaseMetaData.columnNoNulls) {
        column = column.notNull();
    }
    if (columnSize != null) {
        column = column.withSize(columnSize.intValue());
    }
    if (columnDigits != null) {
        column = column.withDigits(columnDigits.intValue());
    }
    property.getData().put("COLUMN", column);
    if (columnAnnotations) {
        property.addAnnotation(new ColumnImpl(normalizedColumnName));
    }
    if (validationAnnotations) {
        if (nullable == DatabaseMetaData.columnNoNulls && columnDefaultValue == null) {
            property.addAnnotation(new NotNullImpl());
        }
        int size = columns.getInt("COLUMN_SIZE");
        if (size > 0 && clazz.equals(String.class)) {
            property.addAnnotation(new SizeImpl(0, size));
        }
    }
    classModel.addProperty(property);
}
Also used : ColumnMetadata(com.querydsl.sql.ColumnMetadata) NotNullImpl(com.querydsl.sql.codegen.support.NotNullImpl) ClassType(com.querydsl.codegen.utils.model.ClassType) SizeImpl(com.querydsl.sql.codegen.support.SizeImpl) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.querydsl.codegen.utils.model.SimpleType) Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) TypeCategory(com.querydsl.codegen.utils.model.TypeCategory) ColumnImpl(com.querydsl.sql.ColumnImpl) Property(com.querydsl.codegen.Property)

Aggregations

Property (com.querydsl.codegen.Property)2 ClassType (com.querydsl.codegen.utils.model.ClassType)2 ColumnImpl (com.querydsl.sql.ColumnImpl)2 EntityType (com.querydsl.codegen.EntityType)1 JavaWriter (com.querydsl.codegen.utils.JavaWriter)1 SimpleCompiler (com.querydsl.codegen.utils.SimpleCompiler)1 SimpleType (com.querydsl.codegen.utils.model.SimpleType)1 Type (com.querydsl.codegen.utils.model.Type)1 TypeCategory (com.querydsl.codegen.utils.model.TypeCategory)1 ColumnMetadata (com.querydsl.sql.ColumnMetadata)1 NotNullImpl (com.querydsl.sql.codegen.support.NotNullImpl)1 PrimaryKeyData (com.querydsl.sql.codegen.support.PrimaryKeyData)1 SizeImpl (com.querydsl.sql.codegen.support.SizeImpl)1 FileWriter (java.io.FileWriter)1 URLClassLoader (java.net.URLClassLoader)1 Test (org.junit.Test)1