2 Copyright (C) 2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "EOQualifier+CoreData.h"
23 #include "NSPredicate+EO.h"
24 #include "NSExpression+EO.h"
28 CoreData / Foundation EOF
30 NSComparisonPredicate EOKeyValueQualifier / EOKeyComparisonQualifier
31 NSCompoundPredicate EOAndQualifier / EOOrQualifier / EONotQualifier
36 - variable EOQualifierVariable
41 EOQualifierOperatorEqual;
42 EOQualifierOperatorNotEqual;
43 EOQualifierOperatorLessThan;
44 EOQualifierOperatorGreaterThan;
45 EOQualifierOperatorLessThanOrEqualTo;
46 EOQualifierOperatorGreaterThanOrEqualTo;
47 EOQualifierOperatorContains;
48 EOQualifierOperatorLike;
49 EOQualifierOperatorCaseInsensitiveLike;
52 @implementation EOQualifier(CoreData)
54 + (NSPredicateOperatorType)predicateOperatorTypeForEOSelector:(SEL)_sel {
55 if (SEL_EQ(_sel, EOQualifierOperatorEqual))
56 return NSEqualToPredicateOperatorType;
57 if (SEL_EQ(_sel, EOQualifierOperatorNotEqual))
58 return NSNotEqualToPredicateOperatorType;
60 if (SEL_EQ(_sel, EOQualifierOperatorLessThan))
61 return NSLessThanPredicateOperatorType;
62 if (SEL_EQ(_sel, EOQualifierOperatorGreaterThan))
63 return NSGreaterThanPredicateOperatorType;
65 if (SEL_EQ(_sel, EOQualifierOperatorLessThanOrEqualTo))
66 return NSLessThanOrEqualToPredicateOperatorType;
67 if (SEL_EQ(_sel, EOQualifierOperatorGreaterThanOrEqualTo))
68 return NSGreaterThanOrEqualToPredicateOperatorType;
70 if (SEL_EQ(_sel, EOQualifierOperatorContains))
71 return NSInPredicateOperatorType;
73 if (SEL_EQ(_sel, EOQualifierOperatorLike) ||
74 SEL_EQ(_sel, EOQualifierOperatorCaseInsensitiveLike))
75 return NSLikePredicateOperatorType;
77 return NSCustomSelectorPredicateOperatorType;
80 + (SEL)eoSelectorForForComparisonPredicate:(NSComparisonPredicate *)_p {
87 hasOpt = [_p options] != 0 ? YES : NO;
89 // TODO: need to check options
91 switch ([_p predicateOperatorType]) {
92 case NSCustomSelectorPredicateOperatorType:
93 sel = hasOpt ? NULL : [_p customSelector];
96 case NSLessThanPredicateOperatorType:
97 sel = hasOpt ? NULL : EOQualifierOperatorLessThan; break;
98 case NSLessThanOrEqualToPredicateOperatorType:
99 sel = hasOpt ? NULL : EOQualifierOperatorLessThanOrEqualTo; break;
100 case NSGreaterThanPredicateOperatorType:
101 sel = hasOpt ? NULL : EOQualifierOperatorGreaterThan; break;
102 case NSGreaterThanOrEqualToPredicateOperatorType:
103 sel = hasOpt ? NULL : EOQualifierOperatorGreaterThanOrEqualTo; break;
105 case NSEqualToPredicateOperatorType:
106 sel = hasOpt ? NULL : EOQualifierOperatorEqual; break;
107 case NSNotEqualToPredicateOperatorType:
108 sel = hasOpt ? NULL : EOQualifierOperatorNotEqual; break;
110 case NSLikePredicateOperatorType:
111 sel = ([_p options] == NSCaseInsensitivePredicateOption)
112 ? EOQualifierOperatorCaseInsensitiveLike
113 : (hasOpt ? NULL : EOQualifierOperatorLike);
116 case NSInPredicateOperatorType:
117 // TODO: for arrays: containsObject:, for strings: containsString:
118 sel = hasOpt ? NULL : EOQualifierOperatorContains; break;
120 case NSBeginsWithPredicateOperatorType:
121 sel = hasOpt ? NULL : @selector(hasPrefix:); break;
122 case NSEndsWithPredicateOperatorType:
123 sel = hasOpt ? NULL : @selector(hasSuffix:); break;
125 /* unsupported predicates */
126 case NSMatchesPredicateOperatorType:
134 NSLog(@"ERROR(%s): cannot map NSComparisonPredicate to "
135 @"EOQualifier selector: %@",
136 __PRETTY_FUNCTION__, _p);
141 - (NSPredicate *)predicateWithLeftExpression:(NSExpression *)_lhs
142 rightExpression:(NSExpression *)_rhs
143 eoSelector:(SEL)_selector
145 // TODO: create non-custom predicate if possible
146 NSComparisonPredicateModifier pmod;
147 NSPredicateOperatorType ptype;
150 if (_selector == NULL) {
151 NSLog(@"ERROR(0x%p/%@): missing selector for predicate construction: %@",
152 self, NSStringFromClass([self class]), self);
156 ptype = [EOQualifier predicateOperatorTypeForEOSelector:_selector];
158 if (ptype == NSCustomSelectorPredicateOperatorType) {
159 return [NSComparisonPredicate predicateWithLeftExpression:_lhs
161 customSelector:_selector];
164 pmod = NSDirectPredicateModifier;
167 if (SEL_EQ(_selector, EOQualifierOperatorCaseInsensitiveLike))
168 popts = NSCaseInsensitivePredicateOption;
170 return [NSComparisonPredicate predicateWithLeftExpression:_lhs
172 modifier:pmod type:ptype options:popts];
175 + (EOQualifier *)qualifierForPredicate:(NSPredicate *)_predicate {
176 if (_predicate == nil)
179 if ([_predicate respondsToSelector:@selector(asQualifier)])
180 return [_predicate asQualifier];
182 NSLog(@"ERROR(%s): cannot convert NSPredicate class %@!",
184 NSStringFromClass([self class]));
188 - (EOQualifier *)asQualifier {
192 - (NSPredicate *)asPredicate {
193 NSLog(@"TODO(%s): implement me for class %@!", __PRETTY_FUNCTION__,
194 NSStringFromClass([self class]));
198 - (NSExpression *)asExpression {
203 /* CoreData compatibility */
205 + (NSPredicate *)andPredicateWithSubpredicates:(NSArray *)_sub {
206 return [NSCompoundPredicate andPredicateWithSubpredicates:
207 [_sub valueForKey:@"asPredicate"]];
210 + (NSPredicate *)orPredicateWithSubpredicates:(NSArray *)_sub {
211 return [NSCompoundPredicate orPredicateWithSubpredicates:
212 [_sub valueForKey:@"asPredicate"]];
215 + (NSPredicate *)notPredicateWithSubpredicate:(id)_predicate {
216 return [NSCompoundPredicate notPredicateWithSubpredicate:
217 [_predicate asPredicate]];
220 - (NSPredicate *)predicateWithSubstitutionVariables:(NSDictionary *)_vars {
221 return [[self asPredicate] predicateWithSubstitutionVariables:_vars];
224 - (NSString *)predicateFormat {
225 return [[self asPredicate] predicateFormat];
228 @end /* EOQualifier(CoreData) */