Search in sources :

Example 1 with DatabaseType

use of com.j256.ormlite.db.DatabaseType in project ormlite-android by j256.

the class DatabaseTableConfigUtil method configFromField.

/**
	 * Extract our configuration information from the field by looking for a {@link DatabaseField} annotation.
	 */
private static DatabaseFieldConfig configFromField(DatabaseType databaseType, String tableName, Field field) throws SQLException {
    if (configFieldNums == null) {
        return DatabaseFieldConfig.fromField(databaseType, tableName, field);
    }
    /*
		 * This, unfortunately, we can't get around. This creates a AnnotationFactory, an array of AnnotationMember
		 * fields, and possibly another array of AnnotationMember values. This creates a lot of GC'd objects.
		 */
    DatabaseField databaseField = field.getAnnotation(DatabaseField.class);
    DatabaseFieldConfig config = null;
    try {
        if (databaseField != null) {
            config = buildConfig(databaseField, tableName, field);
        }
    } catch (Exception e) {
    // ignored so we will configure normally below
    }
    if (config == null) {
        /*
			 * We configure this the old way because we might be using javax annotations, have a ForeignCollectionField,
			 * or may still be using the deprecated annotations. At this point we know that there isn't a @DatabaseField
			 * or we can't do our reflection hacks for some reason.
			 */
        return DatabaseFieldConfig.fromField(databaseType, tableName, field);
    } else {
        workedC++;
        return config;
    }
}
Also used : DatabaseField(com.j256.ormlite.field.DatabaseField) DatabaseFieldConfig(com.j256.ormlite.field.DatabaseFieldConfig) SQLException(java.sql.SQLException)

Example 2 with DatabaseType

use of com.j256.ormlite.db.DatabaseType in project ormlite-android by j256.

the class OrmLiteConfigUtil method writeConfigForTable.

private static void writeConfigForTable(BufferedWriter writer, Class<?> clazz) throws SQLException, IOException {
    String tableName = DatabaseTableConfig.extractTableName(clazz);
    List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
    // walk up the classes finding the fields
    try {
        for (Class<?> working = clazz; working != null; working = working.getSuperclass()) {
            for (Field field : working.getDeclaredFields()) {
                DatabaseFieldConfig fieldConfig = DatabaseFieldConfig.fromField(databaseType, tableName, field);
                if (fieldConfig != null) {
                    fieldConfigs.add(fieldConfig);
                }
            }
        }
    } catch (Error e) {
        System.err.println("Skipping " + clazz + " because we got an error finding its definition: " + e.getMessage());
        return;
    }
    if (fieldConfigs.isEmpty()) {
        System.out.println("Skipping " + clazz + " because no annotated fields found");
        return;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" }) DatabaseTableConfig<?> tableConfig = new DatabaseTableConfig(clazz, tableName, fieldConfigs);
    DatabaseTableConfigLoader.write(writer, tableConfig);
    writer.append("#################################");
    writer.newLine();
    System.out.println("Wrote config for " + clazz);
}
Also used : DatabaseField(com.j256.ormlite.field.DatabaseField) Field(java.lang.reflect.Field) ForeignCollectionField(com.j256.ormlite.field.ForeignCollectionField) ArrayList(java.util.ArrayList) DatabaseFieldConfig(com.j256.ormlite.field.DatabaseFieldConfig) DatabaseTableConfig(com.j256.ormlite.table.DatabaseTableConfig)

Example 3 with DatabaseType

use of com.j256.ormlite.db.DatabaseType in project ormlite-android by j256.

the class DatabaseTableConfigUtil method fromClass.

/**
	 * Build our list table config from a class using some annotation fu around.
	 */
public static <T> DatabaseTableConfig<T> fromClass(ConnectionSource connectionSource, Class<T> clazz) throws SQLException {
    DatabaseType databaseType = connectionSource.getDatabaseType();
    String tableName = DatabaseTableConfig.extractTableName(clazz);
    List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
    for (Class<?> classWalk = clazz; classWalk != null; classWalk = classWalk.getSuperclass()) {
        for (Field field : classWalk.getDeclaredFields()) {
            DatabaseFieldConfig config = configFromField(databaseType, tableName, field);
            if (config != null && config.isPersisted()) {
                fieldConfigs.add(config);
            }
        }
    }
    if (fieldConfigs.size() == 0) {
        return null;
    } else {
        return new DatabaseTableConfig<T>(clazz, tableName, fieldConfigs);
    }
}
Also used : DatabaseField(com.j256.ormlite.field.DatabaseField) Field(java.lang.reflect.Field) DatabaseType(com.j256.ormlite.db.DatabaseType) ArrayList(java.util.ArrayList) DatabaseFieldConfig(com.j256.ormlite.field.DatabaseFieldConfig) DatabaseTableConfig(com.j256.ormlite.table.DatabaseTableConfig)

Aggregations

DatabaseField (com.j256.ormlite.field.DatabaseField)3 DatabaseFieldConfig (com.j256.ormlite.field.DatabaseFieldConfig)3 DatabaseTableConfig (com.j256.ormlite.table.DatabaseTableConfig)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 DatabaseType (com.j256.ormlite.db.DatabaseType)1 ForeignCollectionField (com.j256.ormlite.field.ForeignCollectionField)1 SQLException (java.sql.SQLException)1