forked from couchbase/couchbase-lite-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBLQuery.mm
294 lines (249 loc) · 8.47 KB
/
CBLQuery.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
// CBLQuery.mm
// CouchbaseLite
//
// Copyright (c) 2017 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "CBLQuery.h"
#import "CBLCoreBridge.h"
#import "CBLDatabase+Internal.h"
#import "CBLLiveQuery.h"
#import "CBLPropertyExpression.h"
#import "CBLQuery+Internal.h"
#import "CBLQuery+JSON.h"
#import "CBLQueryExpression+Internal.h"
#import "CBLQueryResultSet+Internal.h"
#import "CBLStatus.h"
#import "c4Query.h"
#import "fleece/slice.hh"
using namespace fleece;
@implementation CBLQuery
{
NSData* _json;
C4Query* _c4Query;
NSDictionary* _columnNames;
CBLLiveQuery* _liveQuery;
CBLQueryDataSource* _from;
}
@synthesize database=_database;
@synthesize JSONRepresentation=_json;
@synthesize parameters=_parameters;
- (instancetype) initWithDatabase: (CBLDatabase*)database
JSONRepresentation: (NSData*)json
{
Assert(database);
Assert(json);
self = [super init];
if (self) {
_database = database;
_json = json;
}
return self;
}
- (instancetype) initWithSelect: (NSArray<CBLQuerySelectResult*>*)select
distinct: (BOOL)distinct
from: (CBLQueryDataSource*)from
join: (nullable NSArray<CBLQueryJoin*>*)_join
where: (nullable CBLQueryExpression*)where
groupBy: (nullable NSArray<CBLQueryExpression*>*)groupBy
having: (nullable CBLQueryExpression*)having
orderBy: (nullable NSArray<CBLQueryOrdering*>*)orderings
limit: (nullable CBLQueryLimit*)limit;
{
// Encode the query to JSON:
NSData* json;
@autoreleasepool {
NSMutableDictionary *root = [NSMutableDictionary dictionary];
// DISTINCT:
if (distinct)
root[@"DISTINCT"] = @(YES);
// JOIN / FROM:
_from = from;
NSMutableArray* fromArray;
NSDictionary* as = [from asJSON];
if (as.count > 0) {
if (!fromArray)
fromArray = [NSMutableArray array];
[fromArray addObject: as];
} if (_join) {
if (!fromArray)
fromArray = [NSMutableArray array];
for (CBLQueryJoin* join in _join) {
[fromArray addObject: [join asJSON]];
}
}
if (fromArray.count > 0)
root[@"FROM"] = fromArray;
// SELECT:
NSMutableArray* selects = [NSMutableArray array];
for (CBLQuerySelectResult* selected in select) {
[selects addObject: [selected asJSON]];
}
if (selects.count == 0) // Empty selects means SELECT *
[selects addObject: [[CBLQuerySelectResult allFrom: as[@"AS"]] asJSON]];
root[@"WHAT"] = selects;
// WHERE:
if (where)
root[@"WHERE"] = [where asJSON];
// GROUPBY:
if (groupBy) {
NSMutableArray* groupByArray = [NSMutableArray array];
for (CBLQueryExpression* expr in groupBy) {
[groupByArray addObject: [expr asJSON]];
}
root[@"GROUP_BY"] = groupByArray;
}
// HAVING:
if (having)
root[@"HAVING"] = [having asJSON];
// ORDERBY:
if (orderings) {
NSMutableArray* orderBy = [NSMutableArray array];
for (CBLQueryOrdering* o in orderings) {
[orderBy addObject: [o asJSON]];
}
root[@"ORDER_BY"] = orderBy;
}
// LIMIT/OFFSET:
if (limit) {
NSArray* limitObj = [limit asJSON];
root[@"LIMIT"] = limitObj[0];
if (limitObj.count > 1)
root[@"OFFSET"] = limitObj[1];
}
NSError* error;
json = [NSJSONSerialization dataWithJSONObject: root options: 0 error: &error];
Assert(json, @"Failed to encode query as JSON: %@", error);
}
return [self initWithDatabase: (CBLDatabase*)from.source JSONRepresentation: json];
}
- (void) dealloc {
[_liveQuery stop];
CBL_LOCK(self.database) {
c4query_free(_c4Query);
}
}
- (NSString*) description {
NSString* desc = [[NSString alloc] initWithData: _json encoding: NSUTF8StringEncoding];
return [NSString stringWithFormat: @"%@[json=%@]", self.class, desc];
}
#pragma mark - Parameters
- (CBLQueryParameters*) parameters {
CBL_LOCK(self) {
return _parameters;
}
}
- (void) setParameters: (CBLQueryParameters*)parameters {
CBL_LOCK(self) {
if (parameters)
_parameters = [[CBLQueryParameters alloc] initWithParameters: parameters readonly: YES];
else
_parameters = nil;
[_liveQuery queryParametersChanged];
}
}
- (NSString*) explain: (NSError**)outError {
if (![self check: outError])
return nil;
CBL_LOCK(self.database) {
return sliceResult2string(c4query_explain(_c4Query));
}
}
- (nullable CBLQueryResultSet*) execute: (NSError**)outError {
if (![self check: outError])
return nil;
C4QueryOptions options = kC4DefaultQueryOptions;
NSData* params = nil;
CBL_LOCK(self) {
params = [_parameters encode: outError];
if (_parameters && !params)
return nil;
}
C4Error c4Err;
C4QueryEnumerator* e;
CBL_LOCK(self.database) {
e = c4query_run(_c4Query, &options, {params.bytes, params.length}, &c4Err);
}
if (!e) {
CBLWarnError(Query, @"CBLQuery failed: %d/%d", c4Err.domain, c4Err.code);
convertError(c4Err, outError);
return nullptr;
}
return [[CBLQueryResultSet alloc] initWithQuery: self
c4Query: _c4Query
enumerator: e
columnNames: _columnNames];
}
- (id<CBLListenerToken>) addChangeListener: (void (^)(CBLQueryChange*))listener {
return [self addChangeListenerWithQueue: nil listener: listener];
}
- (id<CBLListenerToken>) addChangeListenerWithQueue: (nullable dispatch_queue_t)queue
listener: (void (^)(CBLQueryChange*))listener
{
CBLAssertNotNil(listener);
CBL_LOCK(self) {
if (!_liveQuery)
_liveQuery = [[CBLLiveQuery alloc] initWithQuery: self];
return [_liveQuery addChangeListenerWithQueue: queue listener: listener]; // Auto-start
}
}
- (void) removeChangeListenerWithToken: (id<CBLListenerToken>)token {
CBLAssertNotNil(token);
CBL_LOCK(self) {
[_liveQuery removeChangeListenerWithToken: token];
}
}
#pragma mark - Internal
- (instancetype) copyWithZone: (NSZone*)zone {
CBL_LOCK(self) {
CBLQuery* q = [[[self class] alloc] initWithDatabase: _database JSONRepresentation: _json];
q.parameters = _parameters;
return q;
}
}
#pragma mark - Private
- (BOOL) check: (NSError**)outError {
CBL_LOCK(self) {
if (_c4Query)
return YES;
[self.database mustBeOpen];
// Compile JSON query:
C4Error c4Err;
C4Query* query;
CBL_LOCK(self.database) {
query = c4query_new(self.database.c4db, {_json.bytes, _json.length}, &c4Err);
}
if (!query) {
convertError(c4Err, outError);
return NO;
}
Assert(!_c4Query);
_c4Query = query;
// Generate column name dictionary:
NSMutableDictionary* cols = [NSMutableDictionary dictionary];
unsigned n = c4query_columnCount(_c4Query);
for (unsigned i = 0; i < n; ++i) {
slice title = c4query_columnTitle(_c4Query, i);
NSString* titleString = slice2string(title);
if ([titleString hasPrefix: @"*"]) {
titleString = _from.columnName;
}
cols[titleString] = @(i);
}
_columnNames = [cols copy];
return YES;
}
}
@end