From fb59ce3e1a183c8406912b9b21c5b5621497d54e Mon Sep 17 00:00:00 2001 From: dmoagx Date: Wed, 20 Mar 2013 01:00:01 +0000 Subject: * Add support for specifying a collation when creating a table --- Interfaces/English.lproj/DBView.xib | 222 +++++++++++++++++++++++++++++++----- Source/SPTablesList.h | 2 + Source/SPTablesList.m | 91 ++++++++++++++- 3 files changed, 282 insertions(+), 33 deletions(-) diff --git a/Interfaces/English.lproj/DBView.xib b/Interfaces/English.lproj/DBView.xib index 12e07964..d0dbe705 100644 --- a/Interfaces/English.lproj/DBView.xib +++ b/Interfaces/English.lproj/DBView.xib @@ -21,16 +21,17 @@ YES - + + - - - - + + + - + + YES @@ -5069,7 +5070,7 @@ 1034 {{25, 427}, {649, 5}} - {{2, 0}, {645, 5}} + {{2.5, 0}, {644, 5}} {0, 0} @@ -5092,7 +5093,7 @@ 1034 {{24, 316}, {649, 5}} - {{2, 0}, {645, 5}} + {{2.5, 0}, {644, 5}} {0, 0} @@ -7032,24 +7033,24 @@ 9 2 - {{343, 453}, {384, 142}} + {{343, 433}, {384, 162}} 1886912512 New Table NSWindow View - {600, 142} + {600, 162} {384, 142} - + 256 YES 256 - {{5, 105}, {128, 14}} + {{5, 125}, {128, 14}} YES @@ -7065,7 +7066,7 @@ 256 - {{5, 80}, {128, 14}} + {{5, 100}, {128, 14}} YES @@ -7081,7 +7082,7 @@ 258 - {{138, 104}, {226, 18}} + {{138, 124}, {226, 18}} YES @@ -7121,7 +7122,7 @@ 258 - {{135, 75}, {232, 22}} + {{135, 95}, {232, 22}} YES @@ -7153,10 +7154,61 @@ 1 + + + 256 + {{5, 75}, {128, 14}} + + YES + + 68157504 + 71304192 + Table Collation: + + + + + + + + + 258 + {{135, 70}, {232, 22}} + + YES + + -2076049856 + 132096 + + + -2038284288 + 1 + + + + + + 400 + 75 + + YES + + OtherViews + + YES + + + -1 + 3 + YES + YES + 1 + + 256 - {{5, 54}, {128, 14}} + {{5, 49}, {128, 14}} YES @@ -7172,7 +7224,7 @@ 258 - {{135, 50}, {232, 22}} + {{135, 45}, {232, 22}} YES @@ -7226,11 +7278,12 @@ - {384, 142} + {384, 162} + {{0, 0}, {2560, 1418}} {384, 164} - {600, 164} + {600, 184} 9 @@ -16736,6 +16789,22 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 8308 + + + tableCollationButton + + + + 8314 + + + + tableEncodingButtonChanged: + + + + 8315 + @@ -17305,10 +17374,12 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 - - + + + + @@ -23590,6 +23661,43 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 + + 8309 + + + YES + + + + + + 8310 + + + YES + + + + + + 8311 + + + YES + + + + + + 8312 + + + + + 8313 + + + @@ -23828,9 +23936,12 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 5323.IBPluginDependency 5324.IBPluginDependency 5325.IBPluginDependency + 5325.IBViewBoundsToFrameTransform 5326.IBPluginDependency + 5326.IBViewBoundsToFrameTransform 5327.IBPluginDependency 5328.IBPluginDependency + 5328.IBViewBoundsToFrameTransform 5331.IBPluginDependency 5332.IBPluginDependency 5333.IBPluginDependency @@ -23975,9 +24086,12 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 5659.IBPluginDependency 5660.IBPluginDependency 5701.IBPluginDependency + 5701.IBViewBoundsToFrameTransform 5702.IBPluginDependency 5713.IBPluginDependency + 5713.IBViewBoundsToFrameTransform 5714.IBPluginDependency + 5714.IBViewBoundsToFrameTransform 5715.IBPluginDependency 5716.IBPluginDependency 5742.IBPluginDependency @@ -24728,6 +24842,13 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 8289.IBPluginDependency 8290.IBPluginDependency 8291.IBPluginDependency + 8309.IBPluginDependency + 8309.IBViewBoundsToFrameTransform + 8310.IBPluginDependency + 8310.IBViewBoundsToFrameTransform + 8311.IBPluginDependency + 8312.IBPluginDependency + 8313.IBPluginDependency 858.IBPluginDependency 959.IBEditorWindowLastContentRect 959.IBPluginDependency @@ -25089,19 +25210,28 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{329, 669}, {384, 142}} + {{329, 649}, {384, 162}} com.apple.InterfaceBuilder.CocoaPlugin - {{329, 669}, {384, 142}} + {{329, 649}, {384, 162}} - {600, 142} + {600, 162} {384, 142} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + + P4AAAL+AAABDBwAAwuYAAA + com.apple.InterfaceBuilder.CocoaPlugin + + P4AAAL+AAABAoAAAwuAAAA + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + + P4AAAL+AAABDkYAAwmwAAA + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -25252,9 +25382,9 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{521, 945}, {384, 133}} + {{1056, 723}, {384, 133}} com.apple.InterfaceBuilder.CocoaPlugin - {{521, 945}, {384, 133}} + {{1056, 723}, {384, 133}} {600, 133} @@ -25269,9 +25399,18 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + + P4AAAL+AAABDUQAAwmwAAA + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + + P4AAAL+AAABAoAAAwmQAAA + com.apple.InterfaceBuilder.CocoaPlugin + + P4AAAL+AAABDBwAAwnQAAA + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -26319,9 +26458,9 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin {{461, 758}, {224, 6}} com.apple.InterfaceBuilder.CocoaPlugin - {{913, 959}, {384, 119}} + {{1056, 737}, {384, 119}} com.apple.InterfaceBuilder.CocoaPlugin - {{913, 959}, {384, 119}} + {{1056, 737}, {384, 119}} @@ -26362,6 +26501,17 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + + P4AAAL+AAABBIAAAwtYAAA + + com.apple.InterfaceBuilder.CocoaPlugin + + P4AAAL+AAABDDAAAwtwAAA + + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin {{329, 544}, {405, 267}} com.apple.InterfaceBuilder.CocoaPlugin {{329, 544}, {405, 267}} @@ -26388,7 +26538,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 - 8308 + 8315 @@ -31642,6 +31792,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 openTableInNewTab: removeTable: renameTable: + tableEncodingButtonChanged: togglePaneCollapse: truncateTable: updateFilter: @@ -31659,6 +31810,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 id id id + id @@ -31671,6 +31823,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 openTableInNewTab: removeTable: renameTable: + tableEncodingButtonChanged: togglePaneCollapse: truncateTable: updateFilter: @@ -31702,6 +31855,10 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 renameTable: id + + tableEncodingButtonChanged: + id + togglePaneCollapse: id @@ -31753,6 +31910,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 showCreateSyntaxContextMenuItem showCreateSyntaxMenuItem spHistoryControllerInstance + tableCollationButton tableContentInstance tableDataInstance tableDocumentInstance @@ -31805,6 +31963,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 NSMenuItem NSMenuItem SPHistoryController + id SPTableContent id SPDatabaseDocument @@ -31860,6 +32019,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 showCreateSyntaxContextMenuItem showCreateSyntaxMenuItem spHistoryControllerInstance + tableCollationButton tableContentInstance tableDataInstance tableDocumentInstance @@ -31999,6 +32159,10 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 spHistoryControllerInstance SPHistoryController + + tableCollationButton + id + tableContentInstance SPTableContent diff --git a/Source/SPTablesList.h b/Source/SPTablesList.h index a8ba3b50..857263b9 100644 --- a/Source/SPTablesList.h +++ b/Source/SPTablesList.h @@ -73,6 +73,7 @@ IBOutlet id tableSheet; IBOutlet id tableNameField; IBOutlet id tableEncodingButton; + IBOutlet id tableCollationButton; IBOutlet id tableTypeButton; IBOutlet id toolbarAddButton; #ifdef SP_CODA @@ -143,6 +144,7 @@ - (IBAction)addTable:(id)sender; - (IBAction)closeSheet:(id)sender; - (IBAction)removeTable:(id)sender; +- (IBAction)tableEncodingButtonChanged:(id)sender; #ifndef SP_CODA /* method decls */ - (IBAction)copyTable:(id)sender; diff --git a/Source/SPTablesList.m b/Source/SPTablesList.m index e3a28fce..70bab371 100644 --- a/Source/SPTablesList.m +++ b/Source/SPTablesList.m @@ -414,6 +414,9 @@ static NSString *SPDuplicateTable = @"SPDuplicateTable"; else { [tableEncodingButton setEnabled:NO]; } + + //Load the collations for the current charset + [self tableEncodingButtonChanged:self]; // Set the focus to the name field [tableSheet makeFirstResponder:tableNameField]; @@ -425,6 +428,77 @@ static NSString *SPDuplicateTable = @"SPDuplicateTable"; contextInfo:SPAddNewTable]; } + +- (IBAction)tableEncodingButtonChanged:(id)sender +{ + NSString *fmtStrDefaultId = NSLocalizedString(@"Default (%@)",@"Add Table : Collation : Default ($1 = collation name)"); + NSString *fmtStrDefaultUnknown = NSLocalizedString(@"Default",@"Add Table Sheet : Collation : Default (unknown)"); // MySQL < 4.1.0 + + //throw out all items + [tableCollationButton removeAllItems]; + //we'll enable that later if the user can actually change the selection. + [tableCollationButton setEnabled:NO]; + + /* logic below is as follows: + * if the database default charset is selected also use the database default collation + * regardless of default charset or not get the list of all collations that apply + * if a non-default charset is selected look out for it's default collation and promote that to the top as default + * + * Selecting a default charset (or collation) means that we don't want to specify one in the CREATE TABLE statement. + */ + + //is the default charset currently selected? + BOOL isDefaultCharset = ([tableEncodingButton indexOfSelectedItem] == 0); + + if(isDefaultCharset) { + NSString *defaultCollation = [databaseDataInstance getDatabaseDefaultCollation]; + NSString *defaultItemTitle = (defaultCollation)? [NSString stringWithFormat:fmtStrDefaultId,defaultCollation] : fmtStrDefaultUnknown; + [tableCollationButton addItemWithTitle:defaultItemTitle]; + //add the separator for the real items + [[tableCollationButton menu] addItem:[NSMenuItem separatorItem]]; + } + + //if the server actually has support for charsets & collations we will now get a list of all collations + //for the current charset. Even if the default charset is kept by the user he can change the default collation + //so we search in that case, too. + if(![[tableDocumentInstance serverSupport] supportsPost41CharacterSetHandling]) + return; + + //get the charset id the lazy way + NSString *charsetName = [[tableEncodingButton title] stringByMatching:@"\\((.*)\\)\\Z" capture:1L]; + //this should not fail as even default is "Default (charset)" - if it does there's nothing we can do + if(!charsetName) { + NSLog(@"%s: Can't find charset id in encoding name <%@>. Format should be .",__func__,[tableEncodingButton title]); + return; + } + //now let's get the list of collations for the selected charset id + NSArray *applicableCollations = [databaseDataInstance getDatabaseCollationsForEncoding:charsetName]; + + //got something? + if (![applicableCollations count]) + return; + + //add the real items + for (NSDictionary *collation in applicableCollations) + { + NSString *collationName = [collation objectForKey:@"COLLATION_NAME"]; + [tableCollationButton addItemWithTitle:collationName]; + + //if this is not the server default charset let's find it's default collation too + if(!isDefaultCharset && [[collation objectForKey:@"IS_DEFAULT"] isEqualToString:@"Yes"]) { + NSString *defaultCollateTitle = [NSString stringWithFormat:fmtStrDefaultId,collationName]; + //add it to the top of the list + [tableCollationButton insertItemWithTitle:defaultCollateTitle atIndex:0]; + //add a separator underneath + [[tableCollationButton menu] insertItem:[NSMenuItem separatorItem] atIndex:1]; + } + } + //reset selection to first item (it may moved when adding the default item) + [tableCollationButton selectItemAtIndex:0]; + //yay, now there is actually something not the Default item, so we can enable the button + [tableCollationButton setEnabled:YES]; +} + /** * Closes the current sheet and stops the modal session */ @@ -1781,7 +1855,8 @@ static NSString *SPDuplicateTable = @"SPDuplicateTable"; return selectedRows > 0; } - return [super validateMenuItem:menuItem]; + //Default to YES (like Apple) + return YES; } #pragma mark - @@ -2196,8 +2271,9 @@ static NSString *SPDuplicateTable = @"SPDuplicateTable"; [tableDocumentInstance startTaskWithDescription:[NSString stringWithFormat:NSLocalizedString(@"Creating %@...", @"Creating table task string"), [tableNameField stringValue]]]; - NSString *charSetStatement = @""; - NSString *engineStatement = @""; + NSString *charSetStatement = @""; + NSString *collationStatement = @""; + NSString *engineStatement = @""; NSString *tableType = [tableTypeButton title]; NSString *tableName = [tableNameField stringValue]; @@ -2218,13 +2294,20 @@ static NSString *SPDuplicateTable = @"SPDuplicateTable"; 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 DATABASE statement + if ([tableCollationButton indexOfSelectedItem] > 0) { + //collations have no description except for the default item (which is already excluded) so we can directly use the value + NSString *collationName = [tableCollationButton title]; + 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 ([tableTypeButton indexOfSelectedItem] > 0) { engineStatement = [NSString stringWithFormat:@"%@ = %@", [[tableDocumentInstance serverSupport] engineTypeQueryName], [[tableDocumentInstance serverSupport] supportsQuotingEngineTypeInCreateSyntax] ? [tableType backtickQuotedString] : tableType]; } - NSString *createStatement = [NSString stringWithFormat:@"CREATE TABLE %@ (id INT(11) UNSIGNED NOT NULL%@) %@ %@", [tableName backtickQuotedString], [tableType isEqualToString:@"CSV"] ? @"" : @" PRIMARY KEY AUTO_INCREMENT", charSetStatement, engineStatement]; + NSString *createStatement = [NSString stringWithFormat:@"CREATE TABLE %@ (id INT(11) UNSIGNED NOT NULL%@) %@ %@ %@", [tableName backtickQuotedString], [tableType isEqualToString:@"CSV"] ? @"" : @" PRIMARY KEY AUTO_INCREMENT", charSetStatement, collationStatement, engineStatement]; // Create the table [mySQLConnection queryString:createStatement]; -- cgit v1.2.3