Adjust to ObjFW changes

FossilOrigin-Name: 0af9225d647a95b3135fc711065584dcff39b523377528c734edc0ecb2b379f4
This commit is contained in:
Jonathan Schleifer 2021-04-29 22:36:08 +00:00
parent 1ab8b32076
commit f4cee3b139
6 changed files with 35 additions and 50 deletions

View file

@ -29,6 +29,8 @@
OF_ASSUME_NONNULL_BEGIN OF_ASSUME_NONNULL_BEGIN
typedef OFDictionary OF_GENERIC(OFString *, id) *PGRow;
@interface PGConnection: OFObject @interface PGConnection: OFObject
{ {
PGconn *_connection; PGconn *_connection;
@ -44,9 +46,8 @@ OF_ASSUME_NONNULL_BEGIN
- (nullable PGResult *)executeCommand: (OFConstantString *)command; - (nullable PGResult *)executeCommand: (OFConstantString *)command;
- (nullable PGResult *)executeCommand: (OFConstantString *)command - (nullable PGResult *)executeCommand: (OFConstantString *)command
parameters: (id)firstParameter, ... OF_SENTINEL; parameters: (id)firstParameter, ... OF_SENTINEL;
- (void)insertRow: (OFDictionary *)row - (void)insertRow: (PGRow)row intoTable: (OFString *)table;
intoTable: (OFString *)table; - (void)insertRows: (OFArray OF_GENERIC(PGRow) *)rows
- (void)insertRows: (OFArray OF_GENERIC(OFDictionary *) *)rows
intoTable: (OFString *)table; intoTable: (OFString *)table;
@end @end

View file

@ -30,8 +30,7 @@
#import "PGCommandFailedException.h" #import "PGCommandFailedException.h"
@implementation PGConnection @implementation PGConnection
@synthesize pg_connection = _connection; @synthesize pg_connection = _connection, parameters = _parameters;
@synthesize parameters = _parameters;
- (void)dealloc - (void)dealloc
{ {
@ -123,8 +122,7 @@
for (argsCount = 1; va_arg(args2, id) != nil; argsCount++); for (argsCount = 1; va_arg(args2, id) != nil; argsCount++);
values = [self allocMemoryWithSize: sizeof(*values) values = OFAllocMemory(argsCount, sizeof(*values));
count: argsCount];
@try { @try {
size_t i = 0; size_t i = 0;
@ -134,18 +132,15 @@
else if ([parameter isKindOfClass: [OFNumber class]]) { else if ([parameter isKindOfClass: [OFNumber class]]) {
OFNumber *number = parameter; OFNumber *number = parameter;
switch (number.type) { if (strcmp(number.objCType,
case OF_NUMBER_TYPE_BOOL: @encode(bool)) == 0) {
if (number.boolValue) if (number.boolValue)
values[i++] = "t"; values[i++] = "t";
else else
values[i++] = "f"; values[i++] = "f";
break; } else
default:
values[i++] = values[i++] =
number.description.UTF8String; number.description.UTF8String;
break;
}
} else if ([parameter isKindOfClass: [OFNull class]]) } else if ([parameter isKindOfClass: [OFNull class]])
values[i++] = NULL; values[i++] = NULL;
else else
@ -156,7 +151,7 @@
result = PQexecParams(_connection, command.UTF8String, result = PQexecParams(_connection, command.UTF8String,
argsCount, NULL, values, NULL, NULL, 0); argsCount, NULL, values, NULL, NULL, 0);
} @finally { } @finally {
[self freeMemory: values]; OFFreeMemory(values);
} }
objc_autoreleasePoolPop(pool); objc_autoreleasePoolPop(pool);
@ -175,8 +170,7 @@
} }
} }
- (void)insertRow: (OFDictionary *)row - (void)insertRow: (PGRow)row intoTable: (OFString *)table
intoTable: (OFString *)table
{ {
void *pool = objc_autoreleasePoolPush(); void *pool = objc_autoreleasePoolPush();
OFMutableString *command; OFMutableString *command;
@ -205,8 +199,7 @@
[command appendString: @") VALUES ("]; [command appendString: @") VALUES ("];
values = [self allocMemoryWithSize: sizeof(*values) values = OFAllocMemory(count, sizeof(*values));
count: count];
@try { @try {
i = 0; i = 0;
enumerator = [row objectEnumerator]; enumerator = [row objectEnumerator];
@ -224,7 +217,7 @@
result = PQexecParams(_connection, command.UTF8String, result = PQexecParams(_connection, command.UTF8String,
(int)count, NULL, values, NULL, NULL, 0); (int)count, NULL, values, NULL, NULL, 0);
} @finally { } @finally {
[self freeMemory: values]; OFFreeMemory(values);
} }
objc_autoreleasePoolPop(pool); objc_autoreleasePoolPop(pool);
@ -239,11 +232,10 @@
PQclear(result); PQclear(result);
} }
- (void)insertRows: (OFArray OF_GENERIC(OFDictionary *) *)rows - (void)insertRows: (OFArray OF_GENERIC(PGRow) *)rows
intoTable: (OFString *)table intoTable: (OFString *)table
{ {
for (OFDictionary *row in rows) for (OFDictionary *row in rows)
[self insertRow: row [self insertRow: row intoTable: table];
intoTable: table];
} }
@end @end

View file

@ -61,7 +61,6 @@
if (index > PQntuples(_result)) if (index > PQntuples(_result))
@throw [OFOutOfRangeException exception]; @throw [OFOutOfRangeException exception];
return [PGResultRow pg_rowWithResult: self return [PGResultRow pg_rowWithResult: self row: (int)index];
row: (int)index];
} }
@end @end

View file

@ -26,10 +26,8 @@
OF_ASSUME_NONNULL_BEGIN OF_ASSUME_NONNULL_BEGIN
@interface PGResultRow () @interface PGResultRow ()
+ (instancetype)pg_rowWithResult: (PGResult *)result + (instancetype)pg_rowWithResult: (PGResult *)result row: (int)row;
row: (int)row; - (instancetype)pg_initWithResult: (PGResult *)result row: (int)row;
- (instancetype)pg_initWithResult: (PGResult *)result
row: (int)row;
@end @end
OF_ASSUME_NONNULL_END OF_ASSUME_NONNULL_END

View file

@ -34,14 +34,14 @@ convertType(PGresult *res, int column, OFString *string)
else else
return [OFNumber numberWithBool: NO]; return [OFNumber numberWithBool: NO];
case 21: /* INT2OID */ case 21: /* INT2OID */
return [OFNumber numberWithInt16: return [OFNumber numberWithShort:
(int16_t)string.decimalValue]; (short)[string longLongValueWithBase: 10]];
case 23: /* INT4OID */ case 23: /* INT4OID */
return [OFNumber numberWithInt32: return [OFNumber numberWithLong:
(int32_t)string.decimalValue]; (long)[string longLongValueWithBase: 10]];
case 20: /* INT8OID */ case 20: /* INT8OID */
return [OFNumber numberWithInt64: return [OFNumber numberWithLongLong:
(int64_t)string.decimalValue]; [string longLongValueWithBase: 10]];
case 700: /* FLOAT4OID */ case 700: /* FLOAT4OID */
return [OFNumber numberWithFloat: string.floatValue]; return [OFNumber numberWithFloat: string.floatValue];
case 701: /* FLOAT8OID */ case 701: /* FLOAT8OID */
@ -58,8 +58,7 @@ convertType(PGresult *res, int column, OFString *string)
int _row, _pos, _count; int _row, _pos, _count;
} }
- initWithResult: (PGResult*)result - initWithResult: (PGResult*)result row: (int)row;
row: (int)row;
@end @end
@interface PGResultRowKeyEnumerator: PGResultRowEnumerator @interface PGResultRowKeyEnumerator: PGResultRowEnumerator
@ -69,15 +68,12 @@ convertType(PGresult *res, int column, OFString *string)
@end @end
@implementation PGResultRow @implementation PGResultRow
+ (instancetype)rowWithResult: (PGResult *)result + (instancetype)rowWithResult: (PGResult *)result row: (int)row
row: (int)row
{ {
return [[[self alloc] initWithResult: result return [[[self alloc] initWithResult: result row: row] autorelease];
row: row] autorelease];
} }
- (instancetype)initWithResult: (PGResult *)result - (instancetype)initWithResult: (PGResult *)result row: (int)row
row: (int)row
{ {
self = [super init]; self = [super init];
@ -136,7 +132,7 @@ convertType(PGresult *res, int column, OFString *string)
row: _row] autorelease]; row: _row] autorelease];
} }
- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state - (int)countByEnumeratingWithState: (OFFastEnumerationState *)state
objects: (id *)objects objects: (id *)objects
count: (int)count count: (int)count
{ {
@ -170,8 +166,7 @@ convertType(PGresult *res, int column, OFString *string)
@end @end
@implementation PGResultRowEnumerator @implementation PGResultRowEnumerator
- (instancetype)initWithResult: (PGResult *)result - (instancetype)initWithResult: (PGResult *)result row: (int)row
row: (int)row
{ {
self = [super init]; self = [super init];

View file

@ -69,15 +69,15 @@ OF_APPLICATION_DELEGATE(Test)
intoTable: @"test"]; intoTable: @"test"];
result = [_connection executeCommand: @"SELECT * FROM test"]; result = [_connection executeCommand: @"SELECT * FROM test"];
of_log(@"%@", result); OFLog(@"%@", result);
of_log(@"JSON: %@", [result JSONRepresentation]); OFLog(@"JSON: %@", [result JSONRepresentation]);
for (id row in result) for (id row in result)
for (id col in row) for (id col in row)
of_log(@"%@", col); OFLog(@"%@", col);
result = [_connection executeCommand: @"SELECT COUNT(*) FROM test"]; result = [_connection executeCommand: @"SELECT COUNT(*) FROM test"];
of_log(@"%@", result); OFLog(@"%@", result);
[OFApplication terminate]; [OFApplication terminate];
} }