Adjust to recent ObjFW changes

FossilOrigin-Name: fc8e42990e7f6476b8b806521222620bbac0496c61a91b20f9e3787fc20fdbb4
This commit is contained in:
Jonathan Schleifer 2017-05-09 23:19:19 +00:00
parent 6d64ce5a68
commit ae918f26c4
12 changed files with 88 additions and 65 deletions

View file

@ -4,23 +4,28 @@
#import "PGResult.h" #import "PGResult.h"
OF_ASSUME_NONNULL_BEGIN
@interface PGConnection: OFObject @interface PGConnection: OFObject
{ {
PGconn *_connnection; PGconn *_connnection;
OFDictionary *_parameters; OFDictionary OF_GENERIC(OFString *, OFString *) *_parameters;
} }
@property (copy) OFDictionary *parameters; @property (nonatomic, copy)
OFDictionary OF_GENERIC(OFString *, OFString *) *parameters;
- (void)connect; - (void)connect;
- (void)reset; - (void)reset;
- (void)close; - (void)close;
- (PGResult*)executeCommand: (OFConstantString*)command; - (PGResult *)executeCommand: (OFConstantString *)command;
- (PGResult*)executeCommand: (OFConstantString*)command - (PGResult *)executeCommand: (OFConstantString *)command
parameters: (id)firstParameter, ... OF_SENTINEL; parameters: (id)firstParameter, ... OF_SENTINEL;
- (PGconn*)PG_connection; - (PGconn *)PG_connection;
- (void)insertRow: (OFDictionary*)row - (void)insertRow: (OFDictionary *)row
intoTable: (OFString*)table; intoTable: (OFString *)table;
- (void)insertRows: (OFArray*)rows - (void)insertRows: (OFArray OF_GENERIC(OFDictionary *) *)rows
intoTable: (OFString*)table; intoTable: (OFString *)table;
@end @end
OF_ASSUME_NONNULL_END

View file

@ -17,9 +17,11 @@
- (void)connect - (void)connect
{ {
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; void *pool = objc_autoreleasePoolPush();
OFEnumerator *keyEnumerator = [_parameters keyEnumerator]; OFEnumerator OF_GENERIC(OFString *) *keyEnumerator =
OFEnumerator *objectEnumerator = [_parameters objectEnumerator]; [_parameters keyEnumerator];
OFEnumerator OF_GENERIC(OFString *) *objectEnumerator =
[_parameters objectEnumerator];
OFMutableString *connectionInfo = nil; OFMutableString *connectionInfo = nil;
OFString *key, *object; OFString *key, *object;
@ -39,7 +41,7 @@
@throw [PGConnectionFailedException @throw [PGConnectionFailedException
exceptionWithConnection: self]; exceptionWithConnection: self];
[pool release]; objc_autoreleasePoolPop(pool);
} }
- (void)reset - (void)reset
@ -55,7 +57,7 @@
_connnection = NULL; _connnection = NULL;
} }
- (PGResult*)executeCommand: (OFConstantString*)command - (PGResult *)executeCommand: (OFConstantString *)command
{ {
PGresult *result = PQexec(_connnection, [command UTF8String]); PGresult *result = PQexec(_connnection, [command UTF8String]);
@ -80,10 +82,10 @@
} }
} }
- (PGResult*)executeCommand: (OFConstantString*)command - (PGResult *)executeCommand: (OFConstantString *)command
parameters: (id)parameter, ... parameters: (id)parameter, ...
{ {
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; void *pool = objc_autoreleasePoolPush();
PGresult *result; PGresult *result;
const char **values; const char **values;
va_list args, args2; va_list args, args2;
@ -130,7 +132,7 @@
[self freeMemory: values]; [self freeMemory: values];
} }
[pool release]; objc_autoreleasePoolPop(pool);
switch (PQresultStatus(result)) { switch (PQresultStatus(result)) {
case PGRES_TUPLES_OK: case PGRES_TUPLES_OK:
@ -146,10 +148,10 @@
} }
} }
- (void)insertRow: (OFDictionary*)row - (void)insertRow: (OFDictionary *)row
intoTable: (OFString*)table intoTable: (OFString *)table
{ {
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; void *pool = objc_autoreleasePoolPush();
OFMutableString *command; OFMutableString *command;
OFEnumerator *enumerator; OFEnumerator *enumerator;
const char **values; const char **values;
@ -198,7 +200,7 @@
[self freeMemory: values]; [self freeMemory: values];
} }
[pool release]; objc_autoreleasePoolPop(pool);
if (PQresultStatus(result) != PGRES_COMMAND_OK) { if (PQresultStatus(result) != PGRES_COMMAND_OK) {
PQclear(result); PQclear(result);
@ -210,21 +212,15 @@
PQclear(result); PQclear(result);
} }
- (void)insertRows: (OFArray*)rows - (void)insertRows: (OFArray OF_GENERIC(OFDictionary *) *)rows
intoTable: (OFString*)table intoTable: (OFString *)table
{ {
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; for (OFDictionary *row in rows)
OFEnumerator *enumerator = [rows objectEnumerator];
OFDictionary *row;
while ((row = [enumerator nextObject]) != nil)
[self insertRow: row [self insertRow: row
intoTable: table]; intoTable: table];
[pool release];
} }
- (PGconn*)PG_connection - (PGconn *)PG_connection
{ {
return _connnection; return _connnection;
} }

View file

@ -2,12 +2,18 @@
#import <ObjFW/ObjFW.h> #import <ObjFW/ObjFW.h>
@interface PGResult: OFArray OF_ASSUME_NONNULL_BEGIN
@class PGResultRow;
@interface PGResult: OFArray OF_GENERIC(PGResultRow *)
{ {
PGresult *_result; PGresult *_result;
} }
+ PG_resultWithResult: (PGresult*)result; + (instancetype)PG_resultWithResult: (PGresult *)result;
- PG_initWithResult: (PGresult*)result; - PG_initWithResult: (PGresult *)result OF_METHOD_FAMILY(init);
- (PGresult*)PG_result; - (PGresult *)PG_result;
@end @end
OF_ASSUME_NONNULL_END

View file

@ -2,12 +2,12 @@
#import "PGResultRow.h" #import "PGResultRow.h"
@implementation PGResult @implementation PGResult
+ PG_resultWithResult: (PGresult*)result + PG_resultWithResult: (PGresult *)result
{ {
return [[[self alloc] PG_initWithResult: result] autorelease]; return [[[self alloc] PG_initWithResult: result] autorelease];
} }
- PG_initWithResult: (PGresult*)result - PG_initWithResult: (PGresult *)result
{ {
self = [super init]; self = [super init];
@ -38,7 +38,7 @@
row: (int)index]; row: (int)index];
} }
- (PGresult*)PG_result - (PGresult *)PG_result
{ {
return _result; return _result;
} }

View file

@ -4,6 +4,8 @@
#import "PGResult.h" #import "PGResult.h"
OF_ASSUME_NONNULL_BEGIN
@interface PGResultRow: OFDictionary @interface PGResultRow: OFDictionary
{ {
PGResult *_result; PGResult *_result;
@ -11,8 +13,10 @@
int _row; int _row;
} }
+ rowWithResult: (PGResult*)result + rowWithResult: (PGResult *)result
row: (int)row; row: (int)row;
- initWithResult: (PGResult*)result - initWithResult: (PGResult *)result
row: (int)row; row: (int)row;
@end @end
OF_ASSUME_NONNULL_END

View file

@ -45,14 +45,14 @@ convertType(PGresult *res, int column, OFString *string)
@end @end
@implementation PGResultRow @implementation PGResultRow
+ rowWithResult: (PGResult*)result + 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];
} }
- initWithResult: (PGResult*)result - initWithResult: (PGResult *)result
row: (int)row row: (int)row
{ {
self = [super init]; self = [super init];
@ -98,14 +98,14 @@ convertType(PGresult *res, int column, OFString *string)
[OFString stringWithUTF8String: PQgetvalue(_res, _row, column)]); [OFString stringWithUTF8String: PQgetvalue(_res, _row, column)]);
} }
- (OFEnumerator*)keyEnumerator - (OFEnumerator *)keyEnumerator
{ {
return [[[PGResultRowKeyEnumerator alloc] return [[[PGResultRowKeyEnumerator alloc]
initWithResult: _result initWithResult: _result
row: _row] autorelease]; row: _row] autorelease];
} }
- (OFEnumerator*)objectEnumerator - (OFEnumerator *)objectEnumerator
{ {
return [[[PGResultRowObjectEnumerator alloc] return [[[PGResultRowObjectEnumerator alloc]
initWithResult: _result initWithResult: _result
@ -113,7 +113,7 @@ convertType(PGresult *res, int column, OFString *string)
} }
- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state - (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
objects: (id*)objects objects: (id *)objects
count: (int)count count: (int)count
{ {
int i, j; int i, j;
@ -146,7 +146,7 @@ convertType(PGresult *res, int column, OFString *string)
@end @end
@implementation PGResultRowEnumerator @implementation PGResultRowEnumerator
- initWithResult: (PGResult*)result - initWithResult: (PGResult *)result
row: (int)row row: (int)row
{ {
self = [super init]; self = [super init];

View file

@ -1,14 +1,18 @@
#import "PGException.h" #import "PGException.h"
OF_ASSUME_NONNULL_BEGIN
@interface PGCommandFailedException: PGException @interface PGCommandFailedException: PGException
{ {
OFString *_command; OFString *_command;
} }
@property (readonly, copy) OFString *command; @property (readonly, nonatomic) OFString *command;
+ (instancetype)exceptionWithConnection: (PGConnection*)connection + (instancetype)exceptionWithConnection: (PGConnection *)connection
command: (OFString*)command; command: (OFString *)command;
- initWithConnection: (PGConnection*)connection - initWithConnection: (PGConnection *)connection
command: (OFString*)command; command: (OFString *)command;
@end @end
OF_ASSUME_NONNULL_END

View file

@ -3,15 +3,15 @@
@implementation PGCommandFailedException @implementation PGCommandFailedException
@synthesize command = _command; @synthesize command = _command;
+ (instancetype)exceptionWithConnection: (PGConnection*)connection + (instancetype)exceptionWithConnection: (PGConnection *)connection
command: (OFString*)command command: (OFString *)command
{ {
return [[[self alloc] initWithConnection: connection return [[[self alloc] initWithConnection: connection
command: command] autorelease]; command: command] autorelease];
} }
- initWithConnection: (PGConnection*)connection - initWithConnection: (PGConnection *)connection
command: (OFString*)command command: (OFString *)command
{ {
self = [super initWithConnection: connection]; self = [super initWithConnection: connection];
@ -32,7 +32,7 @@
[super dealloc]; [super dealloc];
} }
- (OFString*)description - (OFString *)description
{ {
return [OFString stringWithFormat: @"A PostgreSQL command failed: %@\n" return [OFString stringWithFormat: @"A PostgreSQL command failed: %@\n"
@"Command: %@", _error, _command]; @"Command: %@", _error, _command];

View file

@ -1,4 +1,8 @@
#import "PGException.h" #import "PGException.h"
OF_ASSUME_NONNULL_BEGIN
@interface PGConnectionFailedException: PGException @interface PGConnectionFailedException: PGException
@end @end
OF_ASSUME_NONNULL_END

View file

@ -1,7 +1,7 @@
#import "PGConnectionFailedException.h" #import "PGConnectionFailedException.h"
@implementation PGConnectionFailedException @implementation PGConnectionFailedException
- (OFString*)description - (OFString *)description
{ {
return [OFString stringWithFormat: return [OFString stringWithFormat:
@"Establishing a PostgreSQL connection failed:\n%@\n" @"Establishing a PostgreSQL connection failed:\n%@\n"

View file

@ -2,14 +2,18 @@
#import "PGConnection.h" #import "PGConnection.h"
OF_ASSUME_NONNULL_BEGIN
@interface PGException: OFException @interface PGException: OFException
{ {
PGConnection *_connection; PGConnection *_connection;
OFString *_error; OFString *_error;
} }
@property (readonly, retain) PGConnection *connection; @property (readonly, nonatomic) PGConnection *connection;
+ (instancetype)exceptionWithConnection: (PGConnection*)connection; + (instancetype)exceptionWithConnection: (PGConnection *)connection;
- initWithConnection: (PGConnection*)connection; - initWithConnection: (PGConnection *)connection;
@end @end
OF_ASSUME_NONNULL_END

View file

@ -3,12 +3,12 @@
@implementation PGException @implementation PGException
@synthesize connection = _connection; @synthesize connection = _connection;
+ (instancetype)exceptionWithConnection: (PGConnection*)connection + (instancetype)exceptionWithConnection: (PGConnection *)connection
{ {
return [[[self alloc] initWithConnection: connection] autorelease]; return [[[self alloc] initWithConnection: connection] autorelease];
} }
- initWithConnection: (PGConnection*)connection - initWithConnection: (PGConnection *)connection
{ {
self = [super init]; self = [super init];
@ -33,7 +33,7 @@
[super dealloc]; [super dealloc];
} }
- (OFString*)description - (OFString *)description
{ {
return [OFString stringWithFormat: @"A PostgreSQL operation failed: %@", return [OFString stringWithFormat: @"A PostgreSQL operation failed: %@",
_error]; _error];