Search in sources :

Example 1 with Column

use of com.activeandroid.annotation.Column in project ActiveAndroid by pardom.

the class SQLiteUtils method createColumnDefinition.

@SuppressWarnings("unchecked")
public static String createColumnDefinition(TableInfo tableInfo, Field field) {
    StringBuilder definition = new StringBuilder();
    Class<?> type = field.getType();
    final String name = tableInfo.getColumnName(field);
    final TypeSerializer typeSerializer = Cache.getParserForType(field.getType());
    final Column column = field.getAnnotation(Column.class);
    if (typeSerializer != null) {
        type = typeSerializer.getSerializedType();
    }
    if (TYPE_MAP.containsKey(type)) {
        definition.append(name);
        definition.append(" ");
        definition.append(TYPE_MAP.get(type).toString());
    } else if (ReflectionUtils.isModel(type)) {
        definition.append(name);
        definition.append(" ");
        definition.append(SQLiteType.INTEGER.toString());
    } else if (ReflectionUtils.isSubclassOf(type, Enum.class)) {
        definition.append(name);
        definition.append(" ");
        definition.append(SQLiteType.TEXT.toString());
    }
    if (!TextUtils.isEmpty(definition)) {
        if (name.equals(tableInfo.getIdName())) {
            definition.append(" PRIMARY KEY AUTOINCREMENT");
        } else if (column != null) {
            if (column.length() > -1) {
                definition.append("(");
                definition.append(column.length());
                definition.append(")");
            }
            if (column.notNull()) {
                definition.append(" NOT NULL ON CONFLICT ");
                definition.append(column.onNullConflict().toString());
            }
            if (column.unique()) {
                definition.append(" UNIQUE ON CONFLICT ");
                definition.append(column.onUniqueConflict().toString());
            }
        }
        if (FOREIGN_KEYS_SUPPORTED && ReflectionUtils.isModel(type)) {
            definition.append(" REFERENCES ");
            definition.append(Cache.getTableInfo((Class<? extends Model>) type).getTableName());
            definition.append("(" + tableInfo.getIdName() + ")");
            definition.append(" ON DELETE ");
            definition.append(column.onDelete().toString().replace("_", " "));
            definition.append(" ON UPDATE ");
            definition.append(column.onUpdate().toString().replace("_", " "));
        }
    } else {
        Log.e("No type mapping for: " + type.toString());
    }
    return definition.toString();
}
Also used : Column(com.activeandroid.annotation.Column) TypeSerializer(com.activeandroid.serializer.TypeSerializer) String(java.lang.String)

Example 2 with Column

use of com.activeandroid.annotation.Column in project ActiveAndroid by pardom.

the class SQLiteUtils method createUniqueColumnDefinition.

public static void createUniqueColumnDefinition(TableInfo tableInfo, Field field) {
    final String name = tableInfo.getColumnName(field);
    final Column column = field.getAnnotation(Column.class);
    if (field.getName().equals("mId")) {
        return;
    }
    String[] groups = column.uniqueGroups();
    ConflictAction[] conflictActions = column.onUniqueConflicts();
    if (groups.length != conflictActions.length)
        return;
    for (int i = 0; i < groups.length; i++) {
        String group = groups[i];
        ConflictAction conflictAction = conflictActions[i];
        if (TextUtils.isEmpty(group))
            continue;
        List<String> list = sUniqueGroupMap.get(group);
        if (list == null) {
            list = new ArrayList<String>();
        }
        list.add(name);
        sUniqueGroupMap.put(group, list);
        sOnUniqueConflictsMap.put(group, conflictAction);
    }
}
Also used : ConflictAction(com.activeandroid.annotation.Column.ConflictAction) Column(com.activeandroid.annotation.Column) String(java.lang.String)

Example 3 with Column

use of com.activeandroid.annotation.Column in project ActiveAndroid by pardom.

the class SQLiteUtils method createIndexColumnDefinition.

public static void createIndexColumnDefinition(TableInfo tableInfo, Field field) {
    final String name = tableInfo.getColumnName(field);
    final Column column = field.getAnnotation(Column.class);
    if (field.getName().equals("mId")) {
        return;
    }
    if (column.index()) {
        List<String> list = new ArrayList<String>();
        list.add(name);
        sIndexGroupMap.put(name, list);
    }
    String[] groups = column.indexGroups();
    for (String group : groups) {
        if (TextUtils.isEmpty(group))
            continue;
        List<String> list = sIndexGroupMap.get(group);
        if (list == null) {
            list = new ArrayList<String>();
        }
        list.add(name);
        sIndexGroupMap.put(group, list);
    }
}
Also used : Column(com.activeandroid.annotation.Column) ArrayList(java.util.ArrayList) String(java.lang.String)

Aggregations

Column (com.activeandroid.annotation.Column)3 String (java.lang.String)3 ConflictAction (com.activeandroid.annotation.Column.ConflictAction)1 TypeSerializer (com.activeandroid.serializer.TypeSerializer)1 ArrayList (java.util.ArrayList)1