]> err.no Git - sope/blob - sope-core/EOCoreData/EOQualifier+CoreData.m
6d255c97295107ffd82bb407a618866772e927a7
[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 + (EOQualifier *)qualifierForPredicate:(NSPredicate *)_predicate {
142   if (_predicate == nil)
143     return nil;
144   
145   if ([_predicate respondsToSelector:@selector(asQualifier)])
146     return [_predicate asQualifier];
147   
148   NSLog(@"ERROR(%s): cannot convert NSPredicate class %@!", 
149         __PRETTY_FUNCTION__,
150         NSStringFromClass([self class]));
151   return nil;
152 }
153
154 - (EOQualifier *)asQualifier {
155   return self;
156 }
157
158 - (NSPredicate *)asPredicate {
159   NSLog(@"TODO(%s): implement me for class %@!", __PRETTY_FUNCTION__,
160         NSStringFromClass([self class]));
161   return nil;
162 }
163
164 - (NSExpression *)asExpression {
165   return nil;
166 }
167
168
169 /* CoreData compatibility */
170
171 + (NSPredicate *)andPredicateWithSubpredicates:(NSArray *)_sub {
172   return [NSCompoundPredicate andPredicateWithSubpredicates:
173                                 [_sub valueForKey:@"asPredicate"]];
174 }
175
176 + (NSPredicate *)orPredicateWithSubpredicates:(NSArray *)_sub {
177   return [NSCompoundPredicate orPredicateWithSubpredicates:
178                                 [_sub valueForKey:@"asPredicate"]];
179 }
180
181 + (NSPredicate *)notPredicateWithSubpredicate:(id)_predicate {
182   return [NSCompoundPredicate notPredicateWithSubpredicate:
183                                 [_predicate asPredicate]];
184 }
185
186 @end /* EOQualifier(CoreData) */