}
}
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(type == Float.class || type == Float.TYPE) columnType = Types.FLOAT; // TODO verify
else if(type == Double.class || type == Double.TYPE) columnType = Types.DOUBLE; // TODO verify
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)
column.setSize(""+255); // fixes by default to this value in order to prevent alter tables every time
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;
}