From f4cee3b13957a73adcb8a492f79eddaa3532f195 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Thu, 29 Apr 2021 22:36:08 +0000 Subject: [PATCH] Adjust to ObjFW changes FossilOrigin-Name: 0af9225d647a95b3135fc711065584dcff39b523377528c734edc0ecb2b379f4 --- src/PGConnection.h | 9 +++++---- src/PGConnection.m | 30 +++++++++++------------------- src/PGResult.m | 3 +-- src/PGResultRow+Private.h | 6 ++---- src/PGResultRow.m | 29 ++++++++++++----------------- tests/tests.m | 8 ++++---- 6 files changed, 35 insertions(+), 50 deletions(-) diff --git a/src/PGConnection.h b/src/PGConnection.h index c770e82..06782bf 100644 --- a/src/PGConnection.h +++ b/src/PGConnection.h @@ -29,6 +29,8 @@ OF_ASSUME_NONNULL_BEGIN +typedef OFDictionary OF_GENERIC(OFString *, id) *PGRow; + @interface PGConnection: OFObject { PGconn *_connection; @@ -43,10 +45,9 @@ OF_ASSUME_NONNULL_BEGIN - (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 diff --git a/src/PGConnection.m b/src/PGConnection.m index d16c527..651b491 100644 --- a/src/PGConnection.m +++ b/src/PGConnection.m @@ -30,8 +30,7 @@ #import "PGCommandFailedException.h" @implementation PGConnection -@synthesize pg_connection = _connection; -@synthesize parameters = _parameters; +@synthesize pg_connection = _connection, parameters = _parameters; - (void)dealloc { @@ -123,8 +122,7 @@ 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; @@ -134,18 +132,15 @@ 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 @@ -156,7 +151,7 @@ result = PQexecParams(_connection, command.UTF8String, argsCount, NULL, values, NULL, NULL, 0); } @finally { - [self freeMemory: values]; + OFFreeMemory(values); } objc_autoreleasePoolPop(pool); @@ -175,8 +170,7 @@ } } -- (void)insertRow: (OFDictionary *)row - intoTable: (OFString *)table +- (void)insertRow: (PGRow)row intoTable: (OFString *)table { void *pool = objc_autoreleasePoolPush(); OFMutableString *command; @@ -205,8 +199,7 @@ [command appendString: @") VALUES ("]; - values = [self allocMemoryWithSize: sizeof(*values) - count: count]; + values = OFAllocMemory(count, sizeof(*values)); @try { i = 0; enumerator = [row objectEnumerator]; @@ -224,7 +217,7 @@ result = PQexecParams(_connection, command.UTF8String, (int)count, NULL, values, NULL, NULL, 0); } @finally { - [self freeMemory: values]; + OFFreeMemory(values); } objc_autoreleasePoolPop(pool); @@ -239,11 +232,10 @@ 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 diff --git a/src/PGResult.m b/src/PGResult.m index 3213f34..ee2a045 100644 --- a/src/PGResult.m +++ b/src/PGResult.m @@ -61,7 +61,6 @@ if (index > PQntuples(_result)) @throw [OFOutOfRangeException exception]; - return [PGResultRow pg_rowWithResult: self - row: (int)index]; + return [PGResultRow pg_rowWithResult: self row: (int)index]; } @end diff --git a/src/PGResultRow+Private.h b/src/PGResultRow+Private.h index 62b619a..aa5203b 100644 --- a/src/PGResultRow+Private.h +++ b/src/PGResultRow+Private.h @@ -26,10 +26,8 @@ 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 diff --git a/src/PGResultRow.m b/src/PGResultRow.m index 15a1592..2fbb3c1 100644 --- a/src/PGResultRow.m +++ b/src/PGResultRow.m @@ -34,14 +34,14 @@ convertType(PGresult *res, int column, OFString *string) 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 */ @@ -58,8 +58,7 @@ convertType(PGresult *res, int column, OFString *string) int _row, _pos, _count; } -- initWithResult: (PGResult*)result - row: (int)row; +- initWithResult: (PGResult*)result row: (int)row; @end @interface PGResultRowKeyEnumerator: PGResultRowEnumerator @@ -69,15 +68,12 @@ convertType(PGresult *res, int column, OFString *string) @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]; @@ -136,7 +132,7 @@ convertType(PGresult *res, int column, OFString *string) row: _row] autorelease]; } -- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state +- (int)countByEnumeratingWithState: (OFFastEnumerationState *)state objects: (id *)objects count: (int)count { @@ -170,8 +166,7 @@ convertType(PGresult *res, int column, OFString *string) @end @implementation PGResultRowEnumerator -- (instancetype)initWithResult: (PGResult *)result - row: (int)row +- (instancetype)initWithResult: (PGResult *)result row: (int)row { self = [super init]; diff --git a/tests/tests.m b/tests/tests.m index 1f3ea38..286b9ad 100644 --- a/tests/tests.m +++ b/tests/tests.m @@ -69,15 +69,15 @@ OF_APPLICATION_DELEGATE(Test) 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]; }