diff options
author | stuconnolly <stuart02@gmail.com> | 2012-09-10 11:09:56 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2012-09-10 11:09:56 +0000 |
commit | 7dd33a85a537c410cd3b31d661731b1f8d1e7d59 (patch) | |
tree | 2881a6fc037767140f0d0a0eaed5000a58c98c09 /Frameworks/PostgresKit/Source | |
parent | 12ad4fae862bc157cd133b42ed8f204bc273058a (diff) | |
download | sequelpro-7dd33a85a537c410cd3b31d661731b1f8d1e7d59.tar.gz sequelpro-7dd33a85a537c410cd3b31d661731b1f8d1e7d59.tar.bz2 sequelpro-7dd33a85a537c410cd3b31d661731b1f8d1e7d59.zip |
Rework type handling; reducing the number of paremeters being passed around as well as libpq calls.
Diffstat (limited to 'Frameworks/PostgresKit/Source')
10 files changed, 118 insertions, 105 deletions
diff --git a/Frameworks/PostgresKit/Source/FLXConstants.h b/Frameworks/PostgresKit/Source/FLXConstants.h index 95ab17f6..b3aeaed9 100644 --- a/Frameworks/PostgresKit/Source/FLXConstants.h +++ b/Frameworks/PostgresKit/Source/FLXConstants.h @@ -20,14 +20,6 @@ // License for the specific language governing permissions and limitations under // the License. -// Result set row types -typedef enum -{ - FLXPostgresResultRowAsArray = 1, - FLXPostgresResultRowAsDictionary = 2 -} -FLXPostgresResultRowType; - // Defaults extern NSString *FLXPostgresConnectionDefaultEncoding; extern NSString *FLXPostgresConnectionErrorDomain; @@ -39,3 +31,14 @@ extern NSString *FLXPostgresParameterClientEncoding; extern NSString *FLXPostgresParameterSuperUser; extern NSString *FLXPostgresParameterTimeZone; extern NSString *FLXPostgresParameterIntegerDateTimes; + +// Value specifiers +extern const char *FLXPostgresResultValueMacAddr; +extern const char *FLXPostgresResultValueInet; +extern const char *FLXPostgresResultValueCidr; +extern const char *FLXPostgresResultValueDate; +extern const char *FLXPostgresResultValueTime; +extern const char *FLXPostgresResultValueTimeTZ; +extern const char *FLXPostgresResultValueTimestamp; +extern const char *FLXPostgresResultValueTimestmpTZ; +extern const char *FLXPostgresResultValueInterval; diff --git a/Frameworks/PostgresKit/Source/FLXConstants.m b/Frameworks/PostgresKit/Source/FLXConstants.m index 3dc68ff9..14771072 100644 --- a/Frameworks/PostgresKit/Source/FLXConstants.m +++ b/Frameworks/PostgresKit/Source/FLXConstants.m @@ -29,3 +29,13 @@ NSString *FLXPostgresParameterClientEncoding = @"client_encoding"; NSString *FLXPostgresParameterSuperUser = @"is_superuser"; NSString *FLXPostgresParameterTimeZone = @"TimeZone"; NSString *FLXPostgresParameterIntegerDateTimes = @"integer_datetimes"; + +const char *FLXPostgresResultValueMacAddr = "%macaddr"; +const char *FLXPostgresResultValueInet = "%inet"; +const char *FLXPostgresResultValueCidr = "%cidr"; +const char *FLXPostgresResultValueDate = "%date"; +const char *FLXPostgresResultValueTime = "%time"; +const char *FLXPostgresResultValueTimeTZ = "%timetz"; +const char *FLXPostgresResultValueTimestamp = "%timestamp"; +const char *FLXPostgresResultValueTimestmpTZ = "%timestamptz"; +const char *FLXPostgresResultValueInterval = "%interval"; diff --git a/Frameworks/PostgresKit/Source/FLXPostgresResult.h b/Frameworks/PostgresKit/Source/FLXPostgresResult.h index b779e1b1..4148ba3d 100644 --- a/Frameworks/PostgresKit/Source/FLXPostgresResult.h +++ b/Frameworks/PostgresKit/Source/FLXPostgresResult.h @@ -20,10 +20,16 @@ // License for the specific language governing permissions and limitations under // the License. -#import "FLXConstants.h" - @class FLXPostgresConnection; +// Result set row types +typedef enum +{ + FLXPostgresResultRowAsArray = 1, + FLXPostgresResultRowAsDictionary = 2 +} +FLXPostgresResultRowType; + @interface FLXPostgresResult : NSObject { void *_result; diff --git a/Frameworks/PostgresKit/Source/FLXPostgresResult.m b/Frameworks/PostgresKit/Source/FLXPostgresResult.m index 0d9656d0..9fd44d8c 100644 --- a/Frameworks/PostgresKit/Source/FLXPostgresResult.m +++ b/Frameworks/PostgresKit/Source/FLXPostgresResult.m @@ -215,9 +215,18 @@ static NSString *FLXPostgresResultError = @"FLXPostgresResultError"; if (!bytes || !length) return nil; return [NSData dataWithBytes:bytes length:length]; - } + } + + [handler setRow:row]; + [handler setType:type]; + [handler setColumn:column]; + [handler setResult:_result]; + + id object = [handler objectFromResult]; + + [handler setResult:nil]; - return [handler objectFromResult:_result atRow:row column:column]; + return object; } /** diff --git a/Frameworks/PostgresKit/Source/FLXPostgresTypeDateTimeHandler.m b/Frameworks/PostgresKit/Source/FLXPostgresTypeDateTimeHandler.m index 25b74b4c..3fe08d97 100644 --- a/Frameworks/PostgresKit/Source/FLXPostgresTypeDateTimeHandler.m +++ b/Frameworks/PostgresKit/Source/FLXPostgresTypeDateTimeHandler.m @@ -43,18 +43,23 @@ static FLXPostgresOid FLXPostgresTypeDateTimeTypes[] = @interface FLXPostgresTypeDateTimeHandler () -- (NSDate *)_dateFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column; -- (id)_timeIntervalFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column; - -- (id)_timeFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column type:(FLXPostgresOid)type; -- (id)_timestmpFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column type:(FLXPostgresOid)type; +- (id)_timeFromResult; +- (id)_timestmpFromResult; +- (id)_timeIntervalFromResult; +- (NSDate *)_dateFromResult; - (NSDate *)_dateFromComponents:(NSDateComponents *)components; @end @implementation FLXPostgresTypeDateTimeHandler +@synthesize row = _row; +@synthesize type = _type; +@synthesize column = _column; +@synthesize result = _result; +@synthesize connection = _connection; + #pragma mark - #pragma mark Protocol Implementation @@ -73,23 +78,21 @@ static FLXPostgresOid FLXPostgresTypeDateTimeTypes[] = return nil; } -- (id)objectFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column -{ - FLXPostgresOid type = PQftype(result, column); - - switch (type) +- (id)objectFromResult +{ + switch (_type) { case FLXPostgresOidDate: - return [self _dateFromResult:result atRow:row column:column]; + return [self _dateFromResult]; case FLXPostgresOidTime: case FLXPostgresOidTimeTZ: case FLXPostgresOidAbsTime: - return [self _timeFromResult:result atRow:row column:column type:type]; + return [self _timeFromResult]; case FLXPostgresOidTimestamp: case FLXPostgresOidTimestampTZ: - return [self _timestmpFromResult:result atRow:row column:column type:type]; + return [self _timestmpFromResult]; case FLXPostgresOidInterval: - return [self _timeIntervalFromResult:result atRow:row column:column]; + return [self _timeIntervalFromResult]; default: return [NSNull null]; } @@ -100,18 +103,14 @@ static FLXPostgresOid FLXPostgresTypeDateTimeTypes[] = /** * Returns an NSDate created from a date value. - * - * @param result The result to extract the value from. - * @param row The row to extract the value from. - * @param column The column to extract the value from. * * @return The NSDate representation. */ -- (id)_dateFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column +- (id)_dateFromResult { PGdate date; - if (!PQgetf(result, row, "%date", column, &date)) return [NSNull null]; + if (!PQgetf(_result, _row, FLXPostgresResultValueDate, _column, &date)) return [NSNull null]; NSDateComponents *components = [[NSDateComponents alloc] init]; @@ -124,18 +123,14 @@ static FLXPostgresOid FLXPostgresTypeDateTimeTypes[] = /** * Converts a time interval value to a FLXPostgresTimeInterval instance. - * - * @param result The result to extract the value from. - * @param row The row to extract the value from. - * @param column The column to extract the value from. * * @return The FLXPostgresTimeInterval representation. */ -- (id)_timeIntervalFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column +- (id)_timeIntervalFromResult { PGinterval interval; - if (!PQgetf(result, row, "%interval", column, &interval)) return [NSNull null]; + if (!PQgetf(_result, _row, FLXPostgresResultValueInterval, _column, &interval)) return [NSNull null]; return [FLXPostgresTimeInterval intervalWithPGInterval:&interval]; } @@ -145,20 +140,15 @@ static FLXPostgresOid FLXPostgresTypeDateTimeTypes[] = * * @note The date part should be ignored as it's set to a default value. * - * @param result The result to extract the value from. - * @param row The row to extract the value from. - * @param column The column to extract the value from. - * @type type The type to be converted from (handles times and times with a time zone). - * * @return The object representation. */ -- (id)_timeFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column type:(FLXPostgresOid)type +- (id)_timeFromResult { PGtime time; - BOOL hasTimeZone = type == FLXPostgresOidTimeTZ; + BOOL hasTimeZone = _type == FLXPostgresOidTimeTZ; - if (!PQgetf(result, row, hasTimeZone ? "%timetz" : "%time", column, &time)) return [NSNull null]; + if (!PQgetf(_result, _row, hasTimeZone ? FLXPostgresResultValueTimeTZ : FLXPostgresResultValueTime, _column, &time)) return [NSNull null]; NSDateComponents *components = [[NSDateComponents alloc] init]; @@ -179,20 +169,15 @@ static FLXPostgresOid FLXPostgresTypeDateTimeTypes[] = /** * Returns a native object created from a timestamp value. * - * @param result The result to extract the value from. - * @param row The row to extract the value from. - * @param column The column to extract the value from. - * @type type The type to be converted from (handles timestamps and timestamps with a time zone). - * * @return The object representation. */ -- (id)_timestmpFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column type:(FLXPostgresOid)type +- (id)_timestmpFromResult { PGtimestamp timestamp; - BOOL hasTimeZone = type == FLXPostgresOidTimestampTZ; + BOOL hasTimeZone = _type == FLXPostgresOidTimestampTZ; - if (!PQgetf(result, row, hasTimeZone ? "%timstamptz" : "%timestamp", column, ×tamp)) return [NSNull null]; + if (!PQgetf(_result, _row, hasTimeZone ? FLXPostgresResultValueTimestmpTZ : FLXPostgresResultValueTimestamp, _column, ×tamp)) return [NSNull null]; FLXPostgresTimeTZ *timestampTZ = nil; NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp.epoch]; diff --git a/Frameworks/PostgresKit/Source/FLXPostgresTypeHandler.h b/Frameworks/PostgresKit/Source/FLXPostgresTypeHandler.h index 1b3146b8..7c639100 100644 --- a/Frameworks/PostgresKit/Source/FLXPostgresTypeHandler.h +++ b/Frameworks/PostgresKit/Source/FLXPostgresTypeHandler.h @@ -30,6 +30,8 @@ const PGresult *_result; + FLXPostgresOid _type; + FLXPostgresConnection *_connection; } diff --git a/Frameworks/PostgresKit/Source/FLXPostgresTypeHandler.m b/Frameworks/PostgresKit/Source/FLXPostgresTypeHandler.m index a7b98909..41903d91 100644 --- a/Frameworks/PostgresKit/Source/FLXPostgresTypeHandler.m +++ b/Frameworks/PostgresKit/Source/FLXPostgresTypeHandler.m @@ -30,6 +30,7 @@ - (id)initWithConnection:(FLXPostgresConnection *)connection { if ((self = [super init])) { + _type = -1; _result = nil; _connection = [connection retain]; } @@ -41,6 +42,8 @@ - (void)dealloc { + _result = nil; + if (_connection) [_connection release], _connection = nil; [super dealloc]; diff --git a/Frameworks/PostgresKit/Source/FLXPostgresTypeHandlerProtocol.h b/Frameworks/PostgresKit/Source/FLXPostgresTypeHandlerProtocol.h index a0c04c26..8b22c158 100644 --- a/Frameworks/PostgresKit/Source/FLXPostgresTypeHandlerProtocol.h +++ b/Frameworks/PostgresKit/Source/FLXPostgresTypeHandlerProtocol.h @@ -27,6 +27,14 @@ */ @protocol FLXPostgresTypeHandlerProtocol +@property (readwrite, assign) NSUInteger row; + +@property (readwrite, assign) NSUInteger column; + +@property (readwrite, assign) FLXPostgresOid type; + +@property (readwrite, assign) const PGresult *result; + /** * The remote type values handled by this class (terminated by 0). * @@ -51,12 +59,8 @@ /** * Convert the value at the specified row and column in the supplied result to a native object. * - * @param result The result to extract the value from. - * @param row The row to extract the value from. - * @param column The column to extract the value from. - * * @return An object represenation of the data. */ -- (id)objectFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column; +- (id)objectFromResult; @end diff --git a/Frameworks/PostgresKit/Source/FLXPostgresTypeNumberHandler.m b/Frameworks/PostgresKit/Source/FLXPostgresTypeNumberHandler.m index 467a8821..25fd3839 100644 --- a/Frameworks/PostgresKit/Source/FLXPostgresTypeNumberHandler.m +++ b/Frameworks/PostgresKit/Source/FLXPostgresTypeNumberHandler.m @@ -48,6 +48,12 @@ static FLXPostgresOid FLXPostgresTypeNumberTypes[] = @implementation FLXPostgresTypeNumberHandler +@synthesize row = _row; +@synthesize type = _type; +@synthesize column = _column; +@synthesize result = _result; +@synthesize connection = _connection; + #pragma mark - #pragma mark Integer @@ -145,15 +151,14 @@ static FLXPostgresOid FLXPostgresTypeNumberTypes[] = return nil; } -- (id)objectFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column -{ - FLXPostgresOid type = PQftype(result, column); - NSUInteger length = PQgetlength(result, row, column); - const void *bytes = PQgetvalue(result, row, column); +- (id)objectFromResult +{ + NSUInteger length = PQgetlength(_result, _row, _column); + const void *bytes = PQgetvalue(_result, _row, _column); - if (!bytes || !length) return nil; + if (!bytes || !length) return [NSNull null]; - switch (type) + switch (_type) { case FLXPostgresOidInt8: case FLXPostgresOidInt2: diff --git a/Frameworks/PostgresKit/Source/FLXPostgresTypeStringHandler.m b/Frameworks/PostgresKit/Source/FLXPostgresTypeStringHandler.m index 2c49ee64..dab3dd41 100644 --- a/Frameworks/PostgresKit/Source/FLXPostgresTypeStringHandler.m +++ b/Frameworks/PostgresKit/Source/FLXPostgresTypeStringHandler.m @@ -45,15 +45,20 @@ static FLXPostgresOid FLXPostgresTypeStringTypes[] = @interface FLXPostgresTypeStringHandler () -- (id)_stringFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column; -- (id)_macAddressFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column; -- (id)_inetAddressFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column type:(FLXPostgresOid)type; +- (id)_stringFromResult; +- (id)_macAddressFromResult; +- (id)_inetAddressFromResult; @end - @implementation FLXPostgresTypeStringHandler +@synthesize row = _row; +@synthesize type = _type; +@synthesize column = _column; +@synthesize result = _result; +@synthesize connection = _connection; + #pragma mark - #pragma mark Protocol Implementation @@ -72,21 +77,10 @@ static FLXPostgresOid FLXPostgresTypeStringTypes[] = return [NSArray arrayWithObject:@"NSCFString"]; } -- (id)objectFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column -{ - FLXPostgresOid type = PQftype(result, column); - - _row = row; - _column = column; - _result = result; - - switch (type) +- (id)objectFromResult +{ + switch (_type) { - case FLXPostgresOidMacAddr: - return [self _macAddressFromResult:result atRow:row column:column]; - case FLXPostgresOidInetAddr: - case FLXPostgresOidCidrAddr: - return [self _inetAddressFromResult:result atRow:row column:column type:type]; case FLXPostgresOidText: case FLXPostgresOidChar: case FLXPostgresOidName: @@ -97,7 +91,12 @@ static FLXPostgresOid FLXPostgresTypeStringTypes[] = case FLXPostgresOidBit: case FLXPostgresOidVarBit: case FLXPostgresOidUnknown: - return [self _stringFromResult:result atRow:row column:column]; + return [self _stringFromResult]; + case FLXPostgresOidMacAddr: + return [self _macAddressFromResult]; + case FLXPostgresOidInetAddr: + case FLXPostgresOidCidrAddr: + return [self _inetAddressFromResult]; default: return [NSNull null]; } @@ -109,16 +108,12 @@ static FLXPostgresOid FLXPostgresTypeStringTypes[] = /** * Converts a char value to a string. * - * @param result The result to extract the value from. - * @param row The row to extract the value from. - * @param column The column to extract the value from. - * * @return A string representation of the value. */ -- (id)_stringFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column +- (id)_stringFromResult { - const void *bytes = PQgetvalue(result, row, column); - NSUInteger length = PQgetlength(result, row, column); + const void *bytes = PQgetvalue(_result, _row, _column); + NSUInteger length = PQgetlength(_result, _row, _column); if (!bytes || !length) return [NSNull null]; @@ -128,17 +123,13 @@ static FLXPostgresOid FLXPostgresTypeStringTypes[] = /** * Converts a MAC address value to a string. * - * @param result The result to extract the value from. - * @param row The row to extract the value from. - * @param column The column to extract the value from. - * * @return A string representation of the MAC address. */ -- (id)_macAddressFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column +- (id)_macAddressFromResult { PGmacaddr address; - if (!PQgetf(result, row, "%macaddr", column, &address)) return [NSNull null]; + if (!PQgetf(_result, _row, FLXPostgresResultValueMacAddr, _column, &address)) return [NSNull null]; return [NSString stringWithFormat:@"%02d:%02d:%02d:%02d:%02d:%02d", address.a, address.b, address.c, address.d, address.e, address.f]; } @@ -146,18 +137,13 @@ static FLXPostgresOid FLXPostgresTypeStringTypes[] = /** * Converts a network address value to a string. * - * @param result The result to extract the value from. - * @param row The row to extract the value from. - * @param column The column to extract the value from. - * @param type The type of the value to extract. - * * @return A string representation of the network address. */ -- (id)_inetAddressFromResult:(const PGresult *)result atRow:(NSUInteger)row column:(NSUInteger)column type:(FLXPostgresOid)type +- (id)_inetAddressFromResult { PGinet inet; - if (!PQgetf(result, row, type == FLXPostgresOidInetAddr ? "%inet" : "%cidr", column, &inet)) return [NSNull null]; + if (!PQgetf(_result, _row, _type == FLXPostgresOidInetAddr ? FLXPostgresResultValueInet : FLXPostgresResultValueCidr, _column, &inet)) return [NSNull null]; char ip[80]; struct sockaddr *sa = (struct sockaddr *)inet.sa_buf; |