diff options
author | rowanbeentje <rowan@beent.je> | 2012-03-17 15:32:00 +0000 |
---|---|---|
committer | rowanbeentje <rowan@beent.je> | 2012-03-17 15:32:00 +0000 |
commit | ac8ebfe4bba6d7da6edad87c75b174156d919621 (patch) | |
tree | 08a3c2843d3c520090f40e6341b26043a7ccf014 /Source/SPTableStructure.m | |
parent | 8886d935e933c6239aa9b35e900d96f7d07527c7 (diff) | |
parent | eab6df4de773259f90dd5a1d25e44ca4d2765bbf (diff) | |
download | sequelpro-ac8ebfe4bba6d7da6edad87c75b174156d919621.tar.gz sequelpro-ac8ebfe4bba6d7da6edad87c75b174156d919621.tar.bz2 sequelpro-ac8ebfe4bba6d7da6edad87c75b174156d919621.zip |
Merge in the SPMySQL Framework. This new framework should provide much of the functionality required from MCPKit and is based around its interface for relatively easy integration.
Externally visible changes as a result of this merge:
- Speed improvements, particularly when loading large data sets
- Stability improvements, particularly related to connection state after the connection is dropped (eg Issue #1256)
- Improved support for new MySQL data types, which should address Issue #1052.
- Database structure retrieval and query cancellation now use a single persistent helper connection instead of lots of connections on-demand. This should help Issue #1097.
- More internal commands now use queries instead of MySQL functions; for example USE queries are now used to trigger database selection, improving transcripts. This addresses Issue #1247.
- Improved internal encoding work; while this needs support within the UI, it lays the foundation for issues like Issue #1280.
Code improvements:
- Much improved class layouts including extensive category usage
- Improved documentation across framework methods
- Support for fast enumeration across result objects
- Rewrite fixes use of a number of deprecate functions
- Much less code duplication across result set types
- Improved encapsultation within the framework, limiting the number of methods exposed, and also not exposing all the MySQL headers
From the Readme file:
The SPMySQL Framework is intended to provide a stable MySQL connection framework, with the ability to run text-based queries and rapidly retrieve result sets with conversion from MySQL data types to Cocoa objects.
SPMySQL.framework has an interface loosely based around that provided by MCPKit by Serge Cohen and Bertrand Mansion (http://mysql-cocoa.sourceforge.net/), and in particular the heavily modified Sequel Pro version (http://www.sequelpro.com/). It is a full rewrite of the original framework, although it includes code from patches implementing the following Sequel Pro functionality, largely contributed by Hans-Jörg Bibiko, Stuart Connolly, Jakob Egger, and Rowan Beentje:
- Connection locking (Jakob et al)
- Ping & keepalive (Rowan et al)
- Query cancellation (Rowan et al)
- Delegate setup (Stuart et al)
- SSL support (Rowan et al)
- Connection checking (Rowan et al)
- Version state (Stuart et al)
- Maximum packet size control (Hans et al)
- Result multithreading and streaming (Rowan et al)
- Improved encoding support & switching (Rowan et al)
- Database structure; moved to inside the app (Hans et al)
- Query reattempts and error-handling approach (Rowan et al)
- Geometry result class (Hans et al)
- Connection proxy (Stuart et al)
Diffstat (limited to 'Source/SPTableStructure.m')
-rw-r--r-- | Source/SPTableStructure.m | 102 |
1 files changed, 47 insertions, 55 deletions
diff --git a/Source/SPTableStructure.m b/Source/SPTableStructure.m index f9c56eb9..bdf3a9fd 100644 --- a/Source/SPTableStructure.m +++ b/Source/SPTableStructure.m @@ -36,6 +36,7 @@ #import "SPIndexesController.h" #import "RegexKitLite.h" #import "SPTableFieldValidation.h" +#import "SPMySQL.h" @interface SPTableStructure (PrivateAPI) @@ -232,6 +233,7 @@ { NSArray *theTableIndexes; NSMutableDictionary *theTableEnumLists = [NSMutableDictionary dictionary]; + SPMySQLResult *indexResult; // Check whether a save of the current row is required. if ( ![[self onMainThread] saveRowOnDeselect] ) return; @@ -249,7 +251,7 @@ [theTableFields addObject:[[col mutableCopy] autorelease]]; // Retrieve the indexes for the table - indexResult = [[mySQLConnection queryString:[NSString stringWithFormat:@"SHOW INDEX FROM %@", [aTable backtickQuotedString]]] retain]; + indexResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW INDEX FROM %@", [aTable backtickQuotedString]]]; // If an error occurred, reset the interface and abort if ([mySQLConnection queryErrored]) { @@ -264,17 +266,14 @@ SPBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [NSApp mainWindow], self, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"An error occurred while retrieving information.\nMySQL said: %@", @"message of panel when retrieving information failed"), - [mySQLConnection getLastErrorMessage]]); + [mySQLConnection lastErrorMessage]]); } - - if (indexResult) [indexResult release]; - + return; } // Process the indexes into a local array of dictionaries - theTableIndexes = [self fetchResultAsArray:indexResult]; - [indexResult release]; + theTableIndexes = [self convertIndexResultToArray:indexResult]; // Set the Key column for (NSDictionary* theIndex in theTableIndexes) @@ -357,7 +356,7 @@ // Normalize default if(![theField objectForKey:@"default"]) [theField setObject:@"" forKey:@"default"]; - else if([[theField objectForKey:@"default"] isKindOfClass:[NSNull class]]) + else if([[theField objectForKey:@"default"] isNSNull]) [theField setObject:[prefs stringForKey:SPNullValue] forKey:@"default"]; // Init Extra field @@ -407,7 +406,7 @@ [tableDocumentInstance setStatusRequiresReload:YES]; // Query the structure of all databases in the background (mainly for completion) - [NSThread detachNewThreadSelector:@selector(queryDbStructureWithUserInfo:) toTarget:mySQLConnection withObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"forceUpdate", nil]]; + [NSThread detachNewThreadSelector:@selector(queryDbStructureWithUserInfo:) toTarget:[tableDocumentInstance databaseStructureRetrieval] withObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"forceUpdate", nil]]; [self loadTable:selectedTable]; } @@ -533,7 +532,7 @@ */ - (IBAction)showOptimizedFieldType:(id)sender { - MCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SELECT %@ FROM %@ PROCEDURE ANALYSE(0,8192)", + SPMySQLResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SELECT %@ FROM %@ PROCEDURE ANALYSE(0,8192)", [[[tableFields objectAtIndex:[tableSourceView selectedRow]] objectForKey:@"name"] backtickQuotedString], [selectedTable backtickQuotedString]]]; @@ -546,7 +545,7 @@ defaultButton:@"OK" alternateButton:nil otherButton:nil - informativeTextWithFormat:[NSString stringWithFormat:NSLocalizedString(@"An error occurred while fetching the optimized field type.\n\nMySQL said:%@",@"an error occurred while fetching the optimized field type.\n\nMySQL said:%@"), [mySQLConnection getLastErrorMessage]]] + informativeTextWithFormat:[NSString stringWithFormat:NSLocalizedString(@"An error occurred while fetching the optimized field type.\n\nMySQL said:%@",@"an error occurred while fetching the optimized field type.\n\nMySQL said:%@"), [mySQLConnection lastErrorMessage]]] beginSheetModalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:NULL @@ -556,13 +555,10 @@ return; } - NSArray *result = [theResult fetch2DResultAsType:MCPTypeDictionary]; + NSDictionary *analysisResult = [theResult getRowAsDictionary]; - NSString *type = nil; - - if([result count]) - type = [[result objectAtIndex:0] objectForKey:@"Optimal_fieldtype"]; - if(!type || [type isKindOfClass:[NSNull class]] || ![type length]) + NSString *type = [analysisResult objectForKey:@"Optimal_fieldtype"]; + if (!type || [type isNSNull] || ![type length]) type = NSLocalizedString(@"No optimized field type found.", @"no optimized field type found. message"); [[NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"Optimized type for field '%@'", @"Optimized type for field %@"), [[tableFields objectAtIndex:[tableSourceView selectedRow]] objectForKey:@"name"]] @@ -580,31 +576,31 @@ /** * Control the visibility of the columns */ -- (IBAction)toggleColumnView:(id)sender +- (IBAction)toggleColumnView:(NSMenuItem *)sender { NSString *columnIdentifierName = nil; - switch([sender tag]) { + switch ([sender tag]) { case 7: - columnIdentifierName = @"Key"; + columnIdentifierName = @"Key"; break; case 10: - columnIdentifierName = @"encoding"; + columnIdentifierName = @"encoding"; break; case 11: - columnIdentifierName = @"collation"; + columnIdentifierName = @"collation"; break; case 12: - columnIdentifierName = @"comment"; + columnIdentifierName = @"comment"; break; default: return; } - for(NSTableColumn *col in [tableSourceView tableColumns]) { + for (NSTableColumn *col in [tableSourceView tableColumns]) { - if([[col identifier] isEqualToString:columnIdentifierName]) { + if ([[col identifier] isEqualToString:columnIdentifierName]) { [col setHidden:([sender state] == NSOffState) ? NO : YES]; [(NSMenuItem *)sender setState:![sender state]]; break; @@ -864,7 +860,7 @@ NSLocalizedString(@"OK", @"OK button"), nil, nil, [NSApp mainWindow], nil, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"An error occurred while trying to reset AUTO_INCREMENT of table '%@'.\n\nMySQL said: %@", @"error resetting auto_increment informative message"), - selTable, [mySQLConnection getLastErrorMessage]]); + selTable, [mySQLConnection lastErrorMessage]]); } // reload data @@ -884,9 +880,9 @@ /** * Converts the supplied result to an array containing a (mutable) dictionary for each row */ -- (NSArray *)fetchResultAsArray:(MCPResult *)theResult +- (NSArray *)convertIndexResultToArray:(SPMySQLResult *)theResult { - NSUInteger numOfRows = (NSUInteger)[theResult numOfRows]; + NSUInteger numOfRows = (NSUInteger)[theResult numberOfRows]; NSMutableArray *tempResult = [NSMutableArray arrayWithCapacity:numOfRows]; NSMutableDictionary *tempRow; NSArray *keys; @@ -896,9 +892,8 @@ // Ensure table information is returned as strings to avoid problems with some server versions [theResult setReturnDataAsStrings:YES]; - if (numOfRows) [theResult dataSeek:0]; for ( i = 0 ; i < (NSInteger)numOfRows ; i++ ) { - tempRow = [NSMutableDictionary dictionaryWithDictionary:[theResult fetchRowAsDictionary]]; + tempRow = [NSMutableDictionary dictionaryWithDictionary:[theResult getRowAsDictionary]]; // Replace NSNull instances with the NULL string from preferences keys = [tempRow allKeys]; @@ -1028,7 +1023,7 @@ } // Otherwise, use the provided default else { - [queryString appendFormat:@"\n DEFAULT '%@' ", [mySQLConnection prepareString:[theRow objectForKey:@"default"]]]; + [queryString appendFormat:@"\n DEFAULT %@ ", [mySQLConnection escapeAndQuoteString:[theRow objectForKey:@"default"]]]; } } @@ -1115,7 +1110,7 @@ } // Otherwise, use the provided default else { - [queryString appendFormat:@"\n DEFAULT '%@'", [mySQLConnection prepareString:[theRow objectForKey:@"default"]]]; + [queryString appendFormat:@"\n DEFAULT %@", [mySQLConnection escapeAndQuoteString:[theRow objectForKey:@"default"]]]; } } @@ -1126,7 +1121,7 @@ // Any column comments if ([(NSString *)[theRow objectForKey:@"comment"] length]) { - [queryString appendFormat:@"\n COMMENT '%@'", [mySQLConnection prepareString:[theRow objectForKey:@"comment"]]]; + [queryString appendFormat:@"\n COMMENT %@", [mySQLConnection escapeAndQuoteString:[theRow objectForKey:@"comment"]]]; } if (!isEditingNewRow) { @@ -1201,18 +1196,18 @@ [tableDocumentInstance setContentRequiresReload:YES]; // Query the structure of all databases in the background - [NSThread detachNewThreadSelector:@selector(queryDbStructureWithUserInfo:) toTarget:mySQLConnection withObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"forceUpdate", selectedTable, @"affectedItem", [NSNumber numberWithInteger:[tablesListInstance tableType]], @"affectedItemType", nil]]; + [NSThread detachNewThreadSelector:@selector(queryDbStructureWithUserInfo:) toTarget:[tableDocumentInstance databaseStructureRetrieval] withObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"forceUpdate", selectedTable, @"affectedItem", [NSNumber numberWithInteger:[tablesListInstance tableType]], @"affectedItemType", nil]]; return YES; } else { alertSheetOpened = YES; - if([mySQLConnection getLastErrorID] == 1146) { // If the current table doesn't exist anymore + if([mySQLConnection lastErrorID] == 1146) { // If the current table doesn't exist anymore SPBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [tableDocumentInstance parentWindow], self, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"An error occurred while trying to alter table '%@'.\n\nMySQL said: %@", @"error while trying to alter table message"), - selectedTable, [mySQLConnection getLastErrorMessage]]); + selectedTable, [mySQLConnection lastErrorMessage]]); isEditingRow = NO; isEditingNewRow = NO; @@ -1237,14 +1232,14 @@ NSLocalizedString(@"Edit row", @"Edit row button"), NSLocalizedString(@"Discard changes", @"discard changes button"), nil, [tableDocumentInstance parentWindow], self, @selector(addRowErrorSheetDidEnd:returnCode:contextInfo:), nil, [NSString stringWithFormat:NSLocalizedString(@"An error occurred when trying to add the field '%@' via\n\n%@\n\nMySQL said: %@", @"error adding field informative message"), - [theRow objectForKey:@"name"], queryString, [mySQLConnection getLastErrorMessage]]); + [theRow objectForKey:@"name"], queryString, [mySQLConnection lastErrorMessage]]); } else { SPBeginAlertSheet(NSLocalizedString(@"Error changing field", @"error changing field message"), NSLocalizedString(@"Edit row", @"Edit row button"), NSLocalizedString(@"Discard changes", @"discard changes button"), nil, [tableDocumentInstance parentWindow], self, @selector(addRowErrorSheetDidEnd:returnCode:contextInfo:), nil, [NSString stringWithFormat:NSLocalizedString(@"An error occurred when trying to change the field '%@' via\n\n%@\n\nMySQL said: %@", @"error changing field informative message"), - [theRow objectForKey:@"name"], queryString, [mySQLConnection getLastErrorMessage]]); + [theRow objectForKey:@"name"], queryString, [mySQLConnection lastErrorMessage]]); } return NO; @@ -1431,7 +1426,7 @@ /** * Sets the connection (received from SPDatabaseDocument) and makes things that have to be done only once */ -- (void)setConnection:(MCPConnection *)theConnection +- (void)setConnection:(SPMySQLConnection *)theConnection { mySQLConnection = theConnection; @@ -1449,7 +1444,7 @@ { if ( ![defaultValues objectForKey:field] ) { return [prefs objectForKey:SPNullValue]; - } else if ( [[defaultValues objectForKey:field] isMemberOfClass:[NSNull class]] ) { + } else if ( [[defaultValues objectForKey:field] isNSNull] ) { return [prefs objectForKey:SPNullValue]; } else { return [defaultValues objectForKey:field]; @@ -1502,18 +1497,15 @@ NSString *nullValue = [prefs stringForKey:SPNullValue]; CFStringRef escapedNullValue = CFXMLCreateStringByEscapingEntities(NULL, ((CFStringRef)nullValue), NULL); - MCPResult *structureQueryResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW COLUMNS FROM %@", [selectedTable backtickQuotedString]]]; - MCPResult *indexesQueryResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW INDEXES FROM %@", [selectedTable backtickQuotedString]]]; + SPMySQLResult *structureQueryResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW COLUMNS FROM %@", [selectedTable backtickQuotedString]]]; + SPMySQLResult *indexesQueryResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW INDEXES FROM %@", [selectedTable backtickQuotedString]]]; [structureQueryResult setReturnDataAsStrings:YES]; [indexesQueryResult setReturnDataAsStrings:YES]; - if ([structureQueryResult numOfRows]) [structureQueryResult dataSeek:0]; - if ([indexesQueryResult numOfRows]) [indexesQueryResult dataSeek:0]; - - [tempResult addObject:[structureQueryResult fetchFieldNames]]; + [tempResult addObject:[structureQueryResult fieldNames]]; - NSMutableArray *temp = [[indexesQueryResult fetchFieldNames] mutableCopy]; + NSMutableArray *temp = [[indexesQueryResult fieldNames] mutableCopy]; // Remove the 'table' column [temp removeObjectAtIndex:0]; @@ -1522,8 +1514,8 @@ [temp release]; - for (i = 0; i < [structureQueryResult numOfRows]; i++) { - NSMutableArray *row = [[structureQueryResult fetchRowAsArray] mutableCopy]; + for (i = 0; i < [structureQueryResult numberOfRows]; i++) { + NSMutableArray *row = [[structureQueryResult getRowAsArray] mutableCopy]; // For every NULL value replace it with the user's NULL value placeholder so we can actually print it for (j = 0; j < [row count]; j++) @@ -1538,8 +1530,8 @@ [row release]; } - for (i = 0; i < [indexesQueryResult numOfRows]; i++) { - NSMutableArray *eachIndex = [[indexesQueryResult fetchRowAsArray] mutableCopy]; + for (i = 0; i < [indexesQueryResult numberOfRows]; i++) { + NSMutableArray *eachIndex = [[indexesQueryResult getRowAsArray] mutableCopy]; // Remove the 'table' column values [eachIndex removeObjectAtIndex:0]; @@ -1658,10 +1650,10 @@ [mySQLConnection queryString:[NSString stringWithFormat:@"ALTER TABLE %@ DROP FOREIGN KEY %@", [selectedTable backtickQuotedString], [relationName backtickQuotedString]]]; // Check for errors, but only if the query wasn't cancelled - if ([mySQLConnection queryErrored] && ![mySQLConnection queryCancelled]) { + if ([mySQLConnection queryErrored] && ![mySQLConnection lastQueryWasCancelled]) { NSMutableDictionary *errorDictionary = [NSMutableDictionary dictionary]; [errorDictionary setObject:NSLocalizedString(@"Unable to delete relation", @"error deleting relation message") forKey:@"title"]; - [errorDictionary setObject:[NSString stringWithFormat:NSLocalizedString(@"An error occurred while trying to delete the relation '%@'.\n\nMySQL said: %@", @"error deleting relation informative message"), relationName, [mySQLConnection getLastErrorMessage]] forKey:@"message"]; + [errorDictionary setObject:[NSString stringWithFormat:NSLocalizedString(@"An error occurred while trying to delete the relation '%@'.\n\nMySQL said: %@", @"error deleting relation informative message"), relationName, [mySQLConnection lastErrorMessage]] forKey:@"message"]; [[self onMainThread] showErrorSheetWith:errorDictionary]; } } @@ -1671,12 +1663,12 @@ [selectedTable backtickQuotedString], [[[tableFields objectAtIndex:[tableSourceView selectedRow]] objectForKey:@"name"] backtickQuotedString]]]; // Check for errors, but only if the query wasn't cancelled - if ([mySQLConnection queryErrored] && ![mySQLConnection queryCancelled]) { + if ([mySQLConnection queryErrored] && ![mySQLConnection lastQueryWasCancelled]) { NSMutableDictionary *errorDictionary = [NSMutableDictionary dictionary]; [errorDictionary setObject:NSLocalizedString(@"Error", @"error") forKey:@"title"]; [errorDictionary setObject:[NSString stringWithFormat:NSLocalizedString(@"Couldn't delete field %@.\nMySQL said: %@", @"message of panel when field cannot be deleted"), [[tableFields objectAtIndex:[tableSourceView selectedRow]] objectForKey:@"name"], - [mySQLConnection getLastErrorMessage]] forKey:@"message"]; + [mySQLConnection lastErrorMessage]] forKey:@"message"]; [[self onMainThread] showErrorSheetWith:errorDictionary]; } else { |