diff options
-rw-r--r-- | Frameworks/QueryKit/QKQuery.h | 2 | ||||
-rw-r--r-- | Frameworks/QueryKit/QKQuery.m | 86 | ||||
-rw-r--r-- | Frameworks/QueryKit/QKQueryUtilities.m | 11 | ||||
-rw-r--r-- | UnitTests/QKSelectQueryTests.m | 21 | ||||
-rw-r--r-- | sequel-pro.xcodeproj/project.pbxproj | 4 |
5 files changed, 107 insertions, 17 deletions
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 <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 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; } diff --git a/UnitTests/QKSelectQueryTests.m b/UnitTests/QKSelectQueryTests.m index fca26f99..f1329f46 100644 --- a/UnitTests/QKSelectQueryTests.m +++ b/UnitTests/QKSelectQueryTests.m @@ -41,6 +41,13 @@ static NSString *SPTestFieldFour = @"test_field4"; - (void)setUp { _query = [QKQuery selectQueryFromTable:SPTestTableName]; + + [_query addField:SPTestFieldOne]; + [_query addField:SPTestFieldTwo]; + [_query addField:SPTestFieldThree]; + [_query addField:SPTestFieldFour]; + + [_query addParameter:SPTestFieldOne operator:QKEqualityOperator value:@"10"]; } -(void)tearDown @@ -53,12 +60,16 @@ static NSString *SPTestFieldFour = @"test_field4"; - (void)testSelectQueryTypeIsCorrect { - [_query addField:SPTestFieldOne]; - [_query addField:SPTestFieldTwo]; - [_query addField:SPTestFieldThree]; - [_query addField:SPTestFieldFour]; - STAssertTrue([[_query query] hasPrefix:@"SELECT"], @"query type"); } +- (void)testSelectQueryFieldsAreCorrect +{ + NSString *query = [NSString stringWithFormat:@"SELECT %@, %@, %@, %@", SPTestFieldOne, SPTestFieldTwo, SPTestFieldThree, SPTestFieldFour]; + + NSLog(@"%@", _query); + + STAssertTrue([[_query query] hasPrefix:query], @"query fields"); +} + @end diff --git a/sequel-pro.xcodeproj/project.pbxproj b/sequel-pro.xcodeproj/project.pbxproj index 2172a05d..899a5100 100644 --- a/sequel-pro.xcodeproj/project.pbxproj +++ b/sequel-pro.xcodeproj/project.pbxproj @@ -179,7 +179,6 @@ 17F90E2C1210B34900274C98 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 17F90E2B1210B34900274C98 /* Credits.rtf */; }; 17F90E481210B42700274C98 /* SPExportFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 17F90E471210B42700274C98 /* SPExportFile.m */; }; 17F90E4B1210B43A00274C98 /* SPExportFileUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 17F90E4A1210B43A00274C98 /* SPExportFileUtilities.m */; }; - 17FC35961413CF7200AC3602 /* QKQuery.m in Sources */ = {isa = PBXBuildFile; fileRef = 17FC358E1413CF7200AC3602 /* QKQuery.m */; }; 17FC35C01413D13F00AC3602 /* QKQueryOperators.h in Headers */ = {isa = PBXBuildFile; fileRef = 17FC358B1413CF7200AC3602 /* QKQueryOperators.h */; settings = {ATTRIBUTES = (Public, ); }; }; 17FC35C21413D14000AC3602 /* QKQuery.h in Headers */ = {isa = PBXBuildFile; fileRef = 17FC358D1413CF7200AC3602 /* QKQuery.h */; settings = {ATTRIBUTES = (Public, ); }; }; 17FC35C31413D14100AC3602 /* QKQuery.m in Sources */ = {isa = PBXBuildFile; fileRef = 17FC358E1413CF7200AC3602 /* QKQuery.m */; }; @@ -527,7 +526,7 @@ isa = PBXContainerItemProxy; containerPortal = 2A37F4A9FDCFA73011CA2CEA /* Project object */; proxyType = 1; - remoteGlobalIDString = 17FC35AB1413CFE700AC3602 /* QueryKit */; + remoteGlobalIDString = 17FC35AB1413CFE700AC3602; remoteInfo = QueryKit; }; 5847571D120A1C6D0057631F /* PBXContainerItemProxy */ = { @@ -3514,7 +3513,6 @@ 1713C740140D8AEF00CFD461 /* SPQueryDocumentsController.m in Sources */, 1713C75F140D8D5900CFD461 /* SPQueryConsoleDataSource.m in Sources */, 17902612141025BB005F677F /* SPQueryControllerInitializer.m in Sources */, - 17FC35961413CF7200AC3602 /* QKQuery.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; |