diff --git a/PGConnection.m b/PGConnection.m index 60d8c3c..1607c45 100644 --- a/PGConnection.m +++ b/PGConnection.m @@ -41,13 +41,11 @@ } 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]; } @@ -72,9 +70,8 @@ if (PQresultStatus(result) == PGRES_FATAL_ERROR) { PQclear(result); @throw [PGCommandFailedException - exceptionWithClass: [self class] - connection: self - command: command]; + exceptionWithConnection: self + command: command]; } switch (PQresultStatus(result)) { @@ -86,9 +83,8 @@ default: PQclear(result); @throw [PGCommandFailedException - exceptionWithClass: [self class] - connection: self - command: command]; + exceptionWithConnection: self + command: command]; } } @@ -115,15 +111,17 @@ 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; } @@ -151,9 +149,8 @@ default: PQclear(result); @throw [PGCommandFailedException - exceptionWithClass: [self class] - connection: self - command: command]; + exceptionWithConnection: self + command: command]; } } @@ -214,9 +211,8 @@ if (PQresultStatus(result) != PGRES_COMMAND_OK) { PQclear(result); @throw [PGCommandFailedException - exceptionWithClass: [self class] - connection: self - command: command]; + exceptionWithConnection: self + command: command]; } PQclear(result); diff --git a/PGResult.m b/PGResult.m index 4508863..a29dcbd 100644 --- a/PGResult.m +++ b/PGResult.m @@ -32,8 +32,7 @@ - (id)objectAtIndex: (size_t)index { if (index > PQntuples(_result)) - @throw [OFOutOfRangeException - exceptionWithClass: [self class]]; + @throw [OFOutOfRangeException exception]; return [PGResultRow rowWithResult: self row: (int)index]; diff --git a/PGResultRow.m b/PGResultRow.m index 587663b..fa868db 100644 --- a/PGResultRow.m +++ b/PGResultRow.m @@ -124,7 +124,7 @@ convert_type(PGresult *res, int column, OFString *string) } 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; diff --git a/exceptions/PGCommandFailedException.h b/exceptions/PGCommandFailedException.h index 9205217..3f2d2de 100644 --- a/exceptions/PGCommandFailedException.h +++ b/exceptions/PGCommandFailedException.h @@ -9,11 +9,9 @@ @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 diff --git a/exceptions/PGCommandFailedException.m b/exceptions/PGCommandFailedException.m index f8105c2..708c597 100644 --- a/exceptions/PGCommandFailedException.m +++ b/exceptions/PGCommandFailedException.m @@ -1,21 +1,17 @@ #import "PGCommandFailedException.h" @implementation PGCommandFailedException -+ exceptionWithClass: (Class)class - connection: (PGConnection*)connection - command: (OFString*)command ++ (instancetype)exceptionWithConnection: (PGConnection*)connection + command: (OFString*)command { - return [[[self alloc] initWithClass: class - connection: connection - command: command] autorelease]; + return [[[self alloc] initWithConnection: connection + command: command] autorelease]; } -- initWithClass: (Class)class - connection: (PGConnection*)connection - command: (OFString*)command +- initWithConnection: (PGConnection*)connection + command: (OFString*)command { - self = [super initWithClass: class - connection: connection]; + self = [super initWithConnection: connection]; @try { _command = [command copy]; @@ -36,10 +32,8 @@ - (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 diff --git a/exceptions/PGConnectionFailedException.m b/exceptions/PGConnectionFailedException.m index 2cc33bc..52f5732 100644 --- a/exceptions/PGConnectionFailedException.m +++ b/exceptions/PGConnectionFailedException.m @@ -4,9 +4,7 @@ - (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 diff --git a/exceptions/PGException.h b/exceptions/PGException.h index 1ce3b03..b1198cb 100644 --- a/exceptions/PGException.h +++ b/exceptions/PGException.h @@ -5,15 +5,14 @@ @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 diff --git a/exceptions/PGException.m b/exceptions/PGException.m index 5043c5b..4225d0d 100644 --- a/exceptions/PGException.m +++ b/exceptions/PGException.m @@ -1,19 +1,24 @@ #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; } @@ -21,15 +26,15 @@ - (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