baseten / Sources / BXPGTransactionHandler.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//
// BXPGTransactionHandler.m
// BaseTen
//
// Copyright (C) 2006-2008 Marko Karppinen & Co. LLC.
//
// Before using this software, please review the available licensing options
// by visiting http://basetenframework.org/licensing/ or by contacting
// us at sales@karppinen.fi. Without an additional license, this software
// may be distributed only in compliance with the GNU General Public License.
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
// $Id$
//

#import "PGTS.h"
#import "PGTSAdditions.h"
#import "PGTSHOM.h"
#import "PGTSOids.h"

#import "BXInterface.h"
#import "BXProbes.h"
#import "BXLogger.h"
#import "BXEnumerate.h"

#import "BXPGCertificateVerificationDelegate.h"
#import "BXPGTransactionHandler.h"
#import "BXPGAdditions.h"
#import "BXPGConnectionResetRecoveryAttempter.h"
#import "BXPGModificationHandler.h"
#import "BXPGLockHandler.h"
#import "BXPGClearLocksHandler.h"
#import "BXPGDatabaseDescription.h"
#import "BXPGInterface.h"
#import "BXPGCertificateVerificationDelegate.h"
#import "BXLocalizedString.h"

#import "BXEntityDescriptionPrivate.h"


NSString* kBXPGUserInfoKey = @"kBXPGUserInfoKey";
NSString* kBXPGDelegateKey = @"kBXPGDelegateKey";
NSString* kBXPGCallbackSelectorStringKey = @"kBXPGCallbackSelectorStringKey";


static NSString* 
SSLMode (enum BXSSLMode mode)
{
	NSString* retval = @"require";
	switch (mode) 
	{
		case kBXSSLModeDisable:
			retval = @"disable";
			break;
			
		case kBXSSLModePrefer:
		default:
			break;
	}
	return retval;
}


@interface BXPGResultSetPlaceholder : NSObject <BXPGResultSetPlaceholder>
{
	id mUserInfo;
	BOOL mDidSucceed;
}
- (void) setUserInfo: (id) anObject;
- (void) setQuerySucceeded: (BOOL) aBool;
- (BOOL) querySucceeded;
- (id) userInfo;
@end


@implementation BXPGResultSetPlaceholder
- (void) dealloc
{
	[mUserInfo release];
	[super dealloc];
}

- (void) setUserInfo: (id) anObject
{
	if (mUserInfo != anObject)
	{
		[mUserInfo release];
		mUserInfo = [anObject retain];
	}
}

- (void) setQuerySucceeded: (BOOL) aBool
{
	mDidSucceed = aBool;
}

- (BOOL) querySucceeded
{
	return mDidSucceed;
}

- (id) userInfo
{
	return mUserInfo;
}

- (NSError *) error
{
	return nil;
}
@end


@implementation BXPGTransactionHandler
- (BOOL) logsQueries
{
	return [mConnection logsQueries];
}

- (void) setLogsQueries: (BOOL) shouldLog
{
	[mConnection setLogsQueries: shouldLog];
}

- (void) sendPlaceholderResultTo: (id) receiver callback: (SEL) callback 
					   succeeded: (BOOL) didSucceed userInfo: (id) userInfo
{
	id arg = [[[BXPGResultSetPlaceholder alloc] init] autorelease];
	[arg setQuerySucceeded: didSucceed];
	[arg setUserInfo: userInfo];
	[receiver performSelector: callback withObject: arg];
}

- (void) forwardResult: (id) result
{
	ExpectV (result);
	
	NSDictionary* userInfoDict = [result userInfo];
	
	id receiver = [userInfoDict objectForKey: kBXPGDelegateKey];
	id receiverUserInfo = [userInfoDict objectForKey: kBXPGUserInfoKey];
	SEL callback = NSSelectorFromString ([userInfoDict objectForKey: kBXPGCallbackSelectorStringKey]);
	
	[result setUserInfo: receiverUserInfo];
	[receiver performSelector: callback withObject: result];
}

- (void) reloadDatabaseMetadata
{
	[mConnection reloadDatabaseDescription];
}

- (void) dealloc
{
	[mCertificateVerificationDelegate release];
	[mConnection release];
	[mObservedEntities release];
	[mObservers release];
	[mChangeHandlers release];
	[mLockHandlers release];
	[super dealloc];
}

- (PGTSConnection *) connection
{
	return mConnection;
}

- (void) setInterface: (BXPGInterface *) interface
{
	mInterface = interface;
}

- (BXPGInterface *) interface
{
	return mInterface;
}

- (BOOL) connected
{
	return (CONNECTION_OK == [mConnection connectionStatus]);
}

- (BXPGDatabaseDescription *) databaseDescription
{
	return (id) [mConnection databaseDescription];
}

- (BOOL) isAsync
{
	return mAsync;
}

- (BOOL) isSSLInUse
{
	return ([mConnection SSLStruct] ? YES : NO);
}

- (void) markLocked: (BXEntityDescription *) entity 
	  relationAlias: (NSString *) alias
		 fromClause: (NSString *) fromClause
		whereClause: (NSString *) whereClause 
		 parameters: (NSArray *) parameters
		 willDelete: (BOOL) willDelete
{
	[super doesNotRecognizeSelector: _cmd];
}

- (void) markLocked: (BXEntityDescription *) entity 
	  relationAlias: (NSString *) alias
		 fromClause: (NSString *) fromClause
		whereClause: (NSString *) whereClause 
		 parameters: (NSArray *) parameters
		 willDelete: (BOOL) willDelete
		 connection: (PGTSConnection *) connection 
   notifyConnection: (PGTSConnection *) notifyConnection
{
	ExpectV (entity);
	ExpectV (whereClause);
	
	if (PQTRANS_INTRANS == [connection transactionStatus])
	{
		NSString* funcname = [[mLockHandlers objectForKey: entity] lockFunctionName];
		
		//Lock type
		NSString* format = @"SELECT %@ ('U', %u, %@) FROM %@ WHERE %@";
		if (willDelete)
			format = @"SELECT %@ ('D', %u, %@) FROM %@ WHERE %@";
		
		//Table
		NSError* localError = nil;
		BXPGTableDescription* table = [mInterface tableForEntity: entity];
		BXAssertVoidReturn (table, @"Expected to get a table description. Error: %@", localError);

		//Get and sort the primary key fields.
		NSArray* pkeyFields = [[[[table primaryKey] columns] allObjects] sortedArrayUsingSelector: @selector (indexCompare:)];
		BXAssertVoidReturn (nil != pkeyFields, @"Expected to know the primary key.");
		NSArray* pkeyNames = (id) [[pkeyFields PGTSCollect] name];
		NSArray* attrs = [[entity attributesByName] objectsForKeys: pkeyNames notFoundMarker: [NSNull null]];
		ExpectV (! [attrs containsObject: [NSNull null]]);
		NSString* fieldList = BXPGReturnList (attrs, alias, YES);
		
		//Execute the query.
		NSString* query = [NSString stringWithFormat: format, funcname, 0, fieldList, fromClause, whereClause];
		[notifyConnection sendQuery: query delegate: nil callback: NULL parameterArray: parameters]; 			
	}
}

- (BOOL) canSend: (NSError **) outError
{
	ExpectR (outError, NO);
	BOOL retval = [mConnection canSend];
	if (! retval)
	{
		NSString* title = BXLocalizedString (@"networkError", @"Network Error", @"Title for a sheet");
		NSString* description = BXLocalizedString (@"networkErrorDescription", 
												   @"The database server can no longer be reached.", 
												   @"Explanation");
		
		NSMutableDictionary* userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
										 description, NSLocalizedFailureReasonErrorKey,
										 description, NSLocalizedRecoverySuggestionErrorKey,
										 title, NSLocalizedDescriptionKey,
										 [mInterface databaseContext], kBXDatabaseContextKey,
										 nil];
		
		NSError* error = [NSError errorWithDomain: kBXErrorDomain code: kBXErrorGenericNetworkError userInfo: userInfo];
		*outError = error;
	}
	return retval;
}

#pragma mark Connecting

- (void) didDisconnect
{
	[mObservedEntities removeAllObjects];
	[mObservers removeAllObjects];
	[mChangeHandlers removeAllObjects];
	[mLockHandlers removeAllObjects];
}


static NSString*
ConnectionString (NSDictionary* connectionDict)
{
	NSMutableString* connectionString = [NSMutableString string];
	NSEnumerator* e = [connectionDict keyEnumerator];
	NSString* currentKey;
	NSString* format = @"%@ = '%@' ";
	while ((currentKey = [e nextObject]))
	{
		if ([kPGTSConnectionDictionaryKeys containsObject: currentKey])
			[connectionString appendFormat: format, currentKey, [connectionDict objectForKey: currentKey]];
	}
	return connectionString;
}


- (NSString *) connectionString
{
	BXDatabaseContext* ctx = [mInterface databaseContext];
	NSURL* databaseURI = [ctx databaseURI];
	NSMutableDictionary* connectionDict = [databaseURI BXPGConnectionDictionary];

	enum BXSSLMode sslMode = [ctx sslMode];
	[connectionDict setValue: SSLMode (sslMode) forKey: kPGTSSSLModeKey];
	
	[connectionDict setValue: @"10" forKey: kPGTSConnectTimeoutKey];
	
	return ConnectionString (connectionDict);
}


- (void) prepareForConnecting
{
	mSyncErrorPtr = NULL;
	mConnectionSucceeded = NO;
	
	if (! mCertificateVerificationDelegate)
	{
		mCertificateVerificationDelegate = [[BXPGCertificateVerificationDelegate alloc] init];
		[mCertificateVerificationDelegate setHandler: self];
	}	
	
	if (! mConnection)
	{
		mConnection = [[PGTSConnection alloc] init];
		[mConnection setDelegate: self];
		[mConnection setCertificateVerificationDelegate: mCertificateVerificationDelegate];
		[mConnection setLogsQueries: [mInterface logsQueries]];
	}	
}


- (void) refreshDatabaseDescription
{
	[mConnection reloadDatabaseDescription];
}


- (void) handleConnectionErrorFor: (PGTSConnection *) failedConnection
{
	ExpectV (mAsync || mSyncErrorPtr);

	NSError* error = [failedConnection connectionError];
	if (mAsync)
		[mInterface connectionFailed: error];
	else
		*mSyncErrorPtr = error;
}


- (NSError *) connectionError: (NSError *) error recoveryAttempterClass: (Class) aClass
{
	//FIXME: move (parts of) this to the context.
	
	BXPGConnectionRecoveryAttempter* attempter = [[[aClass alloc] init] autorelease];
	attempter->mHandler = self;
	
	NSMutableDictionary* userInfo = [NSMutableDictionary dictionary];
	[userInfo setObject: attempter forKey: NSRecoveryAttempterErrorKey];
	[userInfo setObject: @"Database Error" forKey: NSLocalizedDescriptionKey]; //FIXME: localization.
	[userInfo setObject: @"Connection to the database was lost." forKey: NSLocalizedFailureReasonErrorKey];
	[userInfo setObject: @"Connection to the database was lost." forKey: NSLocalizedRecoverySuggestionErrorKey];
	if (error) [userInfo setObject: error forKey: NSUnderlyingErrorKey];
	
	NSArray* options = [NSArray arrayWithObjects: @"Try to Reconnect", @"Continue", nil]; //FIXME: localization.
	[userInfo setObject: options forKey: NSLocalizedRecoveryOptionsErrorKey];
	
	return [NSError errorWithDomain: kBXErrorDomain code: kBXErrorConnectionLost userInfo: userInfo];
}


- (void) handleSuccess
{
	mConnectionSucceeded = YES;
	if (mAsync)
	{
		[mInterface connectionSucceeded];
	}
	BXLogDebug (@"mConnection: %p", mConnection);
}


- (BOOL) connectSync: (NSError **) outError
{
	[self doesNotRecognizeSelector: _cmd];
	return NO;
}


- (void) connectAsync
{
	[self doesNotRecognizeSelector: _cmd];
}


- (void) disconnect
{
	[self doesNotRecognizeSelector: _cmd];
}


#pragma mark TransactionHelpers

- (NSString *) savepointQuery
{
    mSavepointIndex++;
    return [NSString stringWithFormat: @"SAVEPOINT BXPGSavepoint%u", mSavepointIndex];
}

- (NSString *) rollbackToSavepointQuery
{
	mSavepointIndex--;
    return [NSString stringWithFormat: @"ROLLBACK TO SAVEPOINT BXPGSavepoint%u", mSavepointIndex];
}

- (void) resetSavepointIndex
{
	mSavepointIndex = 0;
}

- (NSUInteger) savepointIndex
{
	return mSavepointIndex;
}


#pragma mark Transactions

- (void) beginIfNeededFor: (id) delegate callback: (SEL) callback userInfo: (id) userInfo
{
	PGTransactionStatusType status = [mConnection transactionStatus];
	switch (status) 
	{
		case PQTRANS_INTRANS:
            [self sendPlaceholderResultTo: delegate callback: callback succeeded: YES userInfo: userInfo];
			break;
			
		case PQTRANS_IDLE:
		{
			NSString* query = @"BEGIN";
            [mConnection sendQuery: query delegate: delegate callback: callback 
                    parameterArray: nil userInfo: userInfo];
			break;
		}
			
		default:
			[self sendPlaceholderResultTo: delegate callback: callback succeeded: NO userInfo: userInfo];
			//FIXME: set an error.
			BXLogError (@"Transaction status had a strange value: %d", status);
			break;
	}
}

- (BOOL) beginIfNeeded: (NSError **) outError
{
    ExpectR (outError, NO);
	BOOL retval = NO;
	PGTransactionStatusType status = [mConnection transactionStatus];
	switch (status) 
	{
		case PQTRANS_INTRANS:
			retval = YES;
			break;
			
		case PQTRANS_IDLE:
		{
			NSString* query = @"BEGIN";
            PGTSResultSet* res = [mConnection executeQuery: query];
				
            if ([res querySucceeded])
                retval = YES;
            else
                *outError = [res error];				

			break;
		}
			
		default:
			//FIXME: set an error.
			BXLogError (@"Transaction status had a strange value: %d", status);
			break;
	}
	return retval;
}


- (BOOL) save: (NSError **) outError
{
	[self doesNotRecognizeSelector: _cmd];
	return NO;
}


- (BOOL) rollback: (NSError **) outError
{
	[self doesNotRecognizeSelector: _cmd];
	return NO;
}


- (BOOL) savepointIfNeeded: (NSError **) outError
{
	[self doesNotRecognizeSelector: _cmd];
	return NO;
}


- (BOOL) rollbackToLastSavepoint: (NSError **) outError
{
	[self doesNotRecognizeSelector: _cmd];
	return NO;
}


- (BOOL) beginSubTransactionIfNeeded: (NSError **) outError
{
	[self doesNotRecognizeSelector: _cmd];
	return NO;
}


- (void) beginAsyncSubTransactionFor: (id) delegate callback: (SEL) callback userInfo: (NSDictionary *) userInfo
{
	[self doesNotRecognizeSelector: _cmd];
}


- (BOOL) endSubtransactionIfNeeded: (NSError **) outError
{
	[self doesNotRecognizeSelector: _cmd];
	return NO;
}


- (void) rollbackSubtransaction
{
	[self doesNotRecognizeSelector: _cmd];
}


- (BOOL) autocommits
{
	return NO;
}


#pragma mark Observing


- (BOOL) observeIfNeeded: (BXEntityDescription *) entity error: (NSError **) error
{
	[self doesNotRecognizeSelector: _cmd];
	return NO;
}


- (BOOL) observeIfNeeded: (BXEntityDescription *) entity connection: (PGTSConnection *) connection error: (NSError **) error
{
	ExpectR (error, NO);
	ExpectR (entity, NO);
	
	BOOL retval = NO;
	
	if ([mObservedEntities containsObject: entity])
		retval = YES;
	else
	{
		if (! mObservedEntities)
			mObservedEntities = [[NSMutableSet alloc] init];
		if (! mObservers)
			mObservers = [[NSMutableDictionary alloc] init];
		if (! mChangeHandlers)
			mChangeHandlers = [[NSMutableDictionary alloc] init];
		if (! mLockHandlers)
			mLockHandlers = [[NSMutableDictionary alloc] init];
		
		BXPGDatabaseDescription* database = (id) [connection databaseDescription];
		BXPGTableDescription* table = [mInterface tableForEntity: entity inDatabase: database];
		if (table && [self addClearLocksHandler: connection error: error])
		{
			//Start listening to notifications and get the notification names.
			id oid = PGTSOidAsObject ([table oid]);
			NSString* query = 
			@"SELECT "
			@" CURRENT_TIMESTAMP::TIMESTAMP WITHOUT TIME ZONE AS ts, "
			@" null AS relid, "
			@" null AS n_name, "
			@" null AS fn_name, "
			@" null AS t_name "
			@"UNION ALL "
			@"SELECT null, m.* FROM baseten.mod_observe ($1) m "
			@"UNION ALL "
			@"SELECT null, l.* FROM baseten.lock_observe ($1) l";
			PGTSResultSet* res = [connection executeQuery: query parameters: oid];
			if ([res querySucceeded] && 3 == [res count])
			{
				[res advanceRow];
				NSDate* lastCheck = [res valueForKey: @"ts"];
				
				{
					[res advanceRow];
					NSString* notificationName = [res valueForKey: @"n_name"];
					BXPGModificationHandler* handler = [[[BXPGModificationHandler alloc] init] autorelease];
					
					[handler setInterface: mInterface];
					[handler setConnection: connection];
					[handler setLastCheck: lastCheck];
					[handler setEntity: entity];
					[handler setNotificationName: notificationName];
					[handler setOid: [[res valueForKey: @"relid"] PGTSOidValue]];
					[handler prepare];
					
					[mObservers setObject: handler forKey: notificationName];
					[mChangeHandlers setObject: handler forKey: entity];
				}
				
				{
					[res advanceRow];
					NSString* notificationName = [res valueForKey: @"n_name"];
					NSString* functionName = [res valueForKey: @"fn_name"];
					NSString* tableName = [res valueForKey: @"t_name"];
					BXPGLockHandler* handler = [[[BXPGLockHandler alloc] init] autorelease];
					
					[handler setInterface: mInterface];
					[handler setConnection: connection];
					[handler setLastCheck: lastCheck];
					[handler setEntity: entity];
					[handler setNotificationName: notificationName];
					[handler setLockFunctionName: functionName];
					[handler setLockTableName: tableName];
					[handler prepare];
					
					[mObservers setObject: handler forKey: notificationName];
					[mLockHandlers setObject: handler forKey: entity];
				}
								
				[mObservedEntities addObject: entity];			
				retval = YES;
			}
			else
			{
				*error = [res error];
			}
		}
	}
	
	//Inheritance.
	BXEnumerate (currentEntity, e, [[entity inheritedEntities] objectEnumerator])
	{
		if (! retval)
			break;
		retval = [self observeIfNeeded: currentEntity connection: connection error: error];
	}
	
    return retval;
}


- (BOOL) addClearLocksHandler: (PGTSConnection *) connection 
						error: (NSError **) outError
{
	ExpectR (outError, NO);
	
	BOOL retval = NO;
	NSString* nname = [BXPGClearLocksHandler notificationName];
	if ([mObservers objectForKey: nname])
		retval = YES;
	else
	{
		NSString* query = [NSString stringWithFormat: @"LISTEN %@", [nname BXPGEscapedName: connection]];
		PGTSResultSet* res = [connection executeQuery: query];
		if ([res querySucceeded])
		{
			BXPGClearLocksHandler* handler = [[[BXPGClearLocksHandler alloc] init] autorelease];
			[handler setInterface: mInterface];
			[handler setConnection: connection];
			[handler prepare];
			[mObservers setObject: handler forKey: nname];
			
			retval = YES;
		}
		else
		{
			*outError = [res error];
		}
	}
	return retval;
}


- (void) checkSuperEntities: (BXEntityDescription *) entity connection: (PGTSConnection *) connection
{
	BXEnumerate (superEntity, e, [[entity inheritedEntities] objectEnumerator])
		[[mChangeHandlers objectForKey: superEntity] checkModifications: 0];
}


- (void) checkSuperEntities: (BXEntityDescription *) entity
{
	[self doesNotRecognizeSelector: _cmd];
}


- (NSArray *) observedOids
{
	NSMutableArray* retval = [NSMutableArray arrayWithCapacity: [mObservedEntities count]];
	BXEnumerate (currentEntity, e, [mObservedEntities objectEnumerator])
	{
		NSString* name = [currentEntity name];
		NSString* schemaName = [currentEntity schemaName];
		PGTSTableDescription* table = [[mConnection databaseDescription] table: name inSchema: schemaName];
		[retval addObject: PGTSOidAsObject ([table oid])];
	}
	return retval;
}

- (BOOL) usedPassword
{
	return [mConnection usedPassword];
}
@end


@implementation BXPGTransactionHandler (PGTSConnectionDelegate)
- (void) PGTSConnection: (PGTSConnection *) connection receivedNotice: (NSError *) notice
{
	//BXLogDebug (@"%p: %s", connection, message);
	if (BASETEN_RECEIVED_PG_NOTICE_ENABLED ())
	{
		NSString* message = [[notice userInfo] objectForKey: kPGTSErrorMessage];
		char* message_s = strdup ([message UTF8String]);
		BASETEN_RECEIVED_PG_NOTICE (connection, message_s);
		free (message_s);
	}
}

- (void) PGTSConnection: (PGTSConnection *) connection gotNotification: (PGTSNotification *) notification
{
	NSString* notificationName = [notification notificationName];
	BXLogDebug (@"Got notification (%p): %@", self, connection, notificationName);
	[[mObservers objectForKey: notificationName] handleNotification: notification];
}


- (void) PGTSConnectionLost: (PGTSConnection *) connection error: (NSError *) error
{
	[self doesNotRecognizeSelector: _cmd];
}


- (void) PGTSConnectionFailed: (PGTSConnection *) connection
{
	[self doesNotRecognizeSelector: _cmd];
}


- (void) PGTSConnectionEstablished: (PGTSConnection *) connection
{
	[self doesNotRecognizeSelector: _cmd];
}

- (FILE *) PGTSConnectionTraceFile: (PGTSConnection *) connection
{
	return [mInterface traceFile];
}

- (void) PGTSConnection: (PGTSConnection *) connection sentQueryString: (const char *) queryString
{
	[mInterface connection: connection sentQueryString: queryString];
}

- (void) PGTSConnection: (PGTSConnection *) connection sentQuery: (PGTSQuery *) query
{
	[mInterface connection: connection sentQuery: query];
}

- (void) PGTSConnection: (PGTSConnection *) connection receivedResultSet: (PGTSResultSet *) res
{
	[mInterface connection: connection receivedResultSet: res];
}

- (void) PGTSConnection: (PGTSConnection *) connection networkStatusChanged: (SCNetworkConnectionFlags) newFlags
{
	BXDatabaseContext* context = [mInterface databaseContext];
	[context networkStatusChanged: newFlags];
}
@end


@implementation BXPGTransactionHandler (BXPGTrustHandler)
- (BOOL) handleInvalidTrust: (SecTrustRef) trust result: (SecTrustResultType) result
{
	BOOL retval = NO;
	BXDatabaseContext* ctx = [mInterface databaseContext];
	if (mAsync)
	{
		CFRetain (trust);
		struct BXTrustResult trustResult = {trust, result};
		NSValue* resultValue = [NSValue valueWithBytes: &trustResult objCType: @encode (struct BXTrustResult)];				
		[ctx performSelectorOnMainThread: @selector (handleInvalidCopiedTrustAsync:) withObject: resultValue waitUntilDone: NO];
	}
	else
	{
		retval = [ctx handleInvalidTrust: trust result: result];
	}
	return retval;
}


- (void) handledTrust: (SecTrustRef) trust accepted: (BOOL) accepted
{
	if (! accepted)
	{
		[mCertificateVerificationDelegate setCertificates: nil];
	}
}
@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.