This commit is contained in:
Ari Lazier 2016-04-11 16:01:51 -07:00
parent 3a174161d7
commit b542fe8c7b
3 changed files with 72 additions and 20 deletions

View File

@ -426,37 +426,50 @@ void do_add_comparison_to_query(Query &query, const Schema &schema, const Object
template<typename T> template<typename T>
void do_add_null_comparison_to_query(Query &query, Predicate::Operator op, const PropertyExpression &expr, Arguments &args) void do_add_null_comparison_to_query(Query &query, Predicate::Operator op, const PropertyExpression &expr, Arguments &args)
{ {
static realm::null null = realm::null();
Columns<T> column = expr.table_getter()->template column<T>(expr.prop->table_column); Columns<T> column = expr.table_getter()->template column<T>(expr.prop->table_column);
switch (op) { switch (op) {
case Predicate::Operator::NotEqual: case Predicate::Operator::NotEqual:
query.and_query(column != null); query.and_query(column != realm::null());
case Predicate::Operator::Equal: case Predicate::Operator::Equal:
query.and_query(column == null); query.and_query(column == realm::null());
break; break;
default: default:
throw std::runtime_error("Only 'equal' and 'not equal' operators supported for object comparison."); throw std::runtime_error("Only 'equal' and 'not equal' operators supported when comapring against 'null'.");
} }
} }
template<> template<>
void do_add_null_comparison_to_query<Binary>(Query &query, Predicate::Operator op, const PropertyExpression &expr, Arguments &args) void do_add_null_comparison_to_query<Binary>(Query &query, Predicate::Operator op, const PropertyExpression &expr, Arguments &args)
{ {
static realm::null null = realm::null(); precondition(expr.indexes.empty(), "KeyPath queries not supported for data comparisons.");
Columns<Binary> column = expr.table_getter()->template column<Binary>(expr.prop->table_column); Columns<Binary> column = expr.table_getter()->template column<Binary>(expr.prop->table_column);
switch (op) { switch (op) {
case Predicate::Operator::NotEqual: case Predicate::Operator::NotEqual:
query.not_equal(expr.prop->table_column, null); query.not_equal(expr.prop->table_column, realm::null());
case Predicate::Operator::Equal: case Predicate::Operator::Equal:
query.equal(expr.prop->table_column, null); query.equal(expr.prop->table_column, realm::null());
break;
default:
throw std::runtime_error("Only 'equal' and 'not equal' operators supported when comapring against 'null'.");
}
}
template<>
void do_add_null_comparison_to_query<Link>(Query &query, Predicate::Operator op, const PropertyExpression &expr, Arguments &args)
{
precondition(expr.indexes.empty(), "KeyPath queries not supported for object comparisons.");
switch (op) {
case Predicate::Operator::NotEqual:
query.Not();
case Predicate::Operator::Equal:
query.and_query(query.get_table()->column<Link>(expr.prop->table_column).is_null());
break; break;
default: default:
throw std::runtime_error("Only 'equal' and 'not equal' operators supported for object comparison."); throw std::runtime_error("Only 'equal' and 'not equal' operators supported for object comparison.");
} }
} }
void do_add_null_comparison_to_query(Query &query, const Schema &schema, const ObjectSchema &object_schema, Predicate::Comparison cmp, void do_add_null_comparison_to_query(Query &query, const Schema &schema, const ObjectSchema &object_schema, Predicate::Comparison cmp,
const PropertyExpression &expr, Arguments &args) const PropertyExpression &expr, Arguments &args)
{ {
@ -481,20 +494,10 @@ void do_add_null_comparison_to_query(Query &query, const Schema &schema, const O
do_add_null_comparison_to_query<String>(query, cmp.op, expr, args); do_add_null_comparison_to_query<String>(query, cmp.op, expr, args);
break; break;
case PropertyTypeData: case PropertyTypeData:
precondition(expr.indexes.empty(), "KeyPath queries not supported for data comparisons.");
do_add_null_comparison_to_query<Binary>(query, cmp.op, expr, args); do_add_null_comparison_to_query<Binary>(query, cmp.op, expr, args);
break; break;
case PropertyTypeObject: case PropertyTypeObject:
precondition(expr.indexes.empty(), "KeyPath queries not supported for object comparisons."); do_add_null_comparison_to_query<Link>(query, cmp.op, expr, args);
switch (cmp.op) {
case Predicate::Operator::NotEqual:
query.Not();
case Predicate::Operator::Equal:
query.and_query(query.get_table()->column<Link>(expr.prop->table_column).is_null());
break;
default:
throw std::runtime_error("Only 'equal' and 'not equal' operators supported for object comparison.");
}
break; break;
case PropertyTypeArray: case PropertyTypeArray:
throw std::runtime_error((std::string)"Comparing Lists to 'null' is not supported"); throw std::runtime_error((std::string)"Comparing Lists to 'null' is not supported");

View File

@ -245,6 +245,9 @@
["QueryCount", 1, "LinkObject", "linkCol == $0", [0, "linkCol"]], ["QueryCount", 1, "LinkObject", "linkCol == $0", [0, "linkCol"]],
["QueryCount", 1, "LinkObject", "$0 == linkCol", [1, "linkCol"]], ["QueryCount", 1, "LinkObject", "$0 == linkCol", [1, "linkCol"]],
["QueryCount", 2, "LinkObject", "linkCol != $0", [0, "linkCol"]], ["QueryCount", 2, "LinkObject", "linkCol != $0", [0, "linkCol"]],
["QueryCount", 1, "LinkObject", "linkCol = null"],
["QueryCount", 2, "LinkObject", "linkCol != NULL"],
["QueryCount", 1, "LinkObject", "linkCol = $0", null],
["QueryThrows", "LinkObject", "linkCol > $0", [0, "linkCol"]], ["QueryThrows", "LinkObject", "linkCol > $0", [0, "linkCol"]],
["QueryThrows", "LinkObject", "intCol = $0", [0, "linkCol"]] ["QueryThrows", "LinkObject", "intCol = $0", [0, "linkCol"]]
@ -317,6 +320,49 @@
["ObjectSet", [1], "LinkTypesObject", "linkLink.basicLink.floatCol == 0.1"], ["ObjectSet", [1], "LinkTypesObject", "linkLink.basicLink.floatCol == 0.1"],
["ObjectSet", [1, 3], "LinkTypesObject", "linkLink.basicLink.floatCol > 0"] ["ObjectSet", [1, 3], "LinkTypesObject", "linkLink.basicLink.floatCol > 0"]
] ]
},
"optionalTests" : {
"schema" : [
{
"name": "OptionalTypesObject",
"primaryKey": "primaryKey",
"properties": [
{ "name": "primaryKey", "type": "int" },
{ "name": "intCol", "type": "int", "optional": true },
{ "name": "floatCol", "type": "float", "optional": true },
{ "name": "doubleCol", "type": "double", "optional": true },
{ "name": "stringCol", "type": "string", "optional": true },
{ "name": "dateCol", "type": "date", "optional": true },
{ "name": "dataCol", "type": "data", "optional": true }
]
},
{
"name": "LinkTypesObject",
"primaryKey": "primaryKey",
"properties": [
{ "name": "primaryKey", "type": "int" },
{ "name": "basicLink", "type": "object", "objectType": "OptionalTypesObject" }
]
}],
"objects": [
{ "type": "LinkTypesObject", "value": [0, [0, 1, 0.1, 0.001, "1", 1, [1, 10, 100]]] },
{ "type": "LinkTypesObject", "value": [1, [1, null, null, null, null, null, null]] }
],
"tests": [
["ObjectSet", [1], "OptionalTypesObject", "intCol == null"],
["ObjectSet", [0], "OptionalTypesObject", "intCol != null"],
["ObjectSet", [1], "OptionalTypesObject", "floatCol == null"],
["ObjectSet", [0], "OptionalTypesObject", "floatCol != null"],
["ObjectSet", [1], "OptionalTypesObject", "doubleCol == null"],
["ObjectSet", [0], "OptionalTypesObject", "doubleCol != null"],
["ObjectSet", [1], "OptionalTypesObject", "stringCol == null"],
["ObjectSet", [0], "OptionalTypesObject", "stringCol != null"],
["ObjectSet", [1], "OptionalTypesObject", "dateCol == null"],
["ObjectSet", [0], "OptionalTypesObject", "dateCol != null"],
["ObjectSet", [1], "OptionalTypesObject", "dataCol == null"],
["ObjectSet", [0], "OptionalTypesObject", "dataCol != null"]
]
} }
} }

View File

@ -146,6 +146,9 @@ module.exports = BaseTest.extend({
}, },
testKeyPathQueries: function() { testKeyPathQueries: function() {
runQuerySuite(testCases.keyPathTests); runQuerySuite(testCases.keyPathTests);
},
testOptionalQueries: function() {
runQuerySuite(testCases.optionalTests);
} }
}); });