From 4cad6f0e6e4fb497b480256c2abe3de34ebf225c Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Mon, 16 Apr 2012 20:16:52 +0000 Subject: Bring outline view branch up to date with trunk. --- Frameworks/QueryKit/Source/QKQuery.h | 16 ++- Frameworks/QueryKit/Source/QKQuery.m | 129 +++++++++++++++++---- 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 +- 11 files changed, 269 insertions(+), 44 deletions(-) create mode 100644 Frameworks/QueryKit/Source/QKQueryUpdateParameter.h create mode 100644 Frameworks/QueryKit/Source/QKQueryUpdateParameter.m (limited to 'Frameworks/QueryKit/Source') diff --git a/Frameworks/QueryKit/Source/QKQuery.h b/Frameworks/QueryKit/Source/QKQuery.h index b45dc556..c0f925bb 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. */ @@ -94,6 +100,7 @@ - (id)initWithTable:(NSString *)table; - (NSString *)query; +- (void)clear; - (void)addField:(NSString *)field; - (void)addFields:(NSArray *)fields; @@ -101,6 +108,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 21053203..4be29ae2 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]; @@ -98,11 +100,32 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; #pragma mark - #pragma mark Public API +/** + * Requests that the query be built. + * + * @return The generated query. + */ - (NSString *)query { return _query ? [self _buildQuery] : @""; } +/** + * Clears anything this instance should know about the query it's building. + */ +- (void)clear +{ + [self setTable:nil]; + [self setDatabase:nil]; + [self setQueryType:(QKQueryType)-1]; + + [_fields removeAllObjects]; + [_parameters removeAllObjects]; + [_updateParameters removeAllObjects]; + [_groupByFields removeAllObjects]; + [_orderByFields removeAllObjects]; +} + #pragma mark - #pragma mark Fields @@ -150,6 +173,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 +266,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 +292,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; @@ -317,7 +357,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; for (QKQueryParameter *param in _parameters) { - [constraints appendFormat:@"%@ ", param]; + [constraints appendFormat:@"%@", param]; [constraints appendString:@" AND "]; } @@ -383,6 +423,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 +515,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 -- cgit v1.2.3