diff options
-rw-r--r-- | TableSource.m | 463 | ||||
-rw-r--r-- | TablesList.m | 463 |
2 files changed, 462 insertions, 464 deletions
diff --git a/TableSource.m b/TableSource.m index 78e9bb4d..d43b83c5 100644 --- a/TableSource.m +++ b/TableSource.m @@ -28,135 +28,152 @@ @implementation TableSource -- (void)loadTable:(NSString *)aTable /* - loads aTable, put it in an array, update the tableViewColumns and reload the tableView - */ +loads aTable, put it in an array, update the tableViewColumns and reload the tableView +*/ +- (void)loadTable:(NSString *)aTable { - NSEnumerator *enumerator; - id field; - NSScanner *scanner = [NSScanner alloc]; - NSArray *extrasArray; - NSMutableDictionary *tempDefaultValues; - NSEnumerator *extrasEnumerator; - id extra; - int i; + NSEnumerator *enumerator; + id field; + NSScanner *scanner = [NSScanner alloc]; + NSArray *extrasArray; + NSMutableDictionary *tempDefaultValues; + NSEnumerator *extrasEnumerator; + id extra; + int i; - selectedTable = aTable; - [tableSourceView deselectAll:self]; - if ( isEditingRow ) - return; + selectedTable = aTable; + [tableSourceView deselectAll:self]; + + if ( isEditingRow ) + return; // empty variables [enumFields removeAllObjects]; - //query started - [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryWillBePerformed" object:self]; + //query started + [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryWillBePerformed" object:self]; - if ( [aTable isEqualToString:@""] || !aTable ) { - [tableFields removeAllObjects]; - [indexes removeAllObjects]; - [tableSourceView reloadData]; - [indexView reloadData]; - [addFieldButton setEnabled:NO]; - [copyFieldButton setEnabled:NO]; - [removeFieldButton setEnabled:NO]; - [addIndexButton setEnabled:NO]; - [removeIndexButton setEnabled:NO]; + if ( [aTable isEqualToString:@""] || !aTable ) { + [tableFields removeAllObjects]; + [indexes removeAllObjects]; + [tableSourceView reloadData]; + [indexView reloadData]; + [addFieldButton setEnabled:NO]; + [copyFieldButton setEnabled:NO]; + [removeFieldButton setEnabled:NO]; + [addIndexButton setEnabled:NO]; + [removeIndexButton setEnabled:NO]; + + // set the table type menu back to the default, and disable it + [tableTypeButton selectItemAtIndex:0]; + [tableTypeButton setEnabled:NO]; + tableType = nil; + + //query finished + [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryHasBeenPerformed" object:self]; + + [scanner release]; + + return; + } - // set the table type menu back to the default, and disable it - [tableTypeButton selectItemAtIndex:0]; - [tableTypeButton setEnabled:NO]; - tableType = nil; + //perform queries and load results in array (each row as a dictionary) + tableSourceResult = [[mySQLConnection queryString:[NSString stringWithFormat:@"SHOW COLUMNS FROM `%@`", selectedTable]] retain]; - //query finished - [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryHasBeenPerformed" object:self]; + // listFieldsFromTable is broken in the current version of the framework (no back-ticks for table name)! + // tableSourceResult = [[mySQLConnection listFieldsFromTable:selectedTable] retain]; + // [tableFields setArray:[[self fetchResultAsArray:tableSourceResult] retain]]; + [tableFields setArray:[self fetchResultAsArray:tableSourceResult]]; + [tableSourceResult release]; + + indexResult = [[mySQLConnection queryString:[NSString stringWithFormat:@"SHOW INDEX FROM `%@`", selectedTable]] retain]; + // [indexes setArray:[[self fetchResultAsArray:indexResult] retain]]; + [indexes setArray:[self fetchResultAsArray:indexResult]]; + [indexResult release]; - [scanner release]; + CMMCPResult *tableStatusResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW TABLE STATUS LIKE '%@'", selectedTable]]; + [tableType release]; - return; - } - - //perform queries and load results in array (each row as a dictionary) - tableSourceResult = [[mySQLConnection queryString:[NSString stringWithFormat:@"SHOW COLUMNS FROM `%@`", selectedTable]] retain]; - // listFieldsFromTable is broken in the current version of the framework (no back-ticks for table name)! - // tableSourceResult = [[mySQLConnection listFieldsFromTable:selectedTable] retain]; - // [tableFields setArray:[[self fetchResultAsArray:tableSourceResult] retain]]; - [tableFields setArray:[self fetchResultAsArray:tableSourceResult]]; - [tableSourceResult release]; - indexResult = [[mySQLConnection queryString:[NSString stringWithFormat:@"SHOW INDEX FROM `%@`", selectedTable]] retain]; - // [indexes setArray:[[self fetchResultAsArray:indexResult] retain]]; - [indexes setArray:[self fetchResultAsArray:indexResult]]; - [indexResult release]; - CMMCPResult *tableStatusResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW TABLE STATUS LIKE '%@'", selectedTable]]; - [tableType release]; - NSDictionary *tempRow = [tableStatusResult fetchRowAsDictionary]; - if ( [tempRow objectForKey:@"Type"]) { - tableType = [tempRow objectForKey:@"Type"]; - } else { - tableType = [tempRow objectForKey:@"Engine"]; - } - [tableType retain]; - //get table default values - if ( defaultValues ) { - [defaultValues release]; - defaultValues = nil; - } - tempDefaultValues = [NSMutableDictionary dictionary]; - for ( i = 0 ; i < [tableFields count] ; i++ ) { - [tempDefaultValues setObject:[[tableFields objectAtIndex:i] objectForKey:@"Default"] forKey:[[tableFields objectAtIndex:i] objectForKey:@"Field"]]; - } - defaultValues = [[NSDictionary dictionaryWithDictionary:tempDefaultValues] retain]; - //put field length and extras in separate key - enumerator = [tableFields objectEnumerator]; - while ( (field = [enumerator nextObject]) ) { - NSString *type; - NSString *length; - NSString *extras; - // scan for length and extras like unsigned - [scanner initWithString:[field objectForKey:@"Type"]]; - [scanner scanUpToString:@"(" intoString:&type]; - [scanner scanString:@"(" intoString:nil]; - if ( ![scanner scanUpToString:@")" intoString:&length] ) - length = @""; - [scanner scanString:@")" intoString:nil]; - if ( ![scanner scanUpToString:@"" intoString:&extras] ) { - extras = @""; + NSDictionary *tempRow = [tableStatusResult fetchRowAsDictionary]; + if ( [tempRow objectForKey:@"Type"]) { + tableType = [tempRow objectForKey:@"Type"]; + } else { + tableType = [tempRow objectForKey:@"Engine"]; + } + [tableType retain]; + + //get table default values + if ( defaultValues ) { + [defaultValues release]; + defaultValues = nil; + } + + tempDefaultValues = [NSMutableDictionary dictionary]; + for ( i = 0 ; i < [tableFields count] ; i++ ) { + [tempDefaultValues setObject:[[tableFields objectAtIndex:i] objectForKey:@"Default"] forKey:[[tableFields objectAtIndex:i] objectForKey:@"Field"]]; } + defaultValues = [[NSDictionary dictionaryWithDictionary:tempDefaultValues] retain]; + + //put field length and extras in separate key + enumerator = [tableFields objectEnumerator]; + + while ( (field = [enumerator nextObject]) ) { + NSString *type; + NSString *length; + NSString *extras; + + // scan for length and extras like unsigned + [scanner initWithString:[field objectForKey:@"Type"]]; + [scanner scanUpToString:@"(" intoString:&type]; + [scanner scanString:@"(" intoString:nil]; + + if ( ![scanner scanUpToString:@")" intoString:&length] ) + length = @""; + + [scanner scanString:@")" intoString:nil]; + if ( ![scanner scanUpToString:@"" intoString:&extras] ) { + extras = @""; + } + // get possible values if field is enum or set if ( [type isEqualToString:@"enum"] || [type isEqualToString:@"set"] ) { NSMutableArray *possibleValues = [[[length substringWithRange:NSMakeRange(1,[length length]-2)] componentsSeparatedByString:@"','"] mutableCopy]; NSMutableString *possibleValue = [NSMutableString string]; + for ( i = 0 ; i < [possibleValues count] ; i++ ) { [possibleValue setString:[possibleValues objectAtIndex:i]]; [possibleValue replaceOccurrencesOfString:@"''" withString:@"'" options:nil range:NSMakeRange(0,[possibleValue length])]; [possibleValue replaceOccurrencesOfString:@"\\\\" withString:@"\\" options:nil range:NSMakeRange(0,[possibleValue length])]; [possibleValues replaceObjectAtIndex:i withObject:[NSString stringWithString:possibleValue]]; } + [enumFields setObject:[NSArray arrayWithArray:possibleValues] forKey:[field objectForKey:@"Field"]]; [possibleValues release]; } - + // scan extras for values like unsigned, zerofill, binary - extrasArray = [extras componentsSeparatedByString:@" "]; - extrasEnumerator = [extrasArray objectEnumerator]; - while ( (extra = [extrasEnumerator nextObject]) ) { - if ( [extra isEqualToString:@"unsigned"] ) { - [field setObject:@"1" forKey:@"unsigned"]; - } else if ( [extra isEqualToString:@"zerofill"] ) { - [field setObject:@"1" forKey:@"zerofill"]; - } else if ( [extra isEqualToString:@"binary"] ) { - [field setObject:@"1" forKey:@"binary"]; - } else { - if ( ![extra isEqualToString:@""] ) - NSLog(@"ERROR: unknown option in field definition: %@", extra); - } + extrasArray = [extras componentsSeparatedByString:@" "]; + extrasEnumerator = [extrasArray objectEnumerator]; + + while ( (extra = [extrasEnumerator nextObject]) ) { + if ( [extra isEqualToString:@"unsigned"] ) { + [field setObject:@"1" forKey:@"unsigned"]; + } else if ( [extra isEqualToString:@"zerofill"] ) { + [field setObject:@"1" forKey:@"zerofill"]; + } else if ( [extra isEqualToString:@"binary"] ) { + [field setObject:@"1" forKey:@"binary"]; + } else { + if ( ![extra isEqualToString:@""] ) + NSLog(@"ERROR: unknown option in field definition: %@", extra); + } + } + + [field setObject:type forKey:@"Type"]; + [field setObject:length forKey:@"Length"]; } - [field setObject:type forKey:@"Type"]; - [field setObject:length forKey:@"Length"]; - } - - // Determine the table type + + // Determine the table type if ( ![tableType isKindOfClass:[NSNull class]] ) { [tableTypeButton selectItemWithTitle:tableType]; [tableTypeButton setEnabled:YES]; @@ -164,53 +181,56 @@ [tableTypeButton selectItemWithTitle:@"--"]; [tableTypeButton setEnabled:NO]; } - - //enable buttons - [addFieldButton setEnabled:YES]; - [copyFieldButton setEnabled:YES]; - [removeFieldButton setEnabled:YES]; - [addIndexButton setEnabled:YES]; - [removeIndexButton setEnabled:YES]; - - //add columns to indexedColumnsField - [indexedColumnsField removeAllItems]; - enumerator = [tableFields objectEnumerator]; - while ( (field = [enumerator nextObject]) ) { - [indexedColumnsField addItemWithObjectValue:[field objectForKey:@"Field"]]; - } - if ( [tableFields count] < 10 ) { - [indexedColumnsField setNumberOfVisibleItems:[tableFields count]]; - } else { - [indexedColumnsField setNumberOfVisibleItems:10]; - } - - [tableSourceView reloadData]; - [indexView reloadData]; - - // display and *then* tile to force scroll bars to be in the correct position - [[tableSourceView enclosingScrollView] display]; - [[tableSourceView enclosingScrollView] tile]; - - //query finished - [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryHasBeenPerformed" object:self]; - - [scanner release]; + + //enable buttons + [addFieldButton setEnabled:YES]; + [copyFieldButton setEnabled:YES]; + [removeFieldButton setEnabled:YES]; + [addIndexButton setEnabled:YES]; + [removeIndexButton setEnabled:YES]; + + //add columns to indexedColumnsField + [indexedColumnsField removeAllItems]; + enumerator = [tableFields objectEnumerator]; + + while ( (field = [enumerator nextObject]) ) { + [indexedColumnsField addItemWithObjectValue:[field objectForKey:@"Field"]]; + } + + if ( [tableFields count] < 10 ) { + [indexedColumnsField setNumberOfVisibleItems:[tableFields count]]; + } else { + [indexedColumnsField setNumberOfVisibleItems:10]; + } + + [tableSourceView reloadData]; + [indexView reloadData]; + + // display and *then* tile to force scroll bars to be in the correct position + [[tableSourceView enclosingScrollView] display]; + [[tableSourceView enclosingScrollView] tile]; + + //query finished + [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryHasBeenPerformed" object:self]; + + [scanner release]; } -- (IBAction)reloadTable:(id)sender /* reloads the table (performing a new mysql-query) */ +- (IBAction)reloadTable:(id)sender { [self loadTable:selectedTable]; } -//edit methods -- (IBAction)addField:(id)sender +#pragma mark Edit methods + /* adds an empty row to the tableSource-array and goes into edit mode */ +- (IBAction)addField:(id)sender { /* if ( ![self addRowToDB] ) @@ -230,10 +250,10 @@ adds an empty row to the tableSource-array and goes into edit mode [tableSourceView editColumn:0 row:[tableSourceView numberOfRows]-1 withEvent:nil select:YES]; } -- (IBAction)copyField:(id)sender /* copies a field and goes in edit mode for the new field */ +- (IBAction)copyField:(id)sender { NSMutableDictionary *tempRow; @@ -255,10 +275,10 @@ copies a field and goes in edit mode for the new field [tableSourceView editColumn:0 row:[tableSourceView numberOfRows]-1 withEvent:nil select:YES]; } -- (IBAction)addIndex:(id)sender /* adds the index to the mysql-db and stops modal session with code 1 when success, 0 when error and -1 when no columns specified */ +- (IBAction)addIndex:(id)sender { NSString *indexName; NSArray *indexedColumns; @@ -308,10 +328,10 @@ NSLog([NSString stringWithFormat:@"ALTER TABLE `%@` ADD %@ %@ (`%@`)", } } -- (IBAction)removeField:(id)sender /* opens alertsheet and asks for confirmation */ +- (IBAction)removeField:(id)sender { if ( ![tableSourceView numberOfSelectedRows] ) return; @@ -323,10 +343,10 @@ opens alertsheet and asks for confirmation [[tableFields objectAtIndex:[tableSourceView selectedRow]] objectForKey:@"Field"]] ); } -- (IBAction)removeIndex:(id)sender /* opens alertsheet and asks for confirmation */ +- (IBAction)removeIndex:(id)sender { if ( ![indexView numberOfSelectedRows] ) return; @@ -372,11 +392,12 @@ opens alertsheet and asks for confirmation } -//index sheet methods -- (IBAction)openIndexSheet:(id)sender +#pragma mark Index sheet methods + /* opens the indexSheet */ +- (IBAction)openIndexSheet:(id)sender { int code = 0; @@ -396,9 +417,9 @@ opens the indexSheet [NSApp endSheet:indexSheet]; [indexSheet orderOut:nil]; -//code == -1 -> no columns specified -//code == 0 -> error while adding index -//code == 1 -> index added with succes OR sheet closed without adding index + //code == -1 -> no columns specified + //code == 0 -> error while adding index + //code == 1 -> index added with succes OR sheet closed without adding index if ( code == 0 ) { NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"Couldn't add index.\nMySQL said: %@", @"message of panel when index cannot be created"), [mySQLConnection getLastErrorMessage]]); @@ -408,18 +429,18 @@ opens the indexSheet } } -- (IBAction)closeIndexSheet:(id)sender /* closes the indexSheet without adding the index (stops modal session with code 1) */ +- (IBAction)closeIndexSheet:(id)sender { [NSApp stopModalWithCode:1]; } -- (IBAction)chooseIndexType:(id)sender /* invoked when user chooses an index type */ +- (IBAction)chooseIndexType:(id)sender { if ( [[indexTypeField titleOfSelectedItem] isEqualToString:@"PRIMARY KEY"] ) { [indexNameField setEnabled:NO]; @@ -431,29 +452,29 @@ invoked when user chooses an index type } } -- (void)closeAlertSheet /* reopens indexSheet after errorSheet (no columns specified) */ +- (void)closeAlertSheet { [self openIndexSheet:self]; } -//key sheet methods -- (IBAction)closeKeySheet:(id)sender /* closes the keySheet */ +- (IBAction)closeKeySheet:(id)sender { [NSApp stopModalWithCode:[sender tag]]; } -//additional methods -- (void)setConnection:(CMMCPConnection *)theConnection +#pragma mark Additional methods + /* sets the connection (received from TableDocument) and makes things that have to be done only once */ +- (void)setConnection:(CMMCPConnection *)theConnection { NSEnumerator *indexColumnsEnumerator = [[indexView tableColumns] objectEnumerator]; NSEnumerator *fieldColumnsEnumerator = [[tableSourceView tableColumns] objectEnumerator]; @@ -464,7 +485,7 @@ sets the connection (received from TableDocument) and makes things that have to prefs = [[NSUserDefaults standardUserDefaults] retain]; -//set up tableView + //set up tableView [tableSourceView registerForDraggedTypes:[NSArray arrayWithObjects:@"SequelProPasteboard", nil]]; while ( (indexColumn = [indexColumnsEnumerator nextObject]) ) { @@ -479,10 +500,10 @@ sets the connection (received from TableDocument) and makes things that have to } } -- (NSArray *)fetchResultAsArray:(CMMCPResult *)theResult /* fetches the result as an array with a dictionary for each row in it */ +- (NSArray *)fetchResultAsArray:(CMMCPResult *)theResult { NSMutableArray *tempResult = [NSMutableArray array]; NSMutableDictionary *tempRow; @@ -536,7 +557,7 @@ returns YES if no row is beeing edited and nothing has to be written to db theRow = [tableFields objectAtIndex:[tableSourceView selectedRow]]; if ( isEditingNewRow ) { -//ADD syntax + //ADD syntax if ( [[theRow objectForKey:@"Length"] isEqualToString:@""] || ![theRow objectForKey:@"Length"] ) { queryString = [NSMutableString stringWithFormat:@"ALTER TABLE `%@` ADD `%@` %@", selectedTable, [theRow objectForKey:@"Field"], [theRow objectForKey:@"Type"]]; @@ -546,7 +567,7 @@ returns YES if no row is beeing edited and nothing has to be written to db [theRow objectForKey:@"Length"]]; } } else { -//CHANGE syntax + //CHANGE syntax if ( [[theRow objectForKey:@"Length"] isEqualToString:@""] || ![theRow objectForKey:@"Length"] ) { queryString = [NSMutableString stringWithFormat:@"ALTER TABLE `%@` CHANGE `%@` `%@` %@", selectedTable, [oldRow objectForKey:@"Field"], [theRow objectForKey:@"Field"], @@ -557,7 +578,8 @@ returns YES if no row is beeing edited and nothing has to be written to db [theRow objectForKey:@"Type"], [theRow objectForKey:@"Length"]]; } } -//field specification + + //field specification if ( [[theRow objectForKey:@"unsigned"] intValue] == 1 ) { [queryString appendString:@" UNSIGNED"]; } @@ -585,11 +607,12 @@ returns YES if no row is beeing edited and nothing has to be written to db } else { [queryString appendString:@" "]; } - if ( ![[theRow objectForKey:@"Extra"] isEqualToString:@""] && - ![[theRow objectForKey:@"Extra"] isEqualToString:@"None"] && - [theRow objectForKey:@"Extra"] ) + + if ( ![[theRow objectForKey:@"Extra"] isEqualToString:@""] && ![[theRow objectForKey:@"Extra"] isEqualToString:@"None"] && [theRow objectForKey:@"Extra"] ) { [queryString appendString:[theRow objectForKey:@"Extra"]]; -//asks to add an index to query if auto_increment is set and field isn't indexed + } + + //asks to add an index to query if auto_increment is set and field isn't indexed if ( [[theRow objectForKey:@"Extra"] isEqualToString:@"auto_increment"] && ([[theRow objectForKey:@"Key"] isEqualToString:@""] || ![theRow objectForKey:@"Key"]) ) { [chooseKeyButton selectItemAtIndex:0]; @@ -600,7 +623,8 @@ returns YES if no row is beeing edited and nothing has to be written to db [NSApp endSheet:keySheet]; [keySheet orderOut:nil]; - if ( code ) { + + if ( code ) { if ( [chooseKeyButton indexOfSelectedItem] == 0 ) { [queryString appendString:@" PRIMARY KEY"]; } else { @@ -609,9 +633,10 @@ returns YES if no row is beeing edited and nothing has to be written to db } } } + [mySQLConnection queryString:queryString]; -//NSLog(queryString); + //NSLog(queryString); if ( [[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { isEditingRow = NO; @@ -620,7 +645,7 @@ returns YES if no row is beeing edited and nothing has to be written to db return YES; } else { alertSheetOpened = YES; -//problem: alert sheet doesn't respond to first click + //problem: alert sheet doesn't respond to first click if ( isEditingNewRow ) { NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), NSLocalizedString(@"Cancel", @"cancel button"), nil, tableWindow, self, @selector(sheetDidEnd:returnCode:contextInfo:), nil, @"addrow", [NSString stringWithFormat:NSLocalizedString(@"Couldn't add field %@.\nMySQL said: %@", @"message of panel when field cannot be added"), @@ -635,18 +660,19 @@ returns YES if no row is beeing edited and nothing has to be written to db } - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo -/* -if contextInfo == addrow: remain in edit-mode if user hits OK, otherwise cancel editing -if contextInfo == removefield: removes row from mysql-db if user hits ok -if contextInfo == removeindex: removes index from mysql-db if user hits ok -*/ { + /* + if contextInfo == addrow: remain in edit-mode if user hits OK, otherwise cancel editing + if contextInfo == removefield: removes row from mysql-db if user hits ok + if contextInfo == removeindex: removes index from mysql-db if user hits ok + */ + [sheet orderOut:self]; if ( [contextInfo isEqualToString:@"addrow"] ) { alertSheetOpened = NO; if ( returnCode == NSAlertDefaultReturn ) { - //problem: reentering edit mode for first cell doesn't function + //problem: reentering edit mode for first cell doesn't function [tableSourceView editColumn:0 row:[tableSourceView selectedRow] withEvent:nil select:YES]; } else { if ( !isEditingNewRow ) { @@ -662,7 +688,7 @@ if contextInfo == removeindex: removes index from mysql-db if user hits ok [tableSourceView reloadData]; } else if ( [contextInfo isEqualToString:@"removefield"] ) { if ( returnCode == NSAlertDefaultReturn ) { -//remove row + //remove row [mySQLConnection queryString:[NSString stringWithFormat:@"ALTER TABLE `%@` DROP `%@`", selectedTable, [[tableFields objectAtIndex:[tableSourceView selectedRow]] objectForKey:@"Field"]]]; @@ -677,7 +703,7 @@ if contextInfo == removeindex: removes index from mysql-db if user hits ok } } else if ( [contextInfo isEqualToString:@"removeindex"] ) { if ( returnCode == NSAlertDefaultReturn ) { -//remove index + //remove index if ( [[[indexes objectAtIndex:[indexView selectedRow]] objectForKey:@"Key_name"] isEqualToString:@"PRIMARY"] ) { [mySQLConnection queryString:[NSString stringWithFormat:@"ALTER TABLE `%@` DROP PRIMARY KEY", selectedTable]]; } else { @@ -696,11 +722,12 @@ if contextInfo == removeindex: removes index from mysql-db if user hits ok } -//getter methods -- (NSString *)defaultValueForField:(NSString *)field +#pragma mark Getter methods + /* get the default value for a specified field */ +- (NSString *)defaultValueForField:(NSString *)field { if ( ![defaultValues objectForKey:field] ) { return [prefs objectForKey:@"nullValue"]; @@ -711,38 +738,39 @@ get the default value for a specified field } } -- (NSArray *)fieldNames /* - returns an array containing the field names of the selected table - */ +returns an array containing the field names of the selected table +*/ +- (NSArray *)fieldNames { - NSMutableArray *tempArray = [NSMutableArray array]; - NSEnumerator *enumerator; - id field; - - //load table if not already done - if ( ![tablesListInstance structureLoaded] ) { - [self loadTable:(NSString *)[tablesListInstance table]]; - } - //get field names - enumerator = [tableFields objectEnumerator]; - while ( (field = [enumerator nextObject]) ) { - [tempArray addObject:[field objectForKey:@"Field"]]; - } + NSMutableArray *tempArray = [NSMutableArray array]; + NSEnumerator *enumerator; + id field; + + //load table if not already done + if ( ![tablesListInstance structureLoaded] ) { + [self loadTable:(NSString *)[tablesListInstance table]]; + } + + //get field names + enumerator = [tableFields objectEnumerator]; + while ( (field = [enumerator nextObject]) ) { + [tempArray addObject:[field objectForKey:@"Field"]]; + } - return [NSArray arrayWithArray:tempArray]; + return [NSArray arrayWithArray:tempArray]; } -- (NSDictionary *)enumFields /* returns a dictionary containing enum/set field names as key and possible values as array */ +- (NSDictionary *)enumFields { return [NSDictionary dictionaryWithDictionary:enumFields]; } +#pragma mark TableView datasource methods -//tableView datasource methods - (int)numberOfRowsInTableView:(NSTableView *)aTableView { if ( aTableView == tableSourceView ) { @@ -820,16 +848,13 @@ would result in a position change. int originalRow; // Ensure the drop is of the correct type - if (operation == NSTableViewDropAbove && row != -1 && [pboardTypes containsObject:@"SequelProPasteboard"]) - { + if (operation == NSTableViewDropAbove && row != -1 && [pboardTypes containsObject:@"SequelProPasteboard"]) { // Ensure the drag originated within this table - if ([info draggingSource] == tableView) - { + if ([info draggingSource] == tableView) { originalRow = [[[info draggingPasteboard] stringForType:@"SequelProPasteboard"] intValue]; - if (row != originalRow && row != (originalRow+1)) - { + if (row != originalRow && row != (originalRow+1)) { return NSDragOperationMove; } } @@ -852,8 +877,7 @@ Having validated a drop, perform the field/column reordering to match. [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryWillBePerformed" object:self]; // Begin construction of the reordering query - queryString = [NSMutableString stringWithFormat:@"ALTER TABLE `%@` MODIFY COLUMN `%@` %@", - selectedTable, + queryString = [NSMutableString stringWithFormat:@"ALTER TABLE `%@` MODIFY COLUMN `%@` %@", selectedTable, [[tableFields objectAtIndex:originalRow] objectForKey:@"Field"], [[tableFields objectAtIndex:originalRow] objectForKey:@"Type"]]; @@ -866,12 +890,9 @@ Having validated a drop, perform the field/column reordering to match. } // Add the new location - if ( destinationRow == 0 ) - { + if ( destinationRow == 0 ){ [queryString appendString:@" FIRST"]; - } - else - { + } else { [queryString appendString:[NSString stringWithFormat:@" AFTER `%@`", [[tableFields objectAtIndex:destinationRow-1] objectForKey:@"Field"]]]; } @@ -881,9 +902,7 @@ Having validated a drop, perform the field/column reordering to match. if ( ![[mySQLConnection getLastErrorMessage] isEqualTo:@""] ) { NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"Couldn't move field. MySQL said: %@", @"message of panel when field cannot be added in drag&drop operation"), [mySQLConnection getLastErrorMessage]]); - } - else - { + } else { [self loadTable:selectedTable]; if ( originalRow < destinationRow ) { [tableSourceView selectRow:destinationRow-1 byExtendingSelection:NO]; @@ -897,7 +916,8 @@ Having validated a drop, perform the field/column reordering to match. return YES; } -//tableView delegate methods +#pragma mark TtableView delegate methods + - (BOOL)selectionShouldChangeInTableView:(NSTableView *)aTableView { /* @@ -918,10 +938,10 @@ Having validated a drop, perform the field/column reordering to match. return [self addRowToDB]; } -- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command /* traps enter and esc and make/cancel editing without entering next row */ +- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command { int row, column; @@ -953,10 +973,9 @@ traps enter and esc and make/cancel editing without entering next row } } return TRUE; - } - else if ( [[control window] methodForSelector:command] == [[control window] methodForSelector:@selector(_cancelKey:)] || - [textView methodForSelector:command] == [textView methodForSelector:@selector(complete:)] ) //trap esc - { + + } else if ( [[control window] methodForSelector:command] == [[control window] methodForSelector:@selector(_cancelKey:)] || + [textView methodForSelector:command] == [textView methodForSelector:@selector(complete:)] ) { //abort editing [control abortEditing]; if ( isEditingRow && !isEditingNewRow ) { @@ -969,35 +988,25 @@ traps enter and esc and make/cancel editing without entering next row [tableSourceView reloadData]; } return TRUE; - } - else - { + } else { return FALSE; } } -//slitView delegate methods +#pragma mark SplitView delegate methods + - (BOOL)splitView:(NSSplitView *)sender canCollapseSubview:(NSView *)subview -/* -tells the splitView that it can collapse views -*/ { return YES; } - (float)splitView:(NSSplitView *)sender constrainMaxCoordinate:(float)proposedMax ofSubviewAt:(int)offset -/* -defines max position of splitView -*/ { return proposedMax - 150; } - (float)splitView:(NSSplitView *)sender constrainMinCoordinate:(float)proposedMin ofSubviewAt:(int)offset -/* -defines min position of splitView -*/ { return proposedMin + 150; } diff --git a/TablesList.m b/TablesList.m index ee958d1d..0cad02c5 100644 --- a/TablesList.m +++ b/TablesList.m @@ -31,11 +31,13 @@ @implementation TablesList -//IBAction methods -- (IBAction)updateTables:(id)sender + +#pragma mark IBAction methods + /* loads all table names in array tables and reload the tableView */ +- (IBAction)updateTables:(id)sender { CMMCPResult *theResult; int i; @@ -59,10 +61,10 @@ loads all table names in array tables and reload the tableView [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryHasBeenPerformed" object:self]; } -- (IBAction)addTable:(id)sender /* adds a new table to the tables-array (no changes in mysql-db) */ +- (IBAction)addTable:(id)sender { if ( ![tableSourceInstance selectionShouldChangeInTableView:nil] || ![tableContentInstance selectionShouldChangeInTableView:nil] || @@ -76,11 +78,11 @@ adds a new table to the tables-array (no changes in mysql-db) [tablesListView editColumn:0 row:[tables count]-1 withEvent:nil select:YES]; } -- (IBAction)removeTable:(id)sender /* invoked when user hits the remove button alert sheet to ask user if he really wants to delete the table */ +- (IBAction)removeTable:(id)sender { if ( ![tablesListView numberOfSelectedRows] ) return; @@ -99,10 +101,10 @@ alert sheet to ask user if he really wants to delete the table } } -- (IBAction)copyTable:(id)sender /* copies a table, if desired with content */ +- (IBAction)copyTable:(id)sender { CMMCPResult *queryResult; NSScanner *scanner = [NSScanner alloc]; @@ -121,12 +123,15 @@ copies a table, if desired with content return; [tableWindow endEditingFor:nil]; -//open copyTableSheet + //open copyTableSheet [copyTableNameField setStringValue:[NSString stringWithFormat:@"%@Copy", [tables objectAtIndex:[tablesListView selectedRow]]]]; [copyTableContentSwitch setState:NSOffState]; [NSApp beginSheet:copyTableSheet - modalForWindow:tableWindow modalDelegate:self - didEndSelector:nil contextInfo:nil]; + modalForWindow:tableWindow + modalDelegate:self + didEndSelector:nil + contextInfo:nil]; + code = [NSApp runModalForWindow:copyTableSheet]; [NSApp endSheet:copyTableSheet]; @@ -139,100 +144,68 @@ copies a table, if desired with content return; } -//get table structure + //get table structure queryResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW CREATE TABLE `%@`", [tables objectAtIndex:[tablesListView selectedRow]]]]; + if ( ![queryResult numOfRows] ) { -//error while getting table structure - NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"Couldn't get table information.\nMySQL said: %@", @"message of panel when table information cannot be retrieved"), [mySQLConnection getLastErrorMessage]]); - } else { -//insert new table name in create syntax and create new table - [scanner initWithString:[[queryResult fetchRowAsDictionary] objectForKey:@"Create Table"]]; - [scanner scanUpToString:@"(" intoString:nil]; - [scanner scanUpToString:@"" intoString:&scanString]; - [mySQLConnection queryString:[NSString stringWithFormat:@"CREATE TABLE `%@` %@", [copyTableNameField stringValue], scanString]]; - if ( ![[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { -//error while creating new table - NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, - [NSString stringWithFormat:NSLocalizedString(@"Couldn't create table.\nMySQL said: %@", @"message of panel when table cannot be created"), [mySQLConnection getLastErrorMessage]]); - } else { - if ( [copyTableContentSwitch state] == NSOnState ) { -//copy table content -/* - queryResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SELECT * FROM `%@`", - [tables objectAtIndex:[tablesListView selectedRow]]]]; - fieldNames = [queryResult fetchFieldNames]; - rowCount = [queryResult numOfRows]; - for ( i = 0 ; i < rowCount ; i++ ) { - [queryResult dataSeek:i]; - theRow = [queryResult fetchRowAsArray]; - fieldValues = [NSMutableArray array]; - for ( j = 0 ; j < [theRow count] ; j++ ) { - if ( [[theRow objectAtIndex:j] isKindOfClass:[NSData class]] ) { -// [rowValue setString:[[NSString alloc] initWithData:[theRow objectAtIndex:j] -// encoding:[mySQLConnection encoding]]]; - [rowValue setString:[mySQLConnection prepareBinaryData:[theRow objectAtIndex:j]]]; - } else { - [rowValue setString:[mySQLConnection prepareString:[[theRow objectAtIndex:j] description]]]; - } -*/ -/* - //escape special characters - for ( u = 0 ; u < [rowValue length] ; u++ ) { - if ( [rowValue characterAtIndex:u] == '\\' ) { - [rowValue insertString:@"\\" atIndex:u]; - u++; - } else if ( [rowValue characterAtIndex:u] == '"' ) { - [rowValue insertString:@"\\" atIndex:u]; - u++; - } - } -*/ -/* - if ( [[theRow objectAtIndex:j] isKindOfClass:[NSNull class]] ) { - [fieldValues addObject:@"NULL"]; - } else { -// [fieldValues addObject:[NSString stringWithFormat:@"\"%@\"", [mySQLConnection prepareString:rowValue]]]; -// [fieldValues addObject:[NSString stringWithFormat:@"\"%@\"", rowValue]]; -// [fieldValues addObject:[NSString stringWithFormat:@"'%@'", [mySQLConnection prepareString:rowValue]]]; - [fieldValues addObject:[NSString stringWithFormat:@"'%@'", rowValue]]; - - } - } - [mySQLConnection queryString:[NSString stringWithFormat:@"INSERT INTO `%@` (`%@`) VALUES (%@)", - [copyTableNameField stringValue], [fieldNames componentsJoinedByString:@"`,`"], - [fieldValues componentsJoinedByString:@","]]]; - if ( ![[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { - errors = YES; - } - } - if ( errors ) - NSBeginAlertSheet(@"Warning", @"OK", nil, nil, tableWindow, self, nil, nil, nil, - @"There have been errors while copying table content. Please control the new table."); -*/ - [mySQLConnection queryString:[NSString stringWithFormat:@"INSERT INTO `%@` SELECT * FROM `%@`", - [copyTableNameField stringValue], [tables objectAtIndex:[tablesListView selectedRow]]]]; - if ( ![[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { - NSBeginAlertSheet(NSLocalizedString(@"Warning", @"warning"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, - NSLocalizedString(@"There have been errors while copying table content. Please control the new table.", @"message of panel when table content cannot be copied")); - } - } - [tables insertObject:[copyTableNameField stringValue] atIndex:[tablesListView selectedRow]+1]; - [tablesListView reloadData]; - [tablesListView selectRow:[tablesListView selectedRow]+1 byExtendingSelection:NO]; - [tablesListView scrollRowToVisible:[tablesListView selectedRow]]; - } - } + //error while getting table structure + NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, + [NSString stringWithFormat:NSLocalizedString(@"Couldn't get table information.\nMySQL said: %@", @"message of panel when table information cannot be retrieved"), [mySQLConnection getLastErrorMessage]]); + + } else { + //insert new table name in create syntax and create new table + [scanner initWithString:[[queryResult fetchRowAsDictionary] objectForKey:@"Create Table"]]; + [scanner scanUpToString:@"(" intoString:nil]; + [scanner scanUpToString:@"" intoString:&scanString]; + [mySQLConnection queryString:[NSString stringWithFormat:@"CREATE TABLE `%@` %@", [copyTableNameField stringValue], scanString]]; + + if ( ![[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { + //error while creating new table + NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, + [NSString stringWithFormat:NSLocalizedString(@"Couldn't create table.\nMySQL said: %@", @"message of panel when table cannot be created"), [mySQLConnection getLastErrorMessage]]); + } else { + + if ( [copyTableContentSwitch state] == NSOnState ) { + //copy table content + [mySQLConnection queryString:[NSString stringWithFormat: + @"INSERT INTO `%@` SELECT * FROM `%@`", + [copyTableNameField stringValue], + [tables objectAtIndex:[tablesListView selectedRow]] + ]]; + + if ( ![[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { + NSBeginAlertSheet( + NSLocalizedString(@"Warning", @"warning"), + NSLocalizedString(@"OK", @"OK button"), + nil, + nil, + tableWindow, + self, + nil, + nil, + nil, + NSLocalizedString(@"There have been errors while copying table content. Please control the new table.", @"message of panel when table content cannot be copied") + ); + } + } + + [tables insertObject:[copyTableNameField stringValue] atIndex:[tablesListView selectedRow]+1]; + [tablesListView reloadData]; + [tablesListView selectRow:[tablesListView selectedRow]+1 byExtendingSelection:NO]; + [tablesListView scrollRowToVisible:[tablesListView selectedRow]]; + } + } } -//alert sheet methods -- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo +#pragma mark Alert sheet methods + /* method for alert sheets invoked when user wants to delete a table */ +- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo { if ( [contextInfo isEqualToString:@"addRow"] ) { alertSheetOpened = NO; @@ -244,24 +217,25 @@ invoked when user wants to delete a table } } -//copyTableSheet methods -- (IBAction)closeCopyTableSheet:(id)sender /* closes copyTableSheet and stops modal session */ +- (IBAction)closeCopyTableSheet:(id)sender { [NSApp stopModalWithCode:[sender tag]]; } -//additional methods -- (void)removeTable; +#pragma mark Additional methods + /* removes selected table(s) from mysql-db and tableView */ +- (void)removeTable; { NSIndexSet *indexes = [tablesListView selectedRowIndexes]; NSString *errorText; BOOL error = FALSE; + // get last index unsigned currentIndex = [indexes lastIndex]; while (currentIndex != NSNotFound) @@ -269,21 +243,24 @@ removes selected table(s) from mysql-db and tableView [mySQLConnection queryString:[NSString stringWithFormat:@"DROP TABLE `%@`", [tables objectAtIndex:currentIndex]]]; if ( [[mySQLConnection getLastErrorMessage] isEqualTo:@""] ) { - //dropped table with success + //dropped table with success [tables removeObjectAtIndex:currentIndex]; } else { - //couldn't drop table + //couldn't drop table error = TRUE; errorText = [mySQLConnection getLastErrorMessage]; } + // get next index (beginning from the end) currentIndex = [indexes indexLessThanIndex:currentIndex]; } + [tablesListView deselectAll:self]; -// [tableSourceInstance loadTable:nil]; -// [tableContentInstance loadTable:nil]; -// [tableStatusInstance loadTable:nil]; + //[tableSourceInstance loadTable:nil]; + //[tableContentInstance loadTable:nil]; + //[tableStatusInstance loadTable:nil]; [tablesListView reloadData]; + // set window title [tableWindow setTitle:[NSString stringWithFormat:@"(MySQL %@) %@@%@/%@", [tableDocumentInstance mySQLVersion], [tableDocumentInstance user], [tableDocumentInstance host], [tableDocumentInstance database]]]; @@ -292,19 +269,20 @@ removes selected table(s) from mysql-db and tableView [NSString stringWithFormat:NSLocalizedString(@"Couldn't remove table.\nMySQL said: %@", @"message of panel when table cannot be removed"), errorText]); } -- (void)setConnection:(CMMCPConnection *)theConnection /* sets the connection (received from TableDocument) and makes things that have to be done only once */ +- (void)setConnection:(CMMCPConnection *)theConnection { mySQLConnection = theConnection; -// prefs = [[NSUserDefaults standardUserDefaults] retain]; + //prefs = [[NSUserDefaults standardUserDefaults] retain]; -//set smallSystemFonts -// [[[tablesListView tableColumnWithIdentifier:@"tables"] dataCell] setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; -// [copyTableNameField setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; -// if ( [prefs boolForKey:@"useMonospacedFonts"] ) { + //set smallSystemFonts + //[[[tablesListView tableColumnWithIdentifier:@"tables"] dataCell] setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; + //[copyTableNameField setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; + //if ( [prefs boolForKey:@"useMonospacedFonts"] ) { + if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"useMonospacedFonts"] ) { [[[tablesListView tableColumnWithIdentifier:@"tables"] dataCell] setFont:[NSFont fontWithName:@"Monaco" size:[NSFont smallSystemFontSize]]]; @@ -313,21 +291,22 @@ sets the connection (received from TableDocument) and makes things that have to [self updateTables:self]; } -- (void)doPerformQueryService:(NSString *)query /* selects customQuery tab and passes query to customQueryInstance */ +- (void)doPerformQueryService:(NSString *)query { [tabView selectTabViewItemAtIndex:2]; [customQueryInstance doPerformQueryService:query]; } -//getter methods -- (NSString *)table +#pragma mark Getter methods + /* returns the currently selected table or nil if no table or mulitple tables are selected */ +- (NSString *)table { if ( [tablesListView numberOfSelectedRows] == 1 ) { return [tables objectAtIndex:[tablesListView selectedRow]]; @@ -338,32 +317,32 @@ returns the currently selected table or nil if no table or mulitple tables are s } } -- (BOOL)structureLoaded /* returns YES if table source has already been loaded */ +- (BOOL)structureLoaded { return structureLoaded; } -- (BOOL)contentLoaded /* returns YES if table content has already been loaded */ +- (BOOL)contentLoaded { return contentLoaded; } -- (BOOL)statusLoaded /* returns YES if table status has already been loaded */ +- (BOOL)statusLoaded { return statusLoaded; } +#pragma mark Datasource methods -//tableView datasource methods - (int)numberOfRowsInTableView:(NSTableView *)aTableView { return [tables count]; @@ -385,143 +364,149 @@ returns YES if table status has already been loaded forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex { - if ( [[tables objectAtIndex:rowIndex] isEqualToString:@""] ) { - //new table - if ( [anObject isEqualToString:@""] ) { - //table has no name - alertSheetOpened = YES; - NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, - @selector(sheetDidEnd:returnCode:contextInfo:), nil, @"addRow", NSLocalizedString(@"Table must have a name.", @"message of panel when no name is given for table")); - [tables removeObjectAtIndex:rowIndex]; - [tablesListView reloadData]; - } else { + + if ( [[tables objectAtIndex:rowIndex] isEqualToString:@""] ) { + //new table + if ( [anObject isEqualToString:@""] ) { + //table has no name + alertSheetOpened = YES; + NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, + @selector(sheetDidEnd:returnCode:contextInfo:), nil, @"addRow", NSLocalizedString(@"Table must have a name.", @"message of panel when no name is given for table")); + [tables removeObjectAtIndex:rowIndex]; + [tablesListView reloadData]; + } else { if ( [tableDocumentInstance supportsEncoding] ) { [mySQLConnection queryString:[NSString stringWithFormat:@"CREATE TABLE `%@` (id int) DEFAULT CHARACTER SET %@", anObject, [tableDocumentInstance encoding]]]; - } else { + } else { [mySQLConnection queryString:[NSString stringWithFormat:@"CREATE TABLE `%@` (id int)", anObject]]; } + if ( [[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { - //added table with success - // NSLog(@"added new table with success"); - [tables replaceObjectAtIndex:rowIndex withObject:anObject]; - if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0 ) { - [tableSourceInstance loadTable:anObject]; - structureLoaded = YES; - contentLoaded = NO; - statusLoaded = NO; - } else if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 1 ) { - [tableSourceInstance loadTable:anObject]; - [tableContentInstance loadTable:anObject]; - structureLoaded = YES; - contentLoaded = YES; - statusLoaded = NO; - } else if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 3 ) { - [tableStatusInstance loadTable:anObject]; - statusLoaded = YES; - structureLoaded = NO; - contentLoaded = NO; - } else { - statusLoaded = NO; - structureLoaded = NO; - contentLoaded = NO; - } + //added table with success + [tables replaceObjectAtIndex:rowIndex withObject:anObject]; + + if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0 ) { + [tableSourceInstance loadTable:anObject]; + structureLoaded = YES; + contentLoaded = NO; + statusLoaded = NO; + } else if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 1 ) { + [tableSourceInstance loadTable:anObject]; + [tableContentInstance loadTable:anObject]; + structureLoaded = YES; + contentLoaded = YES; + statusLoaded = NO; + } else if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 3 ) { + [tableStatusInstance loadTable:anObject]; + statusLoaded = YES; + structureLoaded = NO; + contentLoaded = NO; + } else { + statusLoaded = NO; + structureLoaded = NO; + contentLoaded = NO; + } + // set window title [tableWindow setTitle:[NSString stringWithFormat:@"(MySQL %@) %@@%@/%@/%@", [tableDocumentInstance mySQLVersion], [tableDocumentInstance user], - [tableDocumentInstance host], [tableDocumentInstance database], anObject]]; - } else { - //error while adding new table - // NSLog(@"couldn't add new table"); - alertSheetOpened = YES; - NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, - @selector(sheetDidEnd:returnCode:contextInfo:), nil, @"addRow", - [NSString stringWithFormat:NSLocalizedString(@"Couldn't add table %@.\nMySQL said: %@", @"message of panel when table cannot be created with the given name"), - anObject, [mySQLConnection getLastErrorMessage]]); - [tables removeObjectAtIndex:rowIndex]; - [tablesListView reloadData]; - } - } - } else { - //table modification - if ( [[tables objectAtIndex:rowIndex] isEqualToString:anObject] ) { - //no changes in table name - // NSLog(@"no changes in table name"); - } else if ( [anObject isEqualToString:@""] ) { - //table has no name - // NSLog(@"name is nil"); - alertSheetOpened = YES; - NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, - @selector(sheetDidEnd:returnCode:contextInfo:), nil, @"addRow", NSLocalizedString(@"Table must have a name.", @"message of panel when no name is given for table")); + [tableDocumentInstance host], [tableDocumentInstance database], anObject]]; + } else { + + //error while adding new table + alertSheetOpened = YES; + NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, + @selector(sheetDidEnd:returnCode:contextInfo:), nil, @"addRow", + [NSString stringWithFormat:NSLocalizedString(@"Couldn't add table %@.\nMySQL said: %@", @"message of panel when table cannot be created with the given name"), + anObject, [mySQLConnection getLastErrorMessage]]); + [tables removeObjectAtIndex:rowIndex]; + [tablesListView reloadData]; + } + } } else { - [mySQLConnection queryString:[NSString stringWithFormat:@"RENAME TABLE `%@` TO `%@`", [tables objectAtIndex:rowIndex], anObject]]; - if ( [[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { - // NSLog(@"renamed table with success"); - //renamed with success - [tables replaceObjectAtIndex:rowIndex withObject:anObject]; - if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0 ) { - [tableSourceInstance loadTable:anObject]; - structureLoaded = YES; - contentLoaded = NO; - statusLoaded = NO; - } else if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 1 ) { - [tableSourceInstance loadTable:anObject]; - [tableContentInstance loadTable:anObject]; - structureLoaded = YES; - contentLoaded = YES; - statusLoaded = NO; - } else if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 3 ) { - [tableStatusInstance loadTable:anObject]; - structureLoaded = NO; - contentLoaded = NO; - statusLoaded = YES; + + //table modification + if ( [[tables objectAtIndex:rowIndex] isEqualToString:anObject] ) { + //no changes in table name +// NSLog(@"no changes in table name"); + } else if ( [anObject isEqualToString:@""] ) { + //table has no name +// NSLog(@"name is nil"); + alertSheetOpened = YES; + NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, + @selector(sheetDidEnd:returnCode:contextInfo:), nil, @"addRow", NSLocalizedString(@"Table must have a name.", @"message of panel when no name is given for table")); } else { - statusLoaded = NO; - structureLoaded = NO; - contentLoaded = NO; - } + [mySQLConnection queryString:[NSString stringWithFormat:@"RENAME TABLE `%@` TO `%@`", [tables objectAtIndex:rowIndex], anObject]]; + if ( [[mySQLConnection getLastErrorMessage] isEqualToString:@""] ) { +// NSLog(@"renamed table with success"); + //renamed with success + [tables replaceObjectAtIndex:rowIndex withObject:anObject]; + + if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0 ) { + [tableSourceInstance loadTable:anObject]; + structureLoaded = YES; + contentLoaded = NO; + statusLoaded = NO; + } else if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 1 ) { + [tableSourceInstance loadTable:anObject]; + [tableContentInstance loadTable:anObject]; + structureLoaded = YES; + contentLoaded = YES; + statusLoaded = NO; + } else if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 3 ) { + [tableStatusInstance loadTable:anObject]; + structureLoaded = NO; + contentLoaded = NO; + statusLoaded = YES; + } else { + statusLoaded = NO; + structureLoaded = NO; + contentLoaded = NO; + } + // set window title [tableWindow setTitle:[NSString stringWithFormat:@"(MySQL %@) %@@%@/%@/%@", [tableDocumentInstance mySQLVersion], [tableDocumentInstance user], - [tableDocumentInstance host], [tableDocumentInstance database], anObject]]; - } else { - //error while renaming - // NSLog(@"couldn't rename table"); - alertSheetOpened = YES; - NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, - @selector(sheetDidEnd:returnCode:contextInfo:), nil, @"addRow", - [NSString stringWithFormat:NSLocalizedString(@"Couldn't rename table.\nMySQL said: %@", @"message of panel when table cannot be renamed"), - [mySQLConnection getLastErrorMessage]]); - } + [tableDocumentInstance host], [tableDocumentInstance database], anObject]]; + } else { + //error while renaming +// NSLog(@"couldn't rename table"); + alertSheetOpened = YES; + NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, + @selector(sheetDidEnd:returnCode:contextInfo:), nil, @"addRow", + [NSString stringWithFormat:NSLocalizedString(@"Couldn't rename table.\nMySQL said: %@", @"message of panel when table cannot be renamed"), + [mySQLConnection getLastErrorMessage]]); + } + } } - } } -//tableView delegate methods -- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command +#pragma mark TableView delegate methods + /* traps enter and esc and edit/cancel without entering next row */ +- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command { - if ( [textView methodForSelector:command] == [textView methodForSelector:@selector(insertNewline:)] ) //trap enter - { + if ( [textView methodForSelector:command] == [textView methodForSelector:@selector(insertNewline:)] ) { //save current line [[control window] makeFirstResponder:control]; return TRUE; - } - else if ( [[control window] methodForSelector:command] == [[control window] methodForSelector:@selector(_cancelKey:)] || - [textView methodForSelector:command] == [textView methodForSelector:@selector(complete:)] ) //trap esc - { + + } else if ( [[control window] methodForSelector:command] == [[control window] methodForSelector:@selector(_cancelKey:)] || + [textView methodForSelector:command] == [textView methodForSelector:@selector(complete:)] ) { + //abort editing [control abortEditing]; + if ( [[tables objectAtIndex:[tablesListView selectedRow]] isEqualToString:@""] ) { //user added new table and then pressed escape [tables removeObjectAtIndex:[tablesListView selectedRow]]; [tablesListView reloadData]; } + return TRUE; - } - else - { - return FALSE; - } + } else{ + return FALSE; + } } - (BOOL)selectionShouldChangeInTableView:(NSTableView *)aTableView @@ -538,13 +523,13 @@ traps enter and esc and edit/cancel without entering next row [cell endEditing:[tablesListView currentEditor]]; } */ -//end editing (otherwise problems when user hits reload button) + //end editing (otherwise problems when user hits reload button) [tableWindow endEditingFor:nil]; if ( alertSheetOpened ) { return NO; } -//we have to be sure that TableSource and TableContent have finished editing + //we have to be sure that TableSource and TableContent have finished editing // if ( ![tableSourceInstance addRowToDB] || ![tableContentInstance addRowToDB] ) { if ( ![tableSourceInstance selectionShouldChangeInTableView:nil] || ![tableContentInstance selectionShouldChangeInTableView:nil] ) { @@ -554,10 +539,10 @@ traps enter and esc and edit/cancel without entering next row } } -- (void)tableViewSelectionDidChange:(NSNotification *)aNotification /* loads a table in content or source view (if tab selected) */ +- (void)tableViewSelectionDidChange:(NSNotification *)aNotification { if ( [tablesListView numberOfSelectedRows] == 1 ) { if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0 ) { @@ -581,6 +566,7 @@ loads a table in content or source view (if tab selected) contentLoaded = NO; statusLoaded = NO; } + // set window title [tableWindow setTitle:[NSString stringWithFormat:@"(MySQL %@) %@@%@/%@/%@", [tableDocumentInstance mySQLVersion], [tableDocumentInstance user], [tableDocumentInstance host], [tableDocumentInstance database], [tables objectAtIndex:[tablesListView selectedRow]]]]; @@ -592,43 +578,46 @@ loads a table in content or source view (if tab selected) structureLoaded = NO; contentLoaded = NO; statusLoaded = NO; + // set window title [tableWindow setTitle:[NSString stringWithFormat:@"(MySQL %@) %@@%@/%@", [tableDocumentInstance mySQLVersion], [tableDocumentInstance user], [tableDocumentInstance host], [tableDocumentInstance database]]]; } } -//tabView delegate methods -- (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem +#pragma mark TabView delegate methods + /* loads structure or source if tab selected the first time */ +- (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem { - if ( [tablesListView numberOfSelectedRows] == 1 ) - { - if ( ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0) && !structureLoaded ) - { + if ( [tablesListView numberOfSelectedRows] == 1 ) { + + if ( ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0) && !structureLoaded ) { + [tableSourceInstance loadTable:[tables objectAtIndex:[tablesListView selectedRow]]]; structureLoaded = YES; } - if ( ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 1) && !contentLoaded ) - { + + if ( ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 1) && !contentLoaded ) { + if ( !structureLoaded ) { [tableSourceInstance loadTable:[tables objectAtIndex:[tablesListView selectedRow]]]; structureLoaded = YES; } + [tableContentInstance loadTable:[tables objectAtIndex:[tablesListView selectedRow]]]; contentLoaded = YES; } - if ( ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 3) && !statusLoaded ) - { + + if ( ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 3) && !statusLoaded ) { [tableStatusInstance loadTable:[tables objectAtIndex:[tablesListView selectedRow]]]; statusLoaded = YES; } } /* - if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 3 ) - { + if ( [tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 3 ) { [tableDumpInstance reloadTables:self]; } */ |