Search in sources :

Example 41 with Table

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

the class RealmSchema method getAll.

/**
     * Returns the {@link RealmObjectSchema} for all RealmObject classes that can be saved in this Realm.
     *
     * @return the set of all classes in this Realm or no RealmObject classes can be saved in the Realm.
     */
public Set<RealmObjectSchema> getAll() {
    if (realm == null) {
        long[] ptrs = nativeGetAll(nativePtr);
        Set<RealmObjectSchema> schemas = new LinkedHashSet<RealmObjectSchema>(ptrs.length);
        for (int i = 0; i < ptrs.length; i++) {
            schemas.add(new RealmObjectSchema(ptrs[i]));
        }
        return schemas;
    } else {
        int tableCount = (int) realm.sharedRealm.size();
        Set<RealmObjectSchema> schemas = new LinkedHashSet<RealmObjectSchema>(tableCount);
        for (int i = 0; i < tableCount; i++) {
            String tableName = realm.sharedRealm.getTableName(i);
            if (!Table.isModelTable(tableName)) {
                continue;
            }
            Table table = realm.sharedRealm.getTable(tableName);
            RealmObjectSchema.DynamicColumnMap columnIndices = new RealmObjectSchema.DynamicColumnMap(table);
            schemas.add(new RealmObjectSchema(realm, table, columnIndices));
        }
        return schemas;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Table(io.realm.internal.Table)

Example 42 with Table

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

the class RealmSchema method get.

/**
     * Returns the Realm schema for a given class.
     *
     * @param className name of the class
     * @return schema object for that class or {@code null} if the class doesn't exists.
     */
public RealmObjectSchema get(String className) {
    checkEmpty(className, EMPTY_STRING_MSG);
    if (realm == null) {
        if (contains(className)) {
            return dynamicClassToSchema.get(className);
        } else {
            return null;
        }
    } else {
        String internalClassName = TABLE_PREFIX + className;
        if (realm.sharedRealm.hasTable(internalClassName)) {
            Table table = realm.sharedRealm.getTable(internalClassName);
            RealmObjectSchema.DynamicColumnMap columnIndices = new RealmObjectSchema.DynamicColumnMap(table);
            return new RealmObjectSchema(realm, table, columnIndices);
        } else {
            return null;
        }
    }
}
Also used : Table(io.realm.internal.Table)

Example 43 with Table

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

the class RealmSchema method create.

/**
     * Adds a new class to the Realm.
     *
     * @param className name of the class.
     * @return a Realm schema object for that class.
     */
public RealmObjectSchema create(String className) {
    // Adding a class is always permitted.
    checkEmpty(className, EMPTY_STRING_MSG);
    if (realm == null) {
        RealmObjectSchema realmObjectSchema = new RealmObjectSchema(className);
        dynamicClassToSchema.put(className, realmObjectSchema);
        return realmObjectSchema;
    } else {
        String internalTableName = TABLE_PREFIX + className;
        if (internalTableName.length() > Table.TABLE_MAX_LENGTH) {
            throw new IllegalArgumentException("Class name is too long. Limit is 56 characters: " + className.length());
        }
        if (realm.sharedRealm.hasTable(internalTableName)) {
            throw new IllegalArgumentException("Class already exists: " + className);
        }
        Table table = realm.sharedRealm.getTable(internalTableName);
        RealmObjectSchema.DynamicColumnMap columnIndices = new RealmObjectSchema.DynamicColumnMap(table);
        return new RealmObjectSchema(realm, table, columnIndices);
    }
}
Also used : Table(io.realm.internal.Table)

Example 44 with Table

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

the class RealmSchema method getTable.

Table getTable(String className) {
    className = Table.TABLE_PREFIX + className;
    Table table = dynamicClassToTable.get(className);
    if (table == null) {
        if (!realm.sharedRealm.hasTable(className)) {
            throw new IllegalArgumentException("The class " + className + " doesn't exist in this Realm.");
        }
        table = realm.sharedRealm.getTable(className);
        dynamicClassToTable.put(className, table);
    }
    return table;
}
Also used : Table(io.realm.internal.Table)

Example 45 with Table

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

the class RealmSchema method rename.

/**
     * Renames a class already in the Realm.
     *
     * @param oldClassName old class name.
     * @param newClassName new class name.
     * @return a schema object for renamed class.
     */
public RealmObjectSchema rename(String oldClassName, String newClassName) {
    // Destructive modifications are not permitted.
    realm.checkNotInSync();
    checkEmpty(oldClassName, "Class names cannot be empty or null");
    checkEmpty(newClassName, "Class names cannot be empty or null");
    String oldInternalName = TABLE_PREFIX + oldClassName;
    String newInternalName = TABLE_PREFIX + newClassName;
    checkHasTable(oldClassName, "Cannot rename class because it doesn't exist in this Realm: " + oldClassName);
    if (realm.sharedRealm.hasTable(newInternalName)) {
        throw new IllegalArgumentException(oldClassName + " cannot be renamed because the new class already exists: " + newClassName);
    }
    // Checks if there is a primary key defined for the old class.
    Table oldTable = getTable(oldClassName);
    String pkField = null;
    if (oldTable.hasPrimaryKey()) {
        pkField = oldTable.getColumnName(oldTable.getPrimaryKey());
        oldTable.setPrimaryKey(null);
    }
    realm.sharedRealm.renameTable(oldInternalName, newInternalName);
    Table table = realm.sharedRealm.getTable(newInternalName);
    // Sets the primary key for the new class if necessary.
    if (pkField != null) {
        table.setPrimaryKey(pkField);
    }
    RealmObjectSchema.DynamicColumnMap columnIndices = new RealmObjectSchema.DynamicColumnMap(table);
    return new RealmObjectSchema(realm, table, columnIndices);
}
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