com.activeandroid.TableInfo Java Examples

The following examples show how to use com.activeandroid.TableInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContentProvider.java    From clear-todolist with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onCreate() {
	ActiveAndroid.initialize(getConfiguration());
	sAuthority = getAuthority();

	final List<TableInfo> tableInfos = new ArrayList<TableInfo>(Cache.getTableInfos());
	final int size = tableInfos.size();
	for (int i = 0; i < size; i++) {
		final TableInfo tableInfo = tableInfos.get(i);
		final int tableKey = (i * 2) + 1;
		final int itemKey = (i * 2) + 2;

		// content://<authority>/<table>
		URI_MATCHER.addURI(sAuthority, tableInfo.getTableName().toLowerCase(), tableKey);
		TYPE_CODES.put(tableKey, tableInfo.getType());

		// content://<authority>/<table>/<id>
		URI_MATCHER.addURI(sAuthority, tableInfo.getTableName().toLowerCase() + "/#", itemKey);
		TYPE_CODES.put(itemKey, tableInfo.getType());
	}

	return true;
}
 
Example #2
Source File: SQLiteUtils.java    From clear-todolist with GNU General Public License v3.0 6 votes vote down vote up
public static ArrayList<String> createUniqueDefinition(TableInfo tableInfo) {
	final ArrayList<String> definitions = new ArrayList<String>();
	sUniqueGroupMap = new HashMap<String, List<String>>();
	sOnUniqueConflictsMap = new HashMap<String, ConflictAction>();

	for (Field field : tableInfo.getFields()) {
		createUniqueColumnDefinition(tableInfo, field);
	}

	if (sUniqueGroupMap.isEmpty()) {
		return definitions;
	}

	Set<String> keySet = sUniqueGroupMap.keySet();
	for (String key : keySet) {
		List<String> group = sUniqueGroupMap.get(key);
		ConflictAction conflictAction = sOnUniqueConflictsMap.get(key);

		definitions.add(String.format("UNIQUE (%s) ON CONFLICT %s",
				TextUtils.join(", ", group), conflictAction.toString()));
	}

	return definitions;
}
 
Example #3
Source File: SQLiteUtils.java    From clear-todolist with GNU General Public License v3.0 6 votes vote down vote up
public static String[] createIndexDefinition(TableInfo tableInfo) {
	final ArrayList<String> definitions = new ArrayList<String>();
	sIndexGroupMap = new HashMap<String, List<String>>();

	for (Field field : tableInfo.getFields()) {
		createIndexColumnDefinition(tableInfo, field);
	}

	if (sIndexGroupMap.isEmpty()) {
		return new String[0];
	}

	for (Map.Entry<String, List<String>> entry : sIndexGroupMap.entrySet()) {
		definitions.add(String.format("CREATE INDEX IF NOT EXISTS %s on %s(%s);",
				"index_" + tableInfo.getTableName() + "_" + entry.getKey(),
				tableInfo.getTableName(), TextUtils.join(", ", entry.getValue())));
	}

	return definitions.toArray(new String[definitions.size()]);
}
 
Example #4
Source File: ContentProvider.java    From mobile-android-survey-app with MIT License 6 votes vote down vote up
@Override
public boolean onCreate() {
	ActiveAndroid.initialize(getConfiguration());
	sAuthority = getAuthority();

	final List<TableInfo> tableInfos = new ArrayList<TableInfo>(Cache.getTableInfos());
	final int size = tableInfos.size();
	for (int i = 0; i < size; i++) {
		final TableInfo tableInfo = tableInfos.get(i);
		final int tableKey = (i * 2) + 1;
		final int itemKey = (i * 2) + 2;

		// content://<authority>/<table>
		URI_MATCHER.addURI(sAuthority, tableInfo.getTableName().toLowerCase(), tableKey);
		TYPE_CODES.put(tableKey, tableInfo.getType());

		// content://<authority>/<table>/<id>
		URI_MATCHER.addURI(sAuthority, tableInfo.getTableName().toLowerCase() + "/#", itemKey);
		TYPE_CODES.put(itemKey, tableInfo.getType());
	}

	return true;
}
 
Example #5
Source File: SQLiteUtils.java    From mobile-android-survey-app with MIT License 6 votes vote down vote up
public static ArrayList<String> createUniqueDefinition(TableInfo tableInfo) {
	final ArrayList<String> definitions = new ArrayList<String>();
	sUniqueGroupMap = new HashMap<String, List<String>>();
	sOnUniqueConflictsMap = new HashMap<String, ConflictAction>();

	for (Field field : tableInfo.getFields()) {
		createUniqueColumnDefinition(tableInfo, field);
	}

	if (sUniqueGroupMap.isEmpty()) {
		return definitions;
	}

	Set<String> keySet = sUniqueGroupMap.keySet();
	for (String key : keySet) {
		List<String> group = sUniqueGroupMap.get(key);
		ConflictAction conflictAction = sOnUniqueConflictsMap.get(key);

		definitions.add(String.format("UNIQUE (%s) ON CONFLICT %s",
				TextUtils.join(", ", group), conflictAction.toString()));
	}

	return definitions;
}
 
Example #6
Source File: SQLiteUtils.java    From mobile-android-survey-app with MIT License 6 votes vote down vote up
public static String[] createIndexDefinition(TableInfo tableInfo) {
	final ArrayList<String> definitions = new ArrayList<String>();
	sIndexGroupMap = new HashMap<String, List<String>>();

	for (Field field : tableInfo.getFields()) {
		createIndexColumnDefinition(tableInfo, field);
	}

	if (sIndexGroupMap.isEmpty()) {
		return new String[0];
	}

	for (Map.Entry<String, List<String>> entry : sIndexGroupMap.entrySet()) {
		definitions.add(String.format("CREATE INDEX IF NOT EXISTS %s on %s(%s);",
				"index_" + tableInfo.getTableName() + "_" + entry.getKey(),
				tableInfo.getTableName(), TextUtils.join(", ", entry.getValue())));
	}

	return definitions.toArray(new String[definitions.size()]);
}
 
Example #7
Source File: SQLiteUtils.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
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);
	}
}
 
Example #8
Source File: SQLiteUtils.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
public static String createTableDefinition(TableInfo tableInfo) {
	final ArrayList<String> definitions = new ArrayList<String>();

	for (Field field : tableInfo.getFields()) {
		String definition = createColumnDefinition(tableInfo, field);
		if (!TextUtils.isEmpty(definition)) {
			definitions.add(definition);
		}
	}

	definitions.addAll(createUniqueDefinition(tableInfo));

	return String.format("CREATE TABLE IF NOT EXISTS %s (%s);", tableInfo.getTableName(),
			TextUtils.join(", ", definitions));
}
 
Example #9
Source File: ModelTest.java    From clear-todolist with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Column names should default to the field name.
 */
public void testColumnNamesDefaulToFieldNames() {
    TableInfo tableInfo = Cache.getTableInfo(MockModel.class);

    for ( Field field : tableInfo.getFields() ) {
        // Id column is a special case, we'll ignore that one.
        if ( field.getName().equals("mId") ) continue;

        assertEquals(field.getName(), tableInfo.getColumnName(field));
    }
}
 
Example #10
Source File: SQLiteUtils.java    From mobile-android-survey-app with MIT License 5 votes vote down vote up
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);
	}
}
 
Example #11
Source File: SQLiteUtils.java    From mobile-android-survey-app with MIT License 5 votes vote down vote up
public static String createTableDefinition(TableInfo tableInfo) {
	final ArrayList<String> definitions = new ArrayList<String>();

	for (Field field : tableInfo.getFields()) {
		String definition = createColumnDefinition(tableInfo, field);
		if (!TextUtils.isEmpty(definition)) {
			definitions.add(definition);
		}
	}

	definitions.addAll(createUniqueDefinition(tableInfo));

	return String.format("CREATE TABLE IF NOT EXISTS %s (%s);", tableInfo.getTableName(),
			TextUtils.join(", ", definitions));
}
 
Example #12
Source File: SQLiteUtils.java    From clear-todolist with GNU General Public License v3.0 4 votes vote down vote up
@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();
}
 
Example #13
Source File: SQLiteUtils.java    From mobile-android-survey-app with MIT License 4 votes vote down vote up
@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();
}