Index: src/PGConnection.h ================================================================== --- src/PGConnection.h +++ src/PGConnection.h @@ -27,10 +27,12 @@ #import "PGResult.h" OF_ASSUME_NONNULL_BEGIN +typedef OFDictionary OF_GENERIC(OFString *, id) *PGRow; + @interface PGConnection: OFObject { PGconn *_connection; OFDictionary OF_GENERIC(OFString *, OFString *) *_parameters; } @@ -41,13 +43,12 @@ - (void)connect; - (void)reset; - (void)close; - (nullable PGResult *)executeCommand: (OFConstantString *)command; - (nullable PGResult *)executeCommand: (OFConstantString *)command - parameters: (id)firstParameter, ... OF_SENTINEL; -- (void)insertRow: (OFDictionary *)row - intoTable: (OFString *)table; -- (void)insertRows: (OFArray OF_GENERIC(OFDictionary *) *)rows + parameters: (id)firstParameter, ... OF_SENTINEL; +- (void)insertRow: (PGRow)row intoTable: (OFString *)table; +- (void)insertRows: (OFArray OF_GENERIC(PGRow) *)rows intoTable: (OFString *)table; @end OF_ASSUME_NONNULL_END Index: src/PGConnection.m ================================================================== --- src/PGConnection.m +++ src/PGConnection.m @@ -28,12 +28,11 @@ #import "PGConnectionFailedException.h" #import "PGCommandFailedException.h" @implementation PGConnection -@synthesize pg_connection = _connection; -@synthesize parameters = _parameters; +@synthesize pg_connection = _connection, parameters = _parameters; - (void)dealloc { [_parameters release]; @@ -121,33 +120,29 @@ va_start(args, parameter); va_copy(args2, args); for (argsCount = 1; va_arg(args2, id) != nil; argsCount++); - values = [self allocMemoryWithSize: sizeof(*values) - count: argsCount]; + values = OFAllocMemory(argsCount, sizeof(*values)); @try { size_t i = 0; do { if ([parameter isKindOfClass: [OFString class]]) values[i++] = [parameter UTF8String]; else if ([parameter isKindOfClass: [OFNumber class]]) { OFNumber *number = parameter; - switch (number.type) { - case OF_NUMBER_TYPE_BOOL: + if (strcmp(number.objCType, + @encode(bool)) == 0) { if (number.boolValue) values[i++] = "t"; else values[i++] = "f"; - break; - default: + } else values[i++] = number.description.UTF8String; - break; - } } else if ([parameter isKindOfClass: [OFNull class]]) values[i++] = NULL; else values[i++] = [parameter description].UTF8String; @@ -154,11 +149,11 @@ } while ((parameter = va_arg(args, id)) != nil); result = PQexecParams(_connection, command.UTF8String, argsCount, NULL, values, NULL, NULL, 0); } @finally { - [self freeMemory: values]; + OFFreeMemory(values); } objc_autoreleasePoolPop(pool); switch (PQresultStatus(result)) { @@ -173,12 +168,11 @@ exceptionWithConnection: self command: command]; } } -- (void)insertRow: (OFDictionary *)row - intoTable: (OFString *)table +- (void)insertRow: (PGRow)row intoTable: (OFString *)table { void *pool = objc_autoreleasePoolPush(); OFMutableString *command; OFEnumerator *enumerator; const char **values; @@ -203,12 +197,11 @@ i++; } [command appendString: @") VALUES ("]; - values = [self allocMemoryWithSize: sizeof(*values) - count: count]; + values = OFAllocMemory(count, sizeof(*values)); @try { i = 0; enumerator = [row objectEnumerator]; while ((value = [enumerator nextObject]) != nil) { if (i > 0) @@ -222,11 +215,11 @@ [command appendString: @")"]; result = PQexecParams(_connection, command.UTF8String, (int)count, NULL, values, NULL, NULL, 0); } @finally { - [self freeMemory: values]; + OFFreeMemory(values); } objc_autoreleasePoolPop(pool); if (PQresultStatus(result) != PGRES_COMMAND_OK) { @@ -237,13 +230,12 @@ } PQclear(result); } -- (void)insertRows: (OFArray OF_GENERIC(OFDictionary *) *)rows +- (void)insertRows: (OFArray OF_GENERIC(PGRow) *)rows intoTable: (OFString *)table { for (OFDictionary *row in rows) - [self insertRow: row - intoTable: table]; + [self insertRow: row intoTable: table]; } @end Index: src/PGResult.m ================================================================== --- src/PGResult.m +++ src/PGResult.m @@ -59,9 +59,8 @@ - (id)objectAtIndex: (size_t)index { if (index > PQntuples(_result)) @throw [OFOutOfRangeException exception]; - return [PGResultRow pg_rowWithResult: self - row: (int)index]; + return [PGResultRow pg_rowWithResult: self row: (int)index]; } @end Index: src/PGResultRow+Private.h ================================================================== --- src/PGResultRow+Private.h +++ src/PGResultRow+Private.h @@ -24,12 +24,10 @@ #import "PGResultRow.h" OF_ASSUME_NONNULL_BEGIN @interface PGResultRow () -+ (instancetype)pg_rowWithResult: (PGResult *)result - row: (int)row; -- (instancetype)pg_initWithResult: (PGResult *)result - row: (int)row; ++ (instancetype)pg_rowWithResult: (PGResult *)result row: (int)row; +- (instancetype)pg_initWithResult: (PGResult *)result row: (int)row; @end OF_ASSUME_NONNULL_END Index: src/PGResultRow.m ================================================================== --- src/PGResultRow.m +++ src/PGResultRow.m @@ -32,18 +32,18 @@ if ([string isEqual: @"t"]) return [OFNumber numberWithBool: YES]; else return [OFNumber numberWithBool: NO]; case 21: /* INT2OID */ - return [OFNumber numberWithInt16: - (int16_t)string.decimalValue]; + return [OFNumber numberWithShort: + (short)[string longLongValueWithBase: 10]]; case 23: /* INT4OID */ - return [OFNumber numberWithInt32: - (int32_t)string.decimalValue]; + return [OFNumber numberWithLong: + (long)[string longLongValueWithBase: 10]]; case 20: /* INT8OID */ - return [OFNumber numberWithInt64: - (int64_t)string.decimalValue]; + return [OFNumber numberWithLongLong: + [string longLongValueWithBase: 10]]; case 700: /* FLOAT4OID */ return [OFNumber numberWithFloat: string.floatValue]; case 701: /* FLOAT8OID */ return [OFNumber numberWithDouble: string.doubleValue]; } @@ -56,30 +56,26 @@ PGResult *_result; PGresult *_res; int _row, _pos, _count; } -- initWithResult: (PGResult*)result - row: (int)row; +- initWithResult: (PGResult*)result row: (int)row; @end @interface PGResultRowKeyEnumerator: PGResultRowEnumerator @end @interface PGResultRowObjectEnumerator: PGResultRowEnumerator @end @implementation PGResultRow -+ (instancetype)rowWithResult: (PGResult *)result - row: (int)row ++ (instancetype)rowWithResult: (PGResult *)result row: (int)row { - return [[[self alloc] initWithResult: result - row: row] autorelease]; + return [[[self alloc] initWithResult: result row: row] autorelease]; } -- (instancetype)initWithResult: (PGResult *)result - row: (int)row +- (instancetype)initWithResult: (PGResult *)result row: (int)row { self = [super init]; _result = [result retain]; _res = result.pg_result; @@ -134,11 +130,11 @@ return [[[PGResultRowObjectEnumerator alloc] initWithResult: _result row: _row] autorelease]; } -- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state +- (int)countByEnumeratingWithState: (OFFastEnumerationState *)state objects: (id *)objects count: (int)count { int i, j; @@ -168,12 +164,11 @@ return j; } @end @implementation PGResultRowEnumerator -- (instancetype)initWithResult: (PGResult *)result - row: (int)row +- (instancetype)initWithResult: (PGResult *)result row: (int)row { self = [super init]; _result = [result retain]; _res = result.pg_result; Index: tests/tests.m ================================================================== --- tests/tests.m +++ tests/tests.m @@ -67,18 +67,18 @@ [_connection insertRow: [OFDictionary dictionaryWithKeysAndObjects: @"content", @"Hallo!", @"name", @"foo", nil] intoTable: @"test"]; result = [_connection executeCommand: @"SELECT * FROM test"]; - of_log(@"%@", result); - of_log(@"JSON: %@", [result JSONRepresentation]); + OFLog(@"%@", result); + OFLog(@"JSON: %@", [result JSONRepresentation]); for (id row in result) for (id col in row) - of_log(@"%@", col); + OFLog(@"%@", col); result = [_connection executeCommand: @"SELECT COUNT(*) FROM test"]; - of_log(@"%@", result); + OFLog(@"%@", result); [OFApplication terminate]; } @end