diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPTableStructure.h | 4 | ||||
-rw-r--r-- | Source/SPTableStructure.m | 107 | ||||
-rw-r--r-- | Source/SPTableStructureDelegate.m | 14 |
3 files changed, 65 insertions, 60 deletions
diff --git a/Source/SPTableStructure.h b/Source/SPTableStructure.h index fe13b944..c7b47e1a 100644 --- a/Source/SPTableStructure.h +++ b/Source/SPTableStructure.h @@ -75,6 +75,7 @@ NSArray *typeSuggestions; NSArray *extraFieldSuggestions; BOOL isCurrentExtraAutoIncrement; + NSString *autoIncrementIndex; BOOL isEditingRow, isEditingNewRow, isSavingRow, alertSheetOpened; } @@ -96,9 +97,6 @@ // Index sheet methods - (IBAction)closeSheet:(id)sender; -// Key sheet methods -- (IBAction)closeKeySheet:(id)sender; - // Additional methods - (void)setConnection:(MCPConnection *)theConnection; - (NSArray *)fetchResultAsArray:(MCPResult *)theResult; diff --git a/Source/SPTableStructure.m b/Source/SPTableStructure.m index 2392c108..86b4a84a 100644 --- a/Source/SPTableStructure.m +++ b/Source/SPTableStructure.m @@ -61,6 +61,7 @@ extraFieldSuggestions = nil; currentlyEditingRow = -1; isCurrentExtraAutoIncrement = NO; + autoIncrementIndex = nil; fieldValidation = [[SPTableFieldValidation alloc] init]; @@ -337,6 +338,7 @@ [[self onMainThread] setTableDetails:tableDetails]; isCurrentExtraAutoIncrement = [tableDataInstance tableHasAutoIncrementField]; + autoIncrementIndex = nil; // Send the query finished/work complete notification [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:@"SMySQLQueryHasBeenPerformed" object:tableDocumentInstance]; @@ -718,6 +720,7 @@ } isEditingRow = NO; isCurrentExtraAutoIncrement = [tableDataInstance tableHasAutoIncrementField]; + autoIncrementIndex = nil; [tableSourceView reloadData]; currentlyEditingRow = -1; [[tableDocumentInstance parentWindow] makeFirstResponder:tableSourceView]; @@ -732,6 +735,7 @@ [tablesIndexesSplitView setPosition:[tablesIndexesSplitView frame].size.height-130 ofDividerAtIndex:0]; } + #pragma mark - #pragma mark Index sheet methods @@ -744,14 +748,6 @@ [[sender window] orderOut:self]; } -/** - * Closes the key sheet. - */ -- (IBAction)closeKeySheet:(id)sender -{ - [NSApp stopModalWithCode:[sender tag]]; -} - #pragma mark - #pragma mark Additional methods @@ -1047,66 +1043,53 @@ } } - // Asks the user to add an index to query if AUTO_INCREMENT is set and field isn't indexed - if ([theRowExtra isEqualToString:@"AUTO_INCREMENT"] && (![theRow objectForKey:@"Key"] || [[theRow objectForKey:@"Key"] isEqualToString:@""])) - { - [chooseKeyButton selectItemAtIndex:0]; - - [NSApp beginSheet:keySheet - modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self - didEndSelector:nil - contextInfo:nil]; - - code = [NSApp runModalForWindow:keySheet]; - - [NSApp endSheet:keySheet]; - [keySheet orderOut:nil]; - - if (code) { - // User wants to add PRIMARY KEY - if ([chooseKeyButton indexOfSelectedItem] == 0) { - [queryString appendString:@"\n PRIMARY KEY"]; + // Process index if given for fields set to AUTO_INCREMENT + if (autoIncrementIndex) { + // User wants to add PRIMARY KEY + if ([autoIncrementIndex isEqualToString:@"PRIMARY KEY"]) { + [queryString appendString:@"\n PRIMARY KEY"]; + + // If the field isn't set to be unsigned and we're making it the primary key then make it unsigned + if (![[theRow objectForKey:@"unsigned"] boolValue]) { - // If the field isn't set to be unsigned and we're making it the primary key then make it unsigned - if (![[theRow objectForKey:@"unsigned"] boolValue]) { - - // Find the occurrence of the table name and data type so we know where to insert the - // UNSIGNED keyword. - NSRange range = [queryString rangeOfString:[NSString stringWithFormat:@"%@ %@", [[theRow objectForKey:@"name"] backtickQuotedString], theRowType] options:NSLiteralSearch]; - - NSInteger index = (range.location + range.length); - - // If the field definition's data type includes the length then we must take this into - // account when inserting the UNSIGNED keyword. Add 2 to the index to accommodate the - // parentheses used. - if (fieldDefIncludesLen) { - index += ([[theRow objectForKey:@"length"] length] + 2); - } - - [queryString insertString:@" UNSIGNED" atIndex:index]; - } - - // Add AFTER ... only if the user added a new field - if (isEditingNewRow) { - [queryString appendFormat:@"\n AFTER %@", [[[tableFields objectAtIndex:(currentlyEditingRow -1)] objectForKey:@"name"] backtickQuotedString]]; + // Find the occurrence of the table name and data type so we know where to insert the + // UNSIGNED keyword. + NSRange range = [queryString rangeOfString:[NSString stringWithFormat:@"%@ %@", [[theRow objectForKey:@"name"] backtickQuotedString], theRowType] options:NSLiteralSearch]; + + NSInteger index = (range.location + range.length); + + // If the field definition's data type includes the length then we must take this into + // account when inserting the UNSIGNED keyword. Add 2 to the index to accommodate the + // parentheses used. + if (fieldDefIncludesLen) { + index += ([[theRow objectForKey:@"length"] length] + 2); } + + [queryString insertString:@" UNSIGNED" atIndex:index]; } - else { - // Add AFTER ... only if the user added a new field - if (isEditingNewRow) { - [queryString appendFormat:@"\n AFTER %@", [[[tableFields objectAtIndex:(currentlyEditingRow -1)] objectForKey:@"name"] backtickQuotedString]]; - } - - [queryString appendFormat:@"\n, ADD %@ (%@)", [chooseKeyButton titleOfSelectedItem], [[theRow objectForKey:@"name"] backtickQuotedString]]; + + // Add AFTER ... only if the user added a new field + if (isEditingNewRow) { + [queryString appendFormat:@"\n AFTER %@", [[[tableFields objectAtIndex:(currentlyEditingRow -1)] objectForKey:@"name"] backtickQuotedString]]; + } + } + else { + // Add AFTER ... only if the user added a new field + if (isEditingNewRow) { + [queryString appendFormat:@"\n AFTER %@", [[[tableFields objectAtIndex:(currentlyEditingRow -1)] objectForKey:@"name"] backtickQuotedString]]; } + + [queryString appendFormat:@"\n, ADD %@ (%@)", autoIncrementIndex, [[theRow objectForKey:@"name"] backtickQuotedString]]; } } + // Add AFTER ... only if the user added a new field else if (isEditingNewRow) { [queryString appendFormat:@"\n AFTER %@", [[[tableFields objectAtIndex:(currentlyEditingRow -1)] objectForKey:@"name"] backtickQuotedString]]; } isCurrentExtraAutoIncrement = NO; + autoIncrementIndex = nil; // Execute query [mySQLConnection queryString:queryString]; @@ -1233,6 +1216,18 @@ [[sheet window] orderOut:nil]; alertSheetOpened = NO; + + if(contextInfo && [contextInfo isEqualToString:@"autoincrementindex"]) { + if(returnCode) { + autoIncrementIndex = [chooseKeyButton titleOfSelectedItem]; + } else { + autoIncrementIndex = nil; + if([tableSourceView selectedRow] > -1 && [extraFieldSuggestions count]) + [[tableFields objectAtIndex:[tableSourceView selectedRow]] setObject:[extraFieldSuggestions objectAtIndex:0] forKey:@"Extra"]; + [tableSourceView reloadData]; + isCurrentExtraAutoIncrement = NO; + } + } } /** diff --git a/Source/SPTableStructureDelegate.m b/Source/SPTableStructureDelegate.m index bdc3cb3f..84c0a80c 100644 --- a/Source/SPTableStructureDelegate.m +++ b/Source/SPTableStructureDelegate.m @@ -114,8 +114,20 @@ if(![[currentRow objectForKey:@"Extra"] isEqualToString:anObject]) { isCurrentExtraAutoIncrement = [[[anObject stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString] isEqualToString:@"AUTO_INCREMENT"]; - if(isCurrentExtraAutoIncrement) + if(isCurrentExtraAutoIncrement) { [currentRow setObject:[NSNumber numberWithInteger:0] forKey:@"null"]; + // Asks the user to add an index to query if AUTO_INCREMENT is set and field isn't indexed + if ((![currentRow objectForKey:@"Key"] || [[currentRow objectForKey:@"Key"] isEqualToString:@""])) { + [chooseKeyButton selectItemAtIndex:0]; + + [NSApp beginSheet:keySheet + modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self + didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) + contextInfo:@"autoincrementindex" ]; + } + } else { + autoIncrementIndex = nil; + } id dataCell = [aTableColumn dataCell]; [dataCell removeAllItems]; |