public static SatsClass copyOrUpdate( Realm realm, SatsClass object, boolean update, Map<RealmObject, RealmObject> cache) { if (object.realm != null && object.realm.getId() == realm.getId()) { return object; } SatsClass realmObject = null; boolean canUpdate = update; if (canUpdate) { Table table = realm.getTable(SatsClass.class); long pkColumnIndex = table.getPrimaryKey(); long rowIndex = table.findFirstString(pkColumnIndex, object.getId()); if (rowIndex != TableOrView.NO_MATCH) { realmObject = new SatsClassRealmProxy(); realmObject.realm = realm; realmObject.row = table.getRow(rowIndex); cache.put(object, realmObject); } else { canUpdate = false; } } if (canUpdate) { return update(realm, realmObject, object, cache); } else { return copy(realm, object, update, cache); } }
private long getIndexForProperty(Table table, String name) { for (int i = 0; i < table.getColumnCount(); i++) { if (table.getColumnName(i).equals(name)) { return i; } } return -1; }
public static Table initTable(ImplicitTransaction transaction) { if (!transaction.hasTable("class_Simple")) { Table table = transaction.getTable("class_Simple"); table.addColumn(RealmFieldType.STRING, "name", Table.NULLABLE); table.addColumn(RealmFieldType.INTEGER, "age", Table.NOT_NULLABLE); table.setPrimaryKey(""); return table; } return transaction.getTable("class_Simple"); }
public static void insertOrUpdate( Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel, Long> cache) { Table table = realm.getTable(some.test.Booleans.class); long tableNativePtr = table.getNativeTablePointer(); BooleansColumnInfo columnInfo = (BooleansColumnInfo) realm.schema.getColumnInfo(some.test.Booleans.class); some.test.Booleans object = null; while (objects.hasNext()) { object = (some.test.Booleans) objects.next(); if (!cache.containsKey(object)) { long rowIndex = Table.nativeAddEmptyRow(tableNativePtr, 1); cache.put(object, rowIndex); Table.nativeSetBoolean( tableNativePtr, columnInfo.doneIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$done()); Table.nativeSetBoolean( tableNativePtr, columnInfo.isReadyIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$isReady()); Table.nativeSetBoolean( tableNativePtr, columnInfo.mCompletedIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$mCompleted()); Table.nativeSetBoolean( tableNativePtr, columnInfo.anotherBooleanIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$anotherBoolean()); } } }
public static long insertOrUpdate( Realm realm, some.test.Booleans object, Map<RealmModel, Long> cache) { Table table = realm.getTable(some.test.Booleans.class); long tableNativePtr = table.getNativeTablePointer(); BooleansColumnInfo columnInfo = (BooleansColumnInfo) realm.schema.getColumnInfo(some.test.Booleans.class); long rowIndex = Table.nativeAddEmptyRow(tableNativePtr, 1); cache.put(object, rowIndex); Table.nativeSetBoolean( tableNativePtr, columnInfo.doneIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$done()); Table.nativeSetBoolean( tableNativePtr, columnInfo.isReadyIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$isReady()); Table.nativeSetBoolean( tableNativePtr, columnInfo.mCompletedIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$mCompleted()); Table.nativeSetBoolean( tableNativePtr, columnInfo.anotherBooleanIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$anotherBoolean()); return rowIndex; }
public static long insert(Realm realm, some.test.Simple object, Map<RealmModel, Long> cache) { Table table = realm.getTable(some.test.Simple.class); long tableNativePtr = table.getNativeTablePointer(); SimpleColumnInfo columnInfo = (SimpleColumnInfo) realm.schema.getColumnInfo(some.test.Simple.class); long rowIndex = Table.nativeAddEmptyRow(tableNativePtr, 1); cache.put(object, rowIndex); String realmGet$name = ((SimpleRealmProxyInterface) object).realmGet$name(); if (realmGet$name != null) { Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name); } Table.nativeSetLong( tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface) object).realmGet$age()); return rowIndex; }
public static void insertOrUpdate( Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel, Long> cache) { Table table = realm.getTable(some.test.Simple.class); long tableNativePtr = table.getNativeTablePointer(); SimpleColumnInfo columnInfo = (SimpleColumnInfo) realm.schema.getColumnInfo(some.test.Simple.class); some.test.Simple object = null; while (objects.hasNext()) { object = (some.test.Simple) objects.next(); if (!cache.containsKey(object)) { long rowIndex = Table.nativeAddEmptyRow(tableNativePtr, 1); cache.put(object, rowIndex); String realmGet$name = ((SimpleRealmProxyInterface) object).realmGet$name(); if (realmGet$name != null) { Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name); } else { Table.nativeSetNull(tableNativePtr, columnInfo.nameIndex, rowIndex); } Table.nativeSetLong( tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface) object).realmGet$age()); } } }
/** * Sets a reference to another object on the given field. * * @param fieldName the field name. * @param value the object to link to. * @throws IllegalArgumentException if field name doesn't exists, it doesn't link to other Realm * objects, or the type of DynamicRealmObject doesn't match. */ public void setObject(String fieldName, DynamicRealmObject value) { long columnIndex = row.getColumnIndex(fieldName); if (value == null) { row.nullifyLink(columnIndex); } else { if (value.realm == null || value.row == null) { throw new IllegalArgumentException( "Cannot link to objects that are not part of the Realm."); } if (!realm.getConfiguration().equals(value.realm.getConfiguration())) { throw new IllegalArgumentException("Cannot add an object from another Realm"); } Table table = row.getTable(); Table inputTable = value.row.getTable(); if (!table.hasSameSchema(inputTable)) { throw new IllegalArgumentException( String.format( "Type of object is wrong. Was %s, expected %s", inputTable.getName(), table.getName())); } row.setLink(columnIndex, value.row.getIndex()); } }
public static SimpleColumnInfo validateTable(ImplicitTransaction transaction) { if (transaction.hasTable("class_Simple")) { Table table = transaction.getTable("class_Simple"); if (table.getColumnCount() != 2) { throw new RealmMigrationNeededException( transaction.getPath(), "Field count does not match - expected 2 but was " + table.getColumnCount()); } Map<String, RealmFieldType> columnTypes = new HashMap<String, RealmFieldType>(); for (long i = 0; i < 2; i++) { columnTypes.put(table.getColumnName(i), table.getColumnType(i)); } final SimpleColumnInfo columnInfo = new SimpleColumnInfo(transaction.getPath(), table); if (!columnTypes.containsKey("name")) { throw new RealmMigrationNeededException( transaction.getPath(), "Missing field 'name' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn()."); } if (columnTypes.get("name") != RealmFieldType.STRING) { throw new RealmMigrationNeededException( transaction.getPath(), "Invalid type 'String' for field 'name' in existing Realm file."); } if (!table.isColumnNullable(columnInfo.nameIndex)) { throw new RealmMigrationNeededException( transaction.getPath(), "Field 'name' is required. Either set @Required to field 'name' or migrate using RealmObjectSchema.setNullable()."); } if (!columnTypes.containsKey("age")) { throw new RealmMigrationNeededException( transaction.getPath(), "Missing field 'age' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn()."); } if (columnTypes.get("age") != RealmFieldType.INTEGER) { throw new RealmMigrationNeededException( transaction.getPath(), "Invalid type 'int' for field 'age' in existing Realm file."); } if (table.isColumnNullable(columnInfo.ageIndex)) { throw new RealmMigrationNeededException( transaction.getPath(), "Field 'age' does support null values in the existing Realm file. Use corresponding boxed type for field 'age' or migrate using RealmObjectSchema.setNullable()."); } return columnInfo; } else { throw new RealmMigrationNeededException( transaction.getPath(), "The 'Simple' class is missing from the schema for this Realm."); } }
public static Table initTable(ImplicitTransaction transaction) { if (!transaction.hasTable("class_Booleans")) { Table table = transaction.getTable("class_Booleans"); table.addColumn(RealmFieldType.BOOLEAN, "done", Table.NOT_NULLABLE); table.addColumn(RealmFieldType.BOOLEAN, "isReady", Table.NOT_NULLABLE); table.addColumn(RealmFieldType.BOOLEAN, "mCompleted", Table.NOT_NULLABLE); table.addColumn(RealmFieldType.BOOLEAN, "anotherBoolean", Table.NOT_NULLABLE); table.setPrimaryKey(""); return table; } return transaction.getTable("class_Booleans"); }
public static BooleansColumnInfo validateTable(ImplicitTransaction transaction) { if (transaction.hasTable("class_Booleans")) { Table table = transaction.getTable("class_Booleans"); if (table.getColumnCount() != 4) { throw new RealmMigrationNeededException( transaction.getPath(), "Field count does not match - expected 4 but was " + table.getColumnCount()); } Map<String, RealmFieldType> columnTypes = new HashMap<String, RealmFieldType>(); for (long i = 0; i < 4; i++) { columnTypes.put(table.getColumnName(i), table.getColumnType(i)); } final BooleansColumnInfo columnInfo = new BooleansColumnInfo(transaction.getPath(), table); if (!columnTypes.containsKey("done")) { throw new RealmMigrationNeededException( transaction.getPath(), "Missing field 'done' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn()."); } if (columnTypes.get("done") != RealmFieldType.BOOLEAN) { throw new RealmMigrationNeededException( transaction.getPath(), "Invalid type 'boolean' for field 'done' in existing Realm file."); } if (table.isColumnNullable(columnInfo.doneIndex)) { throw new RealmMigrationNeededException( transaction.getPath(), "Field 'done' does support null values in the existing Realm file. Use corresponding boxed type for field 'done' or migrate using RealmObjectSchema.setNullable()."); } if (!columnTypes.containsKey("isReady")) { throw new RealmMigrationNeededException( transaction.getPath(), "Missing field 'isReady' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn()."); } if (columnTypes.get("isReady") != RealmFieldType.BOOLEAN) { throw new RealmMigrationNeededException( transaction.getPath(), "Invalid type 'boolean' for field 'isReady' in existing Realm file."); } if (table.isColumnNullable(columnInfo.isReadyIndex)) { throw new RealmMigrationNeededException( transaction.getPath(), "Field 'isReady' does support null values in the existing Realm file. Use corresponding boxed type for field 'isReady' or migrate using RealmObjectSchema.setNullable()."); } if (!columnTypes.containsKey("mCompleted")) { throw new RealmMigrationNeededException( transaction.getPath(), "Missing field 'mCompleted' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn()."); } if (columnTypes.get("mCompleted") != RealmFieldType.BOOLEAN) { throw new RealmMigrationNeededException( transaction.getPath(), "Invalid type 'boolean' for field 'mCompleted' in existing Realm file."); } if (table.isColumnNullable(columnInfo.mCompletedIndex)) { throw new RealmMigrationNeededException( transaction.getPath(), "Field 'mCompleted' does support null values in the existing Realm file. Use corresponding boxed type for field 'mCompleted' or migrate using RealmObjectSchema.setNullable()."); } if (!columnTypes.containsKey("anotherBoolean")) { throw new RealmMigrationNeededException( transaction.getPath(), "Missing field 'anotherBoolean' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn()."); } if (columnTypes.get("anotherBoolean") != RealmFieldType.BOOLEAN) { throw new RealmMigrationNeededException( transaction.getPath(), "Invalid type 'boolean' for field 'anotherBoolean' in existing Realm file."); } if (table.isColumnNullable(columnInfo.anotherBooleanIndex)) { throw new RealmMigrationNeededException( transaction.getPath(), "Field 'anotherBoolean' does support null values in the existing Realm file. Use corresponding boxed type for field 'anotherBoolean' or migrate using RealmObjectSchema.setNullable()."); } return columnInfo; } else { throw new RealmMigrationNeededException( transaction.getPath(), "The 'Booleans' class is missing from the schema for this Realm."); } }
@Override public long execute(Realm realm, long version) { /* // Version 0 class Person String firstName; String lastName; int age; // Version 1 class Person String fullName; // combine firstName and lastName into single field int age; */ // Migrate from version 0 to version 1 if (version == 0) { Table personTable = realm.getTable(Person.class); long fistNameIndex = getIndexForProperty(personTable, "firstName"); long lastNameIndex = getIndexForProperty(personTable, "lastName"); long fullNameIndex = personTable.addColumn(ColumnType.STRING, "fullName"); for (int i = 0; i < personTable.size(); i++) { personTable.setString( fullNameIndex, i, personTable.getString(fistNameIndex, i) + " " + personTable.getString(lastNameIndex, i)); } personTable.removeColumn(getIndexForProperty(personTable, "firstName")); personTable.removeColumn(getIndexForProperty(personTable, "lastName")); version++; } /* // Version 2 class Pet // add a new model class String name; String type; class Person String fullName; int age; RealmList<Pet> pets; // add an array property */ // Migrate from version 1 to version 2 if (version == 1) { Table personTable = realm.getTable(Person.class); Table petTable = realm.getTable(Pet.class); long nameColumnIndex = petTable.addColumn(ColumnType.STRING, "name"); long typeColumnIndex = petTable.addColumn(ColumnType.STRING, "type"); long petsIndex = personTable.addColumnLink(ColumnType.LINK_LIST, "pets", petTable); long fullNameIndex = getIndexForProperty(personTable, "fullName"); for (int i = 0; i < personTable.size(); i++) { if (personTable.getString(fullNameIndex, i).equals("JP McDonald")) { long rowIndex = petTable.addEmptyRow(); petTable.setString(nameColumnIndex, rowIndex, "Jimbo"); petTable.setString(typeColumnIndex, rowIndex, "dog"); personTable.getUncheckedRow(i).getLinkList(petsIndex).add(rowIndex); } } version++; } /* // Version 3 class Pet String name; int type; // type becomes int class Person String fullName; RealmList<Pet> pets; // age and pets re-ordered int age; */ // Migrate from version 2 to version 3 if (version == 2) { Table petTable = realm.getTable(Pet.class); long oldTypeIndex = getIndexForProperty(petTable, "type"); long typeIndex = petTable.addColumn(ColumnType.INTEGER, "type"); for (int i = 0; i < petTable.size(); i++) { String type = petTable.getString(oldTypeIndex, i); if (type.equals("dog")) { petTable.setLong(typeIndex, i, 1); } else if (type.equals("cat")) { petTable.setLong(typeIndex, i, 2); } else if (type.equals("hamster")) { petTable.setLong(typeIndex, i, 3); } } petTable.removeColumn(oldTypeIndex); version++; } return version; }
public static SatsClass createOrUpdateUsingJsonObject( Realm realm, JSONObject json, boolean update) throws JSONException { SatsClass obj = null; if (update) { Table table = realm.getTable(SatsClass.class); long pkColumnIndex = table.getPrimaryKey(); if (!json.isNull("id")) { long rowIndex = table.findFirstString(pkColumnIndex, json.getString("id")); if (rowIndex != TableOrView.NO_MATCH) { obj = new SatsClassRealmProxy(); obj.realm = realm; obj.row = table.getRow(rowIndex); } } } if (obj == null) { obj = realm.createObject(SatsClass.class); } if (!json.isNull("id")) { obj.setId((String) json.getString("id")); } if (!json.isNull("centerFilterId")) { obj.setCenterFilterId((String) json.getString("centerFilterId")); } if (!json.isNull("classTypeId")) { obj.setClassTypeId((String) json.getString("classTypeId")); } if (!json.isNull("durationInMinutes")) { obj.setDurationInMinutes((int) json.getInt("durationInMinutes")); } if (!json.isNull("instructorId")) { obj.setInstructorId((String) json.getString("instructorId")); } if (!json.isNull("name")) { obj.setName((String) json.getString("name")); } if (!json.isNull("startTime")) { obj.setStartTime((String) json.getString("startTime")); } if (!json.isNull("bookedPersonsCount")) { obj.setBookedPersonsCount((int) json.getInt("bookedPersonsCount")); } if (!json.isNull("maxPersonsCount")) { obj.setMaxPersonsCount((int) json.getInt("maxPersonsCount")); } if (!json.isNull("regionId")) { obj.setRegionId((String) json.getString("regionId")); } if (!json.isNull("waitingListCount")) { obj.setWaitingListCount((int) json.getInt("waitingListCount")); } if (!json.isNull("classCategoryIds")) { obj.getClassCategoryIds().clear(); JSONArray array = json.getJSONArray("classCategoryIds"); for (int i = 0; i < array.length(); i++) { se.greatbrain.sats.data.model.ClassCategoryIds item = ClassCategoryIdsRealmProxy.createOrUpdateUsingJsonObject( realm, array.getJSONObject(i), update); obj.getClassCategoryIds().add(item); } } return obj; }
public static void validateTable(ImplicitTransaction transaction) { if (transaction.hasTable("class_SatsClass")) { Table table = transaction.getTable("class_SatsClass"); if (table.getColumnCount() != 12) { throw new IllegalStateException("Column count does not match"); } Map<String, ColumnType> columnTypes = new HashMap<String, ColumnType>(); for (long i = 0; i < 12; i++) { columnTypes.put(table.getColumnName(i), table.getColumnType(i)); } if (!columnTypes.containsKey("id")) { throw new IllegalStateException("Missing column 'id'"); } if (columnTypes.get("id") != ColumnType.STRING) { throw new IllegalStateException("Invalid type 'String' for column 'id'"); } if (table.getPrimaryKey() != table.getColumnIndex("id")) { throw new IllegalStateException("Primary key not defined for field 'id'"); } if (!table.hasIndex(table.getColumnIndex("id"))) { throw new IllegalStateException("Index not defined for field 'id'"); } if (!columnTypes.containsKey("centerFilterId")) { throw new IllegalStateException("Missing column 'centerFilterId'"); } if (columnTypes.get("centerFilterId") != ColumnType.STRING) { throw new IllegalStateException("Invalid type 'String' for column 'centerFilterId'"); } if (!columnTypes.containsKey("classTypeId")) { throw new IllegalStateException("Missing column 'classTypeId'"); } if (columnTypes.get("classTypeId") != ColumnType.STRING) { throw new IllegalStateException("Invalid type 'String' for column 'classTypeId'"); } if (!columnTypes.containsKey("durationInMinutes")) { throw new IllegalStateException("Missing column 'durationInMinutes'"); } if (columnTypes.get("durationInMinutes") != ColumnType.INTEGER) { throw new IllegalStateException("Invalid type 'int' for column 'durationInMinutes'"); } if (!columnTypes.containsKey("instructorId")) { throw new IllegalStateException("Missing column 'instructorId'"); } if (columnTypes.get("instructorId") != ColumnType.STRING) { throw new IllegalStateException("Invalid type 'String' for column 'instructorId'"); } if (!columnTypes.containsKey("name")) { throw new IllegalStateException("Missing column 'name'"); } if (columnTypes.get("name") != ColumnType.STRING) { throw new IllegalStateException("Invalid type 'String' for column 'name'"); } if (!columnTypes.containsKey("startTime")) { throw new IllegalStateException("Missing column 'startTime'"); } if (columnTypes.get("startTime") != ColumnType.STRING) { throw new IllegalStateException("Invalid type 'String' for column 'startTime'"); } if (!columnTypes.containsKey("bookedPersonsCount")) { throw new IllegalStateException("Missing column 'bookedPersonsCount'"); } if (columnTypes.get("bookedPersonsCount") != ColumnType.INTEGER) { throw new IllegalStateException("Invalid type 'int' for column 'bookedPersonsCount'"); } if (!columnTypes.containsKey("maxPersonsCount")) { throw new IllegalStateException("Missing column 'maxPersonsCount'"); } if (columnTypes.get("maxPersonsCount") != ColumnType.INTEGER) { throw new IllegalStateException("Invalid type 'int' for column 'maxPersonsCount'"); } if (!columnTypes.containsKey("regionId")) { throw new IllegalStateException("Missing column 'regionId'"); } if (columnTypes.get("regionId") != ColumnType.STRING) { throw new IllegalStateException("Invalid type 'String' for column 'regionId'"); } if (!columnTypes.containsKey("waitingListCount")) { throw new IllegalStateException("Missing column 'waitingListCount'"); } if (columnTypes.get("waitingListCount") != ColumnType.INTEGER) { throw new IllegalStateException("Invalid type 'int' for column 'waitingListCount'"); } if (!columnTypes.containsKey("classCategoryIds")) { throw new IllegalStateException("Missing column 'classCategoryIds'"); } if (columnTypes.get("classCategoryIds") != ColumnType.LINK_LIST) { throw new IllegalStateException( "Invalid type 'ClassCategoryIds' for column 'classCategoryIds'"); } if (!transaction.hasTable("class_ClassCategoryIds")) { throw new IllegalStateException( "Missing table 'class_ClassCategoryIds' for column 'classCategoryIds'"); } columnIndices = new HashMap<String, Long>(); for (String fieldName : getFieldNames()) { long index = table.getColumnIndex(fieldName); if (index == -1) { throw new RealmMigrationNeededException( "Field '" + fieldName + "' not found for type SatsClass"); } columnIndices.put(fieldName, index); } INDEX_ID = table.getColumnIndex("id"); INDEX_CENTERFILTERID = table.getColumnIndex("centerFilterId"); INDEX_CLASSTYPEID = table.getColumnIndex("classTypeId"); INDEX_DURATIONINMINUTES = table.getColumnIndex("durationInMinutes"); INDEX_INSTRUCTORID = table.getColumnIndex("instructorId"); INDEX_NAME = table.getColumnIndex("name"); INDEX_STARTTIME = table.getColumnIndex("startTime"); INDEX_BOOKEDPERSONSCOUNT = table.getColumnIndex("bookedPersonsCount"); INDEX_MAXPERSONSCOUNT = table.getColumnIndex("maxPersonsCount"); INDEX_REGIONID = table.getColumnIndex("regionId"); INDEX_WAITINGLISTCOUNT = table.getColumnIndex("waitingListCount"); INDEX_CLASSCATEGORYIDS = table.getColumnIndex("classCategoryIds"); } else { throw new RealmMigrationNeededException( "The SatsClass class is missing from the schema for this Realm."); } }
public static Table initTable(ImplicitTransaction transaction) { if (!transaction.hasTable("class_SatsClass")) { Table table = transaction.getTable("class_SatsClass"); table.addColumn(ColumnType.STRING, "id"); table.addColumn(ColumnType.STRING, "centerFilterId"); table.addColumn(ColumnType.STRING, "classTypeId"); table.addColumn(ColumnType.INTEGER, "durationInMinutes"); table.addColumn(ColumnType.STRING, "instructorId"); table.addColumn(ColumnType.STRING, "name"); table.addColumn(ColumnType.STRING, "startTime"); table.addColumn(ColumnType.INTEGER, "bookedPersonsCount"); table.addColumn(ColumnType.INTEGER, "maxPersonsCount"); table.addColumn(ColumnType.STRING, "regionId"); table.addColumn(ColumnType.INTEGER, "waitingListCount"); if (!transaction.hasTable("class_ClassCategoryIds")) { ClassCategoryIdsRealmProxy.initTable(transaction); } table.addColumnLink( ColumnType.LINK_LIST, "classCategoryIds", transaction.getTable("class_ClassCategoryIds")); table.setIndex(table.getColumnIndex("id")); table.setPrimaryKey("id"); return table; } return transaction.getTable("class_SatsClass"); }