Search in sources :

Example 61 with Table

use of io.realm.internal.Table in project realm-java by realm.

the class TestHelper method getTableWithAllColumnTypes.

/**
     * Creates an empty table with 1 column of all our supported column types, currently 9 columns.
     *
     * @return
     */
public static Table getTableWithAllColumnTypes() {
    Table t = new Table();
    t.addColumn(RealmFieldType.BINARY, "binary");
    t.addColumn(RealmFieldType.BOOLEAN, "boolean");
    t.addColumn(RealmFieldType.DATE, "date");
    t.addColumn(RealmFieldType.DOUBLE, "double");
    t.addColumn(RealmFieldType.FLOAT, "float");
    t.addColumn(RealmFieldType.INTEGER, "long");
    t.addColumn(RealmFieldType.STRING, "string");
    return t;
}
Also used : Table(io.realm.internal.Table)

Example 62 with Table

use of io.realm.internal.Table in project realm-java by realm.

the class NullTypesRealmProxy method insertOrUpdate.

public static long insertOrUpdate(Realm realm, some.test.NullTypes object, Map<RealmModel, Long> cache) {
    if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) {
        return ((RealmObjectProxy) object).realmGet$proxyState().getRow$realm().getIndex();
    }
    Table table = realm.getTable(some.test.NullTypes.class);
    long tableNativePtr = table.getNativeTablePointer();
    NullTypesColumnInfo columnInfo = (NullTypesColumnInfo) realm.schema.getColumnInfo(some.test.NullTypes.class);
    long rowIndex = Table.nativeAddEmptyRow(tableNativePtr, 1);
    cache.put(object, rowIndex);
    String realmGet$fieldStringNotNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldStringNotNull();
    if (realmGet$fieldStringNotNull != null) {
        Table.nativeSetString(tableNativePtr, columnInfo.fieldStringNotNullIndex, rowIndex, realmGet$fieldStringNotNull, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldStringNotNullIndex, rowIndex, false);
    }
    String realmGet$fieldStringNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldStringNull();
    if (realmGet$fieldStringNull != null) {
        Table.nativeSetString(tableNativePtr, columnInfo.fieldStringNullIndex, rowIndex, realmGet$fieldStringNull, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldStringNullIndex, rowIndex, false);
    }
    Boolean realmGet$fieldBooleanNotNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldBooleanNotNull();
    if (realmGet$fieldBooleanNotNull != null) {
        Table.nativeSetBoolean(tableNativePtr, columnInfo.fieldBooleanNotNullIndex, rowIndex, realmGet$fieldBooleanNotNull, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldBooleanNotNullIndex, rowIndex, false);
    }
    Boolean realmGet$fieldBooleanNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldBooleanNull();
    if (realmGet$fieldBooleanNull != null) {
        Table.nativeSetBoolean(tableNativePtr, columnInfo.fieldBooleanNullIndex, rowIndex, realmGet$fieldBooleanNull, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldBooleanNullIndex, rowIndex, false);
    }
    byte[] realmGet$fieldBytesNotNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldBytesNotNull();
    if (realmGet$fieldBytesNotNull != null) {
        Table.nativeSetByteArray(tableNativePtr, columnInfo.fieldBytesNotNullIndex, rowIndex, realmGet$fieldBytesNotNull, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldBytesNotNullIndex, rowIndex, false);
    }
    byte[] realmGet$fieldBytesNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldBytesNull();
    if (realmGet$fieldBytesNull != null) {
        Table.nativeSetByteArray(tableNativePtr, columnInfo.fieldBytesNullIndex, rowIndex, realmGet$fieldBytesNull, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldBytesNullIndex, rowIndex, false);
    }
    Number realmGet$fieldByteNotNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldByteNotNull();
    if (realmGet$fieldByteNotNull != null) {
        Table.nativeSetLong(tableNativePtr, columnInfo.fieldByteNotNullIndex, rowIndex, realmGet$fieldByteNotNull.longValue(), false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldByteNotNullIndex, rowIndex, false);
    }
    Number realmGet$fieldByteNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldByteNull();
    if (realmGet$fieldByteNull != null) {
        Table.nativeSetLong(tableNativePtr, columnInfo.fieldByteNullIndex, rowIndex, realmGet$fieldByteNull.longValue(), false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldByteNullIndex, rowIndex, false);
    }
    Number realmGet$fieldShortNotNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldShortNotNull();
    if (realmGet$fieldShortNotNull != null) {
        Table.nativeSetLong(tableNativePtr, columnInfo.fieldShortNotNullIndex, rowIndex, realmGet$fieldShortNotNull.longValue(), false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldShortNotNullIndex, rowIndex, false);
    }
    Number realmGet$fieldShortNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldShortNull();
    if (realmGet$fieldShortNull != null) {
        Table.nativeSetLong(tableNativePtr, columnInfo.fieldShortNullIndex, rowIndex, realmGet$fieldShortNull.longValue(), false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldShortNullIndex, rowIndex, false);
    }
    Number realmGet$fieldIntegerNotNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldIntegerNotNull();
    if (realmGet$fieldIntegerNotNull != null) {
        Table.nativeSetLong(tableNativePtr, columnInfo.fieldIntegerNotNullIndex, rowIndex, realmGet$fieldIntegerNotNull.longValue(), false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldIntegerNotNullIndex, rowIndex, false);
    }
    Number realmGet$fieldIntegerNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldIntegerNull();
    if (realmGet$fieldIntegerNull != null) {
        Table.nativeSetLong(tableNativePtr, columnInfo.fieldIntegerNullIndex, rowIndex, realmGet$fieldIntegerNull.longValue(), false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldIntegerNullIndex, rowIndex, false);
    }
    Number realmGet$fieldLongNotNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldLongNotNull();
    if (realmGet$fieldLongNotNull != null) {
        Table.nativeSetLong(tableNativePtr, columnInfo.fieldLongNotNullIndex, rowIndex, realmGet$fieldLongNotNull.longValue(), false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldLongNotNullIndex, rowIndex, false);
    }
    Number realmGet$fieldLongNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldLongNull();
    if (realmGet$fieldLongNull != null) {
        Table.nativeSetLong(tableNativePtr, columnInfo.fieldLongNullIndex, rowIndex, realmGet$fieldLongNull.longValue(), false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldLongNullIndex, rowIndex, false);
    }
    Float realmGet$fieldFloatNotNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldFloatNotNull();
    if (realmGet$fieldFloatNotNull != null) {
        Table.nativeSetFloat(tableNativePtr, columnInfo.fieldFloatNotNullIndex, rowIndex, realmGet$fieldFloatNotNull, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldFloatNotNullIndex, rowIndex, false);
    }
    Float realmGet$fieldFloatNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldFloatNull();
    if (realmGet$fieldFloatNull != null) {
        Table.nativeSetFloat(tableNativePtr, columnInfo.fieldFloatNullIndex, rowIndex, realmGet$fieldFloatNull, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldFloatNullIndex, rowIndex, false);
    }
    Double realmGet$fieldDoubleNotNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldDoubleNotNull();
    if (realmGet$fieldDoubleNotNull != null) {
        Table.nativeSetDouble(tableNativePtr, columnInfo.fieldDoubleNotNullIndex, rowIndex, realmGet$fieldDoubleNotNull, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldDoubleNotNullIndex, rowIndex, false);
    }
    Double realmGet$fieldDoubleNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldDoubleNull();
    if (realmGet$fieldDoubleNull != null) {
        Table.nativeSetDouble(tableNativePtr, columnInfo.fieldDoubleNullIndex, rowIndex, realmGet$fieldDoubleNull, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldDoubleNullIndex, rowIndex, false);
    }
    java.util.Date realmGet$fieldDateNotNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldDateNotNull();
    if (realmGet$fieldDateNotNull != null) {
        Table.nativeSetTimestamp(tableNativePtr, columnInfo.fieldDateNotNullIndex, rowIndex, realmGet$fieldDateNotNull.getTime(), false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldDateNotNullIndex, rowIndex, false);
    }
    java.util.Date realmGet$fieldDateNull = ((NullTypesRealmProxyInterface) object).realmGet$fieldDateNull();
    if (realmGet$fieldDateNull != null) {
        Table.nativeSetTimestamp(tableNativePtr, columnInfo.fieldDateNullIndex, rowIndex, realmGet$fieldDateNull.getTime(), false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.fieldDateNullIndex, rowIndex, false);
    }
    some.test.NullTypes fieldObjectNullObj = ((NullTypesRealmProxyInterface) object).realmGet$fieldObjectNull();
    if (fieldObjectNullObj != null) {
        Long cachefieldObjectNull = cache.get(fieldObjectNullObj);
        if (cachefieldObjectNull == null) {
            cachefieldObjectNull = NullTypesRealmProxy.insertOrUpdate(realm, fieldObjectNullObj, cache);
        }
        Table.nativeSetLink(tableNativePtr, columnInfo.fieldObjectNullIndex, rowIndex, cachefieldObjectNull, false);
    } else {
        Table.nativeNullifyLink(tableNativePtr, columnInfo.fieldObjectNullIndex, rowIndex);
    }
    return rowIndex;
}
Also used : Table(io.realm.internal.Table) Date(java.util.Date) RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 63 with Table

use of io.realm.internal.Table in project realm-java by realm.

the class SimpleRealmProxy method insertOrUpdate.

public static long insertOrUpdate(Realm realm, some.test.Simple object, Map<RealmModel, Long> cache) {
    if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) {
        return ((RealmObjectProxy) object).realmGet$proxyState().getRow$realm().getIndex();
    }
    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, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.nameIndex, rowIndex, false);
    }
    Table.nativeSetLong(tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface) object).realmGet$age(), false);
    return rowIndex;
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy) Table(io.realm.internal.Table)

Example 64 with Table

use of io.realm.internal.Table in project realm-java by realm.

the class SimpleRealmProxy method validateTable.

public static SimpleColumnInfo validateTable(SharedRealm sharedRealm, boolean allowExtraColumns) {
    if (!sharedRealm.hasTable("class_Simple")) {
        throw new RealmMigrationNeededException(sharedRealm.getPath(), "The 'Simple' class is missing from the schema for this Realm.");
    }
    Table table = sharedRealm.getTable("class_Simple");
    final long columnCount = table.getColumnCount();
    if (columnCount != 2) {
        if (columnCount < 2) {
            throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is less than expected - expected 2 but was " + columnCount);
        }
        if (allowExtraColumns) {
            RealmLog.debug("Field count is more than expected - expected 2 but was %1$d", columnCount);
        } else {
            throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is more than expected - expected 2 but was " + columnCount);
        }
    }
    Map<String, RealmFieldType> columnTypes = new HashMap<String, RealmFieldType>();
    for (long i = 0; i < columnCount; i++) {
        columnTypes.put(table.getColumnName(i), table.getColumnType(i));
    }
    final SimpleColumnInfo columnInfo = new SimpleColumnInfo(sharedRealm.getPath(), table);
    if (table.hasPrimaryKey()) {
        throw new RealmMigrationNeededException(sharedRealm.getPath(), "Primary Key defined for field " + table.getColumnName(table.getPrimaryKey()) + " was removed.");
    }
    if (!columnTypes.containsKey("name")) {
        throw new RealmMigrationNeededException(sharedRealm.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(sharedRealm.getPath(), "Invalid type 'String' for field 'name' in existing Realm file.");
    }
    if (!table.isColumnNullable(columnInfo.nameIndex)) {
        throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'name' is required. Either set @Required to field 'name' or migrate using RealmObjectSchema.setNullable().");
    }
    if (!columnTypes.containsKey("age")) {
        throw new RealmMigrationNeededException(sharedRealm.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(sharedRealm.getPath(), "Invalid type 'int' for field 'age' in existing Realm file.");
    }
    if (table.isColumnNullable(columnInfo.ageIndex)) {
        throw new RealmMigrationNeededException(sharedRealm.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;
}
Also used : Table(io.realm.internal.Table) HashMap(java.util.HashMap) RealmMigrationNeededException(io.realm.exceptions.RealmMigrationNeededException)

Example 65 with Table

use of io.realm.internal.Table in project realm-java by realm.

the class SimpleRealmProxy method initTable.

public static Table initTable(SharedRealm sharedRealm) {
    if (!sharedRealm.hasTable("class_Simple")) {
        Table table = sharedRealm.getTable("class_Simple");
        table.addColumn(RealmFieldType.STRING, "name", Table.NULLABLE);
        table.addColumn(RealmFieldType.INTEGER, "age", Table.NOT_NULLABLE);
        table.setPrimaryKey("");
        return table;
    }
    return sharedRealm.getTable("class_Simple");
}
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