From 0e096fe30fe8af5bee4b9e1f8e422862dc2dd189 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Tue, 6 Sep 2011 21:21:36 +0000 Subject: More QueryKit progress. Still very rough. --- Frameworks/QueryKit/QKQuery.h | 2 +- Frameworks/QueryKit/QKQuery.m | 86 +++++++++++++++++++++++++++++++--- Frameworks/QueryKit/QKQueryUtilities.m | 11 ++++- 3 files changed, 90 insertions(+), 9 deletions(-) (limited to 'Frameworks') diff --git a/Frameworks/QueryKit/QKQuery.h b/Frameworks/QueryKit/QKQuery.h index 9fb7b0f2..1c4f1a8a 100644 --- a/Frameworks/QueryKit/QKQuery.h +++ b/Frameworks/QueryKit/QKQuery.h @@ -78,6 +78,6 @@ - (NSString *)query; - (void)addField:(NSString *)field; -- (void)addParameter:(NSString *)field operator:(QKQueryOperator *)op value:(id)value; +- (void)addParameter:(NSString *)field operator:(QKQueryOperator)operator value:(id)value; @end 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 #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 diff --git a/Frameworks/QueryKit/QKQueryUtilities.m b/Frameworks/QueryKit/QKQueryUtilities.m index f9f43f03..3d566baa 100644 --- a/Frameworks/QueryKit/QKQueryUtilities.m +++ b/Frameworks/QueryKit/QKQueryUtilities.m @@ -25,8 +25,17 @@ #import "QKQueryUtilities.h" +static NSString *QKUnrecognisedQueryOperatorException = @"QKUnrecognisedQueryOperator"; + @implementation QKQueryUtilities +/** + * + * + * @param operator + * + * @return + */ + (NSString *)operatorRepresentationForType:(QKQueryOperator)operator { NSString *opString = nil; @@ -37,7 +46,7 @@ opString = @"="; break; default: - [NSException raise:@"" format:@"Unrecognised query operator type: %d", operator]; + [NSException raise:QKUnrecognisedQueryOperatorException format:@"Unrecognised query operator type: %d", operator]; break; } -- cgit v1.2.3