/** * Convenience method to create the schema for the specified entities using the current settings (table/field name * converter and database provider). * * @param entities the "list" of entity classes to consider for migration. * @see SchemaGenerator#migrate(DatabaseProvider, SchemaConfiguration, NameConverters, boolean, Class[]) */ public void migrate(Class<? extends RawEntity<?>>... entities) throws SQLException { SchemaGenerator.migrate(provider, schemaConfiguration, nameConverters, false, entities); }
static DDLTable[] parseDDL(DatabaseProvider provider, NameConverters nameConverters, Class<? extends RawEntity<?>>... classes) { final Map<Class<? extends RawEntity<?>>, Set<Class<? extends RawEntity<?>>>> deps = new HashMap<Class<? extends RawEntity<?>>, Set<Class<? extends RawEntity<?>>>>(); final Set<Class<? extends RawEntity<?>>> roots = new LinkedHashSet<Class<? extends RawEntity<?>>>(); for (Class<? extends RawEntity<?>> cls : classes) { parseDependencies(nameConverters.getFieldNameConverter(), deps, roots, cls); } ArrayList<DDLTable> parsedTables = new ArrayList<DDLTable>(); parseDDLRoots(provider, nameConverters, deps, roots, parsedTables); if (!deps.isEmpty()) { throw new RuntimeException("Circular dependency detected"); } return parsedTables.toArray(new DDLTable[parsedTables.size()]); }
private static void parseDependencies( final FieldNameConverter fieldConverter, final Map<Class<? extends RawEntity<?>>, Set<Class<? extends RawEntity<?>>>> deps, final Set<Class<? extends RawEntity<?>>> roots, Class<? extends RawEntity<?>> clazz) { if (deps.containsKey(clazz)) { return; } final Set<Class<? extends RawEntity<?>>> individualDeps = new LinkedHashSet<Class<? extends RawEntity<?>>>(); for (final Method method : clazz.getMethods()) { final Class<?> type = Common.getAttributeTypeFromMethod(method); validateManyToManyAnnotation(method); validateOneToOneAnnotation(method); validateOneToManyAnnotation(method); if (fieldConverter.getName(method) != null && type != null && !type.equals(clazz) && RawEntity.class.isAssignableFrom(type) && !individualDeps.contains(type)) { individualDeps.add((Class<? extends RawEntity<?>>) type); addDeps(deps, clazz, individualDeps); parseDependencies(fieldConverter, deps, roots, (Class<? extends RawEntity<?>>) type); } } if (individualDeps.size() == 0) { roots.add(clazz); } else { addDeps(deps, clazz, individualDeps); } }
checkIsSupportedType(method, type); final TypeInfo<?> sqlType = getSQLTypeFromMethod(typeManager, type, method, annotations); field.setType(sqlType); field.setJdbcType(sqlType.getJdbcWriteType()); field.setPrimaryKey(isPrimaryKey(annotations, field)); final boolean isAutoIncrement = isAutoIncrement(type, annotations, field.getType()); field.setAutoIncrement(isAutoIncrement); final Object defaultValue = convertStringDefaultValue(annotations.getAnnotation(Default.class).value(), sqlType, method); if (type.isEnum() && ((Integer) defaultValue) > EnumUtils.size((Class<? extends Enum>) type) - 1) { throw new ActiveObjectsConfigurationException("There is no enum value of '" + type + "'for which the ordinal is " + defaultValue); } else if (ImmutableSet.<Class<?>>of(short.class, float.class, int.class, long.class, double.class).contains(type)) { field.setDefaultValue(convertStringDefaultValue("0", sqlType, method));
@Nullable private DDLIndexField parseIndexField(@Nullable Method method) { if (method == null) { return null; } Class<?> type = Common.getAttributeTypeFromMethod(method); String attributeName = fieldNameConverter.getName(method); AnnotationDelegate annotations = Common.getAnnotationDelegate(fieldNameConverter, method); return DDLIndexField.builder() .fieldName(attributeName) .type(SchemaGenerator.getSQLTypeFromMethod(typeManager, type, method, annotations)) .build(); }
public static void migrate(DatabaseProvider provider, SchemaConfiguration schemaConfiguration, NameConverters nameConverters, final boolean executeDestructiveUpdates, Class<? extends RawEntity<?>>... classes) throws SQLException { final Iterable<Iterable<SQLAction>> actionGroups = generateImpl(provider, schemaConfiguration, nameConverters, executeDestructiveUpdates, classes); final Connection conn = provider.getConnection(); try { final Statement stmt = conn.createStatement(); try { Set<String> completedStatements = new HashSet<String>(); for (Iterable<SQLAction> actionGroup : actionGroups) { addAll(completedStatements, provider.executeUpdatesForActions(stmt, actionGroup, completedStatements)); } } finally { stmt.close(); } } finally { conn.close(); } }
private static Iterable<Iterable<SQLAction>> generateImpl(final DatabaseProvider provider, final SchemaConfiguration schemaConfiguration, final NameConverters nameConverters, final boolean executeDestructiveUpdates, Class<? extends RawEntity<?>>... classes) throws SQLException { final DDLTable[] parsedTables = parseDDL(provider, nameConverters, classes); final DDLTable[] readTables = SchemaReader.readSchema(provider, nameConverters, schemaConfiguration); final DDLAction[] actions = SchemaReader.sortTopologically(SchemaReader.diffSchema(provider.getTypeManager(), parsedTables, readTables, provider.isCaseSensitive())); return Iterables.transform(Iterables.filter(ImmutableList.copyOf(actions), new Predicate<DDLAction>() { @Override public boolean apply(final DDLAction input) { switch (input.getActionType()) { case DROP: case ALTER_DROP_COLUMN: return executeDestructiveUpdates; default: return true; } } }), new Function<DDLAction, Iterable<SQLAction>>() { public Iterable<SQLAction> apply(DDLAction from) { return provider.renderAction(nameConverters, from); } }); }
private static void parseDependencies( final FieldNameConverter fieldConverter, final Map<Class<? extends RawEntity<?>>, Set<Class<? extends RawEntity<?>>>> deps, final Set<Class<? extends RawEntity<?>>> roots, Class<? extends RawEntity<?>> clazz) { if (deps.containsKey(clazz)) { return; } final Set<Class<? extends RawEntity<?>>> individualDeps = new LinkedHashSet<Class<? extends RawEntity<?>>>(); for (final Method method : clazz.getMethods()) { final Class<?> type = Common.getAttributeTypeFromMethod(method); validateManyToManyAnnotation(method); validateOneToOneAnnotation(method); validateOneToManyAnnotation(method); if (fieldConverter.getName(method) != null && type != null && !type.equals(clazz) && RawEntity.class.isAssignableFrom(type) && !individualDeps.contains(type)) { individualDeps.add((Class<? extends RawEntity<?>>) type); addDeps(deps, clazz, individualDeps); parseDependencies(fieldConverter, deps, roots, (Class<? extends RawEntity<?>>) type); } } if (individualDeps.size() == 0) { roots.add(clazz); } else { addDeps(deps, clazz, individualDeps); } }
checkIsSupportedType(method, type); final TypeInfo<?> sqlType = getSQLTypeFromMethod(typeManager, type, method, annotations); field.setType(sqlType); field.setJdbcType(sqlType.getJdbcWriteType()); field.setPrimaryKey(isPrimaryKey(annotations, field)); final boolean isAutoIncrement = isAutoIncrement(type, annotations, field.getType()); field.setAutoIncrement(isAutoIncrement); final Object defaultValue = convertStringDefaultValue(annotations.getAnnotation(Default.class).value(), sqlType, method); if (type.isEnum() && ((Integer) defaultValue) > EnumUtils.size((Class<? extends Enum>) type) - 1) { throw new ActiveObjectsConfigurationException("There is no enum value of '" + type + "'for which the ordinal is " + defaultValue); } else if (ImmutableSet.<Class<?>>of(short.class, float.class, int.class, long.class, double.class).contains(type)) { field.setDefaultValue(convertStringDefaultValue("0", sqlType, method));
@Nullable private DDLIndexField parseIndexField(@Nullable Method method) { if (method == null) { return null; } Class<?> type = Common.getAttributeTypeFromMethod(method); String attributeName = fieldNameConverter.getName(method); AnnotationDelegate annotations = Common.getAnnotationDelegate(fieldNameConverter, method); return DDLIndexField.builder() .fieldName(attributeName) .type(SchemaGenerator.getSQLTypeFromMethod(typeManager, type, method, annotations)) .build(); }
public static void migrate(DatabaseProvider provider, SchemaConfiguration schemaConfiguration, NameConverters nameConverters, final boolean executeDestructiveUpdates, Class<? extends RawEntity<?>>... classes) throws SQLException { final Iterable<Iterable<SQLAction>> actionGroups = generateImpl(provider, schemaConfiguration, nameConverters, executeDestructiveUpdates, classes); final Connection conn = provider.getConnection(); try { final Statement stmt = conn.createStatement(); try { Set<String> completedStatements = new HashSet<String>(); for (Iterable<SQLAction> actionGroup : actionGroups) { addAll(completedStatements, provider.executeUpdatesForActions(stmt, actionGroup, completedStatements)); } } finally { stmt.close(); } } finally { conn.close(); } }
private static Iterable<Iterable<SQLAction>> generateImpl(final DatabaseProvider provider, final SchemaConfiguration schemaConfiguration, final NameConverters nameConverters, final boolean executeDestructiveUpdates, Class<? extends RawEntity<?>>... classes) throws SQLException { final DDLTable[] parsedTables = parseDDL(provider, nameConverters, classes); final DDLTable[] readTables = SchemaReader.readSchema(provider, nameConverters, schemaConfiguration); final DDLAction[] actions = SchemaReader.sortTopologically(SchemaReader.diffSchema(provider.getTypeManager(), parsedTables, readTables, provider.isCaseSensitive())); return Iterables.transform(Iterables.filter(ImmutableList.copyOf(actions), new Predicate<DDLAction>() { @Override public boolean apply(final DDLAction input) { switch (input.getActionType()) { case DROP: case ALTER_DROP_COLUMN: return executeDestructiveUpdates; default: return true; } } }), new Function<DDLAction, Iterable<SQLAction>>() { public Iterable<SQLAction> apply(DDLAction from) { return provider.renderAction(nameConverters, from); } }); }
static DDLTable[] parseDDL(DatabaseProvider provider, NameConverters nameConverters, Class<? extends RawEntity<?>>... classes) { final Map<Class<? extends RawEntity<?>>, Set<Class<? extends RawEntity<?>>>> deps = new HashMap<Class<? extends RawEntity<?>>, Set<Class<? extends RawEntity<?>>>>(); final Set<Class<? extends RawEntity<?>>> roots = new LinkedHashSet<Class<? extends RawEntity<?>>>(); for (Class<? extends RawEntity<?>> cls : classes) { parseDependencies(nameConverters.getFieldNameConverter(), deps, roots, cls); } ArrayList<DDLTable> parsedTables = new ArrayList<DDLTable>(); parseDDLRoots(provider, nameConverters, deps, roots, parsedTables); if (!deps.isEmpty()) { throw new RuntimeException("Circular dependency detected"); } return parsedTables.toArray(new DDLTable[parsedTables.size()]); }
/** * Convenience method to create the schema for the specified entities using the current settings (table/field name * converter and database provider). Note that if the given entities do not include the full set of entities, or * those entities have removed any fields, then the corresponding tables or columns will be dropped, and <b>any data * they contained will be lost</b>. Use this at your own risk. * * @param entities the "list" of entity classes to consider for migration. * @see SchemaGenerator#migrate(DatabaseProvider, SchemaConfiguration, NameConverters, boolean, Class[]) */ public void migrateDestructively(Class<? extends RawEntity<?>>... entities) throws SQLException { SchemaGenerator.migrate(provider, schemaConfiguration, nameConverters, true, entities); }
/** * Convenience method to create the schema for the specified entities using the current settings (table/field name * converter and database provider). * * @param entities the "list" of entity classes to consider for migration. * @see SchemaGenerator#migrate(DatabaseProvider, SchemaConfiguration, NameConverters, boolean, Class[]) */ public void migrate(Class<? extends RawEntity<?>>... entities) throws SQLException { SchemaGenerator.migrate(provider, schemaConfiguration, nameConverters, false, entities); }
/** * Convenience method to create the schema for the specified entities using the current settings (table/field name * converter and database provider). Note that if the given entities do not include the full set of entities, or * those entities have removed any fields, then the corresponding tables or columns will be dropped, and <b>any data * they contained will be lost</b>. Use this at your own risk. * * @param entities the "list" of entity classes to consider for migration. * @see SchemaGenerator#migrate(DatabaseProvider, SchemaConfiguration, NameConverters, boolean, Class[]) */ public void migrateDestructively(Class<? extends RawEntity<?>>... entities) throws SQLException { SchemaGenerator.migrate(provider, schemaConfiguration, nameConverters, true, entities); }