Prefix all ivars with an underscore.

FossilOrigin-Name: 8c8a4313228bba7114d582a6802a2df53a1fb357f5c0805ae58531dd4c799d8c
This commit is contained in:
Jonathan Schleifer 2013-02-13 23:40:58 +00:00
parent 7b323460a5
commit b3eaf2f272
11 changed files with 114 additions and 129 deletions

View file

@ -6,8 +6,8 @@
@interface PGConnection: OFObject @interface PGConnection: OFObject
{ {
PGconn *conn; PGconn *_connnection;
OFDictionary *parameters; OFDictionary *_parameters;
} }
#ifdef OF_HAVE_PROPERTIES #ifdef OF_HAVE_PROPERTIES

View file

@ -6,46 +6,46 @@
@implementation PGConnection @implementation PGConnection
- (void)dealloc - (void)dealloc
{ {
[parameters release]; [_parameters release];
if (conn != NULL) if (_connnection != NULL)
PQfinish(conn); PQfinish(_connnection);
[super dealloc]; [super dealloc];
} }
- (void)setParameters: (OFDictionary*)parameters_ - (void)setParameters: (OFDictionary*)parameters
{ {
OF_SETTER(parameters, parameters_, YES, YES) OF_SETTER(_parameters, parameters, YES, YES)
} }
- (OFDictionary*)parameters - (OFDictionary*)parameters
{ {
OF_GETTER(parameters, YES) OF_GETTER(_parameters, YES)
} }
- (void)connect - (void)connect
{ {
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFEnumerator *keyEnumerator = [parameters keyEnumerator]; OFEnumerator *keyEnumerator = [_parameters keyEnumerator];
OFEnumerator *objectEnumerator = [parameters objectEnumerator]; OFEnumerator *objectEnumerator = [_parameters objectEnumerator];
OFMutableString *conninfo = nil; OFMutableString *connectionInfo = nil;
OFString *key, *object; OFString *key, *object;
while ((key = [keyEnumerator nextObject]) != nil && while ((key = [keyEnumerator nextObject]) != nil &&
(object = [objectEnumerator nextObject]) != nil) { (object = [objectEnumerator nextObject]) != nil) {
if (conninfo != nil) if (connectionInfo != nil)
[conninfo appendFormat: @" %@=%@", key, object]; [connectionInfo appendFormat: @" %@=%@", key, object];
else else
conninfo = [OFMutableString stringWithFormat: connectionInfo = [OFMutableString stringWithFormat:
@"%@=%@", key, object]; @"%@=%@", key, object];
} }
if ((conn = PQconnectdb([conninfo UTF8String])) == NULL) if ((_connnection = PQconnectdb([connectionInfo UTF8String])) == NULL)
@throw [OFOutOfMemoryException @throw [OFOutOfMemoryException
exceptionWithClass: [self class]]; exceptionWithClass: [self class]];
if (PQstatus(conn) == CONNECTION_BAD) if (PQstatus(_connnection) == CONNECTION_BAD)
@throw [PGConnectionFailedException @throw [PGConnectionFailedException
exceptionWithClass: [self class] exceptionWithClass: [self class]
connection: self]; connection: self];
@ -55,12 +55,12 @@
- (void)reset - (void)reset
{ {
PQreset(conn); PQreset(_connnection);
} }
- (PGResult*)executeCommand: (OFConstantString*)command - (PGResult*)executeCommand: (OFConstantString*)command
{ {
PGresult *result = PQexec(conn, [command UTF8String]); PGresult *result = PQexec(_connnection, [command UTF8String]);
if (PQresultStatus(result) == PGRES_FATAL_ERROR) { if (PQresultStatus(result) == PGRES_FATAL_ERROR) {
PQclear(result); PQclear(result);
@ -127,7 +127,7 @@
UTF8String]; UTF8String];
} while ((parameter = va_arg(args, id)) != nil); } while ((parameter = va_arg(args, id)) != nil);
result = PQexecParams(conn, [command UTF8String], result = PQexecParams(_connnection, [command UTF8String],
argsCount, NULL, values, NULL, NULL, 0); argsCount, NULL, values, NULL, NULL, 0);
} @finally { } @finally {
[self freeMemory: values]; [self freeMemory: values];
@ -196,8 +196,8 @@
[command appendString: @")"]; [command appendString: @")"];
result = PQexecParams(conn, [command UTF8String], (int)count, result = PQexecParams(_connnection, [command UTF8String],
NULL, values, NULL, NULL, 0); (int)count, NULL, values, NULL, NULL, 0);
} @finally { } @finally {
[self freeMemory: values]; [self freeMemory: values];
} }
@ -231,6 +231,6 @@
- (PGconn*)PG_connection - (PGconn*)PG_connection
{ {
return conn; return _connnection;
} }
@end @end

View file

@ -4,7 +4,7 @@
@interface PGResult: OFArray @interface PGResult: OFArray
{ {
PGresult *result; PGresult *_result;
} }
+ PG_resultWithResult: (PGresult*)result; + PG_resultWithResult: (PGresult*)result;

View file

@ -7,31 +7,31 @@
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];
result = result_; _result = result;
return self; return self;
} }
- (void)dealloc - (void)dealloc
{ {
if (result != NULL) if (_result != NULL)
PQclear(result); PQclear(_result);
[super dealloc]; [super dealloc];
} }
- (size_t)count - (size_t)count
{ {
return PQntuples(result); return PQntuples(_result);
} }
- (id)objectAtIndex: (size_t)index - (id)objectAtIndex: (size_t)index
{ {
if (index > PQntuples(result)) if (index > PQntuples(_result))
@throw [OFOutOfRangeException @throw [OFOutOfRangeException
exceptionWithClass: [self class]]; exceptionWithClass: [self class]];
@ -41,6 +41,6 @@
- (PGresult*)PG_result - (PGresult*)PG_result
{ {
return result; return _result;
} }
@end @end

View file

@ -6,9 +6,9 @@
@interface PGResultRow: OFDictionary @interface PGResultRow: OFDictionary
{ {
PGResult *result; PGResult *_result;
PGresult *res; PGresult *_res;
int row; int _row;
} }
+ rowWithResult: (PGResult*)result + rowWithResult: (PGResult*)result

View file

@ -1,34 +1,37 @@
#import "PGResultRow.h" #import "PGResultRow.h"
static id static id
convert_type(PGresult *res, int col, OFString *str) convert_type(PGresult *res, int column, OFString *string)
{ {
switch (PQftype(res, col)) { switch (PQftype(res, column)) {
case 16: /* BOOLOID */ case 16: /* BOOLOID */
if ([str isEqual: @"t"]) if ([string isEqual: @"t"])
return [OFNumber numberWithBool: YES]; return [OFNumber numberWithBool: YES];
else else
return [OFNumber numberWithBool: NO]; return [OFNumber numberWithBool: NO];
case 21: /* INT2OID */ case 21: /* INT2OID */
return [OFNumber numberWithInt16: (int16_t)[str decimalValue]]; return [OFNumber numberWithInt16:
(int16_t)[string decimalValue]];
case 23: /* INT4OID */ case 23: /* INT4OID */
return [OFNumber numberWithInt32: (int32_t)[str decimalValue]]; return [OFNumber numberWithInt32:
(int32_t)[string decimalValue]];
case 20: /* INT8OID */ case 20: /* INT8OID */
return [OFNumber numberWithInt64: (int64_t)[str decimalValue]]; return [OFNumber numberWithInt64:
(int64_t)[string decimalValue]];
case 700: /* FLOAT4OID */ case 700: /* FLOAT4OID */
return [OFNumber numberWithFloat: [str floatValue]]; return [OFNumber numberWithFloat: [string floatValue]];
case 701: /* FLOAT8OID */ case 701: /* FLOAT8OID */
return [OFNumber numberWithDouble: [str doubleValue]]; return [OFNumber numberWithDouble: [string doubleValue]];
} }
return str; return string;
} }
@interface PGResultRowEnumerator: OFEnumerator @interface PGResultRowEnumerator: OFEnumerator
{ {
PGResult *result; PGResult *_result;
PGresult *res; PGresult *_res;
int row, pos, count; int _row, _pos, _count;
} }
- initWithResult: (PGResult*)result - initWithResult: (PGResult*)result
@ -49,31 +52,31 @@ convert_type(PGresult *res, int col, OFString *str)
row: row] autorelease]; row: row] autorelease];
} }
- initWithResult: (PGResult*)result_ - initWithResult: (PGResult*)result
row: (int)row_ row: (int)row
{ {
self = [super init]; self = [super init];
result = [result_ retain]; _result = [result retain];
res = [result PG_result]; _res = [result PG_result];
row = row_; _row = row;
return self; return self;
} }
- (void)dealloc - (void)dealloc
{ {
[result release]; [_result release];
[super dealloc]; [super dealloc];
} }
- (size_t)count - (size_t)count
{ {
int i, count, fields = PQnfields(res); int i, count, fields = PQnfields(_res);
for (i = count = 0; i < fields; i++) for (i = count = 0; i < fields; i++)
if (!PQgetisnull(res, row, i)) if (!PQgetisnull(_res, _row, i))
count++; count++;
return count; return count;
@ -81,32 +84,32 @@ convert_type(PGresult *res, int col, OFString *str)
- (id)objectForKey: (id)key - (id)objectForKey: (id)key
{ {
int col; int column;
if ([key isKindOfClass: [OFNumber class]]) if ([key isKindOfClass: [OFNumber class]])
col = [key intValue]; column = [key intValue];
else else
col = PQfnumber(res, [key UTF8String]); column = PQfnumber(_res, [key UTF8String]);
if (PQgetisnull(res, row, col)) if (PQgetisnull(_res, _row, column))
return nil; return nil;
return convert_type(res, col, return convert_type(_res, column,
[OFString stringWithUTF8String: PQgetvalue(res, row, col)]); [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
row: row] autorelease]; row: _row] autorelease];
} }
- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state - (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
@ -117,7 +120,7 @@ convert_type(PGresult *res, int col, OFString *str)
if (state->extra[0] == 0) { if (state->extra[0] == 0) {
state->extra[0] = 1; state->extra[0] = 1;
state->extra[1] = PQnfields(res); state->extra[1] = PQnfields(_res);
} }
if (count > SIZE_MAX - state->state) if (count > SIZE_MAX - state->state)
@ -127,11 +130,11 @@ convert_type(PGresult *res, int col, OFString *str)
count = state->extra[1] - state->state; count = state->extra[1] - state->state;
for (i = j = 0; i < count; i++) { for (i = j = 0; i < count; i++) {
if (PQgetisnull(res, row, state->state + i)) if (PQgetisnull(_res, _row, state->state + i))
continue; continue;
objects[j++] = [OFString stringWithUTF8String: objects[j++] = [OFString stringWithUTF8String:
PQfname(res, state->state + i)]; PQfname(_res, state->state + i)];
} }
state->state += count; state->state += count;
@ -143,61 +146,61 @@ convert_type(PGresult *res, int col, OFString *str)
@end @end
@implementation PGResultRowEnumerator @implementation PGResultRowEnumerator
- initWithResult: (PGResult*)result_ - initWithResult: (PGResult*)result
row: (int)row_ row: (int)row
{ {
self = [super init]; self = [super init];
result = [result_ retain]; _result = [result retain];
res = [result PG_result]; _res = [result PG_result];
row = row_; _row = row;
count = PQnfields(res); _count = PQnfields(_res);
return self; return self;
} }
- (void)dealloc - (void)dealloc
{ {
[result release]; [_result release];
[super dealloc]; [super dealloc];
} }
- (void)reset - (void)reset
{ {
pos = 0; _pos = 0;
} }
@end @end
@implementation PGResultRowKeyEnumerator @implementation PGResultRowKeyEnumerator
- (id)nextObject - (id)nextObject
{ {
if (pos >= count) if (_pos >= _count)
return nil; return nil;
while (pos < count && PQgetisnull(res, row, pos)) while (_pos < _count && PQgetisnull(_res, _row, _pos))
pos++; _pos++;
if (pos >= count) if (_pos >= _count)
return nil; return nil;
return [OFString stringWithUTF8String: PQfname(res, pos++)]; return [OFString stringWithUTF8String: PQfname(_res, _pos++)];
} }
@end @end
@implementation PGResultRowObjectEnumerator @implementation PGResultRowObjectEnumerator
- (id)nextObject - (id)nextObject
{ {
if (pos >= count) if (_pos >= _count)
return nil; return nil;
while (pos < count && PQgetisnull(res, row, pos)) while (_pos < _count && PQgetisnull(_res, _row, _pos))
pos++; _pos++;
if (pos >= count) if (_pos >= _count)
return nil; return nil;
return convert_type(res, pos, return convert_type(_res, _pos,
[OFString stringWithUTF8String: PQgetvalue(res, row, pos++)]); [OFString stringWithUTF8String: PQgetvalue(_res, _row, _pos++)]);
} }
@end @end

View file

@ -2,7 +2,7 @@
@interface PGCommandFailedException: PGException @interface PGCommandFailedException: PGException
{ {
OFString *command; OFString *_command;
} }
#ifdef OF_HAVE_PROPERTIES #ifdef OF_HAVE_PROPERTIES

View file

@ -10,15 +10,15 @@
command: command] autorelease]; command: command] autorelease];
} }
- initWithClass: (Class)class_ - initWithClass: (Class)class
connection: (PGConnection*)connection_ connection: (PGConnection*)connection
command: (OFString*)command_ command: (OFString*)command
{ {
self = [super initWithClass: class_ self = [super initWithClass: class
connection: connection_]; connection: connection];
@try { @try {
command = [command_ copy]; _command = [command copy];
} @catch (id e) { } @catch (id e) {
[self release]; [self release];
@throw e; @throw e;
@ -29,25 +29,21 @@
- (void)dealloc - (void)dealloc
{ {
[command release]; [_command release];
[super dealloc]; [super dealloc];
} }
- (OFString*)description - (OFString*)description
{ {
if (description != nil) return [OFString stringWithFormat:
return description;
description = [[OFString alloc] initWithFormat:
@"A PostgreSQL command in class %@ failed: %s\nCommand: %@", @"A PostgreSQL command in class %@ failed: %s\nCommand: %@",
inClass, PQerrorMessage([connection PG_connection]), command]; [self inClass], PQerrorMessage([_connection PG_connection]),
_command];
return description;
} }
- (OFString*)command - (OFString*)command
{ {
OF_GETTER(command, NO) OF_GETTER(_command, NO)
} }
@end @end

View file

@ -3,19 +3,10 @@
@implementation PGConnectionFailedException @implementation PGConnectionFailedException
- (OFString*)description - (OFString*)description
{ {
OFAutoreleasePool *pool; return [OFString stringWithFormat:
if (description != nil)
return description;
pool = [[OFAutoreleasePool alloc] init];
description = [[OFString alloc] initWithFormat:
@"Establishing a PostgreSQL connection in class %@ failed:\n%s\n" @"Establishing a PostgreSQL connection in class %@ failed:\n%s\n"
"Parameters: %@", inClass, "Parameters: %@", [self inClass],
PQerrorMessage([connection PG_connection]), PQerrorMessage([_connection PG_connection]),
[connection parameters]]; [_connection parameters]];
[pool release];
return description;
} }
@end @end

View file

@ -4,7 +4,7 @@
@interface PGException: OFException @interface PGException: OFException
{ {
PGConnection *connection; PGConnection *_connection;
} }
#ifdef OF_HAVE_PROPERTIES #ifdef OF_HAVE_PROPERTIES

View file

@ -8,37 +8,32 @@
connection: connection] autorelease]; connection: connection] autorelease];
} }
- initWithClass: (Class)class_ - initWithClass: (Class)class
connection: (PGConnection*)connection_ connection: (PGConnection*)connection
{ {
self = [super initWithClass: class_]; self = [super initWithClass: class];
connection = [connection_ retain]; _connection = [connection retain];
return self; return self;
} }
- (void)dealloc - (void)dealloc
{ {
[connection release]; [_connection release];
[super dealloc]; [super dealloc];
} }
- (OFString*)description - (OFString*)description
{ {
if (description != nil) return [OFString stringWithFormat:
return description; @"A PostgreSQL operation in class %@ failed: %s", [self inClass],
PQerrorMessage([_connection PG_connection])];
description = [[OFString alloc] initWithFormat:
@"A PostgreSQL operation in class %@ failed: %s", inClass,
PQerrorMessage([connection PG_connection])];
return description;
} }
- (PGConnection*)connection - (PGConnection*)connection
{ {
OF_GETTER(connection, NO) OF_GETTER(_connection, NO)
} }
@end @end