diff options
author | stuconnolly <stuart02@gmail.com> | 2012-07-15 10:15:11 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2012-07-15 10:15:11 +0000 |
commit | cb00d18102f8c3f88e8a15bb4d9fb7825cf29a59 (patch) | |
tree | 8c266026ae92013dbc028a09d7d98e22bbcfbd5f /Frameworks/QueryKit/Source | |
parent | dd2c7e34d265c7a4a66b7583a186456095b545ee (diff) | |
download | sequelpro-cb00d18102f8c3f88e8a15bb4d9fb7825cf29a59.tar.gz sequelpro-cb00d18102f8c3f88e8a15bb4d9fb7825cf29a59.tar.bz2 sequelpro-cb00d18102f8c3f88e8a15bb4d9fb7825cf29a59.zip |
Add a new order by class.
Diffstat (limited to 'Frameworks/QueryKit/Source')
-rw-r--r-- | Frameworks/QueryKit/Source/QKQuery.h | 13 | ||||
-rw-r--r-- | Frameworks/QueryKit/Source/QKQuery.m | 101 | ||||
-rw-r--r-- | Frameworks/QueryKit/Source/QKQueryConstruct.h | 55 | ||||
-rw-r--r-- | Frameworks/QueryKit/Source/QKQueryConstruct.m | 60 | ||||
-rw-r--r-- | Frameworks/QueryKit/Source/QKQueryGenericParameter.h | 16 | ||||
-rw-r--r-- | Frameworks/QueryKit/Source/QKQueryGenericParameter.m | 16 | ||||
-rw-r--r-- | Frameworks/QueryKit/Source/QKQueryOrderBy.h | 60 | ||||
-rw-r--r-- | Frameworks/QueryKit/Source/QKQueryOrderBy.m | 81 | ||||
-rw-r--r-- | Frameworks/QueryKit/Source/QKQueryStringAdditions.m | 4 | ||||
-rw-r--r-- | Frameworks/QueryKit/Source/QueryKit.h | 1 |
10 files changed, 316 insertions, 91 deletions
diff --git a/Frameworks/QueryKit/Source/QKQuery.h b/Frameworks/QueryKit/Source/QKQuery.h index 117da9b3..9eb0a621 100644 --- a/Frameworks/QueryKit/Source/QKQuery.h +++ b/Frameworks/QueryKit/Source/QKQuery.h @@ -31,8 +31,10 @@ #import "QKQueryTypes.h" #import "QKQueryDatabases.h" #import "QKQueryOperators.h" -#import "QKQueryParameter.h" -#import "QKQueryUpdateParameter.h" + +@class QKQueryOrderBy; +@class QKQueryParameter; +@class QKQueryUpdateParameter; /** * @class QKQuery QKQuery.h @@ -59,7 +61,6 @@ QKQueryDatabase _queryDatabase; BOOL _useQuotedIdentifiers; - BOOL _orderDescending; } /** @@ -100,7 +101,7 @@ /** * @property _useQuotedIdentifiers Indicates whether or not the query's fields should be quoted. */ -@property(readwrite, assign, getter=useQuotedIdentifiers) BOOL _useQuotedIdentifiers; +@property(readwrite, assign, getter=useQuotedIdentifiers, setter=setUseQuotedIdentifiers:) BOOL _useQuotedIdentifiers; /** * @property _groupByFields The group by fields of the query. @@ -129,8 +130,6 @@ - (NSString *)query; - (void)clear; -- (void)setUseQuotedIdentifiers:(BOOL)quote; - - (void)addField:(NSString *)field; - (void)addFields:(NSArray *)fields; @@ -143,7 +142,7 @@ - (void)groupByField:(NSString *)field; - (void)groupByFields:(NSArray *)fields; +- (void)orderBy:(QKQueryOrderBy *)orderBy; - (void)orderByField:(NSString *)field descending:(BOOL)descending; -- (void)orderByFields:(NSArray *)fields descending:(BOOL)descending; @end diff --git a/Frameworks/QueryKit/Source/QKQuery.m b/Frameworks/QueryKit/Source/QKQuery.m index 3834bd82..01dfa1fc 100644 --- a/Frameworks/QueryKit/Source/QKQuery.m +++ b/Frameworks/QueryKit/Source/QKQuery.m @@ -29,8 +29,12 @@ // OTHER DEALINGS IN THE SOFTWARE. #import "QKQuery.h" +#import "QKQueryTypes.h" +#import "QKQueryOrderBy.h" #import "QKQueryUtilities.h" #import "QKQueryConstants.h" +#import "QKQueryParameter.h" +#import "QKQueryUpdateParameter.h" #import "QKQueryGenericParameter.h" static NSString *QKNoQueryTypeException = @"QKNoQueryType"; @@ -39,6 +43,7 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; @interface QKQuery () - (void)_validateRequiements; +- (void)_configureQuoteIdentifiers; - (NSString *)_buildQuery; - (NSString *)_buildFieldList; @@ -117,7 +122,6 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; // Default to MySQL [self setQueryDatabase:QKDatabaseMySQL]; - _orderDescending = NO; _identifierQuote = EMPTY_STRING; _groupByFields = [[NSMutableArray alloc] init]; @@ -165,29 +169,6 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; } #pragma mark - -#pragma mark Accessors - -/** - * Sets whether to quote identifiers in the query. - * - * @param quote A BOOL indicating whether quoting should be used. - */ -- (void)setUseQuotedIdentifiers:(BOOL)quote -{ - _useQuotedIdentifiers = quote; - - for (QKQueryParameter *param in _parameters) - { - [param setUseQuotedIdentifier:_useQuotedIdentifiers]; - } - - for (QKQueryUpdateParameter *param in _updateParameters) - { - [param setUseQuotedIdentifier:_useQuotedIdentifiers]; - } -} - -#pragma mark - #pragma mark Fields /** @@ -295,30 +276,26 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; #pragma mark Ordering /** - * Adds the supplied field to the query's ORDER BY clause. + * Adds the supplied order by instance to this query. * - * @param field The field to ORDER BY. - * @param descending Indicates whether the ORDER BY should be descending. + * @param orderBy The order by instance to add. */ -- (void)orderByField:(NSString *)field descending:(BOOL)descending +- (void)orderBy:(QKQueryOrderBy *)orderBy { - _orderDescending = descending; - - [self _addString:field toArray:_orderByFields]; + if ([orderBy orderByField] && [[orderBy orderByField] length] > 0) { + [_orderByFields addObject:orderBy]; + } } /** - * Convenience method for adding more than one field to the query's ORDER BY clause. + * Convenience method for adding a field to ORDER BY. * - * @param fields An array of fields to ORDER BY. + * @param field The field to ORDER BY. * @param descending Indicates whether the ORDER BY should be descending. */ -- (void)orderByFields:(NSArray *)fields descending:(BOOL)descending +- (void)orderByField:(NSString *)field descending:(BOOL)descending { - for (NSString *field in fields) - { - [self orderByField:field descending:descending]; - } + [self orderBy:[QKQueryOrderBy orderByField:field descending:descending]]; } #pragma mark - @@ -339,6 +316,32 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; } /** + * Configures the query's parameters, update parameters and order by field's quote identifier settings. + */ +- (void)_configureQuoteIdentifiers +{ + _identifierQuote = [QKQueryUtilities identifierQuoteCharacterForDatabase:_queryDatabase]; + + for (QKQueryParameter *param in _parameters) + { + [param setIdentifierQuote:_identifierQuote]; + [param setUseQuotedIdentifier:_useQuotedIdentifiers]; + } + + for (QKQueryOrderBy *orderBy in _orderByFields) + { + [orderBy setIdentifierQuote:_identifierQuote]; + [orderBy setUseQuotedIdentifier:_useQuotedIdentifiers]; + } + + for (QKQueryUpdateParameter *param in _updateParameters) + { + [param setIdentifierQuote:_identifierQuote]; + [param setUseQuotedIdentifier:_useQuotedIdentifiers]; + } +} + +/** * Builds the actual query. * * @return The built SQL query. @@ -352,18 +355,8 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; BOOL isUpdate = _queryType == QKUpdateQuery; BOOL isDelete = _queryType == QKDeleteQuery; - if ([self useQuotedIdentifiers]) { - _identifierQuote = [QKQueryUtilities identifierQuoteCharacterForDatabase:_queryDatabase]; - - for (QKQueryParameter *param in _parameters) - { - [param setIdentifierQuote:_identifierQuote]; - } - - for (QKQueryUpdateParameter *param in _updateParameters) - { - [param setIdentifierQuote:_identifierQuote]; - } + if (_useQuotedIdentifiers) { + [self _configureQuoteIdentifiers]; } NSString *fields = [self _buildFieldList]; @@ -495,17 +488,15 @@ static NSString *QKNoQueryTableException = @"QKNoQueryTable"; [orderBy appendString:@"ORDER BY "]; - for (NSString *field in _orderByFields) + for (NSString *orderByField in _orderByFields) { - [orderBy appendFormat:@"%1$@%2$@%1$@, ", _identifierQuote, field]; + [orderBy appendFormat:@"%@, ", orderByField]; } if ([orderBy hasSuffix:@", "]) { [orderBy setString:[orderBy substringToIndex:([orderBy length] - 2)]]; } - - [orderBy appendString:_orderDescending ? @" DESC" : @" ASC"]; - + return orderBy; } diff --git a/Frameworks/QueryKit/Source/QKQueryConstruct.h b/Frameworks/QueryKit/Source/QKQueryConstruct.h new file mode 100644 index 00000000..be3ab220 --- /dev/null +++ b/Frameworks/QueryKit/Source/QKQueryConstruct.h @@ -0,0 +1,55 @@ +// +// $Id$ +// +// QKQueryConstruct.h +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on July 15, 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 QKQueryConstruct QKQueryConstruct.h + * + * @author Stuart Connolly http://stuconnolly.com/ + * + * Acts as a base of all SQL constructs and provides various common properties. + */ +@interface QKQueryConstruct : NSObject +{ + BOOL _useQuotedIdentifier; + + NSString *_identiferQuote; +} + +/** + * @property _identifierQuote The quoute character to use for identifiers. + */ +@property(readwrite, retain, getter=identifierQuote, setter=setIdentifierQuote:) NSString *_identiferQuote; + +/** + * @property _useQuotedIdentifier Indicates whether or not identifiers should be quoted. + */ +@property(readwrite, assign, getter=useQuotedIdentifier, setter=setUseQuotedIdentifier:) BOOL _useQuotedIdentifier; + +@end diff --git a/Frameworks/QueryKit/Source/QKQueryConstruct.m b/Frameworks/QueryKit/Source/QKQueryConstruct.m new file mode 100644 index 00000000..db432add --- /dev/null +++ b/Frameworks/QueryKit/Source/QKQueryConstruct.m @@ -0,0 +1,60 @@ +// +// $Id$ +// +// QKQueryConstruct.m +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on July 15, 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 "QKQueryConstruct.h" + +@implementation QKQueryConstruct + +@synthesize _useQuotedIdentifier; +@synthesize _identiferQuote; + +#pragma mark - +#pragma mark Initialisation + +- (id)init +{ + if ((self = [super init])) { + [self setUseQuotedIdentifier:YES]; + [self setIdentifierQuote:EMPTY_STRING]; + } + + return self; +} + +#pragma mark - + +- (void)dealloc +{ + if (_identiferQuote) [_identiferQuote release], _identiferQuote = nil; + + [super dealloc]; +} + +@end diff --git a/Frameworks/QueryKit/Source/QKQueryGenericParameter.h b/Frameworks/QueryKit/Source/QKQueryGenericParameter.h index 562689f5..1c782b6a 100644 --- a/Frameworks/QueryKit/Source/QKQueryGenericParameter.h +++ b/Frameworks/QueryKit/Source/QKQueryGenericParameter.h @@ -28,13 +28,13 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. -@interface QKQueryGenericParameter : NSObject +#import "QKQueryConstruct.h" + +@interface QKQueryGenericParameter : QKQueryConstruct { id _value; - BOOL _useQuotedIdentifier; NSString *_field; - NSString *_identiferQuote; } /** @@ -43,16 +43,6 @@ @property(readwrite, retain, getter=field, setter=setField:) NSString *_field; /** - * @property _identifierQuote - */ -@property(readwrite, retain, getter=identifierQuote, setter=setIdentifierQuote:) NSString *_identiferQuote; - -/** - * @property _useQuotedIdentifier Indicates whether or not this parameters field should be quoted. - */ -@property(readwrite, assign, getter=useQuotedIdentifier, setter=setUseQuotedIdentifier:) BOOL _useQuotedIdentifier; - -/** *@property _value The value component of the parameter. */ @property(readwrite, retain, getter=value, setter=setValue:) id _value; diff --git a/Frameworks/QueryKit/Source/QKQueryGenericParameter.m b/Frameworks/QueryKit/Source/QKQueryGenericParameter.m index efaf10d3..57fe58c7 100644 --- a/Frameworks/QueryKit/Source/QKQueryGenericParameter.m +++ b/Frameworks/QueryKit/Source/QKQueryGenericParameter.m @@ -28,29 +28,15 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. +#import "QKQueryConstruct.h" #import "QKQueryGenericParameter.h" @implementation QKQueryGenericParameter @synthesize _field; -@synthesize _useQuotedIdentifier; -@synthesize _identiferQuote; @synthesize _value; #pragma mark - -#pragma mark Initialisation - -- (id)init -{ - if ((self = [super init])) { - [self setUseQuotedIdentifier:YES]; - [self setIdentifierQuote:EMPTY_STRING]; - } - - return self; -} - -#pragma mark - - (void)dealloc { diff --git a/Frameworks/QueryKit/Source/QKQueryOrderBy.h b/Frameworks/QueryKit/Source/QKQueryOrderBy.h new file mode 100644 index 00000000..aa7a80e7 --- /dev/null +++ b/Frameworks/QueryKit/Source/QKQueryOrderBy.h @@ -0,0 +1,60 @@ +// +// $Id$ +// +// QKQueryOrderBy.h +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on July 15, 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 "QKQueryConstruct.h" + +/** + * @class QKQueryOrderBy QKQueryOrderBy.h + * + * @author Stuart Connolly http://stuconnolly.com/ + * + * QueryKit SELECT query ORDER BY class. + */ +@interface QKQueryOrderBy : QKQueryConstruct +{ + NSString *_orderByField; + BOOL _orderByDescending; +} + +/** + * @property _orderByField + */ +@property(readwrite, retain, getter=orderByField, setter=setOrderByField:) NSString *_orderByField; + +/** + * @property _orderByDescending + */ +@property(readwrite, assign, getter=orderByDescending, setter=setOrderByDescending:) BOOL _orderByDescending; + ++ (QKQueryOrderBy *)orderByField:(NSString *)field descending:(BOOL)descending; + +- (id)initWithField:(NSString *)field descending:(BOOL)descending; + +@end diff --git a/Frameworks/QueryKit/Source/QKQueryOrderBy.m b/Frameworks/QueryKit/Source/QKQueryOrderBy.m new file mode 100644 index 00000000..36b43248 --- /dev/null +++ b/Frameworks/QueryKit/Source/QKQueryOrderBy.m @@ -0,0 +1,81 @@ +// +// $Id$ +// +// QKQueryOrderBy.m +// QueryKit +// +// Created by Stuart Connolly (stuconnolly.com) on July 15, 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 "QKQueryOrderBy.h" + +@implementation QKQueryOrderBy + +@synthesize _orderByField; +@synthesize _orderByDescending; + +#pragma mark - +#pragma mark Initialisation + ++ (QKQueryOrderBy *)orderByField:(NSString *)field descending:(BOOL)descending +{ + return [[[QKQueryOrderBy alloc] initWithField:field descending:descending] autorelease]; +} + +- (id)init +{ + return [self initWithField:nil descending:NO]; +} + +- (id)initWithField:(NSString *)field descending:(BOOL)descending +{ + if ((self = [super init])) { + [self setOrderByField:field]; + [self setOrderByDescending:descending]; + } + + return self; +} + +#pragma mark - + +- (NSString *)description +{ + if (!_orderByField || [_orderByField length] == 0) return EMPTY_STRING; + + NSString *field = [_orderByField stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + return [NSString stringWithFormat:@"%1$@%2$@%1$@ %3$@", [self useQuotedIdentifier] ? _identiferQuote : EMPTY_STRING, field, _orderByDescending ? @"DESC" : @"ASC"]; +} + +#pragma mark - + +- (void)dealloc +{ + if (_orderByField) [_orderByField release], _orderByField = nil; + + [super dealloc]; +} + +@end diff --git a/Frameworks/QueryKit/Source/QKQueryStringAdditions.m b/Frameworks/QueryKit/Source/QKQueryStringAdditions.m index 7c87e458..d2167041 100644 --- a/Frameworks/QueryKit/Source/QKQueryStringAdditions.m +++ b/Frameworks/QueryKit/Source/QKQueryStringAdditions.m @@ -35,7 +35,9 @@ /** * Returns the string quoted with supplied character. * - * @return The quoted string + * @param character The character (as a string) to use. + * + * @return The quoted string. */ - (NSString *)quotedStringWithCharacter:(NSString *)character { diff --git a/Frameworks/QueryKit/Source/QueryKit.h b/Frameworks/QueryKit/Source/QueryKit.h index 624ba08b..0ae6b406 100644 --- a/Frameworks/QueryKit/Source/QueryKit.h +++ b/Frameworks/QueryKit/Source/QueryKit.h @@ -30,6 +30,7 @@ #import <QueryKit/QKQuery.h> #import <QueryKit/QKQueryTypes.h> +#import <QueryKit/QKQueryOrderBy.h> #import <QueryKit/QKQueryOperators.h> #import <QueryKit/QKQueryDatabases.h> #import <QueryKit/QKQueryParameter.h> |