Search in sources :

Example 31 with Table

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")));
}
Also used : Table(io.realm.internal.Table) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) Test(org.junit.Test)

Example 32 with Table

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();
}
Also used : Table(io.realm.internal.Table) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 33 with Table

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;
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy) Table(io.realm.internal.Table)

Example 34 with Table

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);
}
Also used : Table(io.realm.internal.Table) RealmException(io.realm.exceptions.RealmException)

Example 35 with Table

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));
}
Also used : Table(io.realm.internal.Table)

Aggregations

Table (io.realm.internal.Table)68 RealmObjectProxy (io.realm.internal.RealmObjectProxy)20 Test (org.junit.Test)19 Date (java.util.Date)10 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)5 RealmMigrationNeededException (io.realm.exceptions.RealmMigrationNeededException)4 HashMap (java.util.HashMap)4 PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)3 RealmException (io.realm.exceptions.RealmException)3 UncheckedRow (io.realm.internal.UncheckedRow)2 JSONObject (org.json.JSONObject)2 TargetApi (android.annotation.TargetApi)1 JsonReader (android.util.JsonReader)1 AllTypes (io.realm.entities.AllTypes)1 Cat (io.realm.entities.Cat)1 IOSAllTypes (io.realm.entities.IOSAllTypes)1 StringOnly (io.realm.entities.StringOnly)1 MigrationFieldTypeToInt (io.realm.entities.migration.MigrationFieldTypeToInt)1 MigrationFieldTypeToInteger (io.realm.entities.migration.MigrationFieldTypeToInteger)1 LinkView (io.realm.internal.LinkView)1