-
Notifications
You must be signed in to change notification settings - Fork 5
/
PGSQLConnection.m
664 lines (530 loc) · 15.4 KB
/
PGSQLConnection.m
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
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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//
// PGSQLConnection.m
// PGSQLKit
//
// Created by Andy Satori on 5/8/07.
// Copyright 2007 Druware Software Designs. All rights reserved.
//
#import "PGSQLConnection.h"
#include "libpq-fe.h"
#import <sys/time.h>
#import <Security/Security.h>
#import <Foundation/Foundation.h>
#import <stdlib.h>
// When a pqlib notice is raised this function gets called
void
handle_pq_notice(void *arg, const char *message)
{
PGSQLConnection *theConn = (PGSQLConnection *) arg;
//NSLog(@"%s", message);
[theConn appendSQLLog:[NSString stringWithFormat: @"Notice: %s\n", message]];
}
@interface PGSQLConnection (Private)
- (PGresult *) openResult:(NSString *)sql numberOfArguments:(int)nParams withParameters:(va_list)list firstParam:(id)params;
@end
@implementation PGSQLConnection
NSString *const PGSQLConnectionDidCompleteNotification = @"PGSQLConnectionDidCompleteNotification";
NSString *const PGSQLCommandDidCompleteNotification = @"PGSQLCommandDidCompleteNotification";
#pragma mark Class Methods
+(id)defaultConnection
{
if (globalPGSQLConnection == nil)
{
return nil;
}
return globalPGSQLConnection;
}
#pragma mark Instance Methods
-(id)init
{
self = [super init];
if (self != nil) {
isConnected = NO;
errorDescription = nil;
sqlLog = [[NSMutableString alloc] init];
// this will default to NSUTF8StringEncoding with PG9
defaultEncoding = NSMacOSRomanStringEncoding;
pgconn = nil;
host = [[NSString alloc] initWithString:@"localhost"];
port = [[NSString alloc] initWithString:@"5432"];
options = nil;
tty = nil;
dbName = [[NSString alloc] initWithString:@"template1"];
userName = nil;
password = nil;
sslMode = nil;
service = nil;
krbsrvName = nil;
connectionString = nil;
commandStatus = nil;
if (globalPGSQLConnection == nil)
{
[self retain];
globalPGSQLConnection = self;
}
}
return self;
}
-(void)dealloc
{
[self close];
[host release];
[port release];
[options release];
[tty release];
[dbName release];
[userName release];
[password release];
[sslMode release];
[service release];
[krbsrvName release];
[connectionString release];
[errorDescription release];
[commandStatus release];
[sqlLog release];
[super dealloc];
}
- (void)connectAsync
{
// perform the connection on a thread
[NSThread detachNewThreadSelector:@selector(performConnectThread) toTarget:self withObject:nil];
}
- (void)performConnectThread
{
// allocate the thread, begin the connection and send the notification when done.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableDictionary *info = [[[NSMutableDictionary alloc] init] autorelease];
if ([self connect])
{
[info setValue:nil forKey:@"Error"];
} else {
[info setValue:[self lastError] forKey:@"Error"];
}
[self performSelectorOnMainThread:@selector(connectThreadResults:) withObject:info waitUntilDone:YES];
[pool release];
}
- (void)connectThreadResults:(NSMutableDictionary *)info {
[[NSNotificationCenter defaultCenter] postNotificationName:PGSQLConnectionDidCompleteNotification
object:self
userInfo:info];
}
- (BOOL)connect {
// replace with postgres connect code
[self close];
if (connectionString == nil)
{
connectionString = [self makeConnectionString];
[connectionString retain];
}
NSAssert( (connectionString != nil), @"Attempted to connect to PostgreSQL with empty connectionString.");
pgconn = (PGconn *)PQconnectdb([connectionString cStringUsingEncoding:NSUTF8StringEncoding]);
#ifdef DEBUG
if (PQoptions(pgconn))
{
NSLog(@"Options: %s", PQoptions(pgconn));
}
#endif
if (PQstatus(pgconn) == CONNECTION_BAD)
{
errorDescription = [NSString stringWithFormat:@"%s", PQerrorMessage(pgconn)];
[errorDescription retain];
NSLog(@"Connection to database '%@' failed.", dbName);
NSLog(@"\t%@", errorDescription);
[self appendSQLLog:[NSString stringWithFormat:@"Connection to database %@ Failed.\n", dbName]];
[self appendSQLLog:[NSString stringWithFormat:@"Connection string: %@\n\n", connectionString]];
// append error too??
PQfinish(pgconn);
pgconn = nil;
isConnected = NO;
return NO;
}
// TODO if good connection should we remove password from memory
// or should it be encrypted?
// TODO password should be asked for in dialog used and then erased?
if (errorDescription)
{
[errorDescription release];
errorDescription = nil;
}
// set up notification
PQsetNoticeProcessor(pgconn, handle_pq_notice, self);
if (sqlLog != nil) {
[sqlLog release];
}
sqlLog = [[NSMutableString alloc] init];
[self appendSQLLog:[NSString stringWithFormat:@"Connected to database %@.\n", dbName]];
isConnected = YES;
return YES;
}
- (BOOL)close
{
if (pgconn == nil) { return NO; }
if (isConnected == NO) { return NO; }
[self appendSQLLog:[NSString stringWithString:@"Disconnected from database.\n"]];
PQfinish(pgconn);
pgconn = nil;
isConnected = NO;
return YES;
}
- (BOOL)reset
{
PQreset(pgconn);
return PQstatus(pgconn) == CONNECTION_OK;
}
void parseParameters(int numArgs, va_list args, Oid *paramTypes, const char **paramValues, int *paramLengths, int *paramFormats, id firstArg) {
id arg = firstArg;
for (int i = 0; i < numArgs; i++) {
paramTypes[i] = 0; // autodetect datatype
paramFormats[i] = 0; // default to textual representation
NSString *val = nil;
if ([arg isKindOfClass:[NSData class]]) {
paramFormats[i] = 1;
paramValues[i] = [arg bytes];
paramLengths[i] = [arg length];
} else {
val = arg ? [arg description] : NULL;
paramValues[i] = [val cStringUsingEncoding:NSUTF8StringEncoding];
paramLengths[i] = 0; // unused for text encoding
}
arg = va_arg(args, id);
}
}
- (PGresult *) openResult:(NSString *)sql numberOfArguments:(int)nParams withParameters:(va_list)list firstParam:(id)params
{
PGresult* res;
Oid paramTypes[nParams];
const char *paramValues[nParams];
int paramLengths[nParams];
int paramFormats[nParams];
parseParameters(nParams, list, paramTypes, paramValues, paramLengths, paramFormats, params);
if(errorDescription) {
[errorDescription release];
errorDescription = nil;
}
if(commandStatus) {
[commandStatus release];
commandStatus = nil;
}
if (pgconn == nil)
{
errorDescription = [NSString stringWithString:@"Object is not Connected."];
[errorDescription retain];
[[NSException exceptionWithName:@"PGSQLError" reason:errorDescription userInfo:nil] raise];
return NO;
}
res = PQexecParams(pgconn, [sql cStringUsingEncoding:defaultEncoding], nParams, paramTypes, paramValues, paramLengths, paramFormats, 0);
if (res == nil)
{
errorDescription = [NSString stringWithString:@"ERROR: No response (PGRES_FATAL_ERROR)"];
[errorDescription retain];
return NO;
}
ExecStatusType status = PQresultStatus(res);
if (status == PGRES_BAD_RESPONSE || status == PGRES_NONFATAL_ERROR || status == PGRES_FATAL_ERROR)
{
errorDescription = [NSString stringWithFormat:@"%s", PQerrorMessage(pgconn)];
[errorDescription retain];
[[NSException exceptionWithName:@"PGSQLError" reason:errorDescription userInfo:nil] raise];
PQclear(res);
return NULL;
}
if (strlen(PQcmdStatus(res)))
{
commandStatus = [NSString stringWithFormat:@"%s", PQcmdStatus(res)];
[commandStatus retain];
[self appendSQLLog:[NSString stringWithFormat:@"%@\n", commandStatus]];
}
return res;
}
- (void)execCommandAsync:(NSString *)sql
{
// perform the connection on a thread
[NSThread detachNewThreadSelector:@selector(performExecCommand:) toTarget:self withObject:sql];
}
- (void)performExecCommand:(id)sqlCommand
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *sql = (NSString *)sqlCommand;
NSMutableDictionary *info = [[[NSMutableDictionary alloc] init] autorelease];
NSNumber *recordCount = [[[NSNumber alloc] initWithInt:[self execCommand:sql]] autorelease];
[info setValue:recordCount forKey:@"RecordCount"];
[info setValue:[self lastError] forKey:@"Error"];
[info setValue:[self lastCmdStatus] forKey:@"Status"];
[[NSNotificationCenter defaultCenter] postNotificationName:PGSQLCommandDidCompleteNotification
object:nil
userInfo:info];
[pool release];
}
- (BOOL)execCommand:(NSString *)sql
{
return [self execCommand:sql numberOfArguments:0 withParameters:nil];
}
- (BOOL)execCommand:(NSString *)sql numberOfArguments:(int)nParams withParameters:(id)params,...
{
PGresult* res;
va_list list;
va_start(list, params);
res = [self openResult:sql numberOfArguments:nParams withParameters:list firstParam:params];
va_end(list);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
errorDescription = [NSString stringWithFormat:@"%s", PQerrorMessage(pgconn)];
[errorDescription retain];
[[NSException exceptionWithName:@"PGSQLError" reason:errorDescription userInfo:nil] raise];
PQclear(res);
return NO;
}
PQclear(res);
return YES;
}
- (void)openAsync:(NSString *)sql
{
// perform the connection on a thread
[NSThread detachNewThreadSelector:@selector(performOpen:) toTarget:self withObject:sql];
}
- (void)performOpen:(id)sqlCommand
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *sql = (NSString *)sqlCommand;
NSMutableDictionary *info = [[[NSMutableDictionary alloc] init] autorelease];
PGSQLRecordset *rs = [self open:sql];
[info setValue:rs forKey:@"Recordset"];
[info setValue:[self lastError] forKey:@"Error"];
[[NSNotificationCenter defaultCenter] postNotificationName:PGSQLCommandDidCompleteNotification
object:nil
userInfo:info];
[pool release];
}
- (PGSQLRecordset *)open:(NSString *)sql
{
return [self open:sql numberOfArguments:0 withParameters:nil];
}
- (PGSQLRecordset *)open:(NSString *)sql numberOfArguments:(int)nParams withParameters:(id)params, ...
{
PGresult* res;
va_list list;
va_start(list, params);
res = [self openResult:sql numberOfArguments:nParams withParameters:list firstParam:params];
va_end(list);
switch (PQresultStatus(res))
{
case PGRES_TUPLES_OK:
{
// build the recordset
PGSQLRecordset *rs = [[[PGSQLRecordset alloc] initWithResult:res] autorelease];
[rs setDefaultEncoding:defaultEncoding];
if (logInfo)
{
long nRecords = PQntuples(res);
[self appendSQLLog:[NSString stringWithFormat: @"%d rows affected.\n\n", nRecords]];
}
return rs;
break;
}
case PGRES_COMMAND_OK:
{
if (logInfo)
{
[self appendSQLLog:@"Query ran successfully.\n"];
}
PQclear(res);
return nil;
break;
}
case PGRES_EMPTY_QUERY:
{
[self appendSQLLog:@"Postgres reported Empty Query\n"];
PQclear(res);
return nil;
break;
}
case PGRES_COPY_OUT:
case PGRES_COPY_IN:
default:
{
errorDescription = [NSString stringWithFormat:@"PostgreSQL Error: %s", PQresultErrorMessage(res)];
[errorDescription retain];
[self appendSQLLog:[NSString stringWithFormat:@"%@\n", errorDescription]];
[[NSException exceptionWithName:@"PGSQLError" reason:errorDescription userInfo:nil] raise];
PQclear(res);
return nil;
}
}
}
-(NSMutableString *)makeConnectionString
{
NSMutableString *connStr = [[[NSMutableString alloc] init] autorelease];
if (connectionString)
{
[connStr appendString:connectionString];
return connStr;
}
if (host)
{
[connStr appendFormat:@" host='%@' ", host];
}
if (port)
{
[connStr appendFormat:@" port='%@' ", port];
}
if (options)
{
[connStr appendFormat:@" options='%@' ", options];
}
if (dbName)
{
[connStr appendFormat:@" dbname='%@' ", dbName];
}
if (userName)
{
[connStr appendFormat:@" user='%@' ", userName];
}
if (password)
{
[connStr appendFormat:@" password='%@' ", password];
}
if (sslMode)
{
[connStr appendFormat:@" sslmode='%@' ", sslMode];
}
if (service)
{
[connStr appendFormat:@" service='%@' ", service];
}
if (krbsrvName)
{
[connStr appendFormat:@" krbsrvname='%@' ", krbsrvName];
}
return connStr;
}
-(NSString *)sqlEncodeData:(NSData *)toEncode
{
unsigned char *result;
size_t resultLength = 0;
result = PQescapeByteaConn ((PGconn *)pgconn, (const unsigned char *)[toEncode bytes],
[toEncode length], &resultLength);
NSString *encodedString = [[[NSString alloc] initWithCString:(const char *)result] autorelease];
PQfreemem(result);
return encodedString;
}
-(NSData *)sqlDecodeData:(NSData *)toDecode
{
unsigned char *result;
size_t resultLength = 0;
result = PQunescapeBytea((const unsigned char *)[toDecode bytes], &resultLength);
NSData *decodedData = [[[NSData alloc] initWithBytes:result length:resultLength] autorelease];
PQfreemem(result);
return decodedData;
}
-(NSString *)sqlEncodeString:(NSString *)toEncode
{
size_t result;
int error;
char *sqlEncodeCharArray = malloc(1 + ([toEncode length] * 2)); // per the libpq doc.
const char *sqlCharArrayToEncode = [toEncode cStringUsingEncoding:defaultEncoding];
size_t length = strlen(sqlCharArrayToEncode);
result = PQescapeStringConn ((PGconn *)pgconn, sqlEncodeCharArray,
(const char *)[toEncode cStringUsingEncoding:defaultEncoding],
length, &error);
NSString *encodedString = [[[NSString alloc] initWithCString:sqlEncodeCharArray] autorelease];
free(sqlEncodeCharArray);
return encodedString;
}
- (void)appendSQLLog:(NSString *)value {
NSLog(@"PGSQL: %@", value);
if (sqlLog == nil)
{
sqlLog = [[NSMutableString alloc] initWithString:value];
}
else
{
[sqlLog appendString:value];
}
}
#pragma mark Dictionary Tools
- (BOOL)insertIntoTable:(NSString *)table fromDictionary:(NSDictionary *)dict
{
return NO;
}
- (BOOL)updateTable:(NSString *)table fromDictionary:(NSDictionary *)dict
{
return NO;
}
#pragma mark Property Accessors
- (BOOL)isConnected {
return isConnected;
}
- (NSString *)connectionString {
return [[connectionString retain] autorelease];
}
- (void)setConnectionString:(NSString *)value {
if (connectionString != value) {
[connectionString release];
connectionString = [value copy];
}
}
- (NSString *)userName {
return [[userName retain] autorelease];
}
- (void)setUserName:(NSString *)value {
if (userName != value) {
[userName release];
userName = [value copy];
}
}
- (NSString *)password {
return [[password retain] autorelease];
}
- (void)setPassword:(NSString *)value {
if (password != value) {
[password release];
password = [value copy];
}
}
- (NSString *)server {
return [[host retain] autorelease];
}
- (void)setServer:(NSString *)value {
if (host != value) {
[host release];
host = [value copy];
}
}
-(NSString *)port {
return [[port retain] autorelease];
}
-(void)setPort:(NSString *)value {
if (port != value) {
[port release];
port = [value copy];
}
}
-(NSString *)databaseName {
return [[dbName retain] autorelease];
}
-(void)setDatabaseName:(NSString *)value {
if (dbName != value) {
[dbName release];
dbName = [value copy];
}
}
- (NSString *)lastError {
return errorDescription;
}
-(NSString *)lastCmdStatus {
return commandStatus;
}
- (NSMutableString *)sqlLog {
return sqlLog;
}
-(NSStringEncoding)defaultEncoding
{
return defaultEncoding;
}
-(void)setDefaultEncoding:(NSStringEncoding)value
{
if (defaultEncoding != value) {
defaultEncoding = value;
}
}
@end