Skip to content

Commit 74b18ee

Browse files
committed
Work around an exception that would occur when changing Query Favorites while an unconnected connection tab existed (#2266)
1 parent c31bc03 commit 74b18ee

6 files changed

+37
-45
lines changed

Source/SPConstants.h

+1
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ extern NSString *SPSelectionDetailTypePrimaryKeyed;
451451
extern NSString *SPSSHEnableMuxingPreference;
452452
extern NSString *SPSSHClientPath;
453453
extern NSString *SPSSLCipherListKey;
454+
extern NSString *SPQueryFavoritesHaveBeenUpdatedNotification;
454455

455456
// URLs
456457
extern NSString *SPDonationsURL;

Source/SPConstants.m

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
NSString *SPSSHEnableMuxingPreference = @"SSHMultiplexingEnabled";
249249
NSString *SPSSHClientPath = @"SSHClientPath";
250250
NSString *SPSSLCipherListKey = @"SSLCipherList";
251+
NSString *SPQueryFavoritesHaveBeenUpdatedNotification = @"QueryFavoritesHaveBeenUpdatedNotification";
251252

252253
// URLs
253254
NSString *SPDonationsURL = @"http://www.sequelpro.com/donate/";

Source/SPCustomQuery.m

+34-28
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ @interface SPCustomQuery (PrivateAPI)
7171
- (id)_resultDataItemAtRow:(NSInteger)row columnIndex:(NSUInteger)column preserveNULLs:(BOOL)preserveNULLs asPreview:(BOOL)asPreview;
7272
+ (NSString *)linkToHelpTopic:(NSString *)aTopic;
7373
- (void)documentWillClose:(NSNotification *)notification;
74+
- (void)queryFavoritesHaveBeenUpdated:(NSNotification *)notification;
7475

7576
@end
7677

@@ -3391,38 +3392,38 @@ - (void)historyItemsHaveBeenUpdated:(id)manager
33913392
/**
33923393
* Called by the query favorites manager whenever the query favorites have been updated.
33933394
*/
3394-
- (void)queryFavoritesHaveBeenUpdated:(id)manager
3395+
- (void)queryFavoritesHaveBeenUpdated:(NSNotification *)notification
33953396
{
3396-
NSMenuItem *headerMenuItem;
3397+
NSURL *fileURL = [tableDocumentInstance fileURL];
3398+
3399+
// Warning: This method may be called before any connection has been made in the current tab (triggered by another tab)!
3400+
// There doesn't seem to be a real indicator for this, but fileURL is the closest thing plus we need it below (#2266)
3401+
if(!fileURL) return;
3402+
33973403
NSMenu *menu = [queryFavoritesButton menu];
33983404

33993405
// Remove all favorites beginning from the end
34003406
while([queryFavoritesButton numberOfItems] > 7)
34013407
[queryFavoritesButton removeItemAtIndex:[queryFavoritesButton numberOfItems]-1];
34023408

34033409
// Build document-based list
3404-
NSString *tblDocName = [[[[tableDocumentInstance fileURL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent];
3405-
if(!tblDocName) {
3406-
//NSMenuItem will not accept nil as title
3407-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
3408-
reason:[NSString stringWithFormat:@"Document name conversion resulted in nil string!? tableDocumentInstance=%@ fileURL=%@",tableDocumentInstance,[tableDocumentInstance fileURL]]
3409-
userInfo:nil];
3410-
}
3411-
headerMenuItem = [[NSMenuItem alloc] initWithTitle:tblDocName action:NULL keyEquivalent:@""];
3412-
[headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
3413-
[headerMenuItem setToolTip:[NSString stringWithFormat:NSLocalizedString(@"‘%@’ based favorites",@"Query Favorites : List : Section Heading : current connection document : tooltip (arg is the name of the spf file)"), tblDocName]];
3414-
[headerMenuItem setIndentationLevel:0];
3415-
[menu addItem:headerMenuItem];
3416-
[headerMenuItem release];
3417-
for (NSDictionary *favorite in [[SPQueryController sharedQueryController] favoritesForFileURL:[tableDocumentInstance fileURL]]) {
3410+
NSString *tblDocName = [[[fileURL absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent];
3411+
{
3412+
NSMenuItem *headerMenuItem = [[NSMenuItem alloc] initWithTitle:tblDocName action:NULL keyEquivalent:@""];
3413+
[headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
3414+
[headerMenuItem setToolTip:[NSString stringWithFormat:NSLocalizedString(@"‘%@’ based favorites",@"Query Favorites : List : Section Heading : current connection document : tooltip (arg is the name of the spf file)"), tblDocName]];
3415+
[headerMenuItem setIndentationLevel:0];
3416+
[menu addItem:headerMenuItem];
3417+
[headerMenuItem release];
3418+
}
3419+
for (NSDictionary *favorite in [[SPQueryController sharedQueryController] favoritesForFileURL:fileURL]) {
34183420
if (![favorite isKindOfClass:[NSDictionary class]] || ![favorite objectForKey:@"name"]) continue;
34193421
NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
34203422
[paraStyle setTabStops:@[]];
34213423
[paraStyle addTabStop:[[[NSTextTab alloc] initWithType:NSRightTabStopType location:190.0f] autorelease]];
34223424
NSDictionary *attributes = @{NSParagraphStyleAttributeName : paraStyle, NSFontAttributeName : [NSFont systemFontOfSize:11]};
3423-
NSAttributedString *titleString = [[[NSAttributedString alloc]
3424-
initWithString:([favorite objectForKey:@"tabtrigger"] && [(NSString*)[favorite objectForKey:@"tabtrigger"] length]) ? [NSString stringWithFormat:@"%@\t%@⇥", [favorite objectForKey:@"name"], [favorite objectForKey:@"tabtrigger"]] : [favorite objectForKey:@"name"]
3425-
attributes:attributes] autorelease];
3425+
NSAttributedString *titleString = [[[NSAttributedString alloc] initWithString:([favorite objectForKey:@"tabtrigger"] && [(NSString*)[favorite objectForKey:@"tabtrigger"] length]) ? [NSString stringWithFormat:@"%@\t%@⇥", [favorite objectForKey:@"name"], [favorite objectForKey:@"tabtrigger"]] : [favorite objectForKey:@"name"]
3426+
attributes:attributes] autorelease];
34263427
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
34273428
if ([favorite objectForKey:@"query"]) {
34283429
[item setToolTip:[NSString stringWithString:[favorite objectForKey:@"query"]]];
@@ -3434,21 +3435,22 @@ - (void)queryFavoritesHaveBeenUpdated:(id)manager
34343435
}
34353436

34363437
// Build global list
3437-
headerMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Global",@"Query Favorites : List : Section Heading : global query favorites") action:NULL keyEquivalent:@""];
3438-
[headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
3439-
[headerMenuItem setToolTip:NSLocalizedString(@"Globally stored favorites",@"Query Favorites : List : Section Heading : global : tooltip")];
3440-
[headerMenuItem setIndentationLevel:0];
3441-
[menu addItem:headerMenuItem];
3442-
[headerMenuItem release];
3438+
{
3439+
NSMenuItem *headerMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Global",@"Query Favorites : List : Section Heading : global query favorites") action:NULL keyEquivalent:@""];
3440+
[headerMenuItem setTag:SP_FAVORITE_HEADER_MENUITEM_TAG];
3441+
[headerMenuItem setToolTip:NSLocalizedString(@"Globally stored favorites",@"Query Favorites : List : Section Heading : global : tooltip")];
3442+
[headerMenuItem setIndentationLevel:0];
3443+
[menu addItem:headerMenuItem];
3444+
[headerMenuItem release];
3445+
}
34433446
for (NSDictionary *favorite in [prefs objectForKey:SPQueryFavorites]) {
34443447
if (![favorite isKindOfClass:[NSDictionary class]] || ![favorite objectForKey:@"name"]) continue;
34453448
NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
34463449
[paraStyle setTabStops:@[]];
34473450
[paraStyle addTabStop:[[[NSTextTab alloc] initWithType:NSRightTabStopType location:190.0f] autorelease]];
34483451
NSDictionary *attributes = @{NSParagraphStyleAttributeName : paraStyle, NSFontAttributeName : [NSFont systemFontOfSize:11]};
3449-
NSAttributedString *titleString = [[[NSAttributedString alloc]
3450-
initWithString:([favorite objectForKey:@"tabtrigger"] && [(NSString*)[favorite objectForKey:@"tabtrigger"] length]) ? [NSString stringWithFormat:@"%@\t%@⇥", [favorite objectForKey:@"name"], [favorite objectForKey:@"tabtrigger"]] : [favorite objectForKey:@"name"]
3451-
attributes:attributes] autorelease];
3452+
NSAttributedString *titleString = [[[NSAttributedString alloc] initWithString:([favorite objectForKey:@"tabtrigger"] && [(NSString*)[favorite objectForKey:@"tabtrigger"] length]) ? [NSString stringWithFormat:@"%@\t%@⇥", [favorite objectForKey:@"name"], [favorite objectForKey:@"tabtrigger"]] : [favorite objectForKey:@"name"]
3453+
attributes:attributes] autorelease];
34523454
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
34533455
if ([favorite objectForKey:@"query"]) {
34543456
[item setToolTip:[NSString stringWithString:[favorite objectForKey:@"query"]]];
@@ -3991,6 +3993,10 @@ - (void)awakeFromNib
39913993
selector:@selector(documentWillClose:)
39923994
name:SPDocumentWillCloseNotification
39933995
object:tableDocumentInstance];
3996+
[[NSNotificationCenter defaultCenter] addObserver:self
3997+
selector:@selector(queryFavoritesHaveBeenUpdated:)
3998+
name:SPQueryFavoritesHaveBeenUpdatedNotification
3999+
object:nil];
39944000

39954001
#ifndef SP_CODA
39964002
[prefs addObserver:self forKeyPath:SPGlobalResultTableFont options:NSKeyValueObservingOptionNew context:NULL];

Source/SPDatabaseDocument.m

-6
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,6 @@ - (void)setConnection:(SPMySQLConnection *)theConnection
425425
#ifndef SP_CODA
426426
// Set the fileURL and init the preferences (query favs, filters, and history) if available for that URL
427427
NSURL *newURL = [[SPQueryController sharedQueryController] registerDocumentWithFileURL:[self fileURL] andContextInfo:spfPreferences];
428-
#warning debug code for #2266
429-
if(!newURL) NSLog(@"#2266: Trying to set nil fileURL in %s from queryController=%@ oldFileURL=%@ contextInfo=%@", __func__, [SPQueryController sharedQueryController], [self fileURL], spfPreferences);
430428
[self setFileURL:newURL];
431429

432430
// ...but hide the icon while the document is temporary
@@ -3569,8 +3567,6 @@ - (BOOL)saveDocumentWithFilePath:(NSString *)fileName inBackground:(BOOL)saveInB
35693567
[[SPQueryController sharedQueryController] registerDocumentWithFileURL:[NSURL fileURLWithPath:fileName] andContextInfo:preferences];
35703568

35713569
NSURL *newURL = [NSURL fileURLWithPath:fileName];
3572-
#warning debug code for #2266
3573-
if(!newURL) NSLog(@"#2266: Trying to set nil fileURL in %s from fileName=%@", __func__, fileName);
35743570
[self setFileURL:newURL];
35753571
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL fileURLWithPath:fileName]];
35763572

@@ -5132,8 +5128,6 @@ - (BOOL)setStateFromConnectionFile:(NSString *)path
51325128

51335129
if (![self isSaveInBundle]) {
51345130
NSURL *newURL = [NSURL fileURLWithPath:path];
5135-
#warning debug code for #2266
5136-
if(!newURL) NSLog(@"#2266: Trying to set nil fileURL in %s from path=%@", __func__, path);
51375131
[self setFileURL:newURL];
51385132
}
51395133

Source/SPQueryFavoriteManager.h

-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@
3232
@class SPDatabaseDocument;
3333
@class SPSplitView;
3434

35-
@interface NSObject (SPQueryFavoriteManagerDelegate)
36-
37-
- (void)queryFavoritesHaveBeenUpdated:(id)manager;
38-
39-
@end
40-
4135
@interface SPQueryFavoriteManager : NSWindowController <NSOpenSavePanelDelegate>
4236
{
4337
#ifndef SP_CODA /* ivars */

Source/SPQueryFavoriteManager.m

+1-5
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
#import "RegexKitLite.h"
3939
#import "SPTextView.h"
4040
#import "SPSplitView.h"
41-
#import "SPAppController.h"
42-
#import "SPAppleScriptSupport.h"
4341

4442
#define SP_MULTIPLE_SELECTION_PLACEHOLDER_STRING NSLocalizedString(@"[multiple selection]", @"[multiple selection]")
4543
#define SP_NO_SELECTION_PLACEHOLDER_STRING NSLocalizedString(@"[no selection]", @"[no selection]")
@@ -486,9 +484,7 @@ - (IBAction)closeQueryManagerSheet:(id)sender
486484
[prefs setObject:[self queryFavoritesForFileURL:nil] forKey:SPQueryFavorites];
487485

488486
// Inform all opened documents to update the query favorites list
489-
for(id doc in [SPAppDelegate orderedDocuments])
490-
if([[doc valueForKeyPath:@"customQueryInstance"] respondsToSelector:@selector(queryFavoritesHaveBeenUpdated:)])
491-
[[doc valueForKeyPath:@"customQueryInstance"] queryFavoritesHaveBeenUpdated:self];
487+
[[NSNotificationCenter defaultCenter] postNotificationName:SPQueryFavoritesHaveBeenUpdatedNotification object:self];
492488
}
493489
#endif
494490

0 commit comments

Comments
 (0)