Skip to content

Commit

Permalink
Fix UI interation from background thread when adding a new table.
Browse files Browse the repository at this point in the history
  • Loading branch information
stuconnolly committed Oct 9, 2018
1 parent c37bea8 commit 54ad42a
Showing 1 changed file with 60 additions and 33 deletions.
93 changes: 60 additions & 33 deletions Source/SPTablesList.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,17 +55,26 @@
#import <SPMySQL/SPMySQL.h> #import <SPMySQL/SPMySQL.h>


// Constants // Constants
//
// Actions
static NSString *SPAddRow = @"SPAddRow"; static NSString *SPAddRow = @"SPAddRow";
static NSString *SPAddNewTable = @"SPAddNewTable"; static NSString *SPAddNewTable = @"SPAddNewTable";
static NSString *SPRemoveTable = @"SPRemoveTable"; static NSString *SPRemoveTable = @"SPRemoveTable";
static NSString *SPTruncateTable = @"SPTruncateTable"; static NSString *SPTruncateTable = @"SPTruncateTable";
static NSString *SPDuplicateTable = @"SPDuplicateTable"; static NSString *SPDuplicateTable = @"SPDuplicateTable";


// New table
static NSString *SPNewTableName = @"SPNewTableName";
static NSString *SPNewTableType = @"SPNewTableType";
static NSString *SPNewTableCharacterSet = @"SPNewTableCharacterSet";
static NSString *SPNewTableCollation = @"SPNewTableCollation";

@interface SPTablesList () <NSSplitViewDelegate, NSTableViewDataSource> @interface SPTablesList () <NSSplitViewDelegate, NSTableViewDataSource>


- (void)_removeTable:(BOOL)force; - (void)_removeTable:(BOOL)force;
- (void)_truncateTable; - (void)_truncateTable;
- (void)_addTable; - (void)_addTable;
- (void)_addTableWithDetails:(NSDictionary *)tableDetails;
- (void)_copyTable; - (void)_copyTable;
- (void)_renameTableOfType:(SPTableType)tableType from:(NSString *)oldTableName to:(NSString *)newTableName; - (void)_renameTableOfType:(SPTableType)tableType from:(NSString *)oldTableName to:(NSString *)newTableName;
- (void)_duplicateConnectionToFrontTab; - (void)_duplicateConnectionToFrontTab;
Expand Down Expand Up @@ -734,30 +743,25 @@ - (void)sheetDidEnd:(id)sheet returnCode:(NSInteger)returnCode contextInfo:(NSSt
} }
else if ([contextInfo isEqualToString:SPRemoveTable]) { else if ([contextInfo isEqualToString:SPRemoveTable]) {
if (returnCode == NSAlertDefaultReturn) { if (returnCode == NSAlertDefaultReturn) {
[self _removeTable:([[(NSAlert *)sheet suppressionButton] state] == NSOnState)]; [self _removeTable:[[(NSAlert *)sheet suppressionButton] state] == NSOnState];
} }
} }
#ifndef SP_CODA
else if ([contextInfo isEqualToString:SPTruncateTable]) { else if ([contextInfo isEqualToString:SPTruncateTable]) {
if (returnCode == NSAlertDefaultReturn) { if (returnCode == NSAlertDefaultReturn) {
[self _truncateTable]; [self _truncateTable];
} }
} }
else else if ([contextInfo isEqualToString:SPAddNewTable]) {
#endif
if ([contextInfo isEqualToString:SPAddNewTable]) {
[addTableCharsetHelper setEnabled:NO]; [addTableCharsetHelper setEnabled:NO];
if (returnCode == NSOKButton) { if (returnCode == NSOKButton) {
[self _addTable]; [self _addTable];
} }
} }
#ifndef SP_CODA
else if ([contextInfo isEqualToString:SPDuplicateTable]) { else if ([contextInfo isEqualToString:SPDuplicateTable]) {
if (returnCode == NSOKButton) { if (returnCode == NSOKButton) {
[self _copyTable]; [self _copyTable];
} }
} }
#endif
} }


#pragma mark - #pragma mark -
Expand Down Expand Up @@ -2355,25 +2359,57 @@ - (void)_truncateTable
#endif #endif


/** /**
* Adds a new table table to the database using the selected character set encoding and storage engine. * Adds a new table table to the database using the selected character set encoding and storage engine on a separate thread.
*
* This method *MUST* be called from the UI thread!
*/ */
- (void)_addTable - (void)_addTable
{ {
// Ensure the task is performed on a background thread to group addition and loads NSString *tableType = [tableTypeButton title];
if ([NSThread isMainThread]) { NSString *tableName = [tableNameField stringValue];
[NSThread detachNewThreadWithName:SPCtxt(@"SPTablesList table addition task", tableDocumentInstance) target:self selector:@selector(_addTable) object:nil]; NSString *tableCharacterSet = [addTableCharsetHelper selectedCharset];
return; NSString *tableColletion = [addTableCharsetHelper selectedCollation];

NSMutableDictionary *tableDetails = [NSMutableDictionary dictionaryWithObject:tableName forKey:SPNewTableName];

if ([tableTypeButton indexOfSelectedItem] > 0) {
[tableDetails setObject:tableType forKey:SPNewTableType];
} }

@autoreleasepool {
[tableDocumentInstance startTaskWithDescription:[NSString stringWithFormat:NSLocalizedString(@"Creating %@...", @"Creating table task string"), [tableNameField stringValue]]];


if (tableCharacterSet) {
[tableDetails setObject:tableCharacterSet forKey:SPNewTableCharacterSet];
}

if (tableColletion) {
[tableDetails setObject:tableColletion forKey:SPNewTableCollation];
}

[tableDocumentInstance startTaskWithDescription:[NSString stringWithFormat:NSLocalizedString(@"Creating %@...", @"Creating table task string"), tableName]];

[NSThread detachNewThreadWithName:SPCtxt(@"SPTablesList table addition task", tableDocumentInstance)
target:self
selector:@selector(_addTableWithDetails:)
object:tableDetails];

// Clear table name
[[tableNameField onMainThread] setStringValue:@""];

[tableDocumentInstance endTask];
}

/**
* Adds a new table table to the database using the selected character set encoding and storage engine.
*/
- (void)_addTableWithDetails:(NSDictionary *)tableDetails
{
@autoreleasepool
{
NSString *charSetStatement = @""; NSString *charSetStatement = @"";
NSString *collationStatement = @""; NSString *collationStatement = @"";
NSString *engineStatement = @""; NSString *engineStatement = @"";


NSString *tableType = [tableTypeButton title]; NSString *tableName = [tableDetails objectForKey:SPNewTableName];
NSString *tableName = [tableNameField stringValue]; NSString *tableType = [tableDetails objectForKey:SPNewTableType];


// Ensure the use of UTF8 when creating new tables // Ensure the use of UTF8 when creating new tables
BOOL changeEncoding = ![[mySQLConnection encoding] isEqualToString:@"utf8"]; BOOL changeEncoding = ![[mySQLConnection encoding] isEqualToString:@"utf8"];
Expand All @@ -2384,15 +2420,17 @@ - (void)_addTable
} }


// If there is an encoding selected other than the default we must specify it in CREATE TABLE statement // If there is an encoding selected other than the default we must specify it in CREATE TABLE statement
NSString *encodingName = [addTableCharsetHelper selectedCharset]; NSString *encodingName = [tableDetails objectForKey:SPNewTableCharacterSet];

if (encodingName) charSetStatement = [NSString stringWithFormat:@"DEFAULT CHARACTER SET %@", [encodingName backtickQuotedString]]; if (encodingName) charSetStatement = [NSString stringWithFormat:@"DEFAULT CHARACTER SET %@", [encodingName backtickQuotedString]];


// If there is a collation selected other than the default we must specify it in the CREATE TABLE statement // If there is a collation selected other than the default we must specify it in the CREATE TABLE statement
NSString *collationName = [addTableCharsetHelper selectedCollation]; NSString *collationName = [tableDetails objectForKey:SPNewTableCollation];
if (collationName) collationStatement = [NSString stringWithFormat:@"DEFAULT COLLATE %@",[collationName backtickQuotedString]];
if (collationName) collationStatement = [NSString stringWithFormat:@"DEFAULT COLLATE %@", [collationName backtickQuotedString]];


// If there is a type selected other than the default we must specify it in CREATE TABLE statement // If there is a type selected other than the default we must specify it in CREATE TABLE statement
if ([tableTypeButton indexOfSelectedItem] > 0) { if (tableType) {
engineStatement = [NSString stringWithFormat:@"%@ = %@", [[tableDocumentInstance serverSupport] engineTypeQueryName], [[tableDocumentInstance serverSupport] supportsQuotingEngineTypeInCreateSyntax] ? [tableType backtickQuotedString] : tableType]; engineStatement = [NSString stringWithFormat:@"%@ = %@", [[tableDocumentInstance serverSupport] engineTypeQueryName], [[tableDocumentInstance serverSupport] supportsQuotingEngineTypeInCreateSyntax] ? [tableType backtickQuotedString] : tableType];
} }


Expand Down Expand Up @@ -2439,17 +2477,11 @@ - (void)_addTable
selectedTableType = SPTableTypeTable; selectedTableType = SPTableTypeTable;


[[self onMainThread] updateFilter:self]; [[self onMainThread] updateFilter:self];
[[tablesListView onMainThread] scrollRowToVisible:[tablesListView selectedRow]]; [[tablesListView onMainThread] scrollRowToVisible:[[tablesListView onMainThread] selectedRow]];


// Select the newly created table and switch to the table structure view for easier setup // Select the newly created table and switch to the table structure view for easier setup
[tableDocumentInstance loadTable:selectedTableName ofType:selectedTableType]; [tableDocumentInstance loadTable:selectedTableName ofType:selectedTableType];
#ifndef SP_CODA
[tableDocumentInstance viewStructure:self]; [tableDocumentInstance viewStructure:self];
#endif

#ifdef SP_CODA
[sidebarViewController setTableNames:[self allTableNames] selectedTableName:selectedTableName];
#endif


// Query the structure of all databases in the background (mainly for completion) // Query the structure of all databases in the background (mainly for completion)
[[tableDocumentInstance databaseStructureRetrieval] queryDbStructureInBackgroundWithUserInfo:@{@"forceUpdate" : @YES}]; [[tableDocumentInstance databaseStructureRetrieval] queryDbStructureInBackgroundWithUserInfo:@{@"forceUpdate" : @YES}];
Expand All @@ -2474,11 +2506,6 @@ - (void)_addTable


[[tablesListView onMainThread] reloadData]; [[tablesListView onMainThread] reloadData];
} }

// Clear table name
[[tableNameField onMainThread] setStringValue:@""];

[tableDocumentInstance endTask];
} }
} }


Expand Down

0 comments on commit 54ad42a

Please sign in to comment.