2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include <EOControl/EOQualifier.h>
23 #include <EOControl/EONull.h>
26 @implementation EOKeyComparisonQualifier
28 static EONull *null = nil;
32 null = [[EONull null] retain];
35 - (id)initWithLeftKey:(NSString *)_leftKey
36 operatorSelector:(SEL)_selector
37 rightKey:(NSString *)_rightKey;
39 self->leftKey = [_leftKey copyWithZone:NULL];
40 self->rightKey = [_rightKey copyWithZone:NULL];
41 self->operator = _selector;
46 [self->leftKey release];
47 [self->rightKey release];
53 - (NSString *)leftKey {
56 - (NSString *)rightKey {
57 return self->rightKey;
60 return self->operator;
65 - (EOQualifier *)qualifierWithBindings:(NSDictionary *)_bindings
66 requiresAllVariables:(BOOL)_reqAll
68 static Class VarClass = Nil;
73 if (VarClass == Nil) VarClass = [EOQualifierVariable class];
76 if ([self->leftKey class] == VarClass) {
78 [_bindings objectForKey:[(EOQualifierVariable *)self->leftKey key]];
79 if (newLeftKey == nil) {
84 newLeftKey = self->leftKey;
90 newLeftKey = self->leftKey;
92 if ([self->rightKey class] == VarClass) {
94 [_bindings objectForKey:[(EOQualifierVariable *)self->rightKey key]];
95 if (newRightKey == nil) {
100 newRightKey = self->rightKey;
106 newRightKey = self->rightKey;
111 return [[[[self class] alloc]
112 initWithLeftKey:newLeftKey
113 operatorSelector:self->operator
114 rightKey:newRightKey]
118 - (NSArray *)bindingKeys {
119 static Class VarClass = Nil;
120 Class lkClass, rkClass;
121 if (VarClass == Nil) VarClass = [EOQualifierVariable class];
123 lkClass = [self->leftKey class];
124 rkClass = [self->rightKey class];
126 if ((lkClass == VarClass) && (rkClass == VarClass)) {
128 o[0] = [(EOQualifierVariable *)self->leftKey key];
129 o[1] = [(EOQualifierVariable *)self->rightKey key];
130 return [NSArray arrayWithObjects:o count:2];
133 if (lkClass == VarClass)
134 return [NSArray arrayWithObject:[(EOQualifierVariable *)self->leftKey key]];
135 if (rkClass == VarClass) {
136 return [NSArray arrayWithObject:
137 [(EOQualifierVariable *)self->rightKey key]];
139 return [NSArray array];
144 - (void)addQualifierKeysToSet:(NSMutableSet *)_keys {
146 [_keys addObject:self->leftKey];
147 [_keys addObject:self->rightKey];
152 - (BOOL)evaluateWithObject:(id)_object inEvalContext:(id)_ctx {
154 BOOL (*m)(id, SEL, id);
157 _ctx = [NSMutableDictionary dictionaryWithCapacity:16];
159 if ((lv = [(NSDictionary *)_ctx objectForKey:self->leftKey]) == nil) {
160 lv = [_object valueForKeyPath:self->leftKey];
161 if (lv == nil) lv = null;
162 [(NSMutableDictionary *)_ctx setObject:lv forKey:self->leftKey];
164 if ((rv = [(NSDictionary *)_ctx objectForKey:self->rightKey]) == nil) {
165 rv = [_object valueForKeyPath:self->rightKey];
166 if (rv == nil) rv = null;
167 [(NSMutableDictionary *)_ctx setObject:rv forKey:self->rightKey];
170 if ((m = (void *)[lv methodForSelector:self->operator]) == NULL) {
171 /* no such operator method ! */
172 [lv doesNotRecognizeSelector:self->operator];
176 return m(lv, self->operator, rv);
178 - (BOOL)evaluateWithObject:(id)_object {
179 return [self evaluateWithObject:_object inEvalContext:nil];
184 - (void)encodeWithCoder:(NSCoder *)_coder {
185 [_coder encodeObject:self->leftKey];
186 [_coder encodeObject:self->rightKey];
187 [_coder encodeValueOfObjCType:@encode(SEL) at:&(self->operator)];
189 - (id)initWithCoder:(NSCoder *)_coder {
190 self->leftKey = [[_coder decodeObject] copyWithZone:[self zone]];
191 self->rightKey = [[_coder decodeObject] copyWithZone:[self zone]];
192 [_coder decodeValueOfObjCType:@encode(SEL) at:&(self->operator)];
198 - (BOOL)isEqualToQualifier:(EOQualifier *)_qual {
199 if (![self->leftKey isEqual:[(EOKeyComparisonQualifier *)_qual leftKey]])
201 if (![self->rightKey isEqual:[(EOKeyComparisonQualifier *)_qual rightKey]])
203 if (sel_eq(self->operator, [(EOKeyComparisonQualifier *)_qual selector]))
210 - (EOQualifier *)qualifierByApplyingTransformer:(id)_transformer
213 if ([_transformer respondsToSelector:
214 @selector(transformKeyComparisonQualifier:inContext:)]) {
215 return [_transformer transformKeyComparisonQualifier:self inContext:_ctx];
218 return [[self retain] autorelease];
221 - (EOQualifier *)qualifierByApplyingKeyMap:(NSDictionary *)_map {
222 EOKeyComparisonQualifier *kcq;
225 l = [_map objectForKey:self->leftKey];
226 if (l == nil) l = self->leftKey;
227 r = [_map objectForKey:self->rightKey];
228 if (r == nil) r = self->rightKey;
230 kcq = [[EOKeyComparisonQualifier alloc]
231 initWithLeftKey:l operatorSelector:self->operator rightKey:r];
232 return [kcq autorelease];
237 - (NSString *)description {
240 s = [NSMutableString stringWithCapacity:64];
241 [s appendString:self->leftKey];
242 [s appendString:@" "];
243 [s appendString:[EOQualifier stringForOperatorSelector:self->operator]];
244 [s appendString:@" "];
245 [s appendString:self->rightKey];
249 @end /* EOKeyComparisonQualifier */