Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Obj::get_parent_object in the C API #5851

Merged
merged 6 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Enhancements
* <New feature description> (PR [#????](https://github.com/realm/realm-core/pull/????))
* Convert object_store::Collection types into Results (PR [#5845](https://github.com/realm/realm-core/pull/5845))
* Expose `realm_object_get_parent` in the C API (PR [#5851](https://github.com/realm/realm-core/pull/5851))

### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
Expand Down
6 changes: 6 additions & 0 deletions src/realm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,12 @@ RLM_API bool realm_get_num_versions(const realm_t*, uint64_t* out_versions_count
*/
RLM_API realm_object_t* realm_get_object(const realm_t*, realm_class_key_t class_key, realm_object_key_t obj_key);

/**
* Get the parent object for the object passed as argument. Only works for embedded objects.
* @return true, if no errors occurred.
*/
RLM_API bool realm_object_get_parent(const realm_object_t* object, realm_object_t* parent);

/**
* Find an object with a particular primary key value.
*
Expand Down
10 changes: 10 additions & 0 deletions src/realm/object-store/c_api/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ RLM_API realm_object_t* realm_get_object(const realm_t* realm, realm_class_key_t
});
}

RLM_API bool realm_object_get_parent(const realm_object_t* object, realm_object_t* parent)
{
return wrap_err([&]() {
if (parent)
*parent = realm_object_t{realm::Object{object->get_realm(), object->obj().get_parent_object()}};
Comment on lines +34 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen the C API littered with these types of checks and I'm no sure I understand their purpose. Not supplying a pointer to store the parent in sounds like a fundamental error in using the API. I'd rather have this be an assert than the method silently not doing anything.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but it is important we keep the same pattern, I can clean all this up in a separate PR.

return true;
});
}


RLM_API realm_object_t* realm_object_find_with_primary_key(const realm_t* realm, realm_class_key_t class_key,
realm_value_t pk, bool* out_found)
{
Expand Down
8 changes: 6 additions & 2 deletions src/realm/object-store/c_api/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,15 @@ RLM_API realm_query_t* realm_query_parse_for_list(const realm_list_t* list, cons
const realm_query_arg_t* args)
{
return wrap_err([&]() {
auto existing_query = list->get_query();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is part of another PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes sorry, I have multiple small PRs open... my bad. let me revert this..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code has been reverted

auto realm = list->get_realm();
auto table = list->get_table();
auto query = parse_and_apply_query(realm, table, query_string, num_args, args);
auto ordering = query.get_ordering();
return new realm_query_t{std::move(query), std::move(ordering), realm};
auto combined = existing_query.and_query(query);
auto ordering_copy = util::make_bind<DescriptorOrdering>();
if (auto ordering = query.get_ordering())
ordering_copy->append(*ordering);
return new realm_query_t{std::move(query), std::move(ordering_copy), realm};
});
}

Expand Down