diff options
author | stuconnolly <stuart02@gmail.com> | 2011-09-06 21:21:36 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2011-09-06 21:21:36 +0000 |
commit | 0e096fe30fe8af5bee4b9e1f8e422862dc2dd189 (patch) | |
tree | c9da2e42bc81245ab2c8e16b8e511184871a12a4 /Frameworks/QueryKit/QKQuery.m | |
parent | 09a79031bed30c1cf95e1b9de85bf73b2ce38300 (diff) | |
download | sequelpro-0e096fe30fe8af5bee4b9e1f8e422862dc2dd189.tar.gz sequelpro-0e096fe30fe8af5bee4b9e1f8e422862dc2dd189.tar.bz2 sequelpro-0e096fe30fe8af5bee4b9e1f8e422862dc2dd189.zip |
More QueryKit progress. Still very rough.
Diffstat (limited to 'Frameworks/QueryKit/QKQuery.m')
-rw-r--r-- | Frameworks/QueryKit/QKQuery.m | 86 |
1 files changed, 79 insertions, 7 deletions
diff --git a/Frameworks/QueryKit/QKQuery.m b/Frameworks/QueryKit/QKQuery.m index 1f0c5005..0c0f4e87 100644 --- a/Frameworks/QueryKit/QKQuery.m +++ b/Frameworks/QueryKit/QKQuery.m @@ -24,13 +24,19 @@ // More info at <http://code.google.com/p/sequel-pro/> #import "QKQuery.h" +#import "QKQueryParameter.h" +#import "QKQueryUtilities.h" static NSString *QKNoQueryTypeException = @"QKNoQueryType"; +static NSString *QKNoQueryTableException = @"QKNoQueryTable"; @interface QKQuery () +- (void)_validateRequiements; + - (NSString *)_buildQuery; - (NSString *)_buildFieldList; +- (NSString *)_buildConstraints; @end @@ -94,34 +100,73 @@ static NSString *QKNoQueryTypeException = @"QKNoQueryType"; /** * Shortcut for adding a new parameter to this query. */ -- (void)addParameter:(NSString *)field operator:(QKQueryOperator *)op value:(id)value +- (void)addParameter:(NSString *)field operator:(QKQueryOperator)operator value:(id)value { + QKQueryParameter *param = [QKQueryParameter queryParamWithField:field operator:operator value:value]; + [_parameters addObject:param]; } #pragma mark - #pragma mark Private API /** - * Builds the actual query. + * */ -- (NSString *)_buildQuery +- (void)_validateRequiements { if (_queryType == -1) { [NSException raise:QKNoQueryTypeException format:@"Attempt to build query with no query type specified."]; } - if (_queryType == QKSelectQuery) { - [_query appendString:@"SELECT "]; + if (!_table || [_table length] == 0) { + [NSException raise:QKNoQueryTableException format:@"Attempt to build query with no query table specified."]; + } +} + +/** + * Builds the actual query. + */ +- (NSString *)_buildQuery +{ + [self _validateRequiements]; + + BOOL isSelect = (_queryType == QKSelectQuery); + BOOL isInsert = (_queryType == QKInsertQuery); + BOOL isUpdate = (_queryType == QKUpdateQuery); + BOOL isDelete = (_queryType == QKDeleteQuery); + + NSString *fields = [self _buildFieldList]; + + if (isSelect) { + [_query appendFormat:@"SELECT %@ FROM ", fields]; + } + else if (isInsert) { + [_query appendString:@"INSERT INTO "]; + } + else if (isUpdate) { + [_query appendString:@"UPDATE "]; + } + else if (isDelete) { + [_query appendString:@"DELETE FROM "]; + } + + if (_database && [_database length] > 0) { + [_query appendFormat:@"%@.", _database]; } - [_query appendString:[self _buildFieldList]]; + [_query appendString:_table]; + + if ([_parameters count] > 0) { + [_query appendString:@" WHERE "]; + [_query appendString:[self _buildConstraints]]; + } return _query; } /** - * Builds the string representation of the field list. + * Builds the string representation of the query's field list. */ - (NSString *)_buildFieldList { @@ -159,6 +204,33 @@ static NSString *QKNoQueryTypeException = @"QKNoQueryType"; return fields; } +/** + * Builds the string representation of the query's constraints. + */ +- (NSString *)_buildConstraints +{ + NSMutableString *constraints = [NSMutableString string]; + + if ([_parameters count] == 0) return constraints; + + for (QKQueryParameter *param in _parameters) + { + NSString *field = [[param field] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + [constraints appendString:field]; + [constraints appendFormat:@" %@ ", [QKQueryUtilities operatorRepresentationForType:[param operator]]]; + [constraints appendString:[[param value] description]]; + + [constraints appendString:@" AND "]; + } + + if ([constraints hasSuffix:@" AND "]) { + [constraints setString:[constraints substringToIndex:([constraints length] - 5)]]; + } + + return constraints; +} + #pragma mark - - (NSString *)description |