API 0.9.5
Foundation/CPPredicate/CPExpression_subquery.j
Go to the documentation of this file.
00001 /*
00002  * CPExpression_subquery.j
00003  *
00004  * Created by cacaodev.
00005  * Copyright 2010.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 
00023 @implementation CPExpression_subquery : CPExpression
00024 {
00025     CPExpression _collection;
00026     CPExpression _variableExpression;
00027     CPPredicate  _subpredicate;
00028 }
00029 
00030 - (id)initWithExpression:(CPExpression)collection usingIteratorVariable:(CPString)variable predicate:(CPPredicate)subpredicate
00031 {
00032     var variableExpression = [CPExpression expressionForVariable:variable];
00033     return [self initWithExpression:collection usingIteratorExpression:variableExpression predicate:subpredicate];
00034 }
00035 
00036 - (id)initWithExpression:(CPExpression)collection usingIteratorExpression:(CPExpression)variableExpression predicate:(CPPredicate)subpredicate
00037 {
00038     self = [super initWithExpressionType:CPSubqueryExpressionType];
00039 
00040     if (self)
00041     {
00042         _subpredicate = subpredicate;
00043         _collection = collection;
00044         _variableExpression = variableExpression;
00045     }
00046     return self;
00047 }
00048 
00049 - (id)expressionValueWithObject:(id)object context:(id)context
00050 {
00051     var collection = [_collection expressionValueWithObject:object context:context],
00052         count = [collection count],
00053         result = [CPArray array],
00054         bindings = [CPDictionary dictionaryWithObject:[CPExpression expressionForEvaluatedObject] forKey:[self variable]],
00055         i = 0;
00056 
00057     for (; i < count; i++)
00058     {
00059         var item = [collection objectAtIndex:i];
00060         if ([_subpredicate evaluateWithObject:item substitutionVariables:bindings])
00061             [result addObject:item];
00062     }
00063 
00064     return result;
00065 }
00066 
00067 - (BOOL)isEqual:(id)object;
00068 {
00069     if (self === object)
00070         return YES;
00071 
00072     if (![_collection isEqual:[object collection]] || ![_subpredicate isEqual:[object predicate]])
00073         return NO;
00074 
00075     return YES;
00076 }
00077 
00078 - (CPExpression)collection
00079 {
00080     return _collection;
00081 }
00082 
00083 - (id)copy
00084 {
00085     return [[CPExpression_subquery alloc] initWithExpression:[_collection copy] usingIteratorExpression:[_variableExpression copy] predicate:[_subpredicate copy]];
00086 }
00087 
00088 - (CPPredicate)predicate
00089 {
00090     return _subpredicate;
00091 }
00092 
00093 - (CPString)description
00094 {
00095     return [self predicateFormat];
00096 }
00097 
00098 - (CPString)predicateFormat
00099 {
00100     return @"SUBQUERY(" + [_collection description] + ", " + [_variableExpression description] + ", " + [_subpredicate predicateFormat] + ")";
00101 }
00102 
00103 - (CPString)variable
00104 {
00105     return [_variableExpression variable];
00106 }
00107 
00108 - (CPExpression)variableExpression
00109 {
00110     return _variableExpression;
00111 }
00112 @end
00113 
00114 var CPExpressionKey     = @"CPExpression",
00115     CPSubpredicateKey   = @"CPSubpredicate",
00116     CPVariableKey       = @"CPVariable";
00117 
00118 @implementation CPExpression_subquery (CPCoding)
00119 
00120 - (id)initWithCoder:(CPCoder)coder
00121 {
00122     var collection = [coder decodeObjectForKey:CPExpressionKey],
00123         subpredicate = [coder decodeObjectForKey:CPSubpredicateKey],
00124         variableExpression = [coder decodeObjectForKey:CPVariableKey];
00125 
00126     return [self initWithExpression:collection usingIteratorExpression:variableExpression predicate:subpredicate];
00127 }
00128 
00129 - (void)encodeWithCoder:(CPCoder)coder
00130 {
00131     [coder encodeObject:_collection forKey:CPExpressionKey];
00132     [coder encodeObject:_subpredicate forKey:CPSubpredicateKey];
00133     [coder encodeObject:_variableExpression forKey:CPVariableKey];
00134 }
00135 
00136 @end
 All Classes Files Functions Variables Defines