]> err.no Git - sope/blob - sope-core/EOCoreData/EOQualifier+CoreData.m
fixed gcc 4.1 warnings
[sope] / sope-core / EOCoreData / EOQualifier+CoreData.m
1 /*
2   Copyright (C) 2005 SKYRIX Software AG
3   
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "EOQualifier+CoreData.h"
23 #include "NSPredicate+EO.h"
24 #include "NSExpression+EO.h"
25 #include "common.h"
26
27 /*
28   CoreData / Foundation       EOF
29     Predicates:
30       NSComparisonPredicate   EOKeyValueQualifier / EOKeyComparisonQualifier
31       NSCompoundPredicate     EOAndQualifier / EOOrQualifier / EONotQualifier
32
33     NSExpressions:
34       - constant
35       - evaluatedObject
36       - variable              EOQualifierVariable
37       - keypath
38       - function
39
40   EOF operators:
41     EOQualifierOperatorEqual;
42     EOQualifierOperatorNotEqual;
43     EOQualifierOperatorLessThan;
44     EOQualifierOperatorGreaterThan;
45     EOQualifierOperatorLessThanOrEqualTo;
46     EOQualifierOperatorGreaterThanOrEqualTo;
47     EOQualifierOperatorContains;
48     EOQualifierOperatorLike;
49     EOQualifierOperatorCaseInsensitiveLike;
50 */
51
52 @implementation EOQualifier(CoreData)
53
54 + (NSPredicateOperatorType)predicateOperatorTypeForEOSelector:(SEL)_sel {
55   if (SEL_EQ(_sel, EOQualifierOperatorEqual))
56     return NSEqualToPredicateOperatorType;
57   if (SEL_EQ(_sel, EOQualifierOperatorNotEqual))
58     return NSNotEqualToPredicateOperatorType;
59   
60   if (SEL_EQ(_sel, EOQualifierOperatorLessThan))
61     return NSLessThanPredicateOperatorType;
62   if (SEL_EQ(_sel, EOQualifierOperatorGreaterThan))
63     return NSGreaterThanPredicateOperatorType;
64   
65   if (SEL_EQ(_sel, EOQualifierOperatorLessThanOrEqualTo))
66     return NSLessThanOrEqualToPredicateOperatorType;
67   if (SEL_EQ(_sel, EOQualifierOperatorGreaterThanOrEqualTo))
68     return NSGreaterThanOrEqualToPredicateOperatorType;
69   
70   if (SEL_EQ(_sel, EOQualifierOperatorContains))
71     return NSInPredicateOperatorType;
72   
73   if (SEL_EQ(_sel, EOQualifierOperatorLike) ||
74       SEL_EQ(_sel, EOQualifierOperatorCaseInsensitiveLike))
75     return NSLikePredicateOperatorType;
76   
77   return NSCustomSelectorPredicateOperatorType;
78 }
79
80 + (SEL)eoSelectorForForComparisonPredicate:(NSComparisonPredicate *)_p {
81   BOOL hasOpt;
82   SEL  sel = NULL;
83   
84   if (_p == nil)
85     return NULL;
86   
87   hasOpt = [_p options] != 0 ? YES : NO;
88   
89   // TODO: need to check options
90   
91   switch ([_p predicateOperatorType]) {
92   case NSCustomSelectorPredicateOperatorType:
93     sel = hasOpt ? NULL : [_p customSelector];
94     break;
95     
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;
104     
105   case NSEqualToPredicateOperatorType:
106     sel = hasOpt ? NULL : EOQualifierOperatorEqual; break;
107   case NSNotEqualToPredicateOperatorType:
108     sel = hasOpt ? NULL : EOQualifierOperatorNotEqual; break;
109     
110   case NSLikePredicateOperatorType:
111     sel = ([_p options] == NSCaseInsensitivePredicateOption)
112       ? EOQualifierOperatorCaseInsensitiveLike 
113       : (hasOpt ? NULL : EOQualifierOperatorLike);
114     break;
115     
116   case NSInPredicateOperatorType:
117     // TODO: for arrays: containsObject:, for strings: containsString:
118     sel = hasOpt ? NULL : EOQualifierOperatorContains; break;
119     
120   case NSBeginsWithPredicateOperatorType:
121     sel = hasOpt ? NULL : @selector(hasPrefix:); break;
122   case NSEndsWithPredicateOperatorType:
123     sel = hasOpt ? NULL : @selector(hasSuffix:); break;
124     
125     /* unsupported predicates */
126   case NSMatchesPredicateOperatorType:
127     // TODO
128   default:
129     sel = NULL;
130     break;
131   }
132   
133   if (sel == NULL) {
134     NSLog(@"ERROR(%s): cannot map NSComparisonPredicate to "
135           @"EOQualifier selector: %@",
136           __PRETTY_FUNCTION__, _p);
137   }
138   return sel;
139 }
140
141 - (NSPredicate *)predicateWithLeftExpression:(NSExpression *)_lhs
142   rightExpression:(NSExpression *)_rhs
143   eoSelector:(SEL)_selector
144 {
145   // TODO: create non-custom predicate if possible
146   NSComparisonPredicateModifier pmod;
147   NSPredicateOperatorType       ptype;
148   unsigned popts;
149
150   if (_selector == NULL) {
151     NSLog(@"ERROR(0x%p/%@): missing selector for predicate construction: %@",
152           self, NSStringFromClass([self class]), self);
153     return nil;
154   }
155   
156   ptype = [EOQualifier predicateOperatorTypeForEOSelector:_selector];
157   
158   if (ptype == NSCustomSelectorPredicateOperatorType) {
159     return [NSComparisonPredicate predicateWithLeftExpression:_lhs
160                                   rightExpression:_rhs
161                                   customSelector:_selector];
162   }
163   
164   pmod  = NSDirectPredicateModifier;
165   popts = 0;
166   
167   if (SEL_EQ(_selector, EOQualifierOperatorCaseInsensitiveLike))
168     popts = NSCaseInsensitivePredicateOption;
169   
170   return [NSComparisonPredicate predicateWithLeftExpression:_lhs
171                                 rightExpression:_rhs
172                                 modifier:pmod type:ptype options:popts];
173 }
174
175 + (EOQualifier *)qualifierForPredicate:(NSPredicate *)_predicate {
176   if (_predicate == nil)
177     return nil;
178   
179   if ([_predicate respondsToSelector:@selector(asQualifier)])
180     return [_predicate asQualifier];
181   
182   NSLog(@"ERROR(%s): cannot convert NSPredicate class %@!", 
183         __PRETTY_FUNCTION__,
184         NSStringFromClass([self class]));
185   return nil;
186 }
187
188 - (EOQualifier *)asQualifier {
189   return self;
190 }
191
192 - (NSPredicate *)asPredicate {
193   NSLog(@"TODO(%s): implement me for class %@!", __PRETTY_FUNCTION__,
194         NSStringFromClass([self class]));
195   return nil;
196 }
197
198 - (NSExpression *)asExpression {
199   return nil;
200 }
201
202
203 /* CoreData compatibility */
204
205 + (NSPredicate *)andPredicateWithSubpredicates:(NSArray *)_sub {
206   return [NSCompoundPredicate andPredicateWithSubpredicates:
207                                 [_sub valueForKey:@"asPredicate"]];
208 }
209
210 + (NSPredicate *)orPredicateWithSubpredicates:(NSArray *)_sub {
211   return [NSCompoundPredicate orPredicateWithSubpredicates:
212                                 [_sub valueForKey:@"asPredicate"]];
213 }
214
215 + (NSPredicate *)notPredicateWithSubpredicate:(id)_predicate {
216   return [NSCompoundPredicate notPredicateWithSubpredicate:
217                                 [_predicate asPredicate]];
218 }
219
220 - (NSPredicate *)predicateWithSubstitutionVariables:(NSDictionary *)_vars {
221   return [[self asPredicate] predicateWithSubstitutionVariables:_vars];
222 }
223
224 - (NSString *)predicateFormat {
225   return [[self asPredicate] predicateFormat];
226 }
227
228 @end /* EOQualifier(CoreData) */