ValueType Object::get_property_value_impl(ContextType& ctx, const Property &property)
{
    verify_attached();

    size_t column = property.table_column;
    if (is_nullable(property.type) && m_row.is_null(column))
        return ctx.null_value();
    if (is_array(property.type) && property.type != PropertyType::LinkingObjects)
        return ctx.box(List(m_realm, *m_row.get_table(), column, m_row.get_index()));

    switch (property.type & ~PropertyType::Flags) {
        case PropertyType::Bool:   return ctx.box(m_row.get_bool(column));
        case PropertyType::Int:    return ctx.box(m_row.get_int(column));
        case PropertyType::Float:  return ctx.box(m_row.get_float(column));
        case PropertyType::Double: return ctx.box(m_row.get_double(column));
        case PropertyType::String: return ctx.box(m_row.get_string(column));
        case PropertyType::Data:   return ctx.box(m_row.get_binary(column));
        case PropertyType::Date:   return ctx.box(m_row.get_timestamp(column));
        case PropertyType::Any:    return ctx.box(m_row.get_mixed(column));
        case PropertyType::Object: {
            auto linkObjectSchema = m_realm->schema().find(property.object_type);
            TableRef table = ObjectStore::table_for_object_type(m_realm->read_group(), property.object_type);
            return ctx.box(Object(m_realm, *linkObjectSchema, table->get(m_row.get_link(column))));
        }
        case PropertyType::LinkingObjects: {
            auto target_object_schema = m_realm->schema().find(property.object_type);
            auto link_property = target_object_schema->property_for_name(property.link_origin_property_name);
            TableRef table = ObjectStore::table_for_object_type(m_realm->read_group(), target_object_schema->name);
            auto tv = m_row.get_table()->get_backlink_view(m_row.get_index(), table.get(), link_property->table_column);
            return ctx.box(Results(m_realm, std::move(tv)));
        }
        default: REALM_UNREACHABLE();
    }
}
ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(name) {
    TableRef tableRef = ObjectStore::table_for_object_type(group, name);
    Table *table = tableRef.get();

    size_t count = table->get_column_count();
    properties.reserve(count);
    for (size_t col = 0; col < count; col++) {
        Property property;
        property.name = table->get_column_name(col).data();
        property.type = (PropertyType)table->get_column_type(col);
        property.is_indexed = table->has_search_index(col);
        property.is_primary = false;
        property.table_column = col;
        if (property.type == PropertyTypeObject || property.type == PropertyTypeArray) {
            // set link type for objects and arrays
            realm::TableRef linkTable = table->get_link_target(col);
            property.object_type = ObjectStore::object_type_for_table_name(linkTable->get_name().data());
        }
        properties.push_back(move(property));
    }

    primary_key = realm::ObjectStore::get_primary_key_for_object(group, name);
    if (primary_key.length()) {
        auto primary_key_prop = primary_key_property();
        if (!primary_key_prop) {
            throw ObjectStoreValidationException({"No property matching primary key '" + primary_key + "'"}, name);
        }
        primary_key_prop->is_primary = true;
    }
}
Exemple #3
0
SyncFileActionMetadata::SyncFileActionMetadata(const SyncMetadataManager& manager,
                                               Action action,
                                               const std::string& original_name,
                                               const std::string& url,
                                               const std::string& user_identity,
                                               util::Optional<std::string> new_name)
: m_schema(manager.m_file_action_schema)
{
    size_t raw_action = static_cast<size_t>(action);

    // Open the Realm.
    m_realm = Realm::get_shared_realm(manager.get_configuration());

    // Retrieve or create the row for this object.
    TableRef table = ObjectStore::table_for_object_type(m_realm->read_group(), c_sync_fileActionMetadata);
    m_realm->begin_transaction();
    size_t row_idx = table->find_first_string(m_schema.idx_original_name, original_name);
    if (row_idx == not_found) {
        row_idx = table->add_empty_row();
        table->set_string(m_schema.idx_original_name, row_idx, original_name);
    }
    table->set_string(m_schema.idx_new_name, row_idx, new_name);
    table->set_int(m_schema.idx_action, row_idx, raw_action);
    table->set_string(m_schema.idx_url, row_idx, url);
    table->set_string(m_schema.idx_user_identity, row_idx, user_identity);
    m_realm->commit_transaction();
    m_row = table->get(row_idx);
}
Exemple #4
0
util::Optional<SyncFileActionMetadata> SyncFileActionMetadata::metadata_for_path(const std::string& original_name, const SyncMetadataManager& manager)
{
    auto realm = Realm::get_shared_realm(manager.get_configuration());
    auto schema = manager.m_file_action_schema;
    TableRef table = ObjectStore::table_for_object_type(realm->read_group(), c_sync_fileActionMetadata);
    size_t row_idx = table->find_first_string(schema.idx_original_name, original_name);
    if (row_idx == not_found) {
        return none;
    }
    return SyncFileActionMetadata(std::move(schema), std::move(realm), table->get(row_idx));
}                   
Exemple #5
0
SyncUserMetadata::SyncUserMetadata(const SyncMetadataManager& manager, std::string identity, bool make_if_absent)
: m_schema(manager.m_user_schema)
{
    // Open the Realm.
    m_realm = Realm::get_shared_realm(manager.get_configuration());

    // Retrieve or create the row for this object.
    TableRef table = ObjectStore::table_for_object_type(m_realm->read_group(), c_sync_userMetadata);
    size_t row_idx = table->find_first_string(m_schema.idx_identity, identity);
    if (row_idx == not_found) {
        if (!make_if_absent) {
            m_invalid = true;
            m_realm = nullptr;
            return;
        }
        m_realm->begin_transaction();
        row_idx = table->find_first_string(m_schema.idx_identity, identity);
        if (row_idx == not_found) {
            row_idx = table->add_empty_row();
            table->set_string(m_schema.idx_identity, row_idx, identity);
            table->set_bool(m_schema.idx_user_is_admin, row_idx, false);
            m_realm->commit_transaction();
        } else {
            // Someone beat us to adding this user.
            m_realm->cancel_transaction();
        }
    }
    m_row = table->get(row_idx);
    if (make_if_absent) {
        // User existed in the table, but had been marked for deletion. Unmark it.
        m_realm->begin_transaction();
        table->set_bool(m_schema.idx_marked_for_removal, row_idx, false);
        m_realm->commit_transaction();
        m_invalid = false;
    } else {
        m_invalid = m_row.get_bool(m_schema.idx_marked_for_removal);
    }
}
Exemple #6
0
Object Object::create(ContextType& ctx, std::shared_ptr<Realm> const& realm,
                      ObjectSchema const& object_schema, ValueType value,
                      bool try_update, Row* out_row)
{
    realm->verify_in_write();

    // get or create our accessor
    bool created = false;

    // try to get existing row if updating
    size_t row_index = realm::not_found;
    TableRef table = ObjectStore::table_for_object_type(realm->read_group(), object_schema.name);

    bool skip_primary = true;
    if (auto primary_prop = object_schema.primary_key_property()) {
        // search for existing object based on primary key type
        auto primary_value = ctx.value_for_property(value, primary_prop->name,
                                                    primary_prop - &object_schema.persisted_properties[0]);
        if (!primary_value)
            primary_value = ctx.default_value_for_property(object_schema, primary_prop->name);
        if (!primary_value) {
            if (!is_nullable(primary_prop->type))
                throw MissingPropertyValueException(object_schema.name, primary_prop->name);
            primary_value = ctx.null_value();
        }
        row_index = get_for_primary_key_impl(ctx, *table, *primary_prop, *primary_value);

        if (row_index == realm::not_found) {
            created = true;
            if (primary_prop->type == PropertyType::Int) {
#if REALM_HAVE_SYNC_STABLE_IDS
                row_index = sync::create_object_with_primary_key(realm->read_group(), *table, ctx.template unbox<util::Optional<int64_t>>(*primary_value));
#else
                row_index = table->add_empty_row();
                if (ctx.is_null(*primary_value))
                    table->set_null_unique(primary_prop->table_column, row_index);
                else
                    table->set_unique(primary_prop->table_column, row_index, ctx.template unbox<int64_t>(*primary_value));
#endif // REALM_HAVE_SYNC_STABLE_IDS
            }
            else if (primary_prop->type == PropertyType::String) {
                auto value = ctx.template unbox<StringData>(*primary_value);
#if REALM_HAVE_SYNC_STABLE_IDS
                row_index = sync::create_object_with_primary_key(realm->read_group(), *table, value);
#else
                row_index = table->add_empty_row();
                table->set_unique(primary_prop->table_column, row_index, value);
#endif // REALM_HAVE_SYNC_STABLE_IDS
            }
            else {
                REALM_TERMINATE("Unsupported primary key type.");
            }
        }
        else if (!try_update) {
            if (realm->is_in_migration()) {
                // Creating objects with duplicate primary keys is allowed in migrations
                // as long as there are no duplicates at the end, as adding an entirely
                // new column which is the PK will inherently result in duplicates at first
                row_index = table->add_empty_row();
                created = true;
                skip_primary = false;
            }
            else {
                throw std::logic_error(util::format("Attempting to create an object of type '%1' with an existing primary key value '%2'.",
                                                    object_schema.name, ctx.print(*primary_value)));
            }
        }
    }
    else {
#if REALM_HAVE_SYNC_STABLE_IDS
        row_index = sync::create_object(realm->read_group(), *table);
#else
        row_index = table->add_empty_row();
#endif // REALM_HAVE_SYNC_STABLE_IDS
        created = true;
    }

    // populate
    Object object(realm, object_schema, table->get(row_index));
    if (out_row)
        *out_row = object.row();
    for (size_t i = 0; i < object_schema.persisted_properties.size(); ++i) {
        auto& prop = object_schema.persisted_properties[i];
        if (skip_primary && prop.is_primary)
            continue;

        auto v = ctx.value_for_property(value, prop.name, i);
        if (!created && !v)
            continue;

        bool is_default = false;
        if (!v) {
            v = ctx.default_value_for_property(object_schema, prop.name);
            is_default = true;
        }
        if ((!v || ctx.is_null(*v)) && !is_nullable(prop.type) && !is_array(prop.type)) {
            if (prop.is_primary || !ctx.allow_missing(value))
                throw MissingPropertyValueException(object_schema.name, prop.name);
        }
        if (v)
            object.set_property_value_impl(ctx, prop, *v, try_update, is_default);
    }
    return object;
}
Exemple #7
0
 REALM_EXPORT Results* object_get_backlinks(Object& object, size_t property_ndx, NativeException::Marshallable& ex)
 {
     return handle_errors(ex, [&] {
         verify_can_get(object);
         const Property& prop = object.get_object_schema().computed_properties[property_ndx];
         REALM_ASSERT_DEBUG(prop.type == PropertyType::LinkingObjects);
         
         const ObjectSchema& relationship = *object.realm()->schema().find(prop.object_type);
         const TableRef table = ObjectStore::table_for_object_type(object.realm()->read_group(), relationship.name);
         const Property& link = *relationship.property_for_name(prop.link_origin_property_name);
         
         TableView backlink_view = object.row().get_table()->get_backlink_view(object.row().get_index(), table.get(), link.table_column);
         return new Results(object.realm(), backlink_view);
     });
 }