2015-10-27 20:59:15 +00:00
|
|
|
/* Copyright 2015 Realm Inc - All Rights Reserved
|
|
|
|
* Proprietary and Confidential
|
|
|
|
*/
|
2015-08-13 16:28:53 +00:00
|
|
|
|
|
|
|
#ifndef REALM_OBJECT_ACCESSOR_HPP
|
|
|
|
#define REALM_OBJECT_ACCESSOR_HPP
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include "shared_realm.hpp"
|
2015-11-26 03:57:15 +00:00
|
|
|
#include "schema.hpp"
|
2015-10-13 22:25:06 +00:00
|
|
|
#include "list.hpp"
|
2015-08-13 16:28:53 +00:00
|
|
|
|
|
|
|
namespace realm {
|
2015-10-13 22:25:06 +00:00
|
|
|
|
|
|
|
class Object {
|
|
|
|
public:
|
2015-10-26 20:32:29 +00:00
|
|
|
Object(SharedRealm r, const ObjectSchema &s, Row o) : m_realm(r), object_schema(s), m_row(o) {}
|
2015-10-13 22:25:06 +00:00
|
|
|
|
|
|
|
// property getter/setter
|
|
|
|
template<typename ValueType, typename ContextType>
|
|
|
|
inline void set_property_value(ContextType ctx, std::string prop_name, ValueType value, bool try_update);
|
|
|
|
|
|
|
|
template<typename ValueType, typename ContextType>
|
|
|
|
inline ValueType get_property_value(ContextType ctx, std::string prop_name);
|
|
|
|
|
|
|
|
// create an Object from a native representation
|
|
|
|
template<typename ValueType, typename ContextType>
|
2015-12-17 23:59:34 +00:00
|
|
|
static inline Object create(ContextType ctx, SharedRealm realm, const ObjectSchema &object_schema, ValueType value, bool try_update);
|
2015-10-26 20:24:27 +00:00
|
|
|
|
2015-12-18 01:32:51 +00:00
|
|
|
const ObjectSchema &object_schema;
|
2015-10-26 20:24:27 +00:00
|
|
|
SharedRealm realm() { return m_realm; }
|
|
|
|
Row row() { return m_row; }
|
|
|
|
|
2015-10-13 22:25:06 +00:00
|
|
|
private:
|
2015-10-26 20:24:27 +00:00
|
|
|
SharedRealm m_realm;
|
|
|
|
Row m_row;
|
|
|
|
|
2015-10-13 22:25:06 +00:00
|
|
|
template<typename ValueType, typename ContextType>
|
2015-10-26 20:24:27 +00:00
|
|
|
inline void set_property_value_impl(ContextType ctx, const Property &property, ValueType value, bool try_update);
|
2015-10-13 22:25:06 +00:00
|
|
|
template<typename ValueType, typename ContextType>
|
2015-10-26 20:24:27 +00:00
|
|
|
inline ValueType get_property_value_impl(ContextType ctx, const Property &property);
|
2015-10-13 22:25:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Value converters - template specializations must be implemented for each platform in order to call templated methods on Object
|
|
|
|
//
|
2015-08-13 16:28:53 +00:00
|
|
|
template<typename ValueType, typename ContextType>
|
|
|
|
class NativeAccessor {
|
|
|
|
public:
|
2015-09-03 21:37:22 +00:00
|
|
|
static bool dict_has_value_for_key(ContextType ctx, ValueType dict, const std::string &prop_name);
|
2015-08-13 16:28:53 +00:00
|
|
|
static ValueType dict_value_for_key(ContextType ctx, ValueType dict, const std::string &prop_name);
|
|
|
|
|
2015-10-19 20:39:21 +00:00
|
|
|
static bool has_default_value_for_property(ContextType ctx, Realm *realm, const ObjectSchema &object_schema, const std::string &prop_name);
|
|
|
|
static ValueType default_value_for_property(ContextType ctx, Realm *realm, const ObjectSchema &object_schema, const std::string &prop_name);
|
2015-09-04 22:43:26 +00:00
|
|
|
|
2015-10-13 22:25:06 +00:00
|
|
|
static bool to_bool(ContextType, ValueType &);
|
|
|
|
static ValueType from_bool(ContextType, bool);
|
|
|
|
static long long to_long(ContextType, ValueType &);
|
|
|
|
static ValueType from_long(ContextType, long long);
|
|
|
|
static float to_float(ContextType, ValueType &);
|
|
|
|
static ValueType from_float(ContextType, float);
|
|
|
|
static double to_double(ContextType, ValueType &);
|
|
|
|
static ValueType from_double(ContextType, double);
|
|
|
|
static std::string to_string(ContextType, ValueType &);
|
|
|
|
static ValueType from_string(ContextType, StringData);
|
2015-11-16 11:26:33 +00:00
|
|
|
static std::string to_binary(ContextType, ValueType &);
|
|
|
|
static ValueType from_binary(ContextType, BinaryData);
|
2015-10-13 22:25:06 +00:00
|
|
|
static DateTime to_datetime(ContextType, ValueType &);
|
|
|
|
static ValueType from_datetime(ContextType, DateTime);
|
|
|
|
|
|
|
|
static bool is_null(ContextType, ValueType &);
|
|
|
|
static ValueType null_value(ContextType);
|
2015-08-13 16:28:53 +00:00
|
|
|
|
|
|
|
// convert value to persisted object
|
|
|
|
// for existing objects return the existing row index
|
|
|
|
// for new/updated objects return the row index
|
2015-10-26 20:32:29 +00:00
|
|
|
static size_t to_object_index(ContextType ctx, SharedRealm realm, ValueType &val, const std::string &type, bool try_update);
|
2015-10-13 22:25:06 +00:00
|
|
|
static ValueType from_object(ContextType ctx, Object);
|
2015-08-13 16:28:53 +00:00
|
|
|
|
2015-11-10 23:58:04 +00:00
|
|
|
// object index for an existing object
|
|
|
|
static size_t to_existing_object_index(ContextType ctx, ValueType &val);
|
|
|
|
|
2015-10-13 22:25:06 +00:00
|
|
|
// list value acessors
|
|
|
|
static size_t list_size(ContextType ctx, ValueType &val);
|
|
|
|
static ValueType list_value_at_index(ContextType ctx, ValueType &val, size_t index);
|
|
|
|
static ValueType from_list(ContextType ctx, List);
|
2015-08-13 16:28:53 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Deprecated
|
|
|
|
//
|
|
|
|
static Mixed to_mixed(ContextType ctx, ValueType &val) { throw std::runtime_error("'Any' type is unsupported"); }
|
|
|
|
};
|
|
|
|
|
2015-10-27 16:12:39 +00:00
|
|
|
class InvalidPropertyException : public std::runtime_error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InvalidPropertyException(const std::string object_type, const std::string property_name, const std::string message) : std::runtime_error(message), object_type(object_type), property_name(property_name) {}
|
|
|
|
const std::string object_type;
|
|
|
|
const std::string property_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MissingPropertyValueException : public std::runtime_error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MissingPropertyValueException(const std::string object_type, const std::string property_name, const std::string message) : std::runtime_error(message), object_type(object_type), property_name(property_name) {}
|
|
|
|
const std::string object_type;
|
|
|
|
const std::string property_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MutationOutsideTransactionException : public std::runtime_error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MutationOutsideTransactionException(std::string message) : std::runtime_error(message) {}
|
|
|
|
};
|
|
|
|
|
2015-08-13 16:28:53 +00:00
|
|
|
//
|
|
|
|
// template method implementations
|
|
|
|
//
|
|
|
|
template <typename ValueType, typename ContextType>
|
|
|
|
inline void Object::set_property_value(ContextType ctx, std::string prop_name, ValueType value, bool try_update)
|
|
|
|
{
|
2015-10-26 20:24:27 +00:00
|
|
|
const Property *prop = object_schema.property_for_name(prop_name);
|
2015-08-13 16:28:53 +00:00
|
|
|
if (!prop) {
|
2015-10-27 16:12:39 +00:00
|
|
|
throw InvalidPropertyException(object_schema.name, prop_name,
|
|
|
|
"Setting invalid property '" + prop_name + "' on object '" + object_schema.name + "'.");
|
2015-08-13 16:28:53 +00:00
|
|
|
}
|
|
|
|
set_property_value_impl(ctx, *prop, value, try_update);
|
|
|
|
};
|
|
|
|
|
2015-10-13 22:25:06 +00:00
|
|
|
template <typename ValueType, typename ContextType>
|
|
|
|
inline ValueType Object::get_property_value(ContextType ctx, std::string prop_name)
|
|
|
|
{
|
2015-10-26 20:24:27 +00:00
|
|
|
const Property *prop = object_schema.property_for_name(prop_name);
|
2015-10-13 22:25:06 +00:00
|
|
|
if (!prop) {
|
2015-10-27 16:12:39 +00:00
|
|
|
throw InvalidPropertyException(object_schema.name, prop_name,
|
|
|
|
"Getting invalid property '" + prop_name + "' on object '" + object_schema.name + "'.");
|
2015-10-13 22:25:06 +00:00
|
|
|
}
|
|
|
|
return get_property_value_impl<ValueType>(ctx, *prop);
|
|
|
|
};
|
|
|
|
|
2015-08-13 16:28:53 +00:00
|
|
|
template <typename ValueType, typename ContextType>
|
2015-10-26 20:24:27 +00:00
|
|
|
inline void Object::set_property_value_impl(ContextType ctx, const Property &property, ValueType value, bool try_update)
|
2015-08-13 16:28:53 +00:00
|
|
|
{
|
|
|
|
using Accessor = NativeAccessor<ValueType, ContextType>;
|
|
|
|
|
2015-10-26 20:24:27 +00:00
|
|
|
if (!m_realm->is_in_transaction()) {
|
2015-10-27 16:12:39 +00:00
|
|
|
throw MutationOutsideTransactionException("Can only set property values within a transaction.");
|
2015-10-12 09:02:23 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 16:28:53 +00:00
|
|
|
size_t column = property.table_column;
|
2015-10-27 17:04:56 +00:00
|
|
|
if (property.is_nullable && Accessor::is_null(ctx, value)) {
|
|
|
|
m_row.set_null(column);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-13 16:28:53 +00:00
|
|
|
switch (property.type) {
|
|
|
|
case PropertyTypeBool:
|
2015-10-26 20:24:27 +00:00
|
|
|
m_row.set_bool(column, Accessor::to_bool(ctx, value));
|
2015-08-13 16:28:53 +00:00
|
|
|
break;
|
|
|
|
case PropertyTypeInt:
|
2015-10-26 20:24:27 +00:00
|
|
|
m_row.set_int(column, Accessor::to_long(ctx, value));
|
2015-08-13 16:28:53 +00:00
|
|
|
break;
|
|
|
|
case PropertyTypeFloat:
|
2015-10-26 20:24:27 +00:00
|
|
|
m_row.set_float(column, Accessor::to_float(ctx, value));
|
2015-08-13 16:28:53 +00:00
|
|
|
break;
|
|
|
|
case PropertyTypeDouble:
|
2015-10-26 20:24:27 +00:00
|
|
|
m_row.set_double(column, Accessor::to_double(ctx, value));
|
2015-08-13 16:28:53 +00:00
|
|
|
break;
|
|
|
|
case PropertyTypeString:
|
2015-10-26 20:24:27 +00:00
|
|
|
m_row.set_string(column, Accessor::to_string(ctx, value));
|
2015-08-13 16:28:53 +00:00
|
|
|
break;
|
|
|
|
case PropertyTypeData:
|
2015-11-16 11:26:33 +00:00
|
|
|
m_row.set_binary(column, BinaryData(Accessor::to_binary(ctx, value)));
|
2015-08-13 16:28:53 +00:00
|
|
|
break;
|
|
|
|
case PropertyTypeAny:
|
2015-10-26 20:24:27 +00:00
|
|
|
m_row.set_mixed(column, Accessor::to_mixed(ctx, value));
|
2015-08-13 16:28:53 +00:00
|
|
|
break;
|
|
|
|
case PropertyTypeDate:
|
2015-10-26 20:24:27 +00:00
|
|
|
m_row.set_datetime(column, Accessor::to_datetime(ctx, value));
|
2015-08-13 16:28:53 +00:00
|
|
|
break;
|
|
|
|
case PropertyTypeObject: {
|
|
|
|
if (Accessor::is_null(ctx, value)) {
|
2015-10-26 20:24:27 +00:00
|
|
|
m_row.nullify_link(column);
|
2015-08-13 16:28:53 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-10-26 20:24:27 +00:00
|
|
|
m_row.set_link(column, Accessor::to_object_index(ctx, m_realm, value, property.object_type, try_update));
|
2015-08-13 16:28:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PropertyTypeArray: {
|
2015-10-26 20:24:27 +00:00
|
|
|
realm::LinkViewRef link_view = m_row.get_linklist(column);
|
2015-08-13 16:28:53 +00:00
|
|
|
link_view->clear();
|
2015-10-13 22:25:06 +00:00
|
|
|
size_t count = Accessor::list_size(ctx, value);
|
2015-08-13 16:28:53 +00:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
2015-10-13 22:25:06 +00:00
|
|
|
ValueType element = Accessor::list_value_at_index(ctx, value, i);
|
2015-10-26 20:24:27 +00:00
|
|
|
link_view->add(Accessor::to_object_index(ctx, m_realm, element, property.object_type, try_update));
|
2015-08-13 16:28:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 22:25:06 +00:00
|
|
|
template <typename ValueType, typename ContextType>
|
2015-10-26 20:24:27 +00:00
|
|
|
inline ValueType Object::get_property_value_impl(ContextType ctx, const Property &property)
|
2015-10-13 22:25:06 +00:00
|
|
|
{
|
|
|
|
using Accessor = NativeAccessor<ValueType, ContextType>;
|
|
|
|
|
|
|
|
size_t column = property.table_column;
|
2015-10-27 17:04:56 +00:00
|
|
|
if (property.is_nullable && m_row.is_null(column)) {
|
|
|
|
return Accessor::null_value(ctx);
|
|
|
|
}
|
|
|
|
|
2015-10-13 22:25:06 +00:00
|
|
|
switch (property.type) {
|
|
|
|
case PropertyTypeBool:
|
2015-10-26 20:24:27 +00:00
|
|
|
return Accessor::from_bool(ctx, m_row.get_bool(column));
|
2015-10-13 22:25:06 +00:00
|
|
|
case PropertyTypeInt:
|
2015-10-26 20:24:27 +00:00
|
|
|
return Accessor::from_long(ctx, m_row.get_int(column));
|
2015-10-13 22:25:06 +00:00
|
|
|
case PropertyTypeFloat:
|
2015-10-26 20:24:27 +00:00
|
|
|
return Accessor::from_float(ctx, m_row.get_float(column));
|
2015-10-13 22:25:06 +00:00
|
|
|
case PropertyTypeDouble:
|
2015-10-26 20:24:27 +00:00
|
|
|
return Accessor::from_double(ctx, m_row.get_double(column));
|
2015-10-13 22:25:06 +00:00
|
|
|
case PropertyTypeString:
|
2015-10-26 20:24:27 +00:00
|
|
|
return Accessor::from_string(ctx, m_row.get_string(column));
|
2015-10-13 22:25:06 +00:00
|
|
|
case PropertyTypeData:
|
2015-11-16 11:26:33 +00:00
|
|
|
return Accessor::from_binary(ctx, m_row.get_binary(column));
|
2015-10-13 22:25:06 +00:00
|
|
|
case PropertyTypeAny:
|
|
|
|
throw "Any not supported";
|
|
|
|
case PropertyTypeDate:
|
2015-10-26 20:24:27 +00:00
|
|
|
return Accessor::from_datetime(ctx, m_row.get_datetime(column));
|
2015-10-13 22:25:06 +00:00
|
|
|
case PropertyTypeObject: {
|
2015-10-26 20:24:27 +00:00
|
|
|
auto linkObjectSchema = m_realm->config().schema->find(property.object_type);
|
|
|
|
TableRef table = ObjectStore::table_for_object_type(m_realm->read_group(), linkObjectSchema->name);
|
|
|
|
if (m_row.is_null_link(property.table_column)) {
|
2015-10-13 22:25:06 +00:00
|
|
|
return Accessor::null_value(ctx);
|
|
|
|
}
|
2015-10-26 20:24:27 +00:00
|
|
|
return Accessor::from_object(ctx, std::move(Object(m_realm, *linkObjectSchema, table->get(m_row.get_link(column)))));
|
2015-10-13 22:25:06 +00:00
|
|
|
}
|
|
|
|
case PropertyTypeArray: {
|
2015-10-26 20:24:27 +00:00
|
|
|
auto arrayObjectSchema = m_realm->config().schema->find(property.object_type);
|
|
|
|
return Accessor::from_list(ctx, std::move(List(m_realm, *arrayObjectSchema, static_cast<LinkViewRef>(m_row.get_linklist(column)))));
|
2015-10-13 22:25:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 16:28:53 +00:00
|
|
|
template<typename ValueType, typename ContextType>
|
2015-12-17 23:59:34 +00:00
|
|
|
inline Object Object::create(ContextType ctx, SharedRealm realm, const ObjectSchema &object_schema, ValueType value, bool try_update)
|
2015-08-13 16:28:53 +00:00
|
|
|
{
|
|
|
|
using Accessor = NativeAccessor<ValueType, ContextType>;
|
|
|
|
|
|
|
|
if (!realm->is_in_transaction()) {
|
2015-10-27 16:12:39 +00:00
|
|
|
throw MutationOutsideTransactionException("Can only create objects within a transaction.");
|
2015-08-13 16:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get or create our accessor
|
|
|
|
bool created;
|
|
|
|
|
|
|
|
// try to get existing row if updating
|
|
|
|
size_t row_index = realm::not_found;
|
|
|
|
realm::TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object_schema.name);
|
2015-10-26 20:24:27 +00:00
|
|
|
const Property *primary_prop = object_schema.primary_key_property();
|
2015-09-03 21:05:56 +00:00
|
|
|
if (primary_prop) {
|
2015-08-13 16:28:53 +00:00
|
|
|
// search for existing object based on primary key type
|
|
|
|
ValueType primary_value = Accessor::dict_value_for_key(ctx, value, object_schema.primary_key);
|
|
|
|
if (primary_prop->type == PropertyTypeString) {
|
|
|
|
row_index = table->find_first_string(primary_prop->table_column, Accessor::to_string(ctx, primary_value));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
row_index = table->find_first_int(primary_prop->table_column, Accessor::to_long(ctx, primary_value));
|
|
|
|
}
|
2015-09-03 21:05:56 +00:00
|
|
|
|
|
|
|
if (!try_update && row_index != realm::not_found) {
|
2015-10-27 16:12:39 +00:00
|
|
|
throw DuplicatePrimaryKeyValueException(object_schema.name, *primary_prop,
|
|
|
|
"Attempting to create an object of type '" + object_schema.name + "' with an exising primary key value.");
|
2015-09-03 21:05:56 +00:00
|
|
|
}
|
2015-08-13 16:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if no existing, create row
|
|
|
|
created = false;
|
|
|
|
if (row_index == realm::not_found) {
|
|
|
|
row_index = table->add_empty_row();
|
|
|
|
created = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// populate
|
|
|
|
Object object(realm, object_schema, table->get(row_index));
|
2015-12-17 23:59:34 +00:00
|
|
|
for (const Property &prop : object_schema.properties) {
|
2015-09-04 22:43:26 +00:00
|
|
|
if (created || !prop.is_primary) {
|
|
|
|
if (Accessor::dict_has_value_for_key(ctx, value, prop.name)) {
|
|
|
|
object.set_property_value_impl(ctx, prop, Accessor::dict_value_for_key(ctx, value, prop.name), try_update);
|
|
|
|
}
|
|
|
|
else if (created) {
|
2015-10-19 20:39:21 +00:00
|
|
|
if (Accessor::has_default_value_for_property(ctx, realm.get(), object_schema, prop.name)) {
|
|
|
|
object.set_property_value_impl(ctx, prop, Accessor::default_value_for_property(ctx, realm.get(), object_schema, prop.name), try_update);
|
2015-09-04 22:43:26 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-10-27 16:12:39 +00:00
|
|
|
throw MissingPropertyValueException(object_schema.name, prop.name,
|
|
|
|
"Missing property value for property " + prop.name);
|
2015-09-04 22:43:26 +00:00
|
|
|
}
|
2015-08-13 16:28:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
2015-11-18 22:36:46 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// List implementation
|
|
|
|
//
|
|
|
|
template<typename ValueType, typename ContextType>
|
|
|
|
void List::add(ContextType ctx, ValueType value)
|
|
|
|
{
|
|
|
|
add(NativeAccessor<ValueType, ContextType>::to_object_index(ctx, m_realm, value, object_schema.name, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ValueType, typename ContextType>
|
|
|
|
void List::insert(ContextType ctx, ValueType value, size_t list_ndx)
|
|
|
|
{
|
|
|
|
insert(list_ndx, NativeAccessor<ValueType, ContextType>::to_object_index(ctx, m_realm, value, object_schema.name, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ValueType, typename ContextType>
|
|
|
|
void List::set(ContextType ctx, ValueType value, size_t list_ndx)
|
|
|
|
{
|
|
|
|
set(list_ndx, NativeAccessor<ValueType, ContextType>::to_object_index(ctx, m_realm, value, object_schema.name, false));
|
|
|
|
}
|
2015-08-13 16:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(REALM_OBJECT_ACCESSOR_HPP) */
|