/** * @param tablePath the query entity * @param path the path * @return the column object */ private static Column createColumn(RelationalPath<?> tablePath, Path<?> path) { final String columnName = path.getMetadata().getExpression().toString(); final ColumnType columnType = getColumnType(path.getType()); final Field field = ClassUtils.getFieldWithValue(tablePath, path); // settings int columnSize = -1; boolean columnNullable = false; boolean hasDefaultValue = false; String defaultValue = null; if (field != null) { final ColumnSize size = field.getAnnotation(ColumnSize.class); if (size != null) { columnSize = size.value(); } final ColumnAutoIncrement autoInc = field.getAnnotation(ColumnAutoIncrement.class); if (autoInc != null) { return new Column(columnName, columnSize, true); } final ColumnNullable nullable = field.getAnnotation(ColumnNullable.class); if (nullable == null) { columnNullable = false; } else { columnNullable = true; } final ColumnDefault def = field.getAnnotation(ColumnDefault.class); if (def == null && nullable != null) { hasDefaultValue = true; defaultValue = null; } else if (def != null) { hasDefaultValue = true; defaultValue = def.value(); } } if (columnType == ColumnType.ENUM) { @SuppressWarnings("unchecked") Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) path.getType(); Enum<?>[] enums = enumClass.getEnumConstants(); final List<String> enumValues = CollectionFactory.newList(); for (final Enum<?> e : enums) { enumValues.add(e.name()); } return new Column(columnName, columnNullable, enumValues, hasDefaultValue, defaultValue); } return new Column( columnName, columnType, columnNullable, columnSize, hasDefaultValue, defaultValue); }
/** * @param type the java type * @return the database column type */ private static ColumnType getColumnType(Class<?> type) { if (ClassUtils.isSubclass(type, String.class)) return ColumnType.STRING; else if (type.isEnum()) return ColumnType.ENUM; else if (ClassUtils.isSubclass(type, Integer.class)) return ColumnType.INTEGER; else if (ClassUtils.isSubclass(type, Long.class)) return ColumnType.INTEGER; else if (ClassUtils.isSubclass(type, Double.class)) return ColumnType.DOUBLE; else if (ClassUtils.isSubclass(type, Float.class)) return ColumnType.DOUBLE; else if (ClassUtils.isSubclass(type, Date.class)) return ColumnType.TIMESTAMP; return null; }