From e4c9f4057bb950d15e101c3222d95325d3ca67e8 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sat, 25 Dec 2010 22:09:13 +0000 Subject: When adding a new index, as well as checking for an existing primary key also check for a composite primary key. Part of issue #928. --- Source/SPIndexesController.m | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Source/SPIndexesController.m b/Source/SPIndexesController.m index a19480e6..6a7c6cb7 100644 --- a/Source/SPIndexesController.m +++ b/Source/SPIndexesController.m @@ -129,17 +129,31 @@ static const NSString *SPNewIndexKeyBlockSize = @"IndexKeyBlockSize"; [indexNameTextField setEnabled:NO]; [indexNameTextField setStringValue:@"PRIMARY"]; - // If the table is of type MyISAM and Spation extension support is available, add the SPATIAL type + // If the table is of type MyISAM and Spatial extension support is available, add the SPATIAL type NSString *engine = [[tableData statusValues] objectForKey:@"Engine"]; if ([engine isEqualToString:@"MyISAM"] && [[dbDocument serverSupport] supportsSpatialExtensions]) { [indexTypePopUpButton addItemWithTitle:@"SPATIAL"]; } - + // Check to see whether a primary key already exists for the table, and if so select INDEX instead for (NSDictionary *field in fields) { - if ([field objectForKey:@"isprimarykey"]) { + BOOL hasCompositePrimaryKey = NO; + BOOL isPrimaryKey = [field objectForKey:@"isprimarykey"]; + + // The 'isprimarykey' key of a field is only present for single column primary keys, not composite keys, + // so we need to check the indexes manually. + if (!isPrimaryKey) { + for (NSDictionary *index in indexes) + { + if ([[index objectForKey:@"Key_name"] isEqualToString:@"PRIMARY"]) { + hasCompositePrimaryKey = YES; + } + } + } + + if (isPrimaryKey || hasCompositePrimaryKey) { // Remove primary key option [indexTypePopUpButton removeItemAtIndex:0]; @@ -400,7 +414,7 @@ static const NSString *SPNewIndexKeyBlockSize = @"IndexKeyBlockSize"; * Returns the item to be displayed in the combo box cell as the supplied index. */ - (id)comboBoxCell:(NSComboBoxCell *)comboBoxCell objectValueForItemAtIndex:(NSInteger)index -{ +{ return [[fields objectAtIndex:index] objectForKey:@"name"]; } -- cgit v1.2.3