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
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
|
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
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
|
-
+
-
-
+
+
-
+
-
+
-
+
-
-
-
+
+
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
|
#import "PGConnection.h"
#import "PGConnectionFailedException.h"
#import "PGCommandFailedException.h"
@implementation PGConnection
- (void)dealloc
{
[parameters release];
[_parameters release];
if (conn != NULL)
PQfinish(conn);
if (_connnection != NULL)
PQfinish(_connnection);
[super dealloc];
}
- (void)setParameters: (OFDictionary*)parameters_
- (void)setParameters: (OFDictionary*)parameters
{
OF_SETTER(parameters, parameters_, YES, YES)
OF_SETTER(_parameters, parameters, YES, YES)
}
- (OFDictionary*)parameters
{
OF_GETTER(parameters, YES)
OF_GETTER(_parameters, YES)
}
- (void)connect
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFEnumerator *keyEnumerator = [parameters keyEnumerator];
OFEnumerator *objectEnumerator = [parameters objectEnumerator];
OFMutableString *conninfo = nil;
OFEnumerator *keyEnumerator = [_parameters keyEnumerator];
OFEnumerator *objectEnumerator = [_parameters objectEnumerator];
OFMutableString *connectionInfo = nil;
OFString *key, *object;
while ((key = [keyEnumerator nextObject]) != nil &&
(object = [objectEnumerator nextObject]) != nil) {
if (conninfo != nil)
[conninfo appendFormat: @" %@=%@", key, object];
if (connectionInfo != nil)
[connectionInfo appendFormat: @" %@=%@", key, object];
else
conninfo = [OFMutableString stringWithFormat:
connectionInfo = [OFMutableString stringWithFormat:
@"%@=%@", key, object];
}
if ((conn = PQconnectdb([conninfo UTF8String])) == NULL)
if ((_connnection = PQconnectdb([connectionInfo UTF8String])) == NULL)
@throw [OFOutOfMemoryException
exceptionWithClass: [self class]];
if (PQstatus(conn) == CONNECTION_BAD)
if (PQstatus(_connnection) == CONNECTION_BAD)
@throw [PGConnectionFailedException
exceptionWithClass: [self class]
connection: self];
[pool release];
}
- (void)reset
{
PQreset(conn);
PQreset(_connnection);
}
- (PGResult*)executeCommand: (OFConstantString*)command
{
PGresult *result = PQexec(conn, [command UTF8String]);
PGresult *result = PQexec(_connnection, [command UTF8String]);
if (PQresultStatus(result) == PGRES_FATAL_ERROR) {
PQclear(result);
@throw [PGCommandFailedException
exceptionWithClass: [self class]
connection: self
command: command];
|
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
-
+
|
} else if ([parameter isKindOfClass: [OFNull class]])
values[i++] = NULL;
else
values[i++] = [[parameter description]
UTF8String];
} while ((parameter = va_arg(args, id)) != nil);
result = PQexecParams(conn, [command UTF8String],
result = PQexecParams(_connnection, [command UTF8String],
argsCount, NULL, values, NULL, NULL, 0);
} @finally {
[self freeMemory: values];
}
[pool release];
|
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
192
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(conn, [command UTF8String], (int)count,
NULL, values, NULL, NULL, 0);
result = PQexecParams(_connnection, [command UTF8String],
(int)count, NULL, values, NULL, NULL, 0);
} @finally {
[self freeMemory: values];
}
[pool release];
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
|
227
228
229
230
231
232
233
234
235
236
|
227
228
229
230
231
232
233
234
235
236
|
-
+
|
intoTable: table];
[pool release];
}
- (PGconn*)PG_connection
{
return conn;
return _connnection;
}
@end
|