diff options
author | Bibiko <bibiko@eva.mpg.de> | 2010-02-20 17:07:38 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2010-02-20 17:07:38 +0000 |
commit | 50c726232203af0ae46a0641ca10bd126c78dc2c (patch) | |
tree | f1dba01d00492198ea2dd270f66ca4e7d0a3b318 | |
parent | ba37b377d84cc24052538aad70257fc082c92dca (diff) | |
download | sequelpro-50c726232203af0ae46a0641ca10bd126c78dc2c.tar.gz sequelpro-50c726232203af0ae46a0641ca10bd126c78dc2c.tar.bz2 sequelpro-50c726232203af0ae46a0641ca10bd126c78dc2c.zip |
• added to method informationForTable:
- the root dict key "primarykeyfield" = <field name>
- the key "isprimarykey" for corresponding table field
- the key "unique" = 1 if corresponding table field has a UNIQUE KEY
-rw-r--r-- | Source/SPTableData.m | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/Source/SPTableData.m b/Source/SPTableData.m index fe13e4f3..20a3dbd7 100644 --- a/Source/SPTableData.m +++ b/Source/SPTableData.m @@ -33,6 +33,7 @@ #import "SPArrayAdditions.h" #import "SPConstants.h" #import "SPAlertSheets.h" +#import "RegexKitLite.h" @implementation SPTableData @@ -303,7 +304,7 @@ /* * Retrieve the CREATE TABLE string for a table and analyse it to extract the field - * details and table encoding. + * details, primary key, unique keys, and table encoding. * In future this could also be used to retrieve the majority of index information * assuming information like cardinality isn't needed. * This function is rather long due to the painful parsing required, but is fast. @@ -371,7 +372,9 @@ NSCharacterSet *whitespaceAndNewlineSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSCharacterSet *quoteSet = [NSCharacterSet characterSetWithCharactersInString:@"`'\""]; NSCharacterSet *bracketSet = [NSCharacterSet characterSetWithCharactersInString:@"()"]; - + + tableData = [NSMutableDictionary dictionary]; + for (i = 0; i < [fieldStrings count]; i++) { // Take this field/key string, trim whitespace from both ends and remove comments @@ -430,7 +433,9 @@ } // Store the column. - [tableColumns addObject:[NSDictionary dictionaryWithDictionary:tableColumn]]; + NSMutableDictionary *d = [NSMutableDictionary dictionaryWithCapacity:1]; + [d setDictionary:tableColumn]; + [tableColumns addObject:d]; // TODO: Otherwise it's a key definition, check, or other 'metadata'. Would be useful to parse/display these! } else { @@ -523,15 +528,36 @@ [constraintDetails release]; } // primary key - else if( [NSArrayObjectAtIndex(parts, 0) hasPrefix:@"PRIMARY"] ) { + // add "isprimarykey" to the corresponding tableColumn + // add dict root "primarykeyfield" = <field> for faster accessing + else if( [NSArrayObjectAtIndex(parts, 0) hasPrefix:@"PRIMARY"] && [parts count] == 3) { + NSString *parsedString = [(NSString*)NSArrayObjectAtIndex(parts, 2) stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + if([parsedString length]>4) { + NSString *priFieldName = [[parsedString substringWithRange:NSMakeRange(2,[parsedString length]-4)] stringByReplacingOccurrencesOfString:@"``" withString:@"`"]; + [tableData setObject:priFieldName forKey:@"primarykeyfield"]; + for(id tableColumn in tableColumns) + if([[tableColumn objectForKey:@"name"] isEqualToString:priFieldName]) { + [tableColumn setObject:[NSNumber numberWithInteger:1] forKey:@"isprimarykey"]; + break; + } + } } - // key - else if( [NSArrayObjectAtIndex(parts, 0) hasPrefix:@"KEY"] ) { - /* - NSLog( @"key %@.%@", - [[parts objectAtIndex:1] stringByTrimmingCharactersInSet:junk], - [[parts objectAtIndex:2] stringByTrimmingCharactersInSet:junk] ); - */ + // unique keys + // add to each corresponding tableColumn the tag "unique" if given + else if( [NSArrayObjectAtIndex(parts, 0) hasPrefix:@"UNIQUE"] && [parts count] == 4) { + NSString *parsedString = [(NSString*)NSArrayObjectAtIndex(parts, 3) stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + if([parsedString length]>4) { + NSArray *uniqueFieldNames = [parsedString componentsSeparatedByString:@"`,`"]; + for(NSString* uniq in uniqueFieldNames) { + NSString *uniqField = [[uniq stringByReplacingOccurrencesOfRegex:@"^\\(`|`\\)" withString:@""] stringByReplacingOccurrencesOfString:@"``" withString:@"`"]; + for(id tableColumn in tableColumns) + if([[tableColumn objectForKey:@"name"] isEqualToString:uniqField]) { + [tableColumn setObject:[NSNumber numberWithInteger:1] forKey:@"unique"]; + break; + } + } + + } } // who knows else { @@ -597,7 +623,6 @@ } - tableData = [NSMutableDictionary dictionary]; // this will be 'Table' or 'View' [tableData setObject:[resultFieldNames objectAtIndex:0] forKey:@"type"]; [tableData setObject:[NSString stringWithString:encodingString] forKey:@"encoding"]; |