From f81ed59ba9aeb8e81bf9deb64c4ebbe5e7620dbd Mon Sep 17 00:00:00 2001 From: Bibiko Date: Fri, 10 Sep 2010 08:37:43 +0000 Subject: =?UTF-8?q?=E2=80=A2=20TableStructure=20-=20moved=20field=20type?= =?UTF-8?q?=20suggestions=20for=20combobox=20as=20data=20source=20to=20SPT?= =?UTF-8?q?ableStructure=20-=20applied=20type=20combobox=20lowercased=20co?= =?UTF-8?q?mpletion=20while=20typing=20-=20improved=20DEFAULT=20handling?= =?UTF-8?q?=20for=20any=20numeric,=20date,=20time=20fields=20if=20DEFAULT?= =?UTF-8?q?=20was=20set=20to=20an=20empty=20string=20''=20-=20if=20so=20sk?= =?UTF-8?q?ip=20it=20to=20avoid=20unnecessary=20error=20messages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Interfaces/English.lproj/DBView.xib | 51 +++++------------ Source/SPTableStructure.h | 2 + Source/SPTableStructure.m | 106 ++++++++++++++++++++++++++++++------ 3 files changed, 104 insertions(+), 55 deletions(-) diff --git a/Interfaces/English.lproj/DBView.xib b/Interfaces/English.lproj/DBView.xib index e5b93be2..da65886c 100644 --- a/Interfaces/English.lproj/DBView.xib +++ b/Interfaces/English.lproj/DBView.xib @@ -23,7 +23,7 @@ YES - + YES @@ -1069,41 +1069,9 @@ 10 YES + YES YES - - YES - TINYINT - SMALLINT - MEDIUMINT - INT - BIGINT - FLOAT - DOUBLE - DECIMAL - -------- - DATE - DATETIME - TIMESTAMP - TIME - YEAR - -------- - CHAR - VARCHAR - TINYBLOB - TINYTEXT - BLOB - TEXT - MEDIUMBLOB - MEDIUMTEXT - LONGBLOB - LONGTEXT - ENUM - SET - -------- - BIT - BINARY - VARBINARY - + 274 @@ -1148,7 +1116,6 @@ tableViewAction: -228556800 - 1 15 @@ -14756,6 +14723,14 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 7496 + + + dataSource + + + + 7498 + @@ -23314,7 +23289,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{-19, 172}, {943, 549}} + {{0, 172}, {943, 549}} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -23665,7 +23640,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 - 7497 + 7498 diff --git a/Source/SPTableStructure.h b/Source/SPTableStructure.h index 0be1082b..6af0a3b3 100644 --- a/Source/SPTableStructure.h +++ b/Source/SPTableStructure.h @@ -70,6 +70,8 @@ NSInteger currentlyEditingRow; NSUserDefaults *prefs; NSArray *collations; + NSArray *typeSuggestions; + } // Table methods diff --git a/Source/SPTableStructure.m b/Source/SPTableStructure.m index 1d05ba7f..48b3cf22 100644 --- a/Source/SPTableStructure.m +++ b/Source/SPTableStructure.m @@ -55,6 +55,7 @@ tableFields = [[NSMutableArray alloc] init]; oldRow = [[NSMutableDictionary alloc] init]; enumFields = [[NSMutableDictionary alloc] init]; + typeSuggestions = nil; currentlyEditingRow = -1; defaultValues = nil; @@ -75,12 +76,43 @@ [tableSourceView setGridStyleMask:([prefs boolForKey:SPDisplayTableViewVerticalGridlines]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone]; // Set the strutcture and index view's font - BOOL useMonospacedFont = [prefs boolForKey:SPUseMonospacedFonts]; - - for (NSTableColumn *fieldColumn in [tableSourceView tableColumns]) - { - [[fieldColumn dataCell] setFont:(useMonospacedFont) ? [NSFont fontWithName:SPDefaultMonospacedFontName size:[NSFont smallSystemFontSize]] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; - } + [tableSourceView setFont:([prefs boolForKey:SPUseMonospacedFonts]) ? [NSFont fontWithName:SPDefaultMonospacedFontName size:[NSFont smallSystemFontSize]] : [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; + + typeSuggestions = [[NSArray arrayWithObjects: + @"TINYINT", + @"SMALLINT", + @"MEDIUMINT", + @"INT", + @"BIGINT", + @"FLOAT", + @"DOUBLE", + @"DOUBLE PRECISION", + @"REAL", + @"DECIMAL", + @"BIT", + @"--------", + @"CHAR", + @"VARCHAR", + @"BINARY", + @"VARBINARY", + @"TINYBLOB", + @"BLOB", + @"TEXT", + @"MEDIUMBLOB", + @"MEDIUMTEXT", + @"LONGBLOB", + @"LONGTEXT", + @"ENUM", + @"SET", + @"--------", + @"DATE", + @"DATETIME", + @"TIMESTAMP", + @"TIME", + @"YEAR", + nil] retain]; + // Hint: in [self addRowToDB] > [typeSuggestions indexOfObject:theRowType] < 11 etc. has to be changed if typeSuggestions was changed! + databaseDataInstance = [tableDocumentInstance valueForKeyPath:@"databaseDataInstance"]; @@ -681,6 +713,7 @@ closes the keySheet NSInteger code; NSDictionary *theRow; NSMutableString *queryString; + NSString *theRowType; if (!isEditingRow || currentlyEditingRow == -1) return YES; @@ -690,6 +723,8 @@ closes the keySheet theRow = [tableFields objectAtIndex:currentlyEditingRow]; + theRowType = [[theRow objectForKey:@"type"] uppercaseString]; + if (isEditingNewRow) { // ADD syntax if ([[theRow objectForKey:@"length"] isEqualToString:@""] || ![theRow objectForKey:@"length"]) { @@ -697,13 +732,13 @@ closes the keySheet queryString = [NSMutableString stringWithFormat:@"ALTER TABLE %@ ADD %@ %@", [selectedTable backtickQuotedString], [[theRow objectForKey:@"name"] backtickQuotedString], - [[theRow objectForKey:@"type"] uppercaseString]]; + theRowType]; } else { queryString = [NSMutableString stringWithFormat:@"ALTER TABLE %@ ADD %@ %@(%@)", [selectedTable backtickQuotedString], [[theRow objectForKey:@"name"] backtickQuotedString], - [[theRow objectForKey:@"type"] uppercaseString], + theRowType, [theRow objectForKey:@"length"]]; } } @@ -724,7 +759,7 @@ closes the keySheet [selectedTable backtickQuotedString], [[oldRow objectForKey:@"name"] backtickQuotedString], [[theRow objectForKey:@"name"] backtickQuotedString], - [[theRow objectForKey:@"type"] uppercaseString]]; + theRowType]; } else { // If the old row and new row dictionaries are equal then the user didn't actually change anything so don't continue @@ -738,7 +773,7 @@ closes the keySheet [selectedTable backtickQuotedString], [[oldRow objectForKey:@"name"] backtickQuotedString], [[theRow objectForKey:@"name"] backtickQuotedString], - [[theRow objectForKey:@"type"] uppercaseString], + theRowType, [theRow objectForKey:@"length"]]; } } @@ -791,7 +826,7 @@ closes the keySheet } } // Otherwise, if CURRENT_TIMESTAMP was specified for timestamps, use that - else if ([[theRow objectForKey:@"type"] isEqualToString:@"timestamp"] && + else if ([theRowType isEqualToString:@"TIMESTAMP"] && [[[theRow objectForKey:@"default"] uppercaseString] isEqualToString:@"CURRENT_TIMESTAMP"]) { [queryString appendString:@" DEFAULT CURRENT_TIMESTAMP "]; @@ -799,9 +834,14 @@ closes the keySheet } // If the field is of type BIT, permit the use of single qoutes and also don't quote the default value. // For example, use DEFAULT b'1' as opposed to DEFAULT 'b\'1\'' which results in an error. - else if ([[[theRow objectForKey:@"type"] lowercaseString] isEqualToString:@"bit"]) { + else if ([[theRow objectForKey:@"default"] length] && [theRowType isEqualToString:@"BIT"]) { [queryString appendFormat:@" DEFAULT %@ ", [theRow objectForKey:@"default"]]; } + // Suppress appending DEFAULT clause for any numerics, date, time fields if default is empty to avoid error messages + // Hint: [typeSuggestions indexOfObject:theRowType] < 11 has to be changed if typeSuggestions was changed! + else if (![[theRow objectForKey:@"default"] length] && ([typeSuggestions indexOfObject:theRowType] < 11 || [typeSuggestions indexOfObject:theRowType] > 25)) { + ; + } // Otherwise, use the provided default else { [queryString appendFormat:@" DEFAULT '%@' ", [mySQLConnection prepareString:[theRow objectForKey:@"default"]]]; @@ -818,12 +858,12 @@ closes the keySheet [queryString appendString:[theRow objectForKey:@"Extra"]]; } - if (!isEditingNewRow) { + // Any column comments + if ([[theRow objectForKey:@"comment"] length]) { + [queryString appendFormat:@" COMMENT '%@'", [mySQLConnection prepareString:[theRow objectForKey:@"comment"]]]; + } - // Any column comments - if ([[theRow objectForKey:@"comment"] length]) { - [queryString appendFormat:@" COMMENT '%@'", [mySQLConnection prepareString:[theRow objectForKey:@"comment"]]]; - } + if (!isEditingNewRow) { // Unparsed details - column formats, storage, reference definitions if ([[theRow objectForKey:@"unparsed"] length]) { @@ -1673,6 +1713,37 @@ would result in a position change. [tablesIndexesSplitView setPosition:[tablesIndexesSplitView frame].size.height-130 ofDividerAtIndex:0]; } +#pragma mark - +#pragma mark NSComboBox delegates + +- (id)comboBoxCell:(NSComboBoxCell *)aComboBoxCell objectValueForItemAtIndex:(NSInteger)index +{ + return [typeSuggestions objectAtIndex:index]; +} + +- (NSInteger)numberOfItemsInComboBoxCell:(NSComboBoxCell *)aComboBoxCell +{ + return [typeSuggestions count]; +} + +/** + * Allow completion for lowercased input + */ +- (NSString *)comboBoxCell:(NSComboBoxCell *)aComboBoxCell completedString:(NSString *)uncompletedString +{ + + if([uncompletedString hasPrefix:@"-"]) return @""; + + NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@", [uncompletedString uppercaseString]]; + NSArray *result = [typeSuggestions filteredArrayUsingPredicate:predicate]; + + if(result && [result count]) + return [result objectAtIndex:0]; + + return @""; + +} + #pragma mark - #pragma mark Private API methods @@ -1751,6 +1822,7 @@ would result in a position change. [tableFields release]; [oldRow release]; [enumFields release]; + [typeSuggestions release]; if (defaultValues) [defaultValues release]; if (selectedTable) [selectedTable release]; -- cgit v1.2.3