@@ -4,6 +4,7 @@ part of flutter_parse_sdk;
4
4
class QueryBuilder <T extends ParseObject > {
5
5
6
6
static const String _NO_OPERATOR_NEEDED = "NO_OP" ;
7
+ static const String _SINGLE_QUERY = "SINGLE_QUERY" ;
7
8
8
9
T object;
9
10
var queries = List <MapEntry >();
@@ -13,137 +14,151 @@ class QueryBuilder<T extends ParseObject> {
13
14
QueryBuilder (this .object) : super ();
14
15
15
16
/// Adds a limit to amount of results return from Parse
16
- void limit (int limit){
17
+ void setLimit (int limit){
17
18
limiters["limit" ] = limit;
18
19
}
19
20
20
21
/// Useful for pagination, skips [int] amount of results
21
- void skip (int skip){
22
+ void setAmountToSkip (int skip){
22
23
limiters["skip" ] = skip;
23
24
}
24
25
25
26
/// Creates a query based on where
26
- void where (String where){
27
+ void whereEquals (String where){
27
28
limiters['where' ] = where;
28
29
}
29
30
30
31
/// Orders the results ascedingly.
31
32
///
32
33
/// [String] order will be the column of the table that the results are
33
34
/// ordered by
34
- void ascending (String order){
35
+ void orderByAscending (String order){
35
36
limiters["order" ] = order;
36
37
}
37
38
38
39
/// Orders the results descendingly.
39
40
///
40
41
/// [String] order will be the column of the table that the results are
41
42
/// ordered by
42
- void descending (String order){
43
+ void orderByDescending (String order){
43
44
limiters["order" ] = "-$order " ;
44
45
}
45
46
46
47
/// Define which keys in an object to return.
47
48
///
48
49
/// [String] keys will only return the columns of a result you want the data for,
49
50
/// this is useful for large objects
50
- void keys ( String keys){
51
- limiters["keys" ] = keys;
51
+ void keysToReturn ( List < String > keys){
52
+ limiters["keys" ] = keys. toString () ;
52
53
}
53
54
54
55
/// Includes other ParseObjects stored as a Pointer
55
- void include (String include ){
56
- limiters["include" ] = include ;
56
+ void includeObject (String objectType ){
57
+ limiters["include" ] = objectType ;
57
58
}
58
59
59
60
/// Returns an object where the [String] column starts with [value]
60
- void startsWith (String column, dynamic value) {
61
- queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, "^$value " ), "\$ regex" ));
61
+ void whereStartsWith (String column, String query, {bool caseSensitive: false }) {
62
+ if (caseSensitive) {
63
+ queries.add (MapEntry (_SINGLE_QUERY , '\" $column \" :{\"\$ regex\" : \" ^$query \" }' ));
64
+ } else {
65
+ queries.add (MapEntry (_SINGLE_QUERY , '\" $column \" :{\"\$ regex\" : \" ^$query \" , \"\$ options\" : \" i\" }' ));
66
+ }
62
67
}
63
68
64
69
/// Returns an object where the [String] column ends with [value]
65
- void endsWith (String column, dynamic value) {
66
- queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, "$value ^" ), "\$ regex" ));
70
+ void whereEndsWith (String column, String query, {bool caseSensitive: false }) {
71
+ if (caseSensitive) {
72
+ queries.add (MapEntry (_SINGLE_QUERY , '\" $column \" :{\"\$ regex\" : \" $query ^\" }' ));
73
+ } else {
74
+ queries.add (MapEntry (_SINGLE_QUERY , '\" $column \" :{\"\$ regex\" : \" $query ^\" , \"\$ options\" : \" i\" }' ));
75
+ }
67
76
}
68
77
69
78
/// Returns an object where the [String] column equals [value]
70
- void equals (String column, dynamic value) {
79
+ void whereEqualTo (String column, dynamic value) {
71
80
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), _NO_OPERATOR_NEEDED ));
72
81
}
73
82
74
83
/// Returns an object where the [String] column contains a value less than
75
84
/// value
76
- void lessThan (String column, dynamic value) {
85
+ void whereLessThan (String column, dynamic value) {
77
86
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ lt" ));
78
87
}
79
88
80
89
/// Returns an object where the [String] column contains a value less or equal
81
90
/// to than value
82
- void lessThanOrEqualTo (String column, dynamic value) {
91
+ void whereLessThanOrEqualTo (String column, dynamic value) {
83
92
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ lte" ));
84
93
}
85
94
86
95
/// Returns an object where the [String] column contains a value greater
87
96
/// than value
88
- void greaterThan (String column, dynamic value) {
97
+ void whereGreaterThan (String column, dynamic value) {
89
98
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ gt" ));
90
99
}
91
100
92
101
/// Returns an object where the [String] column contains a value greater
93
102
/// than equal to value
94
- void greaterThanOrEqualsTo (String column, dynamic value) {
103
+ void whereGreaterThanOrEqualsTo (String column, dynamic value) {
95
104
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ gte" ));
96
105
}
97
106
98
107
/// Returns an object where the [String] column is not equal to value
99
- void notEqualTo (String column, dynamic value) {
108
+ void whereNotEqualTo (String column, dynamic value) {
100
109
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ ne" ));
101
110
}
102
111
103
- /// Returns an object where the [String] column contains value
104
- void contains (String column, dynamic value) {
105
- queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ term" ));
106
- }
107
-
108
112
/// Returns an object where the [String] column is containedIn
109
- void containedIn (String column, dynamic value) {
113
+ void whereContainedIn (String column, List value) {
110
114
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ in" ));
111
115
}
112
116
113
117
/// Returns an object where the [String] column is notContainedIn
114
- void notContainedIn (String column, dynamic value) {
118
+ void whereNotContainedIn (String column, List value) {
115
119
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ nin" ));
116
120
}
117
121
118
- /// Returns an object where the [String] column is exists
119
- void exists (String column, dynamic value) {
122
+ /// Returns an object where the [String] column for the object has data correctley entered/saved
123
+ void whereValueExists (String column, bool value) {
120
124
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ exists" ));
121
125
}
122
126
123
127
/// Returns an object where the [String] column contains select
124
- void select (String column, dynamic value) {
128
+ void selectKeys (String column, dynamic value) {
125
129
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ select" ));
126
130
}
127
131
128
132
/// Returns an object where the [String] column doesn't select
129
- void dontSelect (String column, dynamic value) {
133
+ void dontSelectKeys (String column, dynamic value) {
130
134
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ dontSelect" ));
131
135
}
132
136
133
137
/// Returns an object where the [String] column contains all
134
- void all (String column, dynamic value) {
135
- queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ all" ));
138
+ void whereArrayContainsAll (String column, List value) {
139
+ queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value. toString () ), "\$ all" ));
136
140
}
137
141
138
142
/// Returns an object where the [String] column has a regEx performed on,
139
- /// this can include ^StringsWith, or ^EndsWith
140
- void regEx (String column, dynamic value) {
143
+ /// this can include ^StringsWith, or ^EndsWith. This can be manipulated to the users desire
144
+ void regEx (String column, String value) {
141
145
queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ regex" ));
142
146
}
143
147
144
- /// Returns an object where the [String] column contains the text
145
- void text (String column, dynamic value) {
146
- queries.add (_buildQueryWithColumnValueAndOperator (MapEntry (column, value), "\$ text" ));
148
+ /// Performs a search to see if [String] contains other string
149
+ void whereContains (String column, String value, {bool caseSensitive: false }) {
150
+ if (caseSensitive) {
151
+ queries.add (MapEntry (_SINGLE_QUERY , '\" $column \" :{\"\$ regex\" : \" $query \" }' ));
152
+ } else {
153
+ queries.add (MapEntry (_SINGLE_QUERY , '\" $column \" :{\"\$ regex\" : \" $query \" , \"\$ options\" : \" i\" }' ));
154
+ }
155
+ }
156
+
157
+ /// Powerful search for containing whole words. This search is much quicker than regex and can search for whole words including wether they are case sensitive or not.
158
+ /// This search can also order by the score of the search
159
+ void textContainsWholeWord (String column, String query, {bool caseSensitive: false , bool orderByScore: true }){
160
+ queries.add (MapEntry (_SINGLE_QUERY , '\" $column \" :{\"\$ text\" :{\"\$ search\" :{\"\$ term\" : \" $query \" , \"\$ caseSensitive\" : $caseSensitive }}}' ));
161
+ if (orderByScore) orderByDescending ('score' );
147
162
}
148
163
149
164
/// Finishes the query and calls the server
@@ -208,12 +223,12 @@ class QueryBuilder<T extends ParseObject> {
208
223
for (var query in queries){
209
224
210
225
// Add queries that don't need sanitising
211
- if (query.key == _NO_OPERATOR_NEEDED ) {
226
+ if (query.key == _NO_OPERATOR_NEEDED || query.key == _SINGLE_QUERY ) {
212
227
sanitisedQueries.add (MapEntry (_NO_OPERATOR_NEEDED , query.value));
213
228
}
214
229
215
230
// Check if query with same column name has been sanitised
216
- if (! keysAlreadyCompacted.contains (query.key) && query.key != _NO_OPERATOR_NEEDED ) {
231
+ if (! keysAlreadyCompacted.contains (query.key) && query.key != _NO_OPERATOR_NEEDED && query.key != _SINGLE_QUERY ) {
217
232
218
233
// If not, check that it now has
219
234
keysAlreadyCompacted.add (query.key);
0 commit comments