Search in sources :

Example 1 with SizeImpl

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

EntityType (com.querydsl.codegen.EntityType)1 Property (com.querydsl.codegen.Property)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 TypeCategory (com.querydsl.codegen.utils.model.TypeCategory)1 ColumnImpl (com.querydsl.sql.ColumnImpl)1 ColumnMetadata (com.querydsl.sql.ColumnMetadata)1 NotNullImpl (com.querydsl.sql.codegen.support.NotNullImpl)1 SizeImpl (com.querydsl.sql.codegen.support.SizeImpl)1