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);
}
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;
}
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);
}
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;
}
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;
}
Aggregations