1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2015 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #ifndef REALM_QUERY_BUILDER_HPP
20 #define REALM_QUERY_BUILDER_HPP
26 #include <realm/binary_data.hpp>
27 #include <realm/null.hpp>
28 #include <realm/string_data.hpp>
29 #include <realm/timestamp.hpp>
30 #include <realm/table.hpp>
31 #include <realm/util/any.hpp>
32 #include <realm/util/string_buffer.hpp>
38 template<typename> class BasicRowExpr;
39 using RowExpr = BasicRowExpr<Table>;
45 namespace query_builder {
48 void apply_predicate(Query& query, const parser::Predicate& predicate, Arguments& arguments);
50 void apply_predicate(Query& query, const parser::Predicate& predicate); // zero out of string args version
56 T unbox(const util::Any& wrapper) {
57 return util::any_cast<T>(wrapper);
59 bool is_null(const util::Any& wrapper) {
60 if (!wrapper.has_value()) {
63 if (wrapper.type() == typeid(realm::null)) {
72 virtual bool bool_for_argument(size_t argument_index) = 0;
73 virtual long long long_for_argument(size_t argument_index) = 0;
74 virtual float float_for_argument(size_t argument_index) = 0;
75 virtual double double_for_argument(size_t argument_index) = 0;
76 virtual StringData string_for_argument(size_t argument_index) = 0;
77 virtual BinaryData binary_for_argument(size_t argument_index) = 0;
78 virtual Timestamp timestamp_for_argument(size_t argument_index) = 0;
79 virtual size_t object_index_for_argument(size_t argument_index) = 0;
80 virtual bool is_argument_null(size_t argument_index) = 0;
81 util::StringBuffer buffer_space; // dynamic conversion space with lifetime tied to this
84 template<typename ValueType, typename ContextType>
85 class ArgumentConverter : public Arguments {
87 ArgumentConverter(ContextType& context, const ValueType* arguments, size_t count)
89 , m_arguments(arguments)
93 bool bool_for_argument(size_t i) override { return get<bool>(i); }
94 long long long_for_argument(size_t i) override { return get<int64_t>(i); }
95 float float_for_argument(size_t i) override { return get<float>(i); }
96 double double_for_argument(size_t i) override { return get<double>(i); }
97 StringData string_for_argument(size_t i) override { return get<StringData>(i); }
98 BinaryData binary_for_argument(size_t i) override { return get<BinaryData>(i); }
99 Timestamp timestamp_for_argument(size_t i) override { return get<Timestamp>(i); }
100 size_t object_index_for_argument(size_t i) override { return get<RowExpr>(i).get_index(); }
101 bool is_argument_null(size_t i) override { return m_ctx.is_null(at(i)); }
105 const ValueType* m_arguments;
108 const ValueType& at(size_t index) const
110 if (index >= m_count)
111 throw std::out_of_range("vector");
112 return m_arguments[index];
116 T get(size_t index) const
118 return m_ctx.template unbox<T>(at(index));
121 } // namespace query_builder
124 #endif // REALM_QUERY_BUILDER_HPP