2015-08-14 15:18:49 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-08-13 16:12:48 +00:00
|
|
|
//
|
2015-08-14 15:18:49 +00:00
|
|
|
// Copyright 2015 Realm Inc.
|
2015-08-13 16:12:48 +00:00
|
|
|
//
|
2015-08-14 15:18:49 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-08-13 16:12:48 +00:00
|
|
|
//
|
2015-08-14 15:18:49 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-08-13 16:12:48 +00:00
|
|
|
|
|
|
|
#import "RJSResults.hpp"
|
|
|
|
#import "RJSObject.hpp"
|
|
|
|
#import "object_accessor.hpp"
|
|
|
|
#import "results.hpp"
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RJSObjectCreate(ctx, Object(results->realm, results->object_schema, results->get(std::stol(indexStr))));
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 22:46:31 +00: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) {
|
|
|
|
ascending = RJSValidatedValueToBool(ctx, arguments[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
SortOrder sort = {{prop->table_column}, {ascending}};
|
|
|
|
results->setSort(sort);
|
|
|
|
}
|
|
|
|
catch (std::exception &exp) {
|
|
|
|
if (jsException) {
|
|
|
|
*jsException = RJSMakeError(ctx, exp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, std::string className) {
|
|
|
|
TableRef table = ObjectStore::table_for_object_type(realm->read_group(), className);
|
|
|
|
return RJSWrapObject<Results *>(ctx, RJSResultsClass(), new Results(realm, realm->config().schema->at(className), table->where()));
|
|
|
|
}
|
|
|
|
|
2015-08-17 18:40:13 +00:00
|
|
|
|
|
|
|
void RLMUpdateQueryWithPredicate(realm::Query *query, NSPredicate *predicate, realm::Schema &schema, realm::ObjectSchema &objectSchema);
|
|
|
|
|
2015-08-13 16:12:48 +00:00
|
|
|
JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, std::string className, std::string queryString) {
|
|
|
|
TableRef table = ObjectStore::table_for_object_type(realm->read_group(), className);
|
|
|
|
Query query = table->where();
|
2015-08-17 18:40:13 +00:00
|
|
|
Schema &schema = *realm->config().schema;
|
|
|
|
ObjectSchema &object_schema = realm->config().schema->at(className);
|
|
|
|
@try {
|
|
|
|
RLMUpdateQueryWithPredicate(&query, [NSPredicate predicateWithFormat:@(queryString.c_str())], schema, object_schema);
|
|
|
|
}
|
|
|
|
@catch(NSException *ex) {
|
|
|
|
throw std::runtime_error(ex.description.UTF8String);
|
|
|
|
}
|
|
|
|
return RJSWrapObject<Results *>(ctx, RJSResultsClass(), new Results(realm, object_schema, std::move(query)));
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JSClassRef RJSResultsClass() {
|
2015-09-03 22:46:31 +00:00
|
|
|
const JSStaticFunction resultsFuncs[] = {
|
|
|
|
{"sortByProperty", SortByProperty, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
|
|
|
|
{NULL, NULL},
|
|
|
|
};
|
|
|
|
static JSClassRef s_objectClass = RJSCreateWrapperClass<Object>("Results", ResultsGetProperty, NULL, resultsFuncs, NULL, ResultsPropertyNames);
|
2015-08-13 16:12:48 +00:00
|
|
|
return s_objectClass;
|
|
|
|
}
|