From 43be43856a5f6f57ee08ebc36bc52f2dc247e8e8 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sun, 25 Mar 2012 13:27:36 +0000 Subject: QueryKit: Add support for UPDATE queries. --- .../QueryKit/QueryKit.xcodeproj/project.pbxproj | 26 ++++- Frameworks/QueryKit/Source/QKQuery.h | 15 ++- Frameworks/QueryKit/Source/QKQuery.m | 106 +++++++++++++++++---- Frameworks/QueryKit/Source/QKQueryOperators.h | 4 +- Frameworks/QueryKit/Source/QKQueryParameter.h | 4 +- Frameworks/QueryKit/Source/QKQueryParameter.m | 4 +- Frameworks/QueryKit/Source/QKQueryTypes.h | 4 +- .../QueryKit/Source/QKQueryUpdateParameter.h | 59 ++++++++++++ .../QueryKit/Source/QKQueryUpdateParameter.m | 81 ++++++++++++++++ Frameworks/QueryKit/Source/QKQueryUtilities.h | 4 +- Frameworks/QueryKit/Source/QKQueryUtilities.m | 4 +- Frameworks/QueryKit/Source/QueryKit.h | 4 +- .../QueryKit/Tests/QKSelectQueryGroupByTests.h | 4 +- .../QueryKit/Tests/QKSelectQueryGroupByTests.m | 4 +- .../QueryKit/Tests/QKSelectQueryOrderByTests.h | 4 +- .../QueryKit/Tests/QKSelectQueryOrderByTests.m | 4 +- Frameworks/QueryKit/Tests/QKSelectQueryTests.h | 4 +- Frameworks/QueryKit/Tests/QKSelectQueryTests.m | 4 +- Frameworks/QueryKit/Tests/QKUpdateQueryTests.h | 39 ++++++++ Frameworks/QueryKit/Tests/QKUpdateQueryTests.m | 82 ++++++++++++++++ 20 files changed, 397 insertions(+), 63 deletions(-) create mode 100644 Frameworks/QueryKit/Source/QKQueryUpdateParameter.h create mode 100644 Frameworks/QueryKit/Source/QKQueryUpdateParameter.m create mode 100644 Frameworks/QueryKit/Tests/QKUpdateQueryTests.h create mode 100644 Frameworks/QueryKit/Tests/QKUpdateQueryTests.m diff --git a/Frameworks/QueryKit/QueryKit.xcodeproj/project.pbxproj b/Frameworks/QueryKit/QueryKit.xcodeproj/project.pbxproj index 89c50725..1912d3dd 100644 --- a/Frameworks/QueryKit/QueryKit.xcodeproj/project.pbxproj +++ b/Frameworks/QueryKit/QueryKit.xcodeproj/project.pbxproj @@ -9,6 +9,9 @@ /* Begin PBXBuildFile section */ 1713ECB014F96A5C0013C4F0 /* QKSelectQueryOrderByTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1713ECAF14F96A5C0013C4F0 /* QKSelectQueryOrderByTests.m */; }; 1713ECD814F970BB0013C4F0 /* QKSelectQueryGroupByTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1713ECD714F970BB0013C4F0 /* QKSelectQueryGroupByTests.m */; }; + 1719E47D151E8CA7003F98C5 /* QKQueryUpdateParameter.h in Headers */ = {isa = PBXBuildFile; fileRef = 1719E47B151E8CA7003F98C5 /* QKQueryUpdateParameter.h */; }; + 1719E47E151E8CA7003F98C5 /* QKQueryUpdateParameter.m in Sources */ = {isa = PBXBuildFile; fileRef = 1719E47C151E8CA7003F98C5 /* QKQueryUpdateParameter.m */; }; + 1719E4BD151F51F1003F98C5 /* QKUpdateQueryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1719E4BA151F51EA003F98C5 /* QKUpdateQueryTests.m */; }; 17E5951F14F301DF0054EE08 /* QKQuery.h in Headers */ = {isa = PBXBuildFile; fileRef = 17E5951614F301DF0054EE08 /* QKQuery.h */; settings = {ATTRIBUTES = (Public, ); }; }; 17E5952014F301DF0054EE08 /* QKQuery.m in Sources */ = {isa = PBXBuildFile; fileRef = 17E5951714F301DF0054EE08 /* QKQuery.m */; }; 17E5952114F301DF0054EE08 /* QKQueryOperators.h in Headers */ = {isa = PBXBuildFile; fileRef = 17E5951814F301DF0054EE08 /* QKQueryOperators.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -39,6 +42,10 @@ 1713ECAF14F96A5C0013C4F0 /* QKSelectQueryOrderByTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QKSelectQueryOrderByTests.m; sourceTree = ""; }; 1713ECD614F970BB0013C4F0 /* QKSelectQueryGroupByTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QKSelectQueryGroupByTests.h; sourceTree = ""; }; 1713ECD714F970BB0013C4F0 /* QKSelectQueryGroupByTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QKSelectQueryGroupByTests.m; sourceTree = ""; }; + 1719E47B151E8CA7003F98C5 /* QKQueryUpdateParameter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QKQueryUpdateParameter.h; sourceTree = ""; }; + 1719E47C151E8CA7003F98C5 /* QKQueryUpdateParameter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QKQueryUpdateParameter.m; sourceTree = ""; }; + 1719E4B9151F51EA003F98C5 /* QKUpdateQueryTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QKUpdateQueryTests.h; sourceTree = ""; }; + 1719E4BA151F51EA003F98C5 /* QKUpdateQueryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QKUpdateQueryTests.m; sourceTree = ""; }; 17E5951614F301DF0054EE08 /* QKQuery.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QKQuery.h; sourceTree = ""; }; 17E5951714F301DF0054EE08 /* QKQuery.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QKQuery.m; sourceTree = ""; }; 17E5951814F301DF0054EE08 /* QKQueryOperators.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QKQueryOperators.h; sourceTree = ""; }; @@ -124,15 +131,25 @@ 17E5951E14F301DF0054EE08 /* QueryKit.h */, 17E5951614F301DF0054EE08 /* QKQuery.h */, 17E5951714F301DF0054EE08 /* QKQuery.m */, - 17E5951914F301DF0054EE08 /* QKQueryParameter.h */, - 17E5951A14F301DF0054EE08 /* QKQueryParameter.m */, 17E5951C14F301DF0054EE08 /* QKQueryUtilities.h */, 17E5951D14F301DF0054EE08 /* QKQueryUtilities.m */, + 1719E47A151E8C87003F98C5 /* Model */, 17E5952814F301F40054EE08 /* Constants */, ); path = Source; sourceTree = ""; }; + 1719E47A151E8C87003F98C5 /* Model */ = { + isa = PBXGroup; + children = ( + 17E5951914F301DF0054EE08 /* QKQueryParameter.h */, + 17E5951A14F301DF0054EE08 /* QKQueryParameter.m */, + 1719E47B151E8CA7003F98C5 /* QKQueryUpdateParameter.h */, + 1719E47C151E8CA7003F98C5 /* QKQueryUpdateParameter.m */, + ); + name = Model; + sourceTree = ""; + }; 17322A7214FA645300F0CF9B /* SELECT Tests */ = { isa = PBXGroup; children = ( @@ -149,6 +166,8 @@ 17322A7414FA646000F0CF9B /* UPDATE Tests */ = { isa = PBXGroup; children = ( + 1719E4B9151F51EA003F98C5 /* QKUpdateQueryTests.h */, + 1719E4BA151F51EA003F98C5 /* QKUpdateQueryTests.m */, ); name = "UPDATE Tests"; sourceTree = ""; @@ -200,6 +219,7 @@ 17E5952414F301DF0054EE08 /* QKQueryTypes.h in Headers */, 17E5952514F301DF0054EE08 /* QKQueryUtilities.h in Headers */, 17E5952714F301DF0054EE08 /* QueryKit.h in Headers */, + 1719E47D151E8CA7003F98C5 /* QKQueryUpdateParameter.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -311,6 +331,7 @@ 17E596A814F308160054EE08 /* QKSelectQueryTests.m in Sources */, 1713ECB014F96A5C0013C4F0 /* QKSelectQueryOrderByTests.m in Sources */, 1713ECD814F970BB0013C4F0 /* QKSelectQueryGroupByTests.m in Sources */, + 1719E4BD151F51F1003F98C5 /* QKUpdateQueryTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -321,6 +342,7 @@ 17E5952014F301DF0054EE08 /* QKQuery.m in Sources */, 17E5952314F301DF0054EE08 /* QKQueryParameter.m in Sources */, 17E5952614F301DF0054EE08 /* QKQueryUtilities.m in Sources */, + 1719E47E151E8CA7003F98C5 /* QKQueryUpdateParameter.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Frameworks/QueryKit/Source/QKQuery.h b/Frameworks/QueryKit/Source/QKQuery.h index b45dc556..2132a158 100644 --- a/Frameworks/QueryKit/Source/QKQuery.h +++ b/Frameworks/QueryKit/Source/QKQuery.h @@ -2,7 +2,7 @@ // $Id: QKQuery.h 3421 2011-09-10 22:58:45Z stuart02 $ // // QKQuery.h -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,12 +27,11 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import "QKQueryTypes.h" #import "QKQueryOperators.h" #import "QKQueryParameter.h" +#import "QKQueryUpdateParameter.h" /** * @class QKQuery QKQuery.h @@ -47,8 +46,10 @@ NSString *_table; NSMutableString *_query; + NSMutableArray *_parameters; NSMutableArray *_fields; + NSMutableArray *_updateParameters; NSMutableArray *_groupByFields; NSMutableArray *_orderByFields; @@ -78,6 +79,11 @@ */ @property (readwrite, retain, getter=fields, setter=setFields:) NSMutableArray *_fields; +/** + * @property _updateFields The fields of an UPDATE query. + */ +@property (readwrite, retain, getter=updateParameters, setter=setUpdateParameters:) NSMutableArray *_updateParameters; + /** * @property _queryType The type of query to be built. */ @@ -101,6 +107,9 @@ - (void)addParameter:(QKQueryParameter *)parameter; - (void)addParameter:(NSString *)field operator:(QKQueryOperator)operator value:(id)value; +- (void)addFieldToUpdate:(QKQueryUpdateParameter *)parameter; +- (void)addFieldToUpdate:(NSString *)field toValue:(id)value; + - (void)groupByField:(NSString *)field; - (void)groupByFields:(NSArray *)fields; diff --git a/Frameworks/QueryKit/Source/QKQuery.m b/Frameworks/QueryKit/Source/QKQuery.m index ebd866cb..d26b9f72 100644 --- a/Frameworks/QueryKit/Source/QKQuery.m +++ b/Frameworks/QueryKit/Source/QKQuery.m @@ -2,7 +2,7 @@ // $Id: QKQuery.m 3432 2011-09-27 00:21:35Z stuart02 $ // // QKQuery.h -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import "QKQuery.h" @@ -44,6 +42,8 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; - (NSString *)_buildConstraints; - (NSString *)_buildGroupByClause; - (NSString *)_buildOrderByClause; +- (NSString *)_buildUpdateClause; +- (NSString *)_buildSelectOptions; - (BOOL)_addString:(NSString *)string toArray:(NSMutableArray *)array; @@ -56,6 +56,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; @synthesize _parameters; @synthesize _queryType; @synthesize _fields; +@synthesize _updateParameters; @synthesize _quoteFields; #pragma mark - @@ -80,6 +81,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; if ((self = [super init])) { [self setTable:table]; [self setFields:[[NSMutableArray alloc] init]]; + [self setUpdateParameters:[[NSMutableArray alloc] init]]; [self setParameters:[[NSMutableArray alloc] init]]; [self setQueryType:(QKQueryType)-1]; [self setQuoteFields:NO]; @@ -150,6 +152,29 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; [self addParameter:[QKQueryParameter queryParamWithField:field operator:operator value:value]]; } +#pragma mark - +#pragma mark Update Parameters + +/** + * Adds the supplied update parameter. + * + * @param parameter The parameter to add. + */ +- (void)addFieldToUpdate:(QKQueryUpdateParameter *)parameter +{ + if ([parameter field] && ([[parameter field] length] > 0) && [parameter value]) { + [_updateParameters addObject:parameter]; + } +} + +/** + * Convenience method for adding a new update parameter. + */ +- (void)addFieldToUpdate:(NSString *)field toValue:(id)value +{ + [self addFieldToUpdate:[QKQueryUpdateParameter queryUpdateParamWithField:field value:value]]; +} + #pragma mark - #pragma mark Grouping @@ -220,10 +245,10 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; { [self _validateRequiements]; - BOOL isSelect = (_queryType == QKSelectQuery); - BOOL isInsert = (_queryType == QKInsertQuery); - BOOL isUpdate = (_queryType == QKUpdateQuery); - BOOL isDelete = (_queryType == QKDeleteQuery); + BOOL isSelect = _queryType == QKSelectQuery; + BOOL isInsert = _queryType == QKInsertQuery; + BOOL isUpdate = _queryType == QKUpdateQuery; + BOOL isDelete = _queryType == QKDeleteQuery; NSString *fields = [self _buildFieldList]; @@ -246,22 +271,16 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; [_query appendString:_table]; + if (isUpdate) { + [_query appendFormat:@" %@", [self _buildUpdateClause]]; + } + if ([_parameters count] > 0) { - [_query appendString:@" WHERE "]; - [_query appendString:[self _buildConstraints]]; + [_query appendFormat:@" WHERE %@", [self _buildConstraints]]; } if (isSelect) { - NSString *groupBy = [self _buildGroupByClause]; - NSString *orderBy = [self _buildOrderByClause]; - - if ([groupBy length] > 0) { - [_query appendFormat:@" %@", groupBy]; - } - - if ([orderBy length] > 0) { - [_query appendFormat:@" %@", orderBy]; - } + [_query appendString:[self _buildSelectOptions]]; } return _query; @@ -383,6 +402,54 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; return orderBy; } +/** + * Builds the string representation of the query's UPDATE parameters. + * + * @return The fields to be updated + */ +- (NSString *)_buildUpdateClause +{ + NSMutableString *update = [NSMutableString string]; + + if ([_updateParameters count] == 0) return update; + + [update appendString:@"SET "]; + + for (QKQueryUpdateParameter *param in _updateParameters) + { + [update appendFormat:@"%@, ", param]; + } + + if ([update hasSuffix:@", "]) { + [update setString:[update substringToIndex:([update length] - 2)]]; + } + + return update; +} + +/** + * Builds any SELECT specific query constraints, namely ORDER BY or GROUP BY clauses. + * + * @return The query clauses (if any). + */ +- (NSString *)_buildSelectOptions +{ + NSMutableString *string = [NSMutableString string]; + + NSString *groupBy = [self _buildGroupByClause]; + NSString *orderBy = [self _buildOrderByClause]; + + if ([groupBy length] > 0) { + [string appendFormat:@" %@", groupBy]; + } + + if ([orderBy length] > 0) { + [string appendFormat:@" %@", orderBy]; + } + + return string; +} + /** * Adds the supplied string to the supplied array, but only if the length is greater than zero. * @@ -427,6 +494,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; if (_query) [_query release], _query = nil; if (_parameters) [_parameters release], _parameters = nil; if (_fields) [_fields release], _fields = nil; + if (_updateParameters) [_updateParameters release], _updateParameters = nil; if (_groupByFields) [_groupByFields release], _groupByFields = nil; if (_orderByFields) [_orderByFields release], _orderByFields = nil; diff --git a/Frameworks/QueryKit/Source/QKQueryOperators.h b/Frameworks/QueryKit/Source/QKQueryOperators.h index 32e64563..0c7565e0 100644 --- a/Frameworks/QueryKit/Source/QKQueryOperators.h +++ b/Frameworks/QueryKit/Source/QKQueryOperators.h @@ -2,7 +2,7 @@ // $Id: QKQueryOperators.h 3423 2011-09-12 16:50:15Z stuart02 $ // // QKQueryOperators.h -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at /** * @enum QKQueryOperator diff --git a/Frameworks/QueryKit/Source/QKQueryParameter.h b/Frameworks/QueryKit/Source/QKQueryParameter.h index 78c5739f..18f87929 100644 --- a/Frameworks/QueryKit/Source/QKQueryParameter.h +++ b/Frameworks/QueryKit/Source/QKQueryParameter.h @@ -2,7 +2,7 @@ // $Id: QKQueryParameter.h 3421 2011-09-10 22:58:45Z stuart02 $ // // QKQueryParameter.h -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import "QKQueryOperators.h" diff --git a/Frameworks/QueryKit/Source/QKQueryParameter.m b/Frameworks/QueryKit/Source/QKQueryParameter.m index 7f6064e6..c95bf585 100644 --- a/Frameworks/QueryKit/Source/QKQueryParameter.m +++ b/Frameworks/QueryKit/Source/QKQueryParameter.m @@ -2,7 +2,7 @@ // $Id: QKQueryParameter.m 3432 2011-09-27 00:21:35Z stuart02 $ // // QKQueryParameter.m -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import "QKQueryParameter.h" #import "QKQueryUtilities.h" diff --git a/Frameworks/QueryKit/Source/QKQueryTypes.h b/Frameworks/QueryKit/Source/QKQueryTypes.h index 4ab338ec..96978ce2 100644 --- a/Frameworks/QueryKit/Source/QKQueryTypes.h +++ b/Frameworks/QueryKit/Source/QKQueryTypes.h @@ -2,7 +2,7 @@ // $Id: QKQueryTypes.h 3423 2011-09-12 16:50:15Z stuart02 $ // // QKQueryTypes.h -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at /** * @enum QKQueryType diff --git a/Frameworks/QueryKit/Source/QKQueryUpdateParameter.h b/Frameworks/QueryKit/Source/QKQueryUpdateParameter.h new file mode 100644 index 00000000..5231d272 --- /dev/null +++ b/Frameworks/QueryKit/Source/QKQueryUpdateParameter.h @@ -0,0 +1,59 @@ +// +// $Id$ +// +// QKQueryUpdateParameter.h +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on March 24, 2012 +// Copyright (c) 2012 Stuart Connolly. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +/** + * @class QKQueryUpdateParameter QKQueryUpdateParameter.h + * + * @author Stuart Connolly http://stuconnolly.com/ + * + * QueryKit update query parameter class. + */ +@interface QKQueryUpdateParameter : NSObject +{ + NSString *_field; + + id _value; +} + +/** + * @property _field The field component of the parameter. + */ +@property (readwrite, retain, getter=field, setter=setField:) NSString *_field; + +/** + *@property _value The value component of the parameter. + */ +@property (readwrite, retain, getter=value, setter=setValue:) id _value; + ++ (QKQueryUpdateParameter *)queryUpdateParamWithField:(NSString *)field value:(id)value; + +- (id)initUpdateParamWithField:(NSString *)field value:(id)value; + +@end diff --git a/Frameworks/QueryKit/Source/QKQueryUpdateParameter.m b/Frameworks/QueryKit/Source/QKQueryUpdateParameter.m new file mode 100644 index 00000000..febbfa44 --- /dev/null +++ b/Frameworks/QueryKit/Source/QKQueryUpdateParameter.m @@ -0,0 +1,81 @@ +// +// $Id$ +// +// QKQueryUpdateParameter.m +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on March 24, 2012 +// Copyright (c) 2012 Stuart Connolly. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#import "QKQueryUpdateParameter.h" + +@implementation QKQueryUpdateParameter + +@synthesize _field; +@synthesize _value; + +#pragma mark - +#pragma mark Initialisation + ++ (QKQueryUpdateParameter *)queryUpdateParamWithField:(NSString *)field value:(id)value +{ + return [[[QKQueryUpdateParameter alloc] initUpdateParamWithField:field value:value] autorelease]; +} + +- (id)initUpdateParamWithField:(NSString *)field value:(id)value +{ + if ((self = [super init])) { + [self setField:field]; + [self setValue:value]; + } + + return self; +} + +#pragma mark - + +- (NSString *)description +{ + NSMutableString *string = [NSMutableString string]; + + NSString *field = [_field stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + [string appendString:field]; + [string appendString:@" = "]; + [string appendFormat:(![_value isKindOfClass:[NSNumber class]]) ? @"'%@'" : @"%@", [_value description]]; + + return string; +} + +#pragma mark - + +- (void)dealloc +{ + if (_field) [_field release], _field = nil; + if (_value) [_value release], _value = nil; + + [super dealloc]; +} + +@end diff --git a/Frameworks/QueryKit/Source/QKQueryUtilities.h b/Frameworks/QueryKit/Source/QKQueryUtilities.h index 9049b191..228385f8 100644 --- a/Frameworks/QueryKit/Source/QKQueryUtilities.h +++ b/Frameworks/QueryKit/Source/QKQueryUtilities.h @@ -2,7 +2,7 @@ // $Id: QKQueryUtilities.h 3421 2011-09-10 22:58:45Z stuart02 $ // // QKQueryUtilities.h -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import "QKQueryOperators.h" diff --git a/Frameworks/QueryKit/Source/QKQueryUtilities.m b/Frameworks/QueryKit/Source/QKQueryUtilities.m index dde1bd77..8519eb20 100644 --- a/Frameworks/QueryKit/Source/QKQueryUtilities.m +++ b/Frameworks/QueryKit/Source/QKQueryUtilities.m @@ -2,7 +2,7 @@ // $Id: QKQueryUtilities.m 3421 2011-09-10 22:58:45Z stuart02 $ // // QKQueryUtilities.m -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import "QKQueryUtilities.h" diff --git a/Frameworks/QueryKit/Source/QueryKit.h b/Frameworks/QueryKit/Source/QueryKit.h index f8570025..2b62a7b5 100644 --- a/Frameworks/QueryKit/Source/QueryKit.h +++ b/Frameworks/QueryKit/Source/QueryKit.h @@ -2,7 +2,7 @@ // $Id: QueryKit.h 3431 2011-09-26 22:26:24Z stuart02 $ // // QueryKit.h -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import #import diff --git a/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.h b/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.h index 56907b36..1be5345b 100644 --- a/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.h +++ b/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.h @@ -2,7 +2,7 @@ // $Id$ // // QKSelectQueryGroupByTests.h -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on February 25, 2012 // Copyright (c) 2012 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import #import diff --git a/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.m b/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.m index 9bbe75a2..0a8277e0 100644 --- a/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.m +++ b/Frameworks/QueryKit/Tests/QKSelectQueryGroupByTests.m @@ -2,7 +2,7 @@ // $Id$ // // QKSelectQueryGroupByTests.m -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on February 25, 2012 // Copyright (c) 2012 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import "QKSelectQueryGroupByTests.h" diff --git a/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.h b/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.h index c457491a..1ced6503 100644 --- a/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.h +++ b/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.h @@ -2,7 +2,7 @@ // $Id$ // // QKSelectQueryOrderByTests.h -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on February 25, 2012 // Copyright (c) 2012 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import #import diff --git a/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.m b/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.m index 84bd5ddb..ac70f0fe 100644 --- a/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.m +++ b/Frameworks/QueryKit/Tests/QKSelectQueryOrderByTests.m @@ -2,7 +2,7 @@ // $Id$ // // QKSelectQueryOrderByTests.m -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on February 25, 2012 // Copyright (c) 2012 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import "QKSelectQueryOrderByTests.h" diff --git a/Frameworks/QueryKit/Tests/QKSelectQueryTests.h b/Frameworks/QueryKit/Tests/QKSelectQueryTests.h index df9e3272..80ea92e2 100644 --- a/Frameworks/QueryKit/Tests/QKSelectQueryTests.h +++ b/Frameworks/QueryKit/Tests/QKSelectQueryTests.h @@ -2,7 +2,7 @@ // $Id$ // // QKSelectQueryTests.h -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import #import diff --git a/Frameworks/QueryKit/Tests/QKSelectQueryTests.m b/Frameworks/QueryKit/Tests/QKSelectQueryTests.m index 620dbadd..cb2d3599 100644 --- a/Frameworks/QueryKit/Tests/QKSelectQueryTests.m +++ b/Frameworks/QueryKit/Tests/QKSelectQueryTests.m @@ -2,7 +2,7 @@ // $Id$ // // QKSelectQueryTests.m -// sequel-pro +// QueryKit // // Created by Stuart Connolly (stuconnolly.com) on September 4, 2011 // Copyright (c) 2011 Stuart Connolly. All rights reserved. @@ -27,8 +27,6 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -// -// More info at #import "QKSelectQueryTests.h" diff --git a/Frameworks/QueryKit/Tests/QKUpdateQueryTests.h b/Frameworks/QueryKit/Tests/QKUpdateQueryTests.h new file mode 100644 index 00000000..b4230c1f --- /dev/null +++ b/Frameworks/QueryKit/Tests/QKUpdateQueryTests.h @@ -0,0 +1,39 @@ +// +// $Id$ +// +// QKUpdateQueryTests.h +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on March 25, 2012 +// Copyright (c) 2012 Stuart Connolly. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#import +#import + +@interface QKUpdateQueryTests : SenTestCase +{ + QKQuery *_query; +} + +@end diff --git a/Frameworks/QueryKit/Tests/QKUpdateQueryTests.m b/Frameworks/QueryKit/Tests/QKUpdateQueryTests.m new file mode 100644 index 00000000..fa3bb766 --- /dev/null +++ b/Frameworks/QueryKit/Tests/QKUpdateQueryTests.m @@ -0,0 +1,82 @@ +// +// $Id$ +// +// QKUpdateQueryTests.m +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on March 25, 2012 +// Copyright (c) 2012 Stuart Connolly. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. + +#import "QKUpdateQueryTests.h" + +static NSString *QKTestTableName = @"test_table"; + +static NSString *QKTestFieldOne = @"test_field1"; +static NSString *QKTestFieldTwo = @"test_field2"; + +static NSString *QKTestUpdateValueOne = @"update_one"; +static NSString *QKTestUpdateValueTwo = @"update_two"; + +static NSUInteger QKTestParameterOne = 10; + +@implementation QKUpdateQueryTests + +#pragma mark - +#pragma mark Setup & tear down + +- (void)setUp +{ + _query = [QKQuery queryTable:QKTestTableName]; + + [_query setQueryType:QKUpdateQuery]; + + [_query addFieldToUpdate:QKTestFieldOne toValue:QKTestUpdateValueOne]; + [_query addFieldToUpdate:QKTestFieldTwo toValue:QKTestUpdateValueTwo]; + + [_query addParameter:QKTestFieldOne operator:QKEqualityOperator value:[NSNumber numberWithUnsignedInteger:QKTestParameterOne]]; +} + +#pragma mark - +#pragma mark Tests + +- (void)testUpdateQueryTypeIsCorrect +{ + STAssertTrue([[_query query] hasPrefix:@"UPDATE"], @"query type"); +} + +- (void)testUpdateQueryFieldsAreCorrect +{ + NSString *query = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@', %@ = '%@'", QKTestTableName, QKTestFieldOne, QKTestUpdateValueOne, QKTestFieldTwo, QKTestUpdateValueTwo]; + + STAssertTrue([[_query query] hasPrefix:query], @"query fields"); +} + +- (void)testUpdateQueryConstraintsAreCorrect +{ + NSString *query = [NSString stringWithFormat:@"WHERE %@ %@ %@", QKTestFieldOne, [QKQueryUtilities operatorRepresentationForType:QKEqualityOperator], [NSNumber numberWithUnsignedInteger:QKTestParameterOne]]; + + STAssertTrue(([[_query query] rangeOfString:query].location != NSNotFound), @"query constraints"); +} + +@end -- cgit v1.2.3