12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
-
-
-
-
+
+
+
+
-
-
+
+
-
+
|
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#import "PGConnection.h"
#import "PGConnection+Private.h"
#import "PGResult.h"
#import "PGResult+Private.h"
#import "PGSQLConnection.h"
#import "PGSQLConnection+Private.h"
#import "PGSQLResult.h"
#import "PGSQLResult+Private.h"
#import "PGConnectionFailedException.h"
#import "PGExecuteCommandFailedException.h"
#import "PGSQLConnectionFailedException.h"
#import "PGSQLExecuteCommandFailedException.h"
@implementation PGConnection
@implementation PGSQLConnection
@synthesize pg_connection = _connection, parameters = _parameters;
- (instancetype)init
{
self = [super init];
@try {
|
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
124
125
126
127
128
|
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
124
125
126
127
128
|
-
+
-
+
-
+
-
+
-
+
-
-
+
+
|
@"%@=%@", key, object];
}
if ((_connection = PQconnectdb(connectionInfo.UTF8String)) == NULL)
@throw [OFOutOfMemoryException exception];
if (PQstatus(_connection) == CONNECTION_BAD)
@throw [PGConnectionFailedException
@throw [PGSQLConnectionFailedException
exceptionWithConnection: self];
objc_autoreleasePoolPop(pool);
}
- (void)reset
{
PQreset(_connection);
}
- (void)close
{
if (_connection != NULL)
PQfinish(_connection);
_connection = NULL;
}
- (PGResult *)executeCommand: (OFConstantString *)command
- (PGSQLResult *)executeCommand: (OFConstantString *)command
{
PGresult *result = PQexec(_connection, command.UTF8String);
if (PQresultStatus(result) == PGRES_FATAL_ERROR) {
PQclear(result);
@throw [PGExecuteCommandFailedException
@throw [PGSQLExecuteCommandFailedException
exceptionWithConnection: self
command: command];
}
switch (PQresultStatus(result)) {
case PGRES_TUPLES_OK:
return [PGResult pg_resultWithResult: result];
return [PGSQLResult pg_resultWithResult: result];
case PGRES_COMMAND_OK:
PQclear(result);
return nil;
default:
PQclear(result);
@throw [PGExecuteCommandFailedException
@throw [PGSQLExecuteCommandFailedException
exceptionWithConnection: self
command: command];
}
}
- (PGResult *)executeCommand: (OFConstantString *)command
parameters: (id)parameter, ...
- (PGSQLResult *)executeCommand: (OFConstantString *)command
parameters: (id)parameter, ...
{
void *pool = objc_autoreleasePoolPush();
PGresult *result;
const char **values;
va_list args, args2;
int argsCount;
|
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
-
+
-
+
|
OFFreeMemory(values);
}
objc_autoreleasePoolPop(pool);
switch (PQresultStatus(result)) {
case PGRES_TUPLES_OK:
return [PGResult pg_resultWithResult: result];
return [PGSQLResult pg_resultWithResult: result];
case PGRES_COMMAND_OK:
PQclear(result);
return nil;
default:
PQclear(result);
@throw [PGExecuteCommandFailedException
@throw [PGSQLExecuteCommandFailedException
exceptionWithConnection: self
command: command];
}
}
@end
|