/** * Creates the foreign key list * * @param tablePath the table object * @param columns the columns * @return the foreign key list */ private static List<ForeignKey> createFKs( RelationalPath<?> tablePath, Map<String, Column> columns) { final List<ForeignKey> fks = CollectionFactory.newList(); for (final com.mysema.query.sql.ForeignKey<?> fk : tablePath.getForeignKeys()) { StringBuilder name = new StringBuilder(); final List<Column> cols = CollectionFactory.newList(); for (final Path<?> path : fk.getLocalColumns()) { String colName = path.getMetadata().getExpression().toString(); cols.add(columns.get(colName)); name.append(colName).append("-"); } name.setLength(name.length() - 1); fks.add(new ForeignKey(name.toString(), cols)); } return fks; }
/** * @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); }