From c1bab7abd666d809aa01c330a66c53e1b6abe6c4 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Mon, 9 Jul 2012 09:27:48 +0000 Subject: Improve QueryKit's handling of quotes by making it on by default. Also, add a bunch more tests. --- Frameworks/QueryKit/Source/QKQuery.h | 8 +-- Frameworks/QueryKit/Source/QKQuery.m | 53 +++++++++++-------- Frameworks/QueryKit/Source/QKQueryConstants.h | 32 ++++++++++++ .../QueryKit/Source/QKQueryGenericParameter.h | 53 +++++++++++++++++++ .../QueryKit/Source/QKQueryGenericParameter.m | 61 ++++++++++++++++++++++ Frameworks/QueryKit/Source/QKQueryParameter.h | 19 ++----- Frameworks/QueryKit/Source/QKQueryParameter.m | 17 ++---- .../QueryKit/Source/QKQueryUpdateParameter.h | 19 ++----- .../QueryKit/Source/QKQueryUpdateParameter.m | 16 +----- 9 files changed, 192 insertions(+), 86 deletions(-) create mode 100644 Frameworks/QueryKit/Source/QKQueryConstants.h create mode 100644 Frameworks/QueryKit/Source/QKQueryGenericParameter.h create mode 100644 Frameworks/QueryKit/Source/QKQueryGenericParameter.m (limited to 'Frameworks/QueryKit/Source') diff --git a/Frameworks/QueryKit/Source/QKQuery.h b/Frameworks/QueryKit/Source/QKQuery.h index c0f925bb..1cb93d15 100644 --- a/Frameworks/QueryKit/Source/QKQuery.h +++ b/Frameworks/QueryKit/Source/QKQuery.h @@ -55,7 +55,7 @@ QKQueryType _queryType; - BOOL _quoteFields; + BOOL _useQuotes; BOOL _orderDescending; } @@ -90,9 +90,9 @@ @property (readwrite, assign, getter=queryType, setter=setQueryType:) QKQueryType _queryType; /** - * @property _quoteFields Indicates whether or not the query's fields should be quoted. + * @property _useQuotes Indicates whether or not the query's fields should be quoted. */ -@property (readwrite, assign, getter=quoteFields, setter=setQuoteFields:) BOOL _quoteFields; +@property (readwrite, assign, getter=useQuotes) BOOL _useQuotes; + (QKQuery *)queryTable:(NSString *)table; + (QKQuery *)selectQueryFromTable:(NSString *)table; @@ -102,6 +102,8 @@ - (NSString *)query; - (void)clear; +- (void)setUseQuotes:(BOOL)quote; + - (void)addField:(NSString *)field; - (void)addFields:(NSArray *)fields; diff --git a/Frameworks/QueryKit/Source/QKQuery.m b/Frameworks/QueryKit/Source/QKQuery.m index bb876006..124bb5d2 100644 --- a/Frameworks/QueryKit/Source/QKQuery.m +++ b/Frameworks/QueryKit/Source/QKQuery.m @@ -29,6 +29,8 @@ // OTHER DEALINGS IN THE SOFTWARE. #import "QKQuery.h" +#import "QKQueryConstants.h" +#import "QKQueryGenericParameter.h" static NSString *QKNoQueryTypeException = @"QKNoQueryType"; static NSString *QKNoQueryTableException = @"QKNoQueryTable"; @@ -57,7 +59,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; @synthesize _queryType; @synthesize _fields; @synthesize _updateParameters; -@synthesize _quoteFields; +@synthesize _useQuotes; #pragma mark - #pragma mark Initialization @@ -84,7 +86,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; [self setUpdateParameters:[[NSMutableArray alloc] init]]; [self setParameters:[[NSMutableArray alloc] init]]; [self setQueryType:(QKQueryType)-1]; - [self setQuoteFields:NO]; + [self setUseQuotes:YES]; _orderDescending = NO; @@ -126,6 +128,24 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; [_orderByFields removeAllObjects]; } +#pragma mark - +#pragma mark Accessors + +- (void)setUseQuotes:(BOOL)quote +{ + _useQuotes = quote; + + for (QKQueryParameter *param in _parameters) + { + [param setUseQuotes:_useQuotes]; + } + + for (QKQueryUpdateParameter *param in _updateParameters) + { + [param setUseQuotes:_useQuotes]; + } +} + #pragma mark - #pragma mark Fields @@ -160,7 +180,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; */ - (void)addParameter:(QKQueryParameter *)parameter { - if ([parameter field] && ([[parameter field] length] > 0) && ((NSInteger)[parameter operator] > -1) && [parameter value]) { + if ([parameter field] && ([[parameter field] length] > 0) && ((NSInteger)[parameter operator] > -1) && [parameter value]) { [_parameters addObject:parameter]; } } @@ -183,7 +203,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; */ - (void)addFieldToUpdate:(QKQueryUpdateParameter *)parameter { - if ([parameter field] && ([[parameter field] length] > 0) && [parameter value]) { + if ([parameter field] && ([[parameter field] length] > 0) && [parameter value]) { [_updateParameters addObject:parameter]; } } @@ -287,10 +307,10 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; } if (_database && [_database length] > 0) { - [_query appendFormat:@"%@.", _database]; + [_query appendFormat:@"%@%@%@.", _useQuotes ? QUERY_QUOTE : EMPTY_STRING, _database, _useQuotes ? QUERY_QUOTE : EMPTY_STRING]; } - [_query appendString:_table]; + [_query appendFormat:@"%@%@%@", _useQuotes ? QUERY_QUOTE : EMPTY_STRING, _table, _useQuotes ? QUERY_QUOTE : EMPTY_STRING]; if (isUpdate) { [_query appendFormat:@" %@", [self _buildUpdateClause]]; @@ -326,17 +346,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; if ([field length] == 0) continue; - if (_quoteFields) { - [fields appendString:@"`"]; - } - - [fields appendString:field]; - - if (_quoteFields) { - [fields appendString:@"`"]; - } - - [fields appendString:@", "]; + [fields appendFormat:@"%@%@%@, ", _useQuotes ? QUERY_QUOTE : EMPTY_STRING, field, _useQuotes ? QUERY_QUOTE : EMPTY_STRING]; } if ([fields hasSuffix:@", "]) { @@ -357,8 +367,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; for (QKQueryParameter *param in _parameters) { - [constraints appendFormat:@"%@", param]; - + [constraints appendString:[param description]]; [constraints appendString:@" AND "]; } @@ -384,8 +393,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; for (NSString *field in _groupByFields) { - [groupBy appendString:field]; - [groupBy appendString:@", "]; + [groupBy appendFormat:@"%@%@%@, ", _useQuotes ? QUERY_QUOTE : EMPTY_STRING, field, _useQuotes ? QUERY_QUOTE : EMPTY_STRING]; } if ([groupBy hasSuffix:@", "]) { @@ -410,8 +418,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; for (NSString *field in _orderByFields) { - [orderBy appendString:field]; - [orderBy appendString:@", "]; + [orderBy appendFormat:@"%@%@%@, ", _useQuotes ? QUERY_QUOTE : EMPTY_STRING, field, _useQuotes ? QUERY_QUOTE : EMPTY_STRING]; } if ([orderBy hasSuffix:@", "]) { diff --git a/Frameworks/QueryKit/Source/QKQueryConstants.h b/Frameworks/QueryKit/Source/QKQueryConstants.h new file mode 100644 index 00000000..9d843169 --- /dev/null +++ b/Frameworks/QueryKit/Source/QKQueryConstants.h @@ -0,0 +1,32 @@ +// +// $Id$ +// +// QKQueryConstants.h +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on July 8, 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. + +#define QUERY_QUOTE @"`" +#define EMPTY_STRING @"" diff --git a/Frameworks/QueryKit/Source/QKQueryGenericParameter.h b/Frameworks/QueryKit/Source/QKQueryGenericParameter.h new file mode 100644 index 00000000..ba1e63cd --- /dev/null +++ b/Frameworks/QueryKit/Source/QKQueryGenericParameter.h @@ -0,0 +1,53 @@ +// +// $Id$ +// +// QKQueryGenericParameter.h +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on July 8, 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. + +@interface QKQueryGenericParameter : NSObject +{ + id _value; + NSString *_field; + BOOL _useQuotes; +} + +/** + * @property _field The field component of the parameter. + */ +@property (readwrite, retain, getter=field, setter=setField:) NSString *_field; + +/** + * @property _quoteField Indicates whether or not this parameters field should be quoted. + */ +@property (readwrite, assign, getter=useQuotes, setter=setUseQuotes:) BOOL _useQuotes; + +/** + *@property _value The value component of the parameter. + */ +@property (readwrite, retain, getter=value, setter=setValue:) id _value; + +@end diff --git a/Frameworks/QueryKit/Source/QKQueryGenericParameter.m b/Frameworks/QueryKit/Source/QKQueryGenericParameter.m new file mode 100644 index 00000000..8648dc0a --- /dev/null +++ b/Frameworks/QueryKit/Source/QKQueryGenericParameter.m @@ -0,0 +1,61 @@ +// +// $Id$ +// +// QKQueryGenericParameter.m +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on July 8, 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 "QKQueryGenericParameter.h" + +@implementation QKQueryGenericParameter + +@synthesize _field; +@synthesize _useQuotes; +@synthesize _value; + +#pragma mark - +#pragma mark Initialisation + +- (id)init +{ + if ((self = [super init])) { + [self setUseQuotes:YES]; + } + + return self; +} + +#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/QKQueryParameter.h b/Frameworks/QueryKit/Source/QKQueryParameter.h index 18f87929..b2436468 100644 --- a/Frameworks/QueryKit/Source/QKQueryParameter.h +++ b/Frameworks/QueryKit/Source/QKQueryParameter.h @@ -29,6 +29,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #import "QKQueryOperators.h" +#import "QKQueryGenericParameter.h" /** * @class QKQueryParameter QKQueryParameter.h @@ -37,30 +38,16 @@ * * QueryKit query parameter class. */ -@interface QKQueryParameter : NSObject -{ - NSString *_field; - +@interface QKQueryParameter : QKQueryGenericParameter +{ QKQueryOperator _operator; - - id _value; } -/** - * @property _field The field component of the parameter. - */ -@property (readwrite, retain, getter=field, setter=setField:) NSString *_field; - /** * @property _operator The operator component of the parameter. */ @property (readwrite, assign, getter=operator, setter=setOperator:) QKQueryOperator _operator; -/** - *@property _value The value component of the parameter. - */ -@property (readwrite, retain, getter=value, setter=setValue:) id _value; - + (QKQueryParameter *)queryParamWithField:(NSString *)field operator:(QKQueryOperator)op value:(id)value; - (id)initParamWithField:(NSString *)field operator:(QKQueryOperator)op value:(id)value; diff --git a/Frameworks/QueryKit/Source/QKQueryParameter.m b/Frameworks/QueryKit/Source/QKQueryParameter.m index c95bf585..c4f9cc29 100644 --- a/Frameworks/QueryKit/Source/QKQueryParameter.m +++ b/Frameworks/QueryKit/Source/QKQueryParameter.m @@ -30,12 +30,11 @@ #import "QKQueryParameter.h" #import "QKQueryUtilities.h" +#import "QKQueryConstants.h" @implementation QKQueryParameter -@synthesize _field; @synthesize _operator; -@synthesize _value; #pragma mark - #pragma mark Initialisation @@ -64,21 +63,11 @@ NSString *field = [_field stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - [string appendString:field]; + [string appendFormat:@"%@%@%@", [self useQuotes] ? QUERY_QUOTE : EMPTY_STRING, field, [self useQuotes] ? QUERY_QUOTE : EMPTY_STRING]; [string appendFormat:@" %@ ", [QKQueryUtilities operatorRepresentationForType:_operator]]; - [string appendFormat:(![_value isKindOfClass:[NSNumber class]]) ? @"'%@'" : @"%@", [_value description]]; + [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/QKQueryUpdateParameter.h b/Frameworks/QueryKit/Source/QKQueryUpdateParameter.h index 5231d272..d8f01310 100644 --- a/Frameworks/QueryKit/Source/QKQueryUpdateParameter.h +++ b/Frameworks/QueryKit/Source/QKQueryUpdateParameter.h @@ -28,6 +28,8 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. +#import "QKQueryGenericParameter.h" + /** * @class QKQueryUpdateParameter QKQueryUpdateParameter.h * @@ -35,22 +37,7 @@ * * 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; +@interface QKQueryUpdateParameter : QKQueryGenericParameter + (QKQueryUpdateParameter *)queryUpdateParamWithField:(NSString *)field value:(id)value; diff --git a/Frameworks/QueryKit/Source/QKQueryUpdateParameter.m b/Frameworks/QueryKit/Source/QKQueryUpdateParameter.m index febbfa44..8df3aba5 100644 --- a/Frameworks/QueryKit/Source/QKQueryUpdateParameter.m +++ b/Frameworks/QueryKit/Source/QKQueryUpdateParameter.m @@ -29,12 +29,10 @@ // OTHER DEALINGS IN THE SOFTWARE. #import "QKQueryUpdateParameter.h" +#import "QKQueryConstants.h" @implementation QKQueryUpdateParameter -@synthesize _field; -@synthesize _value; - #pragma mark - #pragma mark Initialisation @@ -61,21 +59,11 @@ NSString *field = [_field stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - [string appendString:field]; + [string appendFormat:@"%@%@%@", [self useQuotes] ? QUERY_QUOTE : EMPTY_STRING, field, [self useQuotes] ? QUERY_QUOTE : EMPTY_STRING]; [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 -- cgit v1.2.3