diff --git a/src/js_realm.cpp b/src/js_realm.cpp index 11d9fbc6..c2196ce5 100644 --- a/src/js_realm.cpp +++ b/src/js_realm.cpp @@ -226,21 +226,10 @@ JSValueRef RealmGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef pr JSValueRef RealmObjects(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { try { - RJSValidateArgumentCountIsAtLeast(argumentCount, 1); + RJSValidateArgumentCount(1, argumentCount); std::string className = RJSValidatedStringForValue(ctx, arguments[0], "objectType"); SharedRealm sharedRealm = *RJSGetInternal(thisObject); - - if (argumentCount == 1) { - return RJSResultsCreate(ctx, sharedRealm, className); - } - else { - std::string query = RJSValidatedStringForValue(ctx, arguments[1], "predicate"); - std::vector args; - for (size_t i = 2; i < argumentCount; i++) { - args.push_back(arguments[i]); - } - return RJSResultsCreate(ctx, sharedRealm, className, query, args); - } + return RJSResultsCreate(ctx, sharedRealm, className); } catch (std::exception &exp) { if (jsException) { diff --git a/src/js_results.cpp b/src/js_results.cpp index fb242ce3..f05d7b57 100644 --- a/src/js_results.cpp +++ b/src/js_results.cpp @@ -117,6 +117,36 @@ JSValueRef ResultsSortByProperty(JSContextRef ctx, JSObjectRef function, JSObjec return NULL; } + +JSValueRef ResultsFiltered(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { + try { + Results *results = RJSGetInternal(thisObject); + + RJSValidateArgumentCountIsAtLeast(argumentCount, 1); + SharedRealm sharedRealm = *RJSGetInternal(thisObject); + + std::string queryString = RJSValidatedStringForValue(ctx, arguments[0], "predicate"); + std::vector args; + for (size_t i = 1; i < argumentCount; i++) { + args.push_back(arguments[i]); + } + + parser::Predicate predicate = parser::parse(queryString); + query_builder::ArgumentConverter arguments(ctx, args); + Query query = results->get_query(); + query_builder::apply_predicate(query, predicate, arguments, *sharedRealm->config().schema, + results->get_object_schema().name); + + return RJSResultsCreate(ctx, sharedRealm, results->get_object_schema(), query); + } + catch (std::exception &exp) { + if (jsException) { + *jsException = RJSMakeError(ctx, exp); + } + } + return NULL; +} + JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, std::string className) { TableRef table = ObjectStore::table_for_object_type(realm->read_group(), className); auto object_schema = realm->config().schema->find(className); @@ -151,6 +181,7 @@ JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, const ObjectSc static const JSStaticFunction RJSResultsFuncs[] = { {"snapshot", ResultsStaticCopy, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"sortByProperty", ResultsSortByProperty, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"filtered", ResultsFiltered, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL}, }; diff --git a/tests/lib/query-tests.js b/tests/lib/query-tests.js index 7ad8d6a5..2dd9fd60 100644 --- a/tests/lib/query-tests.js +++ b/tests/lib/query-tests.js @@ -46,10 +46,9 @@ function runQuerySuite(suite) { } }); - var args; function getArgs(startArg) { - args = test.slice(startArg, startArg + 2); - for (var i = startArg + 2; i < test.length; i++) { + var args = [test[startArg]]; + for (var i = startArg + 1; i < test.length; i++) { var arg = test[i]; if (Array.isArray(arg)) { // aray arguments correspond to [objectAtIndex, propertyName] @@ -65,14 +64,20 @@ function runQuerySuite(suite) { for (var index in suite.tests) { var test = suite.tests[index]; if (test[0] == "QueryCount") { - var length = realm.objects.apply(realm, getArgs(2)).length; - TestCase.assertEqual(test[1], length, "Query '" + args[1] + "' on type '" + args[0] + "' expected " + test[1] + " results, got " + length); + var type = test[2]; + var args = getArgs(3); + var objects = realm.objects(type); + var length = objects.filtered.apply(objects, args).length; + TestCase.assertEqual(test[1], length, "Query '" + args[0] + "' on type '" + type + "' expected " + test[1] + " results, got " + length); } else if (test[0] == "ObjectSet") { - var results = realm.objects.apply(realm, getArgs(2)); - TestCase.assertEqual(test[1].length, results.length, "Query '" + args[1] + "' on type '" + args[0] + "' expected " + test[1].length + " results, got " + results.length); + var type = test[2]; + var args = getArgs(3); + var objects = realm.objects(type); + var results = objects.filtered.apply(objects, args); + TestCase.assertEqual(test[1].length, results.length, "Query '" + args[0] + "' on type '" + type+ "' expected " + test[1].length + " results, got " + results.length); - var objSchema = suite.schema.find(function(el) { return el.name == args[0] }); + var objSchema = suite.schema.find(function(el) { return el.name == type }); var primary = objSchema.primaryKey; if (!primary) { throw "Primary key required for object comparison"; @@ -84,7 +89,8 @@ function runQuerySuite(suite) { } else if (test[0] == "QueryThrows") { TestCase.assertThrows(function() { - realm.objects.apply(realm, getArgs(1)); + var args = getArgs(2); + realm.objects.apply(realm, args); }, "Expected exception not thrown for query: " + JSON.stringify(args)); } else if (test[0] != "Disabled") { diff --git a/tests/lib/realm-tests.js b/tests/lib/realm-tests.js index 42649448..c19736e2 100644 --- a/tests/lib/realm-tests.js +++ b/tests/lib/realm-tests.js @@ -290,7 +290,7 @@ module.exports = BaseTest.extend({ TestCase.assertEqual(objects[0].doubleCol, 7, "wrong property value"); TestCase.assertEqual(objects[1].doubleCol, 8, "wrong property value"); - var threeObjects = realm.objects('TestObject', "doubleCol < 5"); + var threeObjects = realm.objects('TestObject').filtered("doubleCol < 5"); TestCase.assertEqual(threeObjects.length, 3, "wrong results count"); realm.delete(threeObjects); TestCase.assertEqual(objects.length, 4, 'wrong object count'); @@ -341,40 +341,8 @@ module.exports = BaseTest.extend({ realm.objects('InvalidClass'); }); TestCase.assertThrows(function() { - realm.objects('PersonObject', 'invalid query'); + realm.objects('PersonObject', 'truepredicate'); }); - TestCase.assertThrows(function() { - realm.objects('PersonObject', []); - }); - - TestCase.assertEqual(realm.objects('PersonObject', "truepredicate").length, 4); - TestCase.assertEqual(realm.objects('PersonObject').length, 4); - TestCase.assertEqual(realm.objects('PersonObject', 'age = 11').length, 1); - TestCase.assertEqual(realm.objects('PersonObject', 'age = 11')[0].name, 'Tim'); - TestCase.assertEqual(realm.objects('PersonObject', 'age = 12').length, 2); - TestCase.assertEqual(realm.objects('PersonObject', 'age = 13').length, 0); - TestCase.assertEqual(realm.objects('PersonObject', 'age < 12').length, 2); - TestCase.assertEqual(realm.objects('PersonObject', 'age > 10 && age < 13').length, 3); - TestCase.assertEqual(realm.objects('PersonObject', 'age >= 11 && age < 13').length, 3); - TestCase.assertEqual(realm.objects('PersonObject', 'name = "Tim"').length, 1); - TestCase.assertEqual(realm.objects('PersonObject', 'name = \'Tim\'').length, 1); - TestCase.assertEqual(realm.objects('PersonObject', 'married == TRUE').length, 1); - TestCase.assertEqual(realm.objects('PersonObject', 'married == false').length, 3); - - TestCase.assertEqual(realm.objects('PersonObject', 'name = $0', 'Tim').length, 1); - TestCase.assertEqual(realm.objects('PersonObject', 'age > $1 && age < $0', 13, 10).length, 3); - TestCase.assertThrows(function() { - realm.objects('PersonObject', 'age > $2 && age < $0', 13, 10) - }); - - realm.write(function() { - realm.create('DefaultValuesObject', {'dateCol': new Date(3)}); - realm.create('DefaultValuesObject', {'dateCol': new Date(4)}); - realm.create('DefaultValuesObject', {'dateCol': new Date(5)}); - }); - - TestCase.assertEqual(realm.objects('DefaultValuesObject', 'dateCol > $0', new Date(4)).length, 1); - TestCase.assertEqual(realm.objects('DefaultValuesObject', 'dateCol <= $0', new Date(4)).length, 2); }, testNotifications: function() { diff --git a/tests/lib/results-tests.js b/tests/lib/results-tests.js index 26d088a5..9e0cc86a 100644 --- a/tests/lib/results-tests.js +++ b/tests/lib/results-tests.js @@ -91,6 +91,44 @@ module.exports = BaseTest.extend({ TestCase.assertEqual(count, 1); TestCase.assertEqual(keys.length, 1); }, + testResultsFiltered: function() { + var realm = new Realm({schema: [schemas.PersonObject, schemas.DefaultValues, schemas.TestObject]}); + realm.write(function() { + realm.create('PersonObject', {name: 'Ari', age: 10}); + realm.create('PersonObject', {name: 'Tim', age: 11}); + realm.create('PersonObject', {name: 'Bjarne', age: 12}); + realm.create('PersonObject', {name: 'Alex', age: 12, married: true}); + }); + + TestCase.assertEqual(realm.objects('PersonObject').filtered("truepredicate").length, 4); + TestCase.assertEqual(realm.objects('PersonObject').length, 4); + TestCase.assertEqual(realm.objects('PersonObject').filtered('age = 11').length, 1); + TestCase.assertEqual(realm.objects('PersonObject').filtered('age = 11')[0].name, 'Tim'); + TestCase.assertEqual(realm.objects('PersonObject').filtered('age = 12').length, 2); + TestCase.assertEqual(realm.objects('PersonObject').filtered('age = 13').length, 0); + TestCase.assertEqual(realm.objects('PersonObject').filtered('age < 12').length, 2); + TestCase.assertEqual(realm.objects('PersonObject').filtered('age > 10 && age < 13').length, 3); + TestCase.assertEqual(realm.objects('PersonObject').filtered('age >= 11 && age < 13').length, 3); + TestCase.assertEqual(realm.objects('PersonObject').filtered('name = "Tim"').length, 1); + TestCase.assertEqual(realm.objects('PersonObject').filtered('name = \'Tim\'').length, 1); + TestCase.assertEqual(realm.objects('PersonObject').filtered('married == TRUE').length, 1); + TestCase.assertEqual(realm.objects('PersonObject').filtered('married == false').length, 3); + + TestCase.assertEqual(realm.objects('PersonObject').filtered('name = $0', 'Tim').length, 1); + TestCase.assertEqual(realm.objects('PersonObject').filtered('age > $1 && age < $0', 13, 10).length, 3); + TestCase.assertThrows(function() { + realm.objects('PersonObject').filtered('age > $2 && age < $0', 13, 10) + }); + + realm.write(function() { + realm.create('DefaultValuesObject', {'dateCol': new Date(3)}); + realm.create('DefaultValuesObject', {'dateCol': new Date(4)}); + realm.create('DefaultValuesObject', {'dateCol': new Date(5)}); + }); + + TestCase.assertEqual(realm.objects('DefaultValuesObject').filtered('dateCol > $0', new Date(4)).length, 1); + TestCase.assertEqual(realm.objects('DefaultValuesObject').filtered('dateCol <= $0', new Date(4)).length, 2); + }, testSort: function() { var realm = new Realm({schema: [schemas.TestObject]}); var objects = realm.objects('TestObject');