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
typedef OFDictionary OF_GENERIC(OFString *, id) *PGRow;
@interface PGConnection: OFObject
{
PGconn *_connection;
@ -44,9 +46,8 @@ OF_ASSUME_NONNULL_BEGIN
- (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
- (void)insertRow: (PGRow)row intoTable: (OFString *)table;
- (void)insertRows: (OFArray OF_GENERIC(PGRow) *)rows
intoTable: (OFString *)table;
@end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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];

View file

@ -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];
}