diff --git a/src/parser/query_builder.cpp b/src/parser/query_builder.cpp index eea6b8d2..a0746f65 100644 --- a/src/parser/query_builder.cpp +++ b/src/parser/query_builder.cpp @@ -426,37 +426,50 @@ void do_add_comparison_to_query(Query &query, const Schema &schema, const Object template void do_add_null_comparison_to_query(Query &query, Predicate::Operator op, const PropertyExpression &expr, Arguments &args) { - static realm::null null = realm::null(); Columns column = expr.table_getter()->template column(expr.prop->table_column); - switch (op) { case Predicate::Operator::NotEqual: - query.and_query(column != null); + query.and_query(column != realm::null()); case Predicate::Operator::Equal: - query.and_query(column == null); + query.and_query(column == realm::null()); break; 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<> void do_add_null_comparison_to_query(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 column = expr.table_getter()->template column(expr.prop->table_column); - switch (op) { 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: - 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(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(expr.prop->table_column).is_null()); break; default: 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, 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(query, cmp.op, expr, args); break; case PropertyTypeData: - precondition(expr.indexes.empty(), "KeyPath queries not supported for data comparisons."); do_add_null_comparison_to_query(query, cmp.op, expr, args); break; case PropertyTypeObject: - precondition(expr.indexes.empty(), "KeyPath queries not supported for object comparisons."); - switch (cmp.op) { - case Predicate::Operator::NotEqual: - query.Not(); - case Predicate::Operator::Equal: - query.and_query(query.get_table()->column(expr.prop->table_column).is_null()); - break; - default: - throw std::runtime_error("Only 'equal' and 'not equal' operators supported for object comparison."); - } + do_add_null_comparison_to_query(query, cmp.op, expr, args); break; case PropertyTypeArray: throw std::runtime_error((std::string)"Comparing Lists to 'null' is not supported"); diff --git a/tests/query.json b/tests/query.json index e4edd4de..22ef1c72 100644 --- a/tests/query.json +++ b/tests/query.json @@ -245,6 +245,9 @@ ["QueryCount", 1, "LinkObject", "linkCol == $0", [0, "linkCol"]], ["QueryCount", 1, "LinkObject", "$0 == linkCol", [1, "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", "intCol = $0", [0, "linkCol"]] @@ -317,6 +320,49 @@ ["ObjectSet", [1], "LinkTypesObject", "linkLink.basicLink.floatCol == 0.1"], ["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"] + ] } }