@Test public void testCreateColumnDefinition() throws NoSuchFieldException { TableInfo testModelTableInfo = ReActiveAndroid.getTableInfo(FullTestModel.class); Field idField = testModelTableInfo.getColumnFields().get(0); Field stringField = FullTestModel.class.getField("stringField"); Field booleanField = FullTestModel.class.getField("booleanField"); assertEquals("`id` INTEGER PRIMARY KEY AUTOINCREMENT", SQLiteUtils.createColumnDefinition(testModelTableInfo, idField)); assertEquals("`stringField` TEXT COLLATE BINARY UNIQUE ON CONFLICT IGNORE", SQLiteUtils.createColumnDefinition(testModelTableInfo, stringField)); assertEquals("`booleanField` INTEGER", SQLiteUtils.createColumnDefinition(testModelTableInfo, booleanField)); }
@NonNull @SuppressWarnings("unchecked") static String createColumnDefinition(TableInfo tableInfo, Field field) { StringBuilder definition = new StringBuilder(); Column column = field.getAnnotation(Column.class); Unique uniqueAnnotation = field.getAnnotation(Unique.class); Class type = field.getType(); ColumnInfo columnInfo = tableInfo.getColumnInfo(field); SQLiteType sqliteType = columnInfo.type; definition.append("`"); definition.append(columnInfo.name); definition.append("`"); definition.append(" "); definition.append(sqliteType.getName()); if (!TextUtils.isEmpty(definition)) { if (columnInfo.name.equals(tableInfo.getPrimaryKeyColumnName())) { definition.append(" PRIMARY KEY AUTOINCREMENT"); } else if (column != null) { if (column.length() > -1) { definition.append("("); definition.append(column.length()); definition.append(")"); } if (column.collate() != Collate.NONE) { definition.append(" COLLATE "); definition.append(column.collate().toString()); } if (column.notNull()) { definition.append(" NOT NULL ON CONFLICT "); definition.append(column.onNullConflict().toString()); } if (uniqueAnnotation != null && uniqueAnnotation.unique()) { definition.append(" UNIQUE ON CONFLICT "); definition.append(uniqueAnnotation.onUniqueConflict().toString()); } } if (ReflectionUtils.isModel(type)) { TableInfo foreignKeyTableInfo = ReActiveAndroid.getTableInfo(type); definition.append(" REFERENCES "); definition.append(foreignKeyTableInfo.getTableName()); definition.append("(`").append(foreignKeyTableInfo.getPrimaryKeyColumnName()).append("`)"); definition.append(" ON DELETE "); definition.append(column.onDelete().toString().replace("_", " ")); definition.append(" ON UPDATE "); definition.append(column.onUpdate().toString().replace("_", " ")); } } else { ReActiveLog.e(LogLevel.BASIC, "No type mapping for: " + type.toString()); } return definition.toString(); }
SQLiteUtils { @NonNull @SuppressWarnings("unchecked") static String createColumnDefinition(TableInfo tableInfo, Field field) { StringBuilder definition = new StringBuilder(); Column column = field.getAnnotation(Column.class); Unique uniqueAnnotation = field.getAnnotation(Unique.class); Class type = field.getType(); ColumnInfo columnInfo = tableInfo.getColumnInfo(field); SQLiteType sqliteType = columnInfo.type; definition.append("`"); definition.append(columnInfo.name); definition.append("`"); definition.append(" "); definition.append(sqliteType.getName()); if (!TextUtils.isEmpty(definition)) { if (columnInfo.name.equals(tableInfo.getPrimaryKeyColumnName())) { definition.append(" PRIMARY KEY AUTOINCREMENT"); } else if (column != null) { if (column.length() > -1) { definition.append("("); definition.append(column.length()); definition.append(")"); } if (column.collate() != Collate.NONE) { definition.append(" COLLATE "); definition.append(column.collate().toString()); } if (column.notNull()) { definition.append(" NOT NULL ON CONFLICT "); definition.append(column.onNullConflict().toString()); } if (uniqueAnnotation != null && uniqueAnnotation.unique()) { definition.append(" UNIQUE ON CONFLICT "); definition.append(uniqueAnnotation.onUniqueConflict().toString()); } } if (ReflectionUtils.isModel(type)) { TableInfo foreignKeyTableInfo = ReActiveAndroid.getTableInfo(type); definition.append(" REFERENCES "); definition.append(foreignKeyTableInfo.getTableName()); definition.append("(`").append(foreignKeyTableInfo.getPrimaryKeyColumnName()).append("`)"); definition.append(" ON DELETE "); definition.append(column.onDelete().toString().replace("_", " ")); definition.append(" ON UPDATE "); definition.append(column.onUpdate().toString().replace("_", " ")); } } else { ReActiveLog.e(LogLevel.BASIC, "No type mapping for: " + type.toString()); } return definition.toString(); } }
SQLiteUtils { @NonNull @SuppressWarnings("unchecked") static String createColumnDefinition(TableInfo tableInfo, Field field) { StringBuilder definition = new StringBuilder(); Column column = field.getAnnotation(Column.class); Unique uniqueAnnotation = field.getAnnotation(Unique.class); Class type = field.getType(); ColumnInfo columnInfo = tableInfo.getColumnInfo(field); SQLiteType sqliteType = columnInfo.type; definition.append("`"); definition.append(columnInfo.name); definition.append("`"); definition.append(" "); definition.append(sqliteType.getName()); if (!TextUtils.isEmpty(definition)) { if (columnInfo.name.equals(tableInfo.getPrimaryKeyColumnName())) { definition.append(" PRIMARY KEY AUTOINCREMENT"); } else if (column != null) { if (column.length() > -1) { definition.append("("); definition.append(column.length()); definition.append(")"); } if (column.collate() != Collate.NONE) { definition.append(" COLLATE "); definition.append(column.collate().toString()); } if (column.notNull()) { definition.append(" NOT NULL ON CONFLICT "); definition.append(column.onNullConflict().toString()); } if (uniqueAnnotation != null && uniqueAnnotation.unique()) { definition.append(" UNIQUE ON CONFLICT "); definition.append(uniqueAnnotation.onUniqueConflict().toString()); } } if (ReflectionUtils.isModel(type)) { TableInfo foreignKeyTableInfo = ReActiveAndroid.getTableInfo(type); definition.append(" REFERENCES "); definition.append(foreignKeyTableInfo.getTableName()); definition.append("(`").append(foreignKeyTableInfo.getPrimaryKeyColumnName()).append("`)"); definition.append(" ON DELETE "); definition.append(column.onDelete().toString().replace("_", " ")); definition.append(" ON UPDATE "); definition.append(column.onUpdate().toString().replace("_", " ")); } } else { ReActiveLog.e(LogLevel.BASIC, "No type mapping for: " + type.toString()); } return definition.toString(); }  }
SQLiteUtils { @NonNull @SuppressWarnings("unchecked") static String createColumnDefinition(TableInfo tableInfo, Field field) { StringBuilder definition = new StringBuilder(); Column column = field.getAnnotation(Column.class); Unique uniqueAnnotation = field.getAnnotation(Unique.class); Class type = field.getType(); ColumnInfo columnInfo = tableInfo.getColumnInfo(field); SQLiteType sqliteType = columnInfo.type; definition.append("`"); definition.append(columnInfo.name); definition.append("`"); definition.append(" "); definition.append(sqliteType.getName()); if (!TextUtils.isEmpty(definition)) { if (columnInfo.name.equals(tableInfo.getPrimaryKeyColumnName())) { definition.append(" PRIMARY KEY AUTOINCREMENT"); } else if (column != null) { if (column.length() > -1) { definition.append("("); definition.append(column.length()); definition.append(")"); } if (column.collate() != Collate.NONE) { definition.append(" COLLATE "); definition.append(column.collate().toString()); } if (column.notNull()) { definition.append(" NOT NULL ON CONFLICT "); definition.append(column.onNullConflict().toString()); } if (uniqueAnnotation != null && uniqueAnnotation.unique()) { definition.append(" UNIQUE ON CONFLICT "); definition.append(uniqueAnnotation.onUniqueConflict().toString()); } } if (ReflectionUtils.isModel(type)) { TableInfo foreignKeyTableInfo = ReActiveAndroid.getTableInfo(type); definition.append(" REFERENCES "); definition.append(foreignKeyTableInfo.getTableName()); definition.append("(`").append(foreignKeyTableInfo.getPrimaryKeyColumnName()).append("`)"); definition.append(" ON DELETE "); definition.append(column.onDelete().toString().replace("_", " ")); definition.append(" ON UPDATE "); definition.append(column.onUpdate().toString().replace("_", " ")); } } else { ReActiveLog.e(LogLevel.BASIC, "No type mapping for: " + type.toString()); } return definition.toString(); }  @NonNull static String getIdentityHash(Collection<TableInfo> tableInfos); @NonNull static ArrayList<String> getAllTableNames(SQLiteDatabase database); @NonNull static String createTableDefinition(TableInfo tableInfo); @NonNull static List<String> createIndexDefinition(TableInfo tableInfo); static SQLiteType getFieldSQLiteType(Field field, Map<Class<?>, TypeSerializer> typeSerializers); static Object getColumnFieldValue(Class<?> modelClass,
                                             Class<?> fieldType,
                                             Cursor cursor,
                                             int columnIndex); static void fillContentValuesForUpdate(Object model,
                                                  ModelAdapter modelAdapter,
                                                  ContentValues contentValues); static void fillContentValuesForInsert(Object model,
                                                  ModelAdapter modelAdapter,
                                                  ContentValues contentValues); }
SQLiteUtils { @NonNull @SuppressWarnings("unchecked") static String createColumnDefinition(TableInfo tableInfo, Field field) { StringBuilder definition = new StringBuilder(); Column column = field.getAnnotation(Column.class); Unique uniqueAnnotation = field.getAnnotation(Unique.class); Class type = field.getType(); ColumnInfo columnInfo = tableInfo.getColumnInfo(field); SQLiteType sqliteType = columnInfo.type; definition.append("`"); definition.append(columnInfo.name); definition.append("`"); definition.append(" "); definition.append(sqliteType.getName()); if (!TextUtils.isEmpty(definition)) { if (columnInfo.name.equals(tableInfo.getPrimaryKeyColumnName())) { definition.append(" PRIMARY KEY AUTOINCREMENT"); } else if (column != null) { if (column.length() > -1) { definition.append("("); definition.append(column.length()); definition.append(")"); } if (column.collate() != Collate.NONE) { definition.append(" COLLATE "); definition.append(column.collate().toString()); } if (column.notNull()) { definition.append(" NOT NULL ON CONFLICT "); definition.append(column.onNullConflict().toString()); } if (uniqueAnnotation != null && uniqueAnnotation.unique()) { definition.append(" UNIQUE ON CONFLICT "); definition.append(uniqueAnnotation.onUniqueConflict().toString()); } } if (ReflectionUtils.isModel(type)) { TableInfo foreignKeyTableInfo = ReActiveAndroid.getTableInfo(type); definition.append(" REFERENCES "); definition.append(foreignKeyTableInfo.getTableName()); definition.append("(`").append(foreignKeyTableInfo.getPrimaryKeyColumnName()).append("`)"); definition.append(" ON DELETE "); definition.append(column.onDelete().toString().replace("_", " ")); definition.append(" ON UPDATE "); definition.append(column.onUpdate().toString().replace("_", " ")); } } else { ReActiveLog.e(LogLevel.BASIC, "No type mapping for: " + type.toString()); } return definition.toString(); }  @NonNull static String getIdentityHash(Collection<TableInfo> tableInfos); @NonNull static ArrayList<String> getAllTableNames(SQLiteDatabase database); @NonNull static String createTableDefinition(TableInfo tableInfo); @NonNull static List<String> createIndexDefinition(TableInfo tableInfo); static SQLiteType getFieldSQLiteType(Field field, Map<Class<?>, TypeSerializer> typeSerializers); static Object getColumnFieldValue(Class<?> modelClass,
                                             Class<?> fieldType,
                                             Cursor cursor,
                                             int columnIndex); static void fillContentValuesForUpdate(Object model,
                                                  ModelAdapter modelAdapter,
                                                  ContentValues contentValues); static void fillContentValuesForInsert(Object model,
                                                  ModelAdapter modelAdapter,
                                                  ContentValues contentValues);  }