diff options
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 |