2015-10-27 13:59:15 -07:00
|
|
|
/* Copyright 2015 Realm Inc - All Rights Reserved
|
|
|
|
* Proprietary and Confidential
|
|
|
|
*/
|
2015-08-13 09:12:48 -07:00
|
|
|
|
|
|
|
#import "RJSResults.hpp"
|
|
|
|
#import "RJSObject.hpp"
|
|
|
|
#import "object_accessor.hpp"
|
|
|
|
#import "results.hpp"
|
2015-11-09 21:12:18 -08:00
|
|
|
#import "parser.hpp"
|
2015-11-10 12:51:21 -08:00
|
|
|
#import "query_builder.hpp"
|
2015-08-13 09:12:48 -07:00
|
|
|
|
|
|
|
using namespace realm;
|
|
|
|
|
|
|
|
JSValueRef ResultsGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* jsException) {
|
|
|
|
try {
|
|
|
|
// index subscripting
|
|
|
|
Results *results = RJSGetInternal<Results *>(object);
|
|
|
|
size_t size = results->size();
|
|
|
|
|
|
|
|
std::string indexStr = RJSStringForJSString(propertyName);
|
|
|
|
if (indexStr == "length") {
|
|
|
|
return JSValueMakeNumber(ctx, size);
|
|
|
|
}
|
|
|
|
|
2015-10-12 15:35:13 -07:00
|
|
|
return RJSObjectCreate(ctx, Object(results->realm, results->object_schema, results->get(RJSValidatedPositiveIndex(indexStr))));
|
|
|
|
}
|
|
|
|
catch (std::out_of_range &exp) {
|
|
|
|
// getters for nonexistent properties in JS should always return undefined
|
|
|
|
return JSValueMakeUndefined(ctx);
|
2015-08-13 09:12:48 -07:00
|
|
|
}
|
|
|
|
catch (std::invalid_argument &exp) {
|
|
|
|
// for stol failure this could be another property that is handled externally, so ignore
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
catch (std::exception &exp) {
|
|
|
|
if (jsException) {
|
|
|
|
*jsException = RJSMakeError(ctx, exp);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 15:25:15 -07:00
|
|
|
bool ResultsSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef *jsException) {
|
|
|
|
try {
|
|
|
|
std::string indexStr = RJSStringForJSString(propertyName);
|
|
|
|
if (indexStr != "length") {
|
|
|
|
std::stol(RJSStringForJSString(propertyName));
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempts to assign to 'length' or an index should throw an exception
|
2015-10-21 15:59:11 -07:00
|
|
|
if (jsException) {
|
|
|
|
*jsException = RJSMakeError(ctx, "Results objects are readonly");
|
|
|
|
}
|
2015-10-21 15:25:15 -07:00
|
|
|
}
|
|
|
|
catch (std::invalid_argument &exp) {
|
|
|
|
// for stol failure this could be another property that is handled externally, so ignore
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-13 09:12:48 -07:00
|
|
|
void ResultsPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) {
|
|
|
|
Results *results = RJSGetInternal<Results *>(object);
|
|
|
|
char str[32];
|
|
|
|
for (int i = 0; i < results->table_view.size(); i++) {
|
|
|
|
sprintf(str, "%i", i);
|
|
|
|
JSStringRef name = JSStringCreateWithUTF8CString(str);
|
|
|
|
JSPropertyNameAccumulatorAddName(propertyNames, name);
|
|
|
|
JSStringRelease(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 15:46:31 -07:00
|
|
|
JSValueRef SortByProperty(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) {
|
|
|
|
try {
|
|
|
|
Results *results = RJSGetInternal<Results *>(thisObject);
|
|
|
|
RJSValidateArgumentRange(argumentCount, 1, 2);
|
|
|
|
std::string propName = RJSValidatedStringForValue(ctx, arguments[0]);
|
|
|
|
Property *prop = results->object_schema.property_for_name(propName);
|
|
|
|
if (!prop) {
|
|
|
|
throw std::runtime_error("Property '" + propName + "' does not exist on object type '" + results->object_schema.name + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ascending = true;
|
|
|
|
if (argumentCount == 2) {
|
2015-10-12 15:42:56 -07:00
|
|
|
ascending = JSValueToBoolean(ctx, arguments[1]);
|
2015-09-03 15:46:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
SortOrder sort = {{prop->table_column}, {ascending}};
|
|
|
|
results->setSort(sort);
|
|
|
|
}
|
|
|
|
catch (std::exception &exp) {
|
|
|
|
if (jsException) {
|
|
|
|
*jsException = RJSMakeError(ctx, exp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-13 09:12:48 -07:00
|
|
|
JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, std::string className) {
|
|
|
|
TableRef table = ObjectStore::table_for_object_type(realm->read_group(), className);
|
2015-09-09 17:16:32 -07:00
|
|
|
auto object_schema = realm->config().schema->find(className);
|
|
|
|
if (object_schema == realm->config().schema->end()) {
|
|
|
|
throw std::runtime_error("Object type '" + className + "' not present in Realm.");
|
|
|
|
}
|
|
|
|
return RJSWrapObject<Results *>(ctx, RJSResultsClass(), new Results(realm, *object_schema, table->where()));
|
2015-08-13 09:12:48 -07:00
|
|
|
}
|
|
|
|
|
2015-08-17 11:40:13 -07:00
|
|
|
|
2015-11-10 15:58:04 -08:00
|
|
|
JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, std::string className, std::string queryString, std::vector<JSValueRef> args) {
|
2015-08-13 09:12:48 -07:00
|
|
|
TableRef table = ObjectStore::table_for_object_type(realm->read_group(), className);
|
|
|
|
Query query = table->where();
|
2015-08-17 11:40:13 -07:00
|
|
|
Schema &schema = *realm->config().schema;
|
2015-09-09 17:16:32 -07:00
|
|
|
auto object_schema = realm->config().schema->find(className);
|
2015-09-09 17:27:42 -07:00
|
|
|
if (object_schema == realm->config().schema->end()) {
|
|
|
|
throw std::runtime_error("Object type '" + className + "' not present in Realm.");
|
|
|
|
}
|
2015-11-09 21:12:18 -08:00
|
|
|
parser::Predicate predicate = parser::parse(queryString);
|
2015-11-10 15:58:04 -08:00
|
|
|
query_builder::ArgumentConverter<JSValueRef, JSContextRef> arguments(ctx, args);
|
|
|
|
query_builder::apply_predicate(query, predicate, arguments, schema, object_schema->name);
|
2015-11-09 21:12:18 -08:00
|
|
|
|
2015-09-09 17:16:32 -07:00
|
|
|
return RJSWrapObject<Results *>(ctx, RJSResultsClass(), new Results(realm, *object_schema, std::move(query)));
|
2015-08-13 09:12:48 -07:00
|
|
|
}
|
|
|
|
|
2015-10-19 15:52:32 -07:00
|
|
|
static const JSStaticFunction RJSResultsFuncs[] = {
|
2015-10-14 18:53:37 -07:00
|
|
|
{"sortByProperty", SortByProperty, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
|
|
|
|
{NULL, NULL},
|
|
|
|
};
|
2015-08-13 09:12:48 -07:00
|
|
|
|
|
|
|
JSClassRef RJSResultsClass() {
|
2015-10-21 15:25:15 -07:00
|
|
|
static JSClassRef s_objectClass = RJSCreateWrapperClass<Results *>("Results", ResultsGetProperty, ResultsSetProperty, RJSResultsFuncs, ResultsPropertyNames);
|
2015-08-13 09:12:48 -07:00
|
|
|
return s_objectClass;
|
|
|
|
}
|