ObjPgSQL  Check-in [fc8e42990e]

Overview
Comment:Adjust to recent ObjFW changes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: fc8e42990e7f6476b8b806521222620bbac0496c61a91b20f9e3787fc20fdbb4
User & Date: js on 2017-05-09 23:19:20
Other Links: manifest | tags
Context
2017-05-10
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
2017-01-22
04:47
Adjust to ObjFW changes check-in: 959a1e5652 user: js tags: trunk
Changes

Modified src/PGConnection.h from [68ece51d08] to [89f43193e6].

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


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 *_parameters;
	OFDictionary OF_GENERIC(OFString *, OFString *) *_parameters;
}

@property (copy) OFDictionary *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*)rows
	 intoTable: (OFString*)table;
- (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

Modified src/PGConnection.m from [66118a42be] to [12a5dca2c4].

13
14
15
16
17
18
19
20
21
22





23
24
25
26
27
28
29
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
13
14
15
16
17
18
19



20
21
22
23
24
25
26
27
28
29
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







-
-
-
+
+
+
+
+



















-
+















-
+







	[self close];

	[super dealloc];
}

- (void)connect
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFEnumerator *keyEnumerator = [_parameters keyEnumerator];
	OFEnumerator *objectEnumerator = [_parameters objectEnumerator];
	void *pool = objc_autoreleasePoolPush();
	OFEnumerator OF_GENERIC(OFString *) *keyEnumerator =
	    [_parameters keyEnumerator];
	OFEnumerator OF_GENERIC(OFString *) *objectEnumerator =
	    [_parameters objectEnumerator];
	OFMutableString *connectionInfo = nil;
	OFString *key, *object;

	while ((key = [keyEnumerator nextObject]) != nil &&
	    (object = [objectEnumerator nextObject]) != nil) {
		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];

	[pool release];
	objc_autoreleasePoolPop(pool);
}

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

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

	_connnection = NULL;
}

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

	if (PQresultStatus(result) == PGRES_FATAL_ERROR) {
		PQclear(result);
		@throw [PGCommandFailedException
		    exceptionWithConnection: self
76
77
78
79
80
81
82
83
84


85
86

87
88
89
90
91
92
93
78
79
80
81
82
83
84


85
86
87

88
89
90
91
92
93
94
95







-
-
+
+

-
+







		PQclear(result);
		@throw [PGCommandFailedException
		    exceptionWithConnection: self
				    command: command];
	}
}

- (PGResult*)executeCommand: (OFConstantString*)command
		 parameters: (id)parameter, ...
- (PGResult *)executeCommand: (OFConstantString *)command
		  parameters: (id)parameter, ...
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	void *pool = objc_autoreleasePoolPush();
	PGresult *result;
	const char **values;
	va_list args, args2;
	int argsCount;

	va_start(args, parameter);
	va_copy(args2, args);
126
127
128
129
130
131
132
133

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150


151
152

153
154
155
156
157
158
159
128
129
130
131
132
133
134

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150


151
152
153

154
155
156
157
158
159
160
161







-
+















-
-
+
+

-
+








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

	[pool release];
	objc_autoreleasePoolPop(pool);

	switch (PQresultStatus(result)) {
	case PGRES_TUPLES_OK:
		return [PGResult PG_resultWithResult: result];
	case PGRES_COMMAND_OK:
		PQclear(result);
		return nil;
	default:
		PQclear(result);
		@throw [PGCommandFailedException
		    exceptionWithConnection: self
				    command: command];
	}
}

- (void)insertRow: (OFDictionary*)row
	intoTable: (OFString*)table
- (void)insertRow: (OFDictionary *)row
	intoTable: (OFString *)table
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	void *pool = objc_autoreleasePoolPush();
	OFMutableString *command;
	OFEnumerator *enumerator;
	const char **values;
	PGresult *result;
	OFString *key, *value;
	size_t i, count;

194
195
196
197
198
199
200
201

202
203
204
205
206
207
208
209
210
211
212
213
214


215
216
217
218

219
220
221
222
223

224
225

226
227

228
229
230
231
196
197
198
199
200
201
202

203
204
205
206
207
208
209
210
211
212
213
214


215
216
217



218


219
220

221


222


223
224
225
226
227







-
+











-
-
+
+

-
-
-
+
-
-


-
+
-
-
+
-
-
+





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

	[pool release];
	objc_autoreleasePoolPop(pool);

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

	PQclear(result);
}

- (void)insertRows: (OFArray*)rows
	 intoTable: (OFString*)table
- (void)insertRows: (OFArray OF_GENERIC(OFDictionary *) *)rows
	 intoTable: (OFString *)table
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFEnumerator *enumerator = [rows objectEnumerator];
	OFDictionary *row;
	for (OFDictionary *row in rows)

	while ((row = [enumerator nextObject]) != nil)
		[self insertRow: row
		      intoTable: table];

}
	[pool release];
}


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

Modified src/PGResult.h from [6294089c40] to [b70e0a8f15].

1
2
3
4




5

6
7
8
9
10
11
12



13


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
@interface PGResult: OFArray OF_GENERIC(PGResultRow *)
{
	PGresult *_result;
}

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

OF_ASSUME_NONNULL_END

Modified src/PGResult.m from [a066a8eda5] to [1b1566e654].

1
2
3
4
5

6
7
8
9
10

11
12
13
14
15
16
17
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
+ PG_resultWithResult: (PGresult *)result
{
	return [[[self alloc] PG_initWithResult: result] autorelease];
}

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

	_result = result;

	return self;
}
34
35
36
37
38
39
40
41

42
43
44
45
34
35
36
37
38
39
40

41
42
43
44
45







-
+




	if (index > PQntuples(_result))
		@throw [OFOutOfRangeException exception];

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

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

Modified src/PGResultRow.h from [f714b4a0e3] to [4c4b6ee6a8].

1
2
3
4
5


6
7
8
9
10
11
12
13
14

15
16

17
18


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
+ rowWithResult: (PGResult *)result
	    row: (int)row;
- initWithResult: (PGResult*)result
- initWithResult: (PGResult *)result
	     row: (int)row;
@end

OF_ASSUME_NONNULL_END

Modified src/PGResultRow.m from [1fee8156f8] to [2a3cc24231].

41
42
43
44
45
46
47
48

49
50
51
52
53
54
55

56
57
58
59
60
61
62
41
42
43
44
45
46
47

48
49
50
51
52
53
54

55
56
57
58
59
60
61
62







-
+






-
+







@interface PGResultRowKeyEnumerator: PGResultRowEnumerator
@end

@interface PGResultRowObjectEnumerator: PGResultRowEnumerator
@end

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

- initWithResult: (PGResult*)result
- initWithResult: (PGResult *)result
	     row: (int)row
{
	self = [super init];

	_result = [result retain];
	_res = [result PG_result];
	_row = row;
94
95
96
97
98
99
100
101

102
103
104
105
106
107
108

109
110
111
112
113
114
115
116

117
118
119
120
121
122
123
94
95
96
97
98
99
100

101
102
103
104
105
106
107

108
109
110
111
112
113
114
115

116
117
118
119
120
121
122
123







-
+






-
+







-
+







	if (PQgetisnull(_res, _row, column))
		return nil;

	return convertType(_res, column,
	    [OFString stringWithUTF8String: PQgetvalue(_res, _row, column)]);
}

- (OFEnumerator*)keyEnumerator
- (OFEnumerator *)keyEnumerator
{
	return [[[PGResultRowKeyEnumerator alloc]
	    initWithResult: _result
		       row: _row] autorelease];
}

- (OFEnumerator*)objectEnumerator
- (OFEnumerator *)objectEnumerator
{
	return [[[PGResultRowObjectEnumerator alloc]
	    initWithResult: _result
		       row: _row] autorelease];
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			   objects: (id *)objects
			     count: (int)count
{
	int i, j;

	if (state->extra[0] == 0) {
		state->extra[0] = 1;
		state->extra[1] = PQnfields(_res);
142
143
144
145
146
147
148
149

150
151
152
153
154
155
156
142
143
144
145
146
147
148

149
150
151
152
153
154
155
156







-
+







	state->mutationsPtr = (unsigned long*)self;

	return j;
}
@end

@implementation PGResultRowEnumerator
- initWithResult: (PGResult*)result
- initWithResult: (PGResult *)result
	     row: (int)row
{
	self = [super init];

	_result = [result retain];
	_res = [result PG_result];
	_row = row;

Modified src/exceptions/PGCommandFailedException.h from [c0757cbfd8] to [aa497f3805].

1


2
3
4
5
6
7
8

9
10
11
12
13




14


1
2
3
4
5
6
7
8
9

10
11




12
13
14
15
16
17
18

+
+






-
+

-
-
-
-
+
+
+
+

+
+
#import "PGException.h"

OF_ASSUME_NONNULL_BEGIN

@interface PGCommandFailedException: PGException
{
	OFString *_command;
}

@property (readonly, copy) OFString *command;
@property (readonly, nonatomic) OFString *command;

+ (instancetype)exceptionWithConnection: (PGConnection*)connection
				command: (OFString*)command;
- initWithConnection: (PGConnection*)connection
	     command: (OFString*)command;
+ (instancetype)exceptionWithConnection: (PGConnection *)connection
				command: (OFString *)command;
- initWithConnection: (PGConnection *)connection
	     command: (OFString *)command;
@end

OF_ASSUME_NONNULL_END

Modified src/exceptions/PGCommandFailedException.m from [7e22faf8ff] to [39366017cc].

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
32
33
34
35

36
37
38
39
40
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
32
33
34

35
36
37
38
39
40





-
-
+
+





-
-
+
+




















-
+





#import "PGCommandFailedException.h"

@implementation PGCommandFailedException
@synthesize command = _command;

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

- initWithConnection: (PGConnection*)connection
	     command: (OFString*)command
- initWithConnection: (PGConnection *)connection
	     command: (OFString *)command
{
	self = [super initWithConnection: connection];

	@try {
		_command = [command copy];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_command release];

	[super dealloc];
}

- (OFString*)description
- (OFString *)description
{
	return [OFString stringWithFormat: @"A PostgreSQL command failed: %@\n"
					   @"Command: %@", _error, _command];
}
@end

Modified src/exceptions/PGConnectionFailedException.h from [bfb4590c76] to [57af5c1f34].

1


2
3
4


1
2
3
4
5
6
7
8

+
+



+
+
#import "PGException.h"

OF_ASSUME_NONNULL_BEGIN

@interface PGConnectionFailedException: PGException
@end

OF_ASSUME_NONNULL_END

Modified src/exceptions/PGConnectionFailedException.m from [b1115b7f5a] to [386de510d2].

1
2
3
4

5
6
7
8
9
10
1
2
3

4
5
6
7
8
9
10



-
+






#import "PGConnectionFailedException.h"

@implementation PGConnectionFailedException
- (OFString*)description
- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Establishing a PostgreSQL connection failed:\n%@\n"
	    "Parameters: %@", _error, [_connection parameters]];
}
@end

Modified src/exceptions/PGException.h from [f5eecc1954] to [5ee22db605].

1
2
3


4
5
6
7
8
9
10
11

12
13
14


15


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

13
14


15
16
17
18
19



+
+







-
+

-
-
+
+

+
+
#import <ObjFW/ObjFW.h>

#import "PGConnection.h"

OF_ASSUME_NONNULL_BEGIN

@interface PGException: OFException
{
	PGConnection *_connection;
	OFString *_error;
}

@property (readonly, retain) PGConnection *connection;
@property (readonly, nonatomic) PGConnection *connection;

+ (instancetype)exceptionWithConnection: (PGConnection*)connection;
- initWithConnection: (PGConnection*)connection;
+ (instancetype)exceptionWithConnection: (PGConnection *)connection;
- initWithConnection: (PGConnection *)connection;
@end

OF_ASSUME_NONNULL_END

Modified src/exceptions/PGException.m from [0be804aea6] to [f39a379181].

1
2
3
4
5
6

7
8
9
10
11

12
13
14
15
16
17
18
1
2
3
4
5

6
7
8
9
10

11
12
13
14
15
16
17
18





-
+




-
+







#import "PGException.h"

@implementation PGException
@synthesize connection = _connection;

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

- initWithConnection: (PGConnection*)connection
- initWithConnection: (PGConnection *)connection
{
	self = [super init];

	@try {
		_connection = [connection retain];
		_error = [[OFString alloc]
		    initWithCString: PQerrorMessage([_connection PG_connection])
29
30
31
32
33
34
35
36

37
38
39
40
41
29
30
31
32
33
34
35

36
37
38
39
40
41







-
+





{
	[_connection release];
	[_error release];

	[super dealloc];
}

- (OFString*)description
- (OFString *)description
{
	return [OFString stringWithFormat: @"A PostgreSQL operation failed: %@",
					   _error];
}
@end