mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-12 07:14:23 +00:00
add binary query support
This commit is contained in:
parent
07e818bdf2
commit
0e8ca5ed05
@ -102,24 +102,23 @@ void add_bool_constraint_to_query(Query &query, Predicate::Operator operatorType
|
|||||||
void add_string_constraint_to_query(Query &query,
|
void add_string_constraint_to_query(Query &query,
|
||||||
Predicate::Operator op,
|
Predicate::Operator op,
|
||||||
Columns<String> &&column,
|
Columns<String> &&column,
|
||||||
StringData value) {
|
std::string value) {
|
||||||
bool case_sensitive = true;
|
bool case_sensitive = true;
|
||||||
StringData sd = value;
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case Predicate::Operator::BeginsWith:
|
case Predicate::Operator::BeginsWith:
|
||||||
query.and_query(column.begins_with(sd, case_sensitive));
|
query.and_query(column.begins_with(value, case_sensitive));
|
||||||
break;
|
break;
|
||||||
case Predicate::Operator::EndsWith:
|
case Predicate::Operator::EndsWith:
|
||||||
query.and_query(column.ends_with(sd, case_sensitive));
|
query.and_query(column.ends_with(value, case_sensitive));
|
||||||
break;
|
break;
|
||||||
case Predicate::Operator::Contains:
|
case Predicate::Operator::Contains:
|
||||||
query.and_query(column.contains(sd, case_sensitive));
|
query.and_query(column.contains(value, case_sensitive));
|
||||||
break;
|
break;
|
||||||
case Predicate::Operator::Equal:
|
case Predicate::Operator::Equal:
|
||||||
query.and_query(column.equal(sd, case_sensitive));
|
query.and_query(column.equal(value, case_sensitive));
|
||||||
break;
|
break;
|
||||||
case Predicate::Operator::NotEqual:
|
case Predicate::Operator::NotEqual:
|
||||||
query.and_query(column.not_equal(sd, case_sensitive));
|
query.and_query(column.not_equal(value, case_sensitive));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw std::runtime_error("Unsupported operator for string queries.");
|
throw std::runtime_error("Unsupported operator for string queries.");
|
||||||
@ -128,16 +127,56 @@ void add_string_constraint_to_query(Query &query,
|
|||||||
|
|
||||||
void add_string_constraint_to_query(realm::Query &query,
|
void add_string_constraint_to_query(realm::Query &query,
|
||||||
Predicate::Operator op,
|
Predicate::Operator op,
|
||||||
StringData value,
|
std::string value,
|
||||||
Columns<String> &&column) {
|
Columns<String> &&column) {
|
||||||
bool case_sensitive = true;
|
bool case_sensitive = true;
|
||||||
StringData sd = value;
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case Predicate::Operator::Equal:
|
case Predicate::Operator::Equal:
|
||||||
query.and_query(column.equal(sd, case_sensitive));
|
query.and_query(column.equal(value, case_sensitive));
|
||||||
break;
|
break;
|
||||||
case Predicate::Operator::NotEqual:
|
case Predicate::Operator::NotEqual:
|
||||||
query.and_query(column.not_equal(sd, case_sensitive));
|
query.and_query(column.not_equal(value, case_sensitive));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw std::runtime_error("Substring comparison not supported for keypath substrings.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_binary_constraint_to_query(Query &query,
|
||||||
|
Predicate::Operator op,
|
||||||
|
Columns<Binary> &&column,
|
||||||
|
std::string value) {
|
||||||
|
switch (op) {
|
||||||
|
case Predicate::Operator::BeginsWith:
|
||||||
|
query.begins_with(column.m_column, value);
|
||||||
|
break;
|
||||||
|
case Predicate::Operator::EndsWith:
|
||||||
|
query.ends_with(column.m_column, value);
|
||||||
|
break;
|
||||||
|
case Predicate::Operator::Contains:
|
||||||
|
query.contains(column.m_column, value);
|
||||||
|
break;
|
||||||
|
case Predicate::Operator::Equal:
|
||||||
|
query.equal(column.m_column, value);
|
||||||
|
break;
|
||||||
|
case Predicate::Operator::NotEqual:
|
||||||
|
query.not_equal(column.m_column, value);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw std::runtime_error("Unsupported operator for binary queries.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_binary_constraint_to_query(realm::Query &query,
|
||||||
|
Predicate::Operator op,
|
||||||
|
std::string value,
|
||||||
|
Columns<Binary> &&column) {
|
||||||
|
switch (op) {
|
||||||
|
case Predicate::Operator::Equal:
|
||||||
|
query.equal(column.m_column, value);
|
||||||
|
break;
|
||||||
|
case Predicate::Operator::NotEqual:
|
||||||
|
query.not_equal(column.m_column, value);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw std::runtime_error("Substring comparison not supported for keypath substrings.");
|
throw std::runtime_error("Substring comparison not supported for keypath substrings.");
|
||||||
@ -274,6 +313,17 @@ struct ValueGetter<String, TableGetter> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename TableGetter>
|
||||||
|
struct ValueGetter<Binary, TableGetter> {
|
||||||
|
static std::string convert(TableGetter&&, const parser::Expression & value, Arguments &args)
|
||||||
|
{
|
||||||
|
if (value.type == parser::Expression::Type::Argument) {
|
||||||
|
return args.binary_for_argument(std::stoi(value.s));
|
||||||
|
}
|
||||||
|
throw std::runtime_error("Binary properties must be compared against a binary argument.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <typename RetType, typename Value, typename TableGetter>
|
template <typename RetType, typename Value, typename TableGetter>
|
||||||
auto value_of_type_for_query(TableGetter&& tables, Value&& value, Arguments &args)
|
auto value_of_type_for_query(TableGetter&& tables, Value&& value, Arguments &args)
|
||||||
{
|
{
|
||||||
@ -309,10 +359,13 @@ void do_add_comparison_to_query(Query &query, Schema &schema, ObjectSchema &obje
|
|||||||
value_of_type_for_query<Int>(expr.table_getter, rhs, args));
|
value_of_type_for_query<Int>(expr.table_getter, rhs, args));
|
||||||
break;
|
break;
|
||||||
case PropertyTypeString:
|
case PropertyTypeString:
|
||||||
case PropertyTypeData:
|
|
||||||
add_string_constraint_to_query(query, op, value_of_type_for_query<String>(expr.table_getter, lhs, args),
|
add_string_constraint_to_query(query, op, value_of_type_for_query<String>(expr.table_getter, lhs, args),
|
||||||
value_of_type_for_query<String>(expr.table_getter, rhs, args));
|
value_of_type_for_query<String>(expr.table_getter, rhs, args));
|
||||||
break;
|
break;
|
||||||
|
case PropertyTypeData:
|
||||||
|
add_binary_constraint_to_query(query, op, value_of_type_for_query<Binary>(expr.table_getter, lhs, args),
|
||||||
|
value_of_type_for_query<Binary>(expr.table_getter, rhs, args));
|
||||||
|
break;
|
||||||
default: {
|
default: {
|
||||||
throw std::runtime_error((std::string)"Object type " + string_for_property_type(type) + " not supported");
|
throw std::runtime_error((std::string)"Object type " + string_for_property_type(type) + " not supported");
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@ namespace realm {
|
|||||||
virtual float float_for_argument(size_t argument_index) = 0;
|
virtual float float_for_argument(size_t argument_index) = 0;
|
||||||
virtual double double_for_argument(size_t argument_index) = 0;
|
virtual double double_for_argument(size_t argument_index) = 0;
|
||||||
virtual std::string string_for_argument(size_t argument_index) = 0;
|
virtual std::string string_for_argument(size_t argument_index) = 0;
|
||||||
|
virtual std::string binary_for_argument(size_t argument_index) = 0;
|
||||||
virtual DateTime datetime_for_argument(size_t argument_index) = 0;
|
virtual DateTime datetime_for_argument(size_t argument_index) = 0;
|
||||||
virtual size_t object_index_for_argument(size_t argument_index) = 0;
|
virtual size_t object_index_for_argument(size_t argument_index) = 0;
|
||||||
virtual bool is_argument_null(size_t argument_index) = 0;
|
virtual bool is_argument_null(size_t argument_index) = 0;
|
||||||
@ -57,6 +58,7 @@ namespace realm {
|
|||||||
virtual float float_for_argument(size_t argument_index) { return Accessor::to_float(m_ctx, argument_at(argument_index)); }
|
virtual float float_for_argument(size_t argument_index) { return Accessor::to_float(m_ctx, argument_at(argument_index)); }
|
||||||
virtual double double_for_argument(size_t argument_index) { return Accessor::to_double(m_ctx, argument_at(argument_index)); }
|
virtual double double_for_argument(size_t argument_index) { return Accessor::to_double(m_ctx, argument_at(argument_index)); }
|
||||||
virtual std::string string_for_argument(size_t argument_index) { return Accessor::to_string(m_ctx, argument_at(argument_index)); }
|
virtual std::string string_for_argument(size_t argument_index) { return Accessor::to_string(m_ctx, argument_at(argument_index)); }
|
||||||
|
virtual std::string binary_for_argument(size_t argument_index) { return Accessor::to_binary(m_ctx, argument_at(argument_index)); }
|
||||||
virtual DateTime datetime_for_argument(size_t argument_index) { return Accessor::to_datetime(m_ctx, argument_at(argument_index)); }
|
virtual DateTime datetime_for_argument(size_t argument_index) { return Accessor::to_datetime(m_ctx, argument_at(argument_index)); }
|
||||||
virtual size_t object_index_for_argument(size_t argument_index) { return Accessor::to_existing_object_index(m_ctx, argument_at(argument_index)); }
|
virtual size_t object_index_for_argument(size_t argument_index) { return Accessor::to_existing_object_index(m_ctx, argument_at(argument_index)); }
|
||||||
virtual bool is_argument_null(size_t argument_index) { return Accessor::is_null(m_ctx, argument_at(argument_index)); }
|
virtual bool is_argument_null(size_t argument_index) { return Accessor::is_null(m_ctx, argument_at(argument_index)); }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user