/*
Project: database-access
Copyright (C) 2007 Free Software Foundation
Author: Daniel Santos,,,
Created: 2007-08-11 13:00:06 +0000 by dlsa
This application is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This application 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 General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#include "SRWhereClause.h"
#include "Functions.h"
@implementation SRWhereClause
/**
initializer
*/
- (SRWhereClause*)init {
_conditions = [[NSMutableArray alloc] init];
return self;
}
/**
deallocation
*/
- (void)dealloc {
}
/**
or's an expression to the current value of the clause. if it's
the first one, the or is omitted
*/
- (void)orExpression: (NSString*)expression {
[self addExpression: expression withOperator: OR];
}
/**
and's an expression to the current value of the clause. if it's
the first one, the and is omitted
*/
- (void)andExpression: (NSString*)expression {
[self addExpression: expression withOperator: AND];
}
/**
adds an expression to the where clause joining it with an operator
only instances of this class get a response, other receive an exception
*/
- (void)addExpression: (NSString*)expression withOperator:(NSString*)operator {
NSString *trimmedExpr = [expression stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
if (nil == _conditions)
_conditions = [[NSMutableArray alloc] init];
if ([_conditions count] != 0)
[_conditions addObject: operator];
[_conditions addObject: trimmedExpr];
}
/**
outputs the string representation
*/
- (NSString*)stringValue {
if (nil == _conditions)
_conditions = [[NSMutableArray alloc] init];
NSMutableString *result = [[NSMutableString alloc] init];
[result appendString:@" WHERE "];
int i = 0;
for (i = 0; i < [_conditions count]; i++) {
NSString *element = [_conditions objectAtIndex: i];
[result appendString: element];
[result appendString: @" "];
}
return result;
}
@end