-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNSFetchRequest+Explain.m
More file actions
569 lines (434 loc) · 16.3 KB
/
NSFetchRequest+Explain.m
File metadata and controls
569 lines (434 loc) · 16.3 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Copyright 2012 Florian Agsteiner
*/
#import "NSFetchRequest+Explain.h"
#import "NSObject+Explain.h"
#import <CoreData/NSManagedObjectID.h>
#import <Foundation/NSKeyValueCoding.h>
#import <Foundation/NSComparisonPredicate.h>
#import <Foundation/NSCompoundPredicate.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSExpression.h>
#import <Foundation/NSSet.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSException.h>
#import <objc/runtime.h>
/**
* Stores the result of a predicate
*/
@interface FAExplainResult : NSObject
/**
* Constructor
*/
+ (id) result;
/**
* Did the predicate match?
*/
@property(nonatomic,assign, getter = isMatch) BOOL match;
/**
* Aggegation: How many the predicates matched?
*/
@property(nonatomic,assign) BOOL numberOfMatches;
/**
* Description of the predicate?
*/
@property(nonatomic,copy) NSString* description;
/**
* Value the predicate was tested with or an error that accured
*/
@property(nonatomic,copy) NSString* value;
/**
* Create a deep copy of the object
*/
- (id)deepCopy;
/**
* modify the number of matches accordingly
*/
- (void) aggregateResult:(FAExplainResult*)result;
/**
* Print a description based on match, description and value
*/
- (NSString*)recursiveDescription;
/**
* Print a description based on the number of matches and description
*/
- (NSString*)recursiveAggregateDescription;
@end
/**
* Stores the result of a compound predicate
*/
@interface FAExplainCompountResult : FAExplainResult
/**
* The sub result for the subpredicates
*/
@property(nonatomic,copy) NSArray* subResults;
@end
@implementation FAExplainResult
+ (id) result{
return [[[[self class] alloc] init] autorelease];
}
/**
* Beautification for the match
*/
- (NSString*) matchString{
return self.match ? @"✔" : @"✘";
}
- (id)deepCopy{
FAExplainResult* result = [[self class] result];
result.match = self.match;
result.numberOfMatches = self.numberOfMatches;
result.description = self.description;
return result;
}
- (void) aggregateResult:(FAExplainResult*)result{
if (result.match) {
self.numberOfMatches ++;
}
}
- (NSString*)recursiveDescription{
NSString* description = [self recursiveTreeDescriptionWithRecursiveBlockReturningChildren:^(FAExplainResult* node, NSArray** children) {
NSString* description =[NSString stringWithFormat:@"%@ %@",[node matchString],[node description]];
if (node.value != nil) {
description = [description stringByAppendingFormat:@": %@",node.value];
}
if ([node isKindOfClass:[FAExplainCompountResult class]]) {
*children = [(FAExplainCompountResult*)node subResults];
}
return description;
}];
return description;
}
- (NSString*)recursiveAggregateDescription{
NSString* description = [self recursiveTreeDescriptionWithRecursiveBlockReturningChildren:^(FAExplainResult* node, NSArray** children) {
NSString* description =[NSString stringWithFormat:@"%d %@",[node numberOfMatches],[node description]];
if ([node isKindOfClass:[FAExplainCompountResult class]]) {
*children = [(FAExplainCompountResult*)node subResults];
}
return description;
}];
return description;
}
@end
@implementation FAExplainCompountResult
- (id)deepCopy{
FAExplainCompountResult* result = [super deepCopy];
NSMutableArray* subresults = [NSMutableArray arrayWithCapacity:[self.subResults count]];
for (FAExplainResult* subresult in self.subResults) {
[subresults addObject:[subresult deepCopy]];
}
result.subResults = subresults;
return result;
}
- (void) aggregateResult:(FAExplainCompountResult*)result{
[super aggregateResult:result];
for (NSUInteger i =0; i< MIN([self.subResults count], [result.subResults count]); i++) {
FAExplainResult* subresult = [self.subResults objectAtIndex:i];
[subresult aggregateResult:[result.subResults objectAtIndex:i]];
}
}
@end
@implementation NSPredicate (Explain)
- (NSString*)explainDescription{
NSString* description = [self description];
NSRange lineBreak = [description rangeOfString:@"\n"];
if (lineBreak.location != NSNotFound) {
description = [[description substringToIndex:lineBreak.location] stringByAppendingString:@"..."];
}
else if ([description length] >200) {
description = [[description substringFromIndex:200] stringByAppendingString:@"..."];
}
return description;
}
/**
* Evaluate a predicate recursive and return a explain result
*/
- (FAExplainResult*)explainResultWithObject:(id)object substitutionVariables:(NSDictionary *)bindings context:(NSManagedObjectContext*)context{
FAExplainResult* result = [FAExplainResult result];
result.description = [self explainDescription];
if (object != nil) {
@try {
if ([object isKindOfClass:[NSManagedObjectID class]] && context != nil) {
object = [context objectWithID:object];
}
result.match = [self evaluateWithObject:object substitutionVariables:nil];
}
@catch (NSException *exception) {
result.match = NO;
result.value = [exception reason];
}
}
return result;
}
/**
* Creates a description to explain a collection
*/
+ (NSString*) explainWithPredicate:(NSPredicate*)predicate collection:(id<NSFastEnumeration>)collection context:(NSManagedObjectContext*)context aggregateOnly:(BOOL)aggregateOnly{
NSString* explain = @"NO PREDICATE";
if (predicate != nil) {
NSMutableString* mutableDescription = [NSMutableString string];
NSUInteger count = 0;
FAExplainResult* aggregate = nil;
for (id object in collection) {
FAExplainResult* result = [predicate explainResultWithObject:object substitutionVariables:nil context:context];
if (aggregate == nil) {
aggregate = [result deepCopy];
}
else{
[aggregate aggregateResult:result];
}
if (!aggregateOnly) {
[mutableDescription appendFormat:@"%d. %@ %@\n",count, [object explainDescription], [result matchString]];
[mutableDescription appendFormat:@"\t%@\n",[[result recursiveDescription] stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t"]];
}
count++;
}
if (aggregate == nil) {
explain = @"NO OBJECTS FOUND";
}
else{
[mutableDescription appendString:@"Aggregate:\n"];
[mutableDescription appendString:[aggregate recursiveAggregateDescription]];
explain = mutableDescription;
}
}
return explain;
}
/**
* Creates a description to explain a object
*/
+ (NSString*) explainWithPredicate:(NSPredicate*)predicate object:(id)object{
NSString* explain = @"NO PREDICATE";
if (predicate != nil) {
explain = [predicate explainWithObject:object substitutionVariables:nil];
}
return explain;
}
/**
* Creates a description to explain a predicate
*/
+ (NSString*) explainWithPredicate:(NSPredicate*)predicate{
return [self explainWithPredicate:predicate object:nil];
}
- (NSString*) explainWithObject:(id)object substitutionVariables:(NSDictionary *)bindings{
NSString* explain = nil;
if ([object conformsToProtocol:@protocol(NSFastEnumeration)]) {
explain = [NSPredicate explainWithPredicate:self collection:object context:nil aggregateOnly:NO];
}
else{
FAExplainResult* result = [self explainResultWithObject:object substitutionVariables:nil context:nil];
explain = [result recursiveDescription];
}
return explain;
}
-(NSString *)explainWithObject:(id)object{
return [self explainWithObject:object substitutionVariables:nil];
}
-(NSString *)explain{
return [self explainWithObject:nil];
}
@end
@implementation NSCompoundPredicate (Explain)
- (NSString*) explainDescription{
NSString* description = nil;
switch (self.compoundPredicateType) {
case NSNotPredicateType:
{
description = @"NOT";
break;
}
case NSAndPredicateType:
{
description = @"AND";
break;
}
case NSOrPredicateType:
{
description = @"OR";
break;
}
default:
break;
}
return description;
}
- (FAExplainCompountResult*)explainResultWithObject:(id)object substitutionVariables:(NSDictionary *)bindings context:(NSManagedObjectContext*)context{
BOOL allMatch = YES;
BOOL anyMatch = NO;
NSMutableArray* results = [NSMutableArray arrayWithCapacity:[self.subpredicates count]];
for (NSPredicate* predicate in self.subpredicates) {
FAExplainResult* result = [predicate explainResultWithObject:object substitutionVariables:bindings context:context];
[results addObject:result];
allMatch = allMatch && result.match;
anyMatch = anyMatch || result.match;
}
BOOL match = NO;
NSString* description = [self explainDescription];
switch (self.compoundPredicateType) {
case NSNotPredicateType:
{
match = !allMatch;
break;
}
case NSAndPredicateType:
{
match = allMatch;
break;
}
case NSOrPredicateType:
{
match = anyMatch;
break;
}
default:
break;
}
FAExplainCompountResult* result = [FAExplainCompountResult result];
result.match = match;
result.description = description;
result.subResults = results;
return result;
}
@end
@implementation NSComparisonPredicate (Explain)
- (NSString*)explainDescription{
NSString* description = nil;
if (self.predicateOperatorType == NSEqualToPredicateOperatorType || self.predicateOperatorType == NSNotEqualToPredicateOperatorType) {
NSMutableString* mutableDescription = nil;
if (self.comparisonPredicateModifier == NSAllPredicateModifier) {
mutableDescription = [NSMutableString stringWithString:@"ALL "];
}
else if (self.comparisonPredicateModifier == NSAnyPredicateModifier) {
mutableDescription = [NSMutableString stringWithString:@"ANY "];
}
else{
mutableDescription = [NSMutableString string];
}
[mutableDescription appendString:[self.leftExpression explainDescription]];
if (self.predicateOperatorType == NSEqualToPredicateOperatorType){
[mutableDescription appendString:@" == "];
}
else if(self.predicateOperatorType == NSNotEqualToPredicateOperatorType) {
[mutableDescription appendString:@" != "];
}
else{
@throw [NSException exceptionWithName:@"This should never happen" reason:nil userInfo:nil];
}
[mutableDescription appendString:[self.rightExpression explainDescription]];
description = mutableDescription;
}
else{
description = [self description];
}
NSRange lineBreak = [description rangeOfString:@"\n"];
if (lineBreak.location != NSNotFound) {
description = [[description substringToIndex:lineBreak.location] stringByAppendingString:@"..."];
}
else if ([description length] >200) {
description = [[description substringFromIndex:200] stringByAppendingString:@"..."];
}
return description;
}
- (FAExplainResult*)explainResultWithObject:(id)object substitutionVariables:(NSDictionary *)bindings context:(NSManagedObjectContext*)context{
FAExplainResult* result = [FAExplainResult result];
result.description = [self explainDescription];
if (object != nil) {
@try {
if ([object isKindOfClass:[NSManagedObjectID class]] && context != nil) {
object = [context objectWithID:object];
}
result.match = [self evaluateWithObject:object substitutionVariables:nil];
if (self.leftExpression.expressionType == NSKeyPathExpressionType) {
NSString* keyPath = self.leftExpression.keyPath;
result.value = [[object valueForKeyPath:keyPath] explainDescription];
}
}
@catch (NSException *exception) {
result.match = NO;
result.value = [exception reason];
}
}
return result;
}
@end
@implementation NSFetchRequest (Explain)
- (NSString*) explainWithObject:(id)object{
return [NSPredicate explainWithPredicate:self.predicate object:object];
}
- (NSString*) explainWithResult:(NSArray*)result aggregateOnly:(BOOL)aggregateOnly{
return [NSPredicate explainWithPredicate:self.predicate collection:result context:nil aggregateOnly:aggregateOnly];
}
- (NSString *)explainWithResult:(NSArray *)result{
return [self explainWithResult:result aggregateOnly:NO];
}
-(NSString *)explain{
return [NSPredicate explainWithPredicate:self.predicate];
}
@end
@implementation NSManagedObjectContext (Explain)
- (NSFetchRequest*)explainFetchRequestForRequest:(NSFetchRequest*)request ignorePredicate:(BOOL)ignorePredicate fetchLimit:(NSUInteger)fetchLimit{
NSFetchRequest* fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:request.entity];
[fetchRequest setReturnsObjectsAsFaults:NO];
if (fetchLimit == 0) {
fetchLimit = [[NSUserDefaults standardUserDefaults] integerForKey:@"ExplainFetchRequestsLimit"];
}
if (fetchLimit < 100) {
fetchLimit = 100;
}
[fetchRequest setFetchLimit:fetchLimit];
if (!ignorePredicate) {
[fetchRequest setPredicate:request.predicate];
}
return fetchRequest;
}
- (NSString*) explainFetchRequest:(NSFetchRequest*)request showIgnored:(BOOL)showIgnored fetchLimit:(NSUInteger)fetchLimit aggregateOnly:(BOOL)aggregateOnly{
NSArray* result = nil;
if (request.predicate != nil) {
NSFetchRequest* explainRequest = [self explainFetchRequestForRequest:request ignorePredicate:showIgnored fetchLimit:fetchLimit];
result = [self executeFetchRequest:explainRequest error:NULL];
}
return [NSPredicate explainWithPredicate:request.predicate collection:result context:self aggregateOnly:NO];
}
- (NSString*) explainFetchRequest:(NSFetchRequest*)request showIgnored:(BOOL)showIgnored{
return [self explainFetchRequest:request showIgnored:showIgnored fetchLimit:0 aggregateOnly:NO];
}
-(NSString *)explainFetchRequest:(NSFetchRequest *)request{
return [self explainFetchRequest:request showIgnored:NO fetchLimit:0 aggregateOnly:NO];
}
+ (void) load {
@autoreleasepool{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ExplainFetchRequests"]) {
Method old = class_getInstanceMethod([self class], @selector(executeFetchRequest:error:));
Method new = class_getInstanceMethod([self class], @selector(executeFetchRequestAndExplain:error:));
method_exchangeImplementations(old,new);
}
};
}
-(NSArray *)executeFetchRequestAndExplain:(NSFetchRequest *)request error:(NSError **)error{
NSArray* result = nil;
if (request.predicate != nil) {
NSFetchRequest* explainRequest = [self explainFetchRequestForRequest:request ignorePredicate:YES fetchLimit:0];
result = [self executeFetchRequestAndExplain:explainRequest error:NULL];
BOOL aggregateOnly = [[NSUserDefaults standardUserDefaults] boolForKey:@"ExplainFetchRequestsAggregateOnly"];
NSLog(@"%@",[result explainWithPredicate:request.predicate aggregateOnly:aggregateOnly]);
}
result = [self executeFetchRequestAndExplain:request error:error];
return result;
}
@end
@implementation NSArray (Explain)
- (NSString*) explainWithPredicate:(NSPredicate*)predicate aggregateOnly:(BOOL)aggregateOnly{
return [NSPredicate explainWithPredicate:predicate collection:self context:nil aggregateOnly:aggregateOnly];
}
-(NSString *)explainWithPredicate:(NSPredicate *)predicate{
return [self explainWithPredicate:predicate aggregateOnly:NO];
}
@end
@implementation NSSet (Explain)
- (NSString*) explainWithPredicate:(NSPredicate*)predicate aggregateOnly:(BOOL)aggregateOnly{
return [NSPredicate explainWithPredicate:predicate collection:self context:nil aggregateOnly:aggregateOnly];
}
-(NSString *)explainWithPredicate:(NSPredicate *)predicate{
return [self explainWithPredicate:predicate aggregateOnly:NO];
}
@end