Index: PGConnection.m ================================================================== --- PGConnection.m +++ PGConnection.m @@ -39,17 +39,15 @@ connectionInfo = [OFMutableString stringWithFormat: @"%@=%@", key, object]; } if ((_connnection = PQconnectdb([connectionInfo UTF8String])) == NULL) - @throw [OFOutOfMemoryException - exceptionWithClass: [self class]]; + @throw [OFOutOfMemoryException exception]; if (PQstatus(_connnection) == CONNECTION_BAD) @throw [PGConnectionFailedException - exceptionWithClass: [self class] - connection: self]; + exceptionWithConnection: self]; [pool release]; } - (void)reset @@ -70,13 +68,12 @@ PGresult *result = PQexec(_connnection, [command UTF8String]); if (PQresultStatus(result) == PGRES_FATAL_ERROR) { PQclear(result); @throw [PGCommandFailedException - exceptionWithClass: [self class] - connection: self - command: command]; + exceptionWithConnection: self + command: command]; } switch (PQresultStatus(result)) { case PGRES_TUPLES_OK: return [PGResult PG_resultWithResult: result]; @@ -84,13 +81,12 @@ PQclear(result); return nil; default: PQclear(result); @throw [PGCommandFailedException - exceptionWithClass: [self class] - connection: self - command: command]; + exceptionWithConnection: self + command: command]; } } - (PGResult*)executeCommand: (OFConstantString*)command parameters: (id)parameter, ... @@ -113,19 +109,21 @@ do { if ([parameter isKindOfClass: [OFString class]]) values[i++] = [parameter UTF8String]; else if ([parameter isKindOfClass: [OFNumber class]]) { - switch ([parameter type]) { + OFNumber *number = parameter; + + switch ([number type]) { case OF_NUMBER_BOOL: - if ([parameter boolValue]) + if ([number boolValue]) values[i++] = "t"; else values[i++] = "f"; break; default: - values[i++] = [[parameter description] + values[i++] = [[number description] UTF8String]; break; } } else if ([parameter isKindOfClass: [OFNull class]]) values[i++] = NULL; @@ -149,13 +147,12 @@ PQclear(result); return nil; default: PQclear(result); @throw [PGCommandFailedException - exceptionWithClass: [self class] - connection: self - command: command]; + exceptionWithConnection: self + command: command]; } } - (void)insertRow: (OFDictionary*)row intoTable: (OFString*)table @@ -212,13 +209,12 @@ [pool release]; if (PQresultStatus(result) != PGRES_COMMAND_OK) { PQclear(result); @throw [PGCommandFailedException - exceptionWithClass: [self class] - connection: self - command: command]; + exceptionWithConnection: self + command: command]; } PQclear(result); } Index: PGResult.m ================================================================== --- PGResult.m +++ PGResult.m @@ -30,12 +30,11 @@ } - (id)objectAtIndex: (size_t)index { if (index > PQntuples(_result)) - @throw [OFOutOfRangeException - exceptionWithClass: [self class]]; + @throw [OFOutOfRangeException exception]; return [PGResultRow rowWithResult: self row: (int)index]; } Index: PGResultRow.m ================================================================== --- PGResultRow.m +++ PGResultRow.m @@ -122,11 +122,11 @@ state->extra[0] = 1; state->extra[1] = PQnfields(_res); } if (count > SIZE_MAX - state->state) - @throw [OFOutOfRangeException exceptionWithClass: [self class]]; + @throw [OFOutOfRangeException exception]; if (state->state + count > state->extra[1]) count = state->extra[1] - state->state; for (i = j = 0; i < count; i++) { Index: exceptions/PGCommandFailedException.h ================================================================== --- exceptions/PGCommandFailedException.h +++ exceptions/PGCommandFailedException.h @@ -7,13 +7,11 @@ #ifdef OF_HAVE_PROPERTIES @property (readonly, copy, nonatomic) OFString *command; #endif -+ exceptionWithClass: (Class)class_ - connection: (PGConnection*)connection ++ (instancetype)exceptionWithConnection: (PGConnection*)connection + command: (OFString*)command; +- initWithConnection: (PGConnection*)connection command: (OFString*)command; -- initWithClass: (Class)class_ - connection: (PGConnection*)connection - command: (OFString*)command; - (OFString*)command; @end Index: exceptions/PGCommandFailedException.m ================================================================== --- exceptions/PGCommandFailedException.m +++ exceptions/PGCommandFailedException.m @@ -1,23 +1,19 @@ #import "PGCommandFailedException.h" @implementation PGCommandFailedException -+ exceptionWithClass: (Class)class - connection: (PGConnection*)connection - command: (OFString*)command -{ - return [[[self alloc] initWithClass: class - connection: connection - command: command] autorelease]; -} - -- initWithClass: (Class)class - connection: (PGConnection*)connection - command: (OFString*)command -{ - self = [super initWithClass: class - connection: connection]; ++ (instancetype)exceptionWithConnection: (PGConnection*)connection + command: (OFString*)command +{ + return [[[self alloc] initWithConnection: connection + command: command] autorelease]; +} + +- initWithConnection: (PGConnection*)connection + command: (OFString*)command +{ + self = [super initWithConnection: connection]; @try { _command = [command copy]; } @catch (id e) { [self release]; @@ -34,16 +30,14 @@ [super dealloc]; } - (OFString*)description { - return [OFString stringWithFormat: - @"A PostgreSQL command in class %@ failed: %s\nCommand: %@", - [self inClass], PQerrorMessage([_connection PG_connection]), - _command]; + return [OFString stringWithFormat: @"A PostgreSQL command failed: %@\n" + @"Command: %@", _error, _command]; } - (OFString*)command { OF_GETTER(_command, NO) } @end Index: exceptions/PGConnectionFailedException.m ================================================================== --- exceptions/PGConnectionFailedException.m +++ exceptions/PGConnectionFailedException.m @@ -2,11 +2,9 @@ @implementation PGConnectionFailedException - (OFString*)description { return [OFString stringWithFormat: - @"Establishing a PostgreSQL connection in class %@ failed:\n%s\n" - "Parameters: %@", [self inClass], - PQerrorMessage([_connection PG_connection]), - [_connection parameters]]; + @"Establishing a PostgreSQL connection failed:\n%@\n" + "Parameters: %@", _error, [_connection parameters]]; } @end Index: exceptions/PGException.h ================================================================== --- exceptions/PGException.h +++ exceptions/PGException.h @@ -3,17 +3,16 @@ #import "PGConnection.h" @interface PGException: OFException { PGConnection *_connection; + OFString *_error; } #ifdef OF_HAVE_PROPERTIES @property (readonly, retain, nonatomic) PGConnection *connection; #endif -+ exceptionWithClass: (Class)class_ - connection: (PGConnection*)connection; -- initWithClass: (Class)class_ - connection: (PGConnection*)connection; ++ (instancetype)exceptionWithConnection: (PGConnection*)connection; +- initWithConnection: (PGConnection*)connection; - (PGConnection*)connection; @end Index: exceptions/PGException.m ================================================================== --- exceptions/PGException.m +++ exceptions/PGException.m @@ -1,37 +1,42 @@ #import "PGException.h" @implementation PGException -+ exceptionWithClass: (Class)class - connection: (PGConnection*)connection ++ (instancetype)exceptionWithConnection: (PGConnection*)connection { - return [[[self alloc] initWithClass: class - connection: connection] autorelease]; + return [[[self alloc] initWithConnection: connection] autorelease]; } -- initWithClass: (Class)class - connection: (PGConnection*)connection +- initWithConnection: (PGConnection*)connection { - self = [super initWithClass: class]; + self = [super init]; - _connection = [connection retain]; + @try { + _connection = [connection retain]; + _error = [[OFString alloc] + initWithCString: PQerrorMessage([_connection PG_connection]) + encoding: OF_STRING_ENCODING_NATIVE]; + } @catch (id e) { + [self release]; + @throw e; + } return self; } - (void)dealloc { [_connection release]; + [_error release]; [super dealloc]; } - (OFString*)description { - return [OFString stringWithFormat: - @"A PostgreSQL operation in class %@ failed: %s", [self inClass], - PQerrorMessage([_connection PG_connection])]; + return [OFString stringWithFormat: @"A PostgreSQL operation failed: %@", + _error]; } - (PGConnection*)connection { OF_GETTER(_connection, NO)