︙ | | |
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
|