ObjPgSQL  Check-in [6307a38198]

Overview
Comment:Move private methods to separate headers

Also fixes a typo and adds two missing nullability annotations.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6307a381989719ad89067e1ae2cb5d454dd0b77a83cc6141785067365767e7f6
User & Date: js 2017-05-10 23:46:05
Context
2017-05-10
23:51
Do not depend on object literals for tests check-in: 2105581c29 user: js tags: trunk
23:46
Move private methods to separate headers check-in: 6307a38198 user: js tags: trunk
2017-05-09
23:19
Adjust to recent ObjFW changes check-in: fc8e42990e user: js tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added src/PGConnection+Private.h.



















>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
#import "PGConnection.h"

OF_ASSUME_NONNULL_BEGIN

@interface PGConnection ()
- (PGconn *)PG_connection;
@end

OF_ASSUME_NONNULL_END

Changes to src/PGConnection.h.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <libpq-fe.h>

#import <ObjFW/ObjFW.h>

#import "PGResult.h"

OF_ASSUME_NONNULL_BEGIN

@interface PGConnection: OFObject
{
	PGconn *_connnection;
	OFDictionary OF_GENERIC(OFString *, OFString *) *_parameters;
}

@property (nonatomic, copy)
    OFDictionary OF_GENERIC(OFString *, OFString *) *parameters;

- (void)connect;
- (void)reset;
- (void)close;
- (PGResult *)executeCommand: (OFConstantString *)command;
- (PGResult *)executeCommand: (OFConstantString *)command
		  parameters: (id)firstParameter, ... OF_SENTINEL;
- (PGconn *)PG_connection;
- (void)insertRow: (OFDictionary *)row
	intoTable: (OFString *)table;
- (void)insertRows: (OFArray OF_GENERIC(OFDictionary *) *)rows
	 intoTable: (OFString *)table;
@end

OF_ASSUME_NONNULL_END










|









|
|

<







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

24
25
26
27
28
29
30
#include <libpq-fe.h>

#import <ObjFW/ObjFW.h>

#import "PGResult.h"

OF_ASSUME_NONNULL_BEGIN

@interface PGConnection: OFObject
{
	PGconn *_connection;
	OFDictionary OF_GENERIC(OFString *, OFString *) *_parameters;
}

@property (nonatomic, copy)
    OFDictionary OF_GENERIC(OFString *, OFString *) *parameters;

- (void)connect;
- (void)reset;
- (void)close;
- (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
	 intoTable: (OFString *)table;
@end

OF_ASSUME_NONNULL_END

Changes to src/PGConnection.m.

1



2
3
4
5
6
7
8
#import "PGConnection.h"




#import "PGConnectionFailedException.h"
#import "PGCommandFailedException.h"

@implementation PGConnection
@synthesize parameters = _parameters;


>
>
>







1
2
3
4
5
6
7
8
9
10
11
#import "PGConnection.h"
#import "PGConnection+Private.h"
#import "PGResult.h"
#import "PGResult+Private.h"

#import "PGConnectionFailedException.h"
#import "PGCommandFailedException.h"

@implementation PGConnection
@synthesize parameters = _parameters;

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
		if (connectionInfo != nil)
			[connectionInfo appendFormat: @" %@=%@", key, object];
		else
			connectionInfo = [OFMutableString stringWithFormat:
			    @"%@=%@", key, object];
	}

	if ((_connnection = PQconnectdb([connectionInfo UTF8String])) == NULL)
		@throw [OFOutOfMemoryException exception];

	if (PQstatus(_connnection) == CONNECTION_BAD)
		@throw [PGConnectionFailedException
		    exceptionWithConnection: self];

	objc_autoreleasePoolPop(pool);
}

- (void)reset
{
	PQreset(_connnection);
}

- (void)close
{
	if (_connnection != NULL)
		PQfinish(_connnection);

	_connnection = NULL;
}

- (PGResult *)executeCommand: (OFConstantString *)command
{
	PGresult *result = PQexec(_connnection, [command UTF8String]);

	if (PQresultStatus(result) == PGRES_FATAL_ERROR) {
		PQclear(result);
		@throw [PGCommandFailedException
		    exceptionWithConnection: self
				    command: command];
	}







|


|








|




|
|

|




|







33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
		if (connectionInfo != nil)
			[connectionInfo appendFormat: @" %@=%@", key, object];
		else
			connectionInfo = [OFMutableString stringWithFormat:
			    @"%@=%@", key, object];
	}

	if ((_connection = PQconnectdb([connectionInfo UTF8String])) == NULL)
		@throw [OFOutOfMemoryException exception];

	if (PQstatus(_connection) == CONNECTION_BAD)
		@throw [PGConnectionFailedException
		    exceptionWithConnection: self];

	objc_autoreleasePoolPop(pool);
}

- (void)reset
{
	PQreset(_connection);
}

- (void)close
{
	if (_connection != NULL)
		PQfinish(_connection);

	_connection = NULL;
}

- (PGResult *)executeCommand: (OFConstantString *)command
{
	PGresult *result = PQexec(_connection, [command UTF8String]);

	if (PQresultStatus(result) == PGRES_FATAL_ERROR) {
		PQclear(result);
		@throw [PGCommandFailedException
		    exceptionWithConnection: self
				    command: command];
	}
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
			} else if ([parameter isKindOfClass: [OFNull class]])
				values[i++] = NULL;
			else
				values[i++] = [[parameter description]
				    UTF8String];
		} while ((parameter = va_arg(args, id)) != nil);

		result = PQexecParams(_connnection, [command UTF8String],
		    argsCount, NULL, values, NULL, NULL, 0);
	} @finally {
		[self freeMemory: values];
	}

	objc_autoreleasePoolPop(pool);








|







125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
			} else if ([parameter isKindOfClass: [OFNull class]])
				values[i++] = NULL;
			else
				values[i++] = [[parameter description]
				    UTF8String];
		} while ((parameter = va_arg(args, id)) != nil);

		result = PQexecParams(_connection, [command UTF8String],
		    argsCount, NULL, values, NULL, NULL, 0);
	} @finally {
		[self freeMemory: values];
	}

	objc_autoreleasePoolPop(pool);

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
			values[i] = [value UTF8String];

			[command appendFormat: @"$%zd", ++i];
		}

		[command appendString: @")"];

		result = PQexecParams(_connnection, [command UTF8String],
		    (int)count, NULL, values, NULL, NULL, 0);
	} @finally {
		[self freeMemory: values];
	}

	objc_autoreleasePoolPop(pool);








|







193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
			values[i] = [value UTF8String];

			[command appendFormat: @"$%zd", ++i];
		}

		[command appendString: @")"];

		result = PQexecParams(_connection, [command UTF8String],
		    (int)count, NULL, values, NULL, NULL, 0);
	} @finally {
		[self freeMemory: values];
	}

	objc_autoreleasePoolPop(pool);

218
219
220
221
222
223
224
225
226
227
	for (OFDictionary *row in rows)
		[self insertRow: row
		      intoTable: table];
}

- (PGconn *)PG_connection
{
	return _connnection;
}
@end







|


221
222
223
224
225
226
227
228
229
230
	for (OFDictionary *row in rows)
		[self insertRow: row
		      intoTable: table];
}

- (PGconn *)PG_connection
{
	return _connection;
}
@end

Added src/PGResult+Private.h.























>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
#import "PGResult.h"

OF_ASSUME_NONNULL_BEGIN

@interface PGResult ()
+ (instancetype)PG_resultWithResult: (PGresult *)result;
- PG_initWithResult: (PGresult *)result OF_METHOD_FAMILY(init);
- (PGresult *)PG_result;
@end

OF_ASSUME_NONNULL_END

Changes to src/PGResult.h.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <libpq-fe.h>

#import <ObjFW/ObjFW.h>

OF_ASSUME_NONNULL_BEGIN

@class PGResultRow;

@interface PGResult: OFArray OF_GENERIC(PGResultRow *)
{
	PGresult *_result;
}

+ (instancetype)PG_resultWithResult: (PGresult *)result;
- PG_initWithResult: (PGresult *)result OF_METHOD_FAMILY(init);
- (PGresult *)PG_result;
@end

OF_ASSUME_NONNULL_END












<
<
<
<



1
2
3
4
5
6
7
8
9
10
11
12




13
14
15
#include <libpq-fe.h>

#import <ObjFW/ObjFW.h>

OF_ASSUME_NONNULL_BEGIN

@class PGResultRow;

@interface PGResult: OFArray OF_GENERIC(PGResultRow *)
{
	PGresult *_result;
}




@end

OF_ASSUME_NONNULL_END

Changes to src/PGResult.m.

1
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import "PGResult.h"
#import "PGResultRow.h"


@implementation PGResult
+ PG_resultWithResult: (PGresult *)result
{
	return [[[self alloc] PG_initWithResult: result] autorelease];
}

- PG_initWithResult: (PGresult *)result
{
	self = [super init];

	_result = result;

	return self;
}


>


|




|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import "PGResult.h"
#import "PGResultRow.h"
#import "PGResultRow+Private.h"

@implementation PGResult
+ (instancetype)PG_resultWithResult: (PGresult *)result
{
	return [[[self alloc] PG_initWithResult: result] autorelease];
}

- (instancetype)PG_initWithResult: (PGresult *)result
{
	self = [super init];

	_result = result;

	return self;
}
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
}

- (id)objectAtIndex: (size_t)index
{
	if (index > PQntuples(_result))
		@throw [OFOutOfRangeException exception];

	return [PGResultRow rowWithResult: self
				      row: (int)index];
}

- (PGresult *)PG_result
{
	return _result;
}
@end







|
|







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}

- (id)objectAtIndex: (size_t)index
{
	if (index > PQntuples(_result))
		@throw [OFOutOfRangeException exception];

	return [PGResultRow PG_rowWithResult: self
					 row: (int)index];
}

- (PGresult *)PG_result
{
	return _result;
}
@end

Added src/PGResultRow+Private.h.

























>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
#import "PGResultRow.h"

OF_ASSUME_NONNULL_BEGIN

@interface PGResultRow ()
+ (instancetype)PG_rowWithResult: (PGResult *)result
			     row: (int)row;
- PG_initWithResult: (PGResult *)result
		row: (int)row OF_METHOD_FAMILY(init);
@end

OF_ASSUME_NONNULL_END

Changes to src/PGResultRow.h.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <libpq-fe.h>

#import <ObjFW/ObjFW.h>

#import "PGResult.h"

OF_ASSUME_NONNULL_BEGIN

@interface PGResultRow: OFDictionary
{
	PGResult *_result;
	PGresult *_res;
	int _row;
}

+ rowWithResult: (PGResult *)result
	    row: (int)row;
- initWithResult: (PGResult *)result
	     row: (int)row;
@end

OF_ASSUME_NONNULL_END








|





<
<
<
<
<



1
2
3
4
5
6
7
8
9
10
11
12
13
14





15
16
17
#include <libpq-fe.h>

#import <ObjFW/ObjFW.h>

#import "PGResult.h"

OF_ASSUME_NONNULL_BEGIN

@interface PGResultRow: OFDictionary OF_GENERIC(OFString *, id)
{
	PGResult *_result;
	PGresult *_res;
	int _row;
}





@end

OF_ASSUME_NONNULL_END

Changes to src/PGResultRow.m.

1

2
3
4
5
6
7
8
#import "PGResultRow.h"


static id
convertType(PGresult *res, int column, OFString *string)
{
	switch (PQftype(res, column)) {
	case 16:  /* BOOLOID */
		if ([string isEqual: @"t"])

>







1
2
3
4
5
6
7
8
9
#import "PGResultRow.h"
#import "PGResult+Private.h"

static id
convertType(PGresult *res, int column, OFString *string)
{
	switch (PQftype(res, column)) {
	case 16:  /* BOOLOID */
		if ([string isEqual: @"t"])
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@interface PGResultRowKeyEnumerator: PGResultRowEnumerator
@end

@interface PGResultRowObjectEnumerator: PGResultRowEnumerator
@end

@implementation PGResultRow
+ rowWithResult: (PGResult *)result
	    row: (int)row
{
	return [[[self alloc] initWithResult: result
					 row: row] autorelease];
}

- initWithResult: (PGResult *)result
	     row: (int)row







|
|







42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@interface PGResultRowKeyEnumerator: PGResultRowEnumerator
@end

@interface PGResultRowObjectEnumerator: PGResultRowEnumerator
@end

@implementation PGResultRow
+ (instancetype)rowWithResult: (PGResult *)result
			  row: (int)row
{
	return [[[self alloc] initWithResult: result
					 row: row] autorelease];
}

- initWithResult: (PGResult *)result
	     row: (int)row

Changes to src/exceptions/PGException.m.

1

2
3
4
5
6
7
8
#import "PGException.h"


@implementation PGException
@synthesize connection = _connection;

+ (instancetype)exceptionWithConnection: (PGConnection *)connection
{
	return [[[self alloc] initWithConnection: connection] autorelease];

>







1
2
3
4
5
6
7
8
9
#import "PGException.h"
#import "PGConnection+Private.h"

@implementation PGException
@synthesize connection = _connection;

+ (instancetype)exceptionWithConnection: (PGConnection *)connection
{
	return [[[self alloc] initWithConnection: connection] autorelease];