2002-11-15 22:57:05 +00:00
|
|
|
/**
|
|
|
|
EOKeyComparisonQualifier.m <title>EOKeyComparisonQualifier</title>
|
|
|
|
|
|
|
|
Copyright (C) 2000 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Author: Mirko Viviani <mirko.viviani@rccr.cremona.it>
|
|
|
|
Date: February 2000
|
|
|
|
|
|
|
|
$Revision$
|
|
|
|
$Date$
|
|
|
|
|
|
|
|
<abstract></abstract>
|
|
|
|
|
|
|
|
This file is part of the GNUstep Database Library.
|
|
|
|
|
|
|
|
<license>
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, write to the Free Software Foundation,
|
|
|
|
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
</license>
|
|
|
|
**/
|
|
|
|
|
2002-12-31 16:25:21 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
RCS_ID("$Id$")
|
2002-11-15 22:57:05 +00:00
|
|
|
|
2003-03-31 00:24:15 +00:00
|
|
|
#ifndef NeXT_Foundation_LIBRARY
|
|
|
|
#include <Foundation/NSDictionary.h>
|
|
|
|
#include <Foundation/NSSet.h>
|
|
|
|
#include <Foundation/NSUtilities.h>
|
|
|
|
#include <Foundation/NSDebug.h>
|
|
|
|
#else
|
|
|
|
#include <Foundation/Foundation.h>
|
|
|
|
#endif
|
2002-11-15 22:57:05 +00:00
|
|
|
|
2003-03-31 00:24:15 +00:00
|
|
|
#include <EOControl/EOQualifier.h>
|
|
|
|
#include <EOControl/EOKeyValueCoding.h>
|
|
|
|
#include <EOControl/EODebug.h>
|
2002-11-15 22:57:05 +00:00
|
|
|
|
2002-12-30 22:10:43 +00:00
|
|
|
|
2002-11-15 22:57:05 +00:00
|
|
|
@implementation EOKeyComparisonQualifier
|
|
|
|
|
2003-02-13 11:39:01 +00:00
|
|
|
/**
|
|
|
|
* Returns an autoreleased EOKeyComparisonQualifier using leftKey, selector
|
|
|
|
* and right key. The selector should take a single id as an argument and
|
|
|
|
* return a BOOL value. This method calls
|
|
|
|
* [EOKeyComparisonQualifier-initWithLeftKey:operatorSelectot:rightKey:].
|
|
|
|
*/
|
|
|
|
+ (EOQualifier *) qualifierWithLeftKey: (NSString *)leftKey
|
|
|
|
operatorSelector: (SEL)selector
|
|
|
|
rightKey: (id)rightKey
|
2002-11-15 22:57:05 +00:00
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
return AUTORELEASE([[self alloc] initWithLeftKey: leftKey
|
|
|
|
operatorSelector: selector
|
|
|
|
rightKey: rightKey]);
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
|
2003-02-13 11:39:01 +00:00
|
|
|
/** <init />
|
|
|
|
* Initializes the receiver with a copy of leftKey, selector and a copy of
|
|
|
|
* rightKey. The selector should take a single id as an argument and return a
|
|
|
|
* BOOL value.
|
|
|
|
*/
|
|
|
|
- (id) initWithLeftKey: (NSString *)leftKey
|
|
|
|
operatorSelector: (SEL)selector
|
|
|
|
rightKey: (id)rightKey
|
2002-11-15 22:57:05 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
/*Ayers (09-02-2002): Maybe we should assert the correct signature
|
|
|
|
but we currently don't have the object which should implement it.
|
|
|
|
Assertion during evaluation (i.e. when we have an object) could be
|
|
|
|
too expensive.*/
|
|
|
|
|
2002-11-15 22:57:05 +00:00
|
|
|
_selector = selector;
|
2003-02-13 11:39:01 +00:00
|
|
|
ASSIGNCOPY(_leftKey, leftKey);
|
|
|
|
ASSIGNCOPY(_rightKey, rightKey);
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
DESTROY(_leftKey);
|
|
|
|
DESTROY(_rightKey);
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2003-02-13 11:39:01 +00:00
|
|
|
/**
|
|
|
|
* Returns the selector used by the receiver during in-memory evaluation.
|
|
|
|
* The selector should take a single id as an argument and return a BOOL value.
|
|
|
|
* (More docs to follow for EOQualifierSQLGeneration.)
|
|
|
|
*/
|
|
|
|
- (SEL) selector
|
2002-11-15 22:57:05 +00:00
|
|
|
{
|
|
|
|
return _selector;
|
|
|
|
}
|
|
|
|
|
2003-02-13 11:39:01 +00:00
|
|
|
/**
|
|
|
|
* Returns the key with which the receiver obtains the left value during
|
|
|
|
* in-memory evaluation. (More docs to follow for EOQualifierSQLGeneration.)
|
|
|
|
*/
|
|
|
|
- (NSString *) leftKey
|
2002-11-15 22:57:05 +00:00
|
|
|
{
|
|
|
|
return _leftKey;
|
|
|
|
}
|
|
|
|
|
2003-02-13 11:39:01 +00:00
|
|
|
/**
|
|
|
|
* Returns the key with which the receiver obtains the right value during
|
|
|
|
* in-memory evaluation. (More docs to follow for EOQualifierSQLGeneration.)
|
|
|
|
*/
|
|
|
|
- (NSString *) rightKey
|
2002-11-15 22:57:05 +00:00
|
|
|
{
|
|
|
|
return _rightKey;
|
|
|
|
}
|
|
|
|
|
2003-02-13 11:39:01 +00:00
|
|
|
/**
|
|
|
|
* EOQualifierEvaluation protocol<br/>
|
|
|
|
* Evaluates the object according to the receivers definition. First the left
|
|
|
|
* value is obtained by invoking valueForKey: on the provided object with the
|
|
|
|
* receivers leftKey and the right value by invoking valueForKey: with the
|
|
|
|
* recievers rightKey. If the left value implements the receivers selector,
|
|
|
|
* this method returns the return value of the invocation of this method with
|
|
|
|
* the right value as the parameter.<br/>
|
|
|
|
* If the left object doesn't implement the receivers selector, but the
|
|
|
|
* selector of the reciever is one of:<br/>
|
2003-03-31 00:24:15 +00:00
|
|
|
* <list>
|
|
|
|
* <item>EOQualifierOperatorEqual</item>
|
|
|
|
* <item>EOQualifierOperatorNotEqual</item>
|
|
|
|
* <item>EOQualifierOperatorLessThan</item>
|
|
|
|
* <item>EOQualifierOperatorGreaterThan</item>
|
|
|
|
* <item>EOQualifierOperatorLessThanOrEqual</item>
|
|
|
|
* <item>EOQualifierOperatorGreaterThanOrEqual</item>
|
|
|
|
* <item>EOQualifierOperatorContains</item>
|
|
|
|
* <item>EOQualifierOperatorLike</item>
|
|
|
|
* <item>EOQualifierOperatorCaseInsensitiveLike</item>
|
2003-02-13 11:39:01 +00:00
|
|
|
* </list>
|
|
|
|
* then GDL2 tries to evaluate the qualifier by invoking
|
|
|
|
* isEqual:, compare:, rangeOfString: respectively and interpreting the
|
|
|
|
* results accoring to the selector. In the case of
|
|
|
|
* EOQualifierOperatorCaseInsensitiveLike, the values are converted using
|
|
|
|
* uppercaseString for evaluation.<br/>
|
|
|
|
* Both 'Like' fallback implementations are currently implemented by using
|
|
|
|
* isEqual: and do not yet take the ? and * wildcards into account.<br/>
|
|
|
|
* If the receivers selector is neither implemented by the left value nor
|
|
|
|
* corresponds to one of the EOQualifierOperators, this method simply
|
|
|
|
* returns NO.
|
|
|
|
*/
|
|
|
|
- (BOOL) evaluateWithObject: (id)object
|
2002-11-15 22:57:05 +00:00
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
id leftVal, rightVal;
|
|
|
|
BOOL (*imp)(id, SEL, id);
|
2002-11-15 22:57:05 +00:00
|
|
|
|
2003-02-13 11:39:01 +00:00
|
|
|
leftVal = [object valueForKey: _leftKey];
|
|
|
|
rightVal = [object valueForKey: _rightKey];
|
2002-11-15 22:57:05 +00:00
|
|
|
|
2003-02-13 11:39:01 +00:00
|
|
|
imp = (BOOL (*)(id, SEL, id))[leftVal methodForSelector: _selector];
|
|
|
|
if (imp != NULL)
|
|
|
|
{
|
|
|
|
return (*imp) (leftVal, _selector, rightVal);
|
|
|
|
}
|
2002-11-15 22:57:05 +00:00
|
|
|
if (sel_eq(_selector, EOQualifierOperatorEqual) == YES)
|
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
return [leftVal isEqual: rightVal];
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
else if (sel_eq(_selector, EOQualifierOperatorNotEqual) == YES)
|
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
return ([leftVal isEqual: rightVal]?NO:YES);
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
else if (sel_eq(_selector, EOQualifierOperatorLessThan) == YES)
|
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
return [leftVal compare: rightVal] == NSOrderedAscending;
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
else if (sel_eq(_selector, EOQualifierOperatorGreaterThan) == YES)
|
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
return [leftVal compare: rightVal] == NSOrderedDescending;
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
else if (sel_eq(_selector, EOQualifierOperatorLessThanOrEqualTo) == YES)
|
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
return [leftVal compare: rightVal] != NSOrderedDescending;
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
else if (sel_eq(_selector, EOQualifierOperatorGreaterThanOrEqualTo) == YES)
|
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
return [leftVal compare: rightVal] != NSOrderedAscending;
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
else if (sel_eq(_selector, EOQualifierOperatorContains) == YES)
|
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
return [leftVal rangeOfString: rightVal].location != NSNotFound;
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
else if (sel_eq(_selector, EOQualifierOperatorLike) == YES)
|
|
|
|
{
|
|
|
|
NSEmitTODO(); //TODO
|
2003-02-13 11:39:01 +00:00
|
|
|
return [leftVal isEqual: rightVal]
|
2002-11-15 22:57:05 +00:00
|
|
|
== NSOrderedSame;
|
|
|
|
}
|
|
|
|
else if (sel_eq(_selector, EOQualifierOperatorCaseInsensitiveLike) == YES)
|
|
|
|
{
|
|
|
|
NSEmitTODO(); //TODO
|
2003-02-13 11:39:01 +00:00
|
|
|
return [[leftVal uppercaseString] isEqual: [rightVal uppercaseString]]
|
2002-11-15 22:57:05 +00:00
|
|
|
== NSOrderedSame;
|
|
|
|
}
|
2003-02-13 11:39:01 +00:00
|
|
|
/*Ayers (09-02-2002): Maybe we should raise instead of returning NO.*/
|
2002-11-15 22:57:05 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2003-02-13 11:39:01 +00:00
|
|
|
/**
|
|
|
|
* Returns a human readable representation of the receiver.
|
|
|
|
*/
|
|
|
|
- (NSString *) description
|
2002-11-15 22:57:05 +00:00
|
|
|
{
|
2003-02-13 11:39:01 +00:00
|
|
|
NSString *selectorString;
|
|
|
|
selectorString = [isa stringForOperatorSelector: _selector];
|
|
|
|
if (selectorString == nil)
|
|
|
|
{
|
|
|
|
selectorString = NSStringFromSelector(_selector);
|
|
|
|
}
|
|
|
|
return [NSString stringWithFormat:@"<%s %p - %@ %@ %@>",
|
|
|
|
object_get_class_name(self),
|
|
|
|
(void*)self,
|
|
|
|
_leftKey,
|
|
|
|
selectorString,
|
|
|
|
_rightKey];
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|