1616#define FIREBASE_DATABASE_CLIENT_CPP_SRC_COMMON_QUERY_SPEC_H_
1717
1818#include " app/src/include/firebase/variant.h"
19+ #include " app/src/optional.h"
1920#include " app/src/path.h"
2021
2122namespace firebase {
@@ -43,6 +44,18 @@ struct QueryParams {
4344 limit_first == other.limit_first && limit_last == other.limit_last ;
4445 }
4546
47+ template <typename T>
48+ int OptionalCmp (const Optional<T>& a, const Optional<T>& b) const {
49+ if (a.has_value () && b.has_value ()) {
50+ if (*a < *b) return -1 ;
51+ if (*a > *b) return 1 ;
52+ return 0 ;
53+ }
54+ if (!a.has_value () && b.has_value ()) return -1 ;
55+ if (a.has_value () && !b.has_value ()) return 1 ;
56+ return 0 ;
57+ }
58+
4659 // Less-than operator, required so we can place QuerySpec instances in an
4760 // std::map. This is an arbitrary operation, but must be consistent.
4861 bool operator <(const QueryParams& other) const {
@@ -52,18 +65,32 @@ struct QueryParams {
5265 if (order_by_child < other.order_by_child ) return true ;
5366 if (order_by_child > other.order_by_child ) return false ;
5467 }
55- if (start_at_value < other.start_at_value ) return true ;
56- if (start_at_value > other.start_at_value ) return false ;
57- if (start_at_child_key < other.start_at_child_key ) return true ;
58- if (start_at_child_key > other.start_at_child_key ) return false ;
59- if (end_at_value < other.end_at_value ) return true ;
60- if (end_at_value > other.end_at_value ) return false ;
61- if (end_at_child_key < other.end_at_child_key ) return true ;
62- if (end_at_child_key > other.end_at_child_key ) return false ;
63- if (equal_to_value < other.equal_to_value ) return true ;
64- if (equal_to_value > other.equal_to_value ) return false ;
65- if (equal_to_child_key < other.equal_to_child_key ) return true ;
66- if (equal_to_child_key > other.equal_to_child_key ) return false ;
68+ // clang-format off
69+ switch (OptionalCmp (start_at_value, other.start_at_value )) {
70+ case -1 : return true ;
71+ case 1 : return false ;
72+ }
73+ switch (OptionalCmp (start_at_child_key, other.start_at_child_key )) {
74+ case -1 : return true ;
75+ case 1 : return false ;
76+ }
77+ switch (OptionalCmp (end_at_value, other.end_at_value )) {
78+ case -1 : return true ;
79+ case 1 : return false ;
80+ }
81+ switch (OptionalCmp (end_at_child_key, other.end_at_child_key )) {
82+ case -1 : return true ;
83+ case 1 : return false ;
84+ }
85+ switch (OptionalCmp (equal_to_value, other.equal_to_value )) {
86+ case -1 : return true ;
87+ case 1 : return false ;
88+ }
89+ switch (OptionalCmp (equal_to_child_key, other.equal_to_child_key )) {
90+ case -1 : return true ;
91+ case 1 : return false ;
92+ }
93+ // clang-format on
6794 if (limit_first < other.limit_first ) return true ;
6895 if (limit_first > other.limit_first ) return false ;
6996 if (limit_last < other.limit_last ) return true ;
@@ -82,19 +109,19 @@ struct QueryParams {
82109 std::string order_by_child;
83110
84111 // Set by Query::StartAt(). Variant::Null() if unspecified.
85- Variant start_at_value;
112+ Optional< Variant> start_at_value;
86113 // Set by Query::StartAt() with child specified. Blank if unspecified.
87- std::string start_at_child_key;
114+ Optional< std::string> start_at_child_key;
88115
89116 // Set by Query::EndAt(). Variant::Null() if unspecified.
90- Variant end_at_value;
117+ Optional< Variant> end_at_value;
91118 // Set by Query::EndAt() with child specified. Blank if unspecified.
92- std::string end_at_child_key;
119+ Optional< std::string> end_at_child_key;
93120
94121 // Set by Query::EqualTo(). Variant::Null() if unspecified.
95- Variant equal_to_value;
122+ Optional< Variant> equal_to_value;
96123 // Set by Query::EqualTo() with child specified. Blank if unspecified.
97- std::string equal_to_child_key;
124+ Optional< std::string> equal_to_child_key;
98125
99126 // Set by Query::LimitToFirst(). 0 means no limit.
100127 size_t limit_first;
0 commit comments