Search in sources :

Example 1 with Max

use of siena.Max in project siena by mandubian.

the class DdlGenerator method createColumn.

private Column createColumn(Class<?> clazz, Field field, String col) {
    Class<?> type = field.getType();
    Column column = new Column();
    column.setName(col);
    int columnType;
    if (type == Byte.class || type == Byte.TYPE)
        columnType = Types.TINYINT;
    else if (type == Short.class || type == Short.TYPE)
        columnType = Types.SMALLINT;
    else if (type == Integer.class || type == Integer.TYPE)
        columnType = Types.INTEGER;
    else if (type == Long.class || type == Long.TYPE)
        columnType = Types.BIGINT;
    else if (// TODO verify
    type == Float.class || type == Float.TYPE)
        // TODO verify
        columnType = Types.FLOAT;
    else if (// TODO verify
    type == Double.class || type == Double.TYPE)
        // TODO verify
        columnType = Types.DOUBLE;
    else if (type == String.class) {
        if (field.getAnnotation(Text.class) != null) {
            columnType = Types.LONGVARCHAR;
        } else {
            columnType = Types.VARCHAR;
            Max max = field.getAnnotation(Max.class);
            if (max == null) {
                //throw new SienaRestrictedApiException(DB, "createColumn", "Field "+field.getName()+" in class "
                //		+clazz.getName()+" doesn't have a @Max annotation");
                // default is 255 chars as in hibernate
                column.setSize("255");
            } else
                column.setSize("" + max.value());
        }
    } else if (type == Boolean.class || type == Boolean.TYPE)
        columnType = Types.BOOLEAN;
    else if (type == Date.class) {
        if (field.getAnnotation(DateTime.class) != null)
            columnType = Types.TIMESTAMP;
        else if (field.getAnnotation(Time.class) != null)
            columnType = Types.TIME;
        else if (field.getAnnotation(SimpleDate.class) != null)
            columnType = Types.DATE;
        else
            columnType = Types.TIMESTAMP;
    } else if (type == Json.class) {
        columnType = Types.LONGVARCHAR;
    } else if (type == byte[].class) {
        columnType = Types.BLOB;
    } else if (Enum.class.isAssignableFrom(type)) {
        // enums are stored as string
        columnType = Types.VARCHAR;
        Max max = field.getAnnotation(Max.class);
        if (max == null)
            // fixes by default to this value in order to prevent alter tables every time
            column.setSize("" + 255);
        else
            column.setSize("" + max.value());
    } else if (type == BigDecimal.class) {
        DecimalPrecision an = field.getAnnotation(DecimalPrecision.class);
        if (an == null) {
            columnType = Types.DECIMAL;
            column.setSizeAndScale(19, 2);
        } else {
            if (an.storageType() == DecimalPrecision.StorageType.NATIVE) {
                columnType = Types.DECIMAL;
                column.setSizeAndScale(an.size(), an.scale());
            } else if (an.storageType() == DecimalPrecision.StorageType.STRING) {
                columnType = Types.VARCHAR;
                // should be an.size+"."+sign
                column.setSize((an.size() + 2) + "");
            } else if (an.storageType() == DecimalPrecision.StorageType.DOUBLE) {
                columnType = Types.DOUBLE;
            } else {
                columnType = Types.DECIMAL;
                column.setSizeAndScale(19, 2);
            }
        }
    } else {
        Embedded embedded = field.getAnnotation(Embedded.class);
        if (embedded != null) {
            if ("h2".equals(DB)) {
                columnType = Types.CLOB;
            } else {
                columnType = Types.LONGVARCHAR;
            }
        } else if (field.isAnnotationPresent(Polymorphic.class)) {
            columnType = Types.BLOB;
        } else {
            throw new SienaRestrictedApiException(DB, "createColumn", "Unsupported type for field " + clazz.getName() + "." + field.getName());
        }
    }
    column.setTypeCode(columnType);
    return column;
}
Also used : Max(siena.Max) SienaRestrictedApiException(siena.SienaRestrictedApiException) DecimalPrecision(siena.core.DecimalPrecision) Text(siena.Text) Json(siena.Json) DateTime(siena.DateTime) IndexColumn(org.apache.ddlutils.model.IndexColumn) Column(org.apache.ddlutils.model.Column) SimpleDate(siena.SimpleDate) Embedded(siena.embed.Embedded) Polymorphic(siena.core.Polymorphic)

Aggregations

Column (org.apache.ddlutils.model.Column)1 IndexColumn (org.apache.ddlutils.model.IndexColumn)1 DateTime (siena.DateTime)1 Json (siena.Json)1 Max (siena.Max)1 SienaRestrictedApiException (siena.SienaRestrictedApiException)1 SimpleDate (siena.SimpleDate)1 Text (siena.Text)1 DecimalPrecision (siena.core.DecimalPrecision)1 Polymorphic (siena.core.Polymorphic)1 Embedded (siena.embed.Embedded)1