use of io.realm.internal.Table in project realm-java by realm.
the class RealmAnnotationTests method primaryKey_isIndexed.
@Test
public void primaryKey_isIndexed() {
Table table = realm.getTable(PrimaryKeyAsString.class);
assertTrue(table.hasPrimaryKey());
assertTrue(table.hasSearchIndex(table.getColumnIndex("name")));
table = realm.getTable(PrimaryKeyAsLong.class);
assertTrue(table.hasPrimaryKey());
assertTrue(table.hasSearchIndex(table.getColumnIndex("id")));
}
use of io.realm.internal.Table in project realm-java by realm.
the class RealmAnnotationTests method primaryKey_migration_long.
// Tests migrating primary key from string to long with existing data.
@Test
public void primaryKey_migration_long() {
realm.beginTransaction();
for (int i = 1; i <= 2; i++) {
PrimaryKeyAsString obj = realm.createObject(PrimaryKeyAsString.class, "String" + i);
obj.setId(i);
}
Table table = realm.getTable(PrimaryKeyAsString.class);
table.setPrimaryKey("id");
assertEquals(1, table.getPrimaryKey());
realm.cancelTransaction();
}
use of io.realm.internal.Table in project realm-java by realm.
the class BaseRealm method get.
// Used by RealmList/RealmResults
// Invariant: if dynamicClassName != null -> clazz == DynamicRealmObject
// TODO: Remove this after RealmList is backed by OS Results.
<E extends RealmModel> E get(Class<E> clazz, String dynamicClassName, long rowIndex) {
final boolean isDynamicRealmObject = dynamicClassName != null;
final Table table = isDynamicRealmObject ? schema.getTable(dynamicClassName) : schema.getTable(clazz);
E result;
if (isDynamicRealmObject) {
@SuppressWarnings("unchecked") E dynamicObj = (E) new DynamicRealmObject(this, (rowIndex != Table.NO_MATCH) ? table.getCheckedRow(rowIndex) : InvalidRow.INSTANCE);
result = dynamicObj;
} else {
result = configuration.getSchemaMediator().newInstance(clazz, this, (rowIndex != Table.NO_MATCH) ? table.getUncheckedRow(rowIndex) : InvalidRow.INSTANCE, schema.getColumnInfo(clazz), false, Collections.<String>emptyList());
}
RealmObjectProxy proxy = (RealmObjectProxy) result;
if (rowIndex != Table.NO_MATCH) {
proxy.realmGet$proxyState().setTableVersion$realm();
}
return result;
}
use of io.realm.internal.Table in project realm-java by realm.
the class DynamicRealm method createObject.
/**
* Instantiates and adds a new object to the Realm.
*
* @param className the class name of the object to create.
* @return the new object.
* @throws RealmException if the object could not be created.
*/
public DynamicRealmObject createObject(String className) {
checkIfValid();
Table table = schema.getTable(className);
// Check and throw the exception earlier for a better exception message.
if (table.hasPrimaryKey()) {
throw new RealmException(String.format("'%s' has a primary key, use" + " 'createObject(String, Object)' instead.", className));
}
long rowIndex = table.addEmptyRow();
return get(DynamicRealmObject.class, className, rowIndex);
}
use of io.realm.internal.Table in project realm-java by realm.
the class DynamicRealm method createObject.
/**
* Creates an object with a given primary key. Classes without a primary key defined must use
* {@link #createObject(String)}} instead.
*
* @return the new object. All fields will have default values for their type, except for the
* primary key field which will have the provided value.
* @throws RealmException if object could not be created due to the primary key being invalid.
* @throws IllegalStateException if the model clazz does not have an primary key defined.
* @throws IllegalArgumentException if the {@code primaryKeyValue} doesn't have a value that can be converted to the
* expected value.
*/
public DynamicRealmObject createObject(String className, Object primaryKeyValue) {
Table table = schema.getTable(className);
long index = table.addEmptyRowWithPrimaryKey(primaryKeyValue);
return new DynamicRealmObject(this, table.getCheckedRow(index));
}
Aggregations