Search in sources :

Example 11 with Table

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

the class Realm method createObjectInternal.

/**
     * Same as {@link #createObject(Class)} but this does not check the thread.
     *
     * @param clazz the Class of the object to create.
     * @param acceptDefaultValue if {@code true}, default value of the object will be applied and
     *                           if {@code false}, it will be ignored.
     * @return the new object.
     * @throws RealmException if the primary key is defined in the model class or an object cannot be created.
     */
// Called from proxy classes.
<E extends RealmModel> E createObjectInternal(Class<E> clazz, boolean acceptDefaultValue, List<String> excludeFields) {
    Table table = schema.getTable(clazz);
    // Checks and throws the exception earlier for a better exception message.
    if (table.hasPrimaryKey()) {
        throw new RealmException(String.format("'%s' has a primary key, use" + " 'createObject(Class<E>, Object)' instead.", Table.tableNameToClassName(table.getName())));
    }
    long rowIndex = table.addEmptyRow();
    return get(clazz, rowIndex, acceptDefaultValue, excludeFields);
}
Also used : Table(io.realm.internal.Table) RealmException(io.realm.exceptions.RealmException)

Example 12 with Table

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

the class Realm method createObjectFromJson.

/**
     * Creates a Realm object pre-filled with data from a JSON object. This must be done inside a transaction. JSON
     * properties with unknown properties will be ignored. If a {@link RealmObject} field is not present in the JSON
     * object the {@link RealmObject} field will be set to the default value for that type.
     * <p>
     * This API is only available in API level 11 or later.
     *
     * @param clazz type of Realm object to create.
     * @param inputStream the JSON object data as a InputStream.
     * @return created object or {@code null} if JSON string was empty or null.
     * @throws RealmException if the mapping from JSON failed.
     * @throws IllegalArgumentException if the JSON object doesn't have a primary key property but the corresponding
     * {@link RealmObjectSchema} has a {@link io.realm.annotations.PrimaryKey} defined.
     * @throws IOException if something went wrong with the input stream.
     */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public <E extends RealmModel> E createObjectFromJson(Class<E> clazz, InputStream inputStream) throws IOException {
    if (clazz == null || inputStream == null) {
        return null;
    }
    checkIfValid();
    E realmObject;
    Table table = schema.getTable(clazz);
    if (table.hasPrimaryKey()) {
        // As we need the primary key value we have to first parse the entire input stream as in the general
        // case that value might be the last property. :(
        Scanner scanner = null;
        try {
            scanner = getFullStringScanner(inputStream);
            JSONObject json = new JSONObject(scanner.next());
            realmObject = configuration.getSchemaMediator().createOrUpdateUsingJsonObject(clazz, this, json, false);
        } catch (JSONException e) {
            throw new RealmException("Failed to read JSON", e);
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    } else {
        JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
        try {
            realmObject = configuration.getSchemaMediator().createUsingJsonStream(clazz, this, reader);
        } finally {
            reader.close();
        }
    }
    return realmObject;
}
Also used : Scanner(java.util.Scanner) Table(io.realm.internal.Table) JSONObject(org.json.JSONObject) InputStreamReader(java.io.InputStreamReader) JSONException(org.json.JSONException) JsonReader(android.util.JsonReader) RealmException(io.realm.exceptions.RealmException) TargetApi(android.annotation.TargetApi)

Example 13 with Table

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

the class RealmSchema method remove.

/**
     * Removes a class from the Realm. All data will be removed. Removing a class while other classes point
     * to it will throw an {@link IllegalStateException}. Removes those classes or fields first.
     *
     * @param className name of the class to remove.
     */
public void remove(String className) {
    // Destructive modifications are not permitted.
    realm.checkNotInSync();
    checkEmpty(className, EMPTY_STRING_MSG);
    String internalTableName = TABLE_PREFIX + className;
    checkHasTable(className, "Cannot remove class because it is not in this Realm: " + className);
    Table table = getTable(className);
    if (table.hasPrimaryKey()) {
        table.setPrimaryKey(null);
    }
    realm.sharedRealm.removeTable(internalTableName);
}
Also used : Table(io.realm.internal.Table)

Example 14 with Table

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

the class RealmSchema method getSchemaForClass.

RealmObjectSchema getSchemaForClass(Class<? extends RealmModel> clazz) {
    RealmObjectSchema classSchema = classToSchema.get(clazz);
    if (classSchema == null) {
        Class<? extends RealmModel> originalClass = Util.getOriginalModelClass(clazz);
        if (isProxyClass(originalClass, clazz)) {
            // If passed 'clazz' is the proxy, try again with model class.
            classSchema = classToSchema.get(originalClass);
        }
        if (classSchema == null) {
            Table table = getTable(clazz);
            classSchema = new RealmObjectSchema(realm, table, columnIndices.getColumnInfo(originalClass).getIndicesMap());
            classToSchema.put(originalClass, classSchema);
        }
        if (isProxyClass(originalClass, clazz)) {
            // 'clazz' is the proxy class for 'originalClass'.
            classToSchema.put(clazz, classSchema);
        }
    }
    return classSchema;
}
Also used : Table(io.realm.internal.Table)

Example 15 with Table

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

the class BaseRealm method get.

<E extends RealmModel> E get(Class<E> clazz, long rowIndex, boolean acceptDefaultValue, List<String> excludeFields) {
    Table table = schema.getTable(clazz);
    UncheckedRow row = table.getUncheckedRow(rowIndex);
    E result = configuration.getSchemaMediator().newInstance(clazz, this, row, schema.getColumnInfo(clazz), acceptDefaultValue, excludeFields);
    RealmObjectProxy proxy = (RealmObjectProxy) result;
    proxy.realmGet$proxyState().setTableVersion$realm();
    return result;
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy) Table(io.realm.internal.Table) UncheckedRow(io.realm.internal.UncheckedRow)

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