From 05f1612cbb7e33cf9135a346fc2505cc0e87e853 Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Thu, 23 Feb 2012 02:13:56 +0000 Subject: Warning: this branch commit is largely untested, and known to throw exceptions as database structure retrieval is currently missing! Further work on SPMySQLFramework integration: - Improve SPMySQL framework build settings including correct ppc builds and a Distribution configuration for the build distributions to match - Add new convenience querying and result methods to the framework - Amend Sequel Pro source to use the new SPMySQL.framework methods everywhere, replacing MCPKit methods where they differ and improving some functions - Remove MCPKit from the source - Fix a number of warnings on Release-style builds --- Source/SPTableData.m | 62 ++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) (limited to 'Source/SPTableData.m') diff --git a/Source/SPTableData.m b/Source/SPTableData.m index 297718b9..5f043548 100644 --- a/Source/SPTableData.m +++ b/Source/SPTableData.m @@ -30,6 +30,8 @@ #import "SPAlertSheets.h" #import "RegexKitLite.h" #import "SPServerSupport.h" +#import "SPMySQL.h" +#include @interface SPTableData (PrivateAPI) @@ -70,7 +72,7 @@ * * @param theConnection The used connection for the SPDatabaseDocument */ -- (void) setConnection:(MCPConnection *)theConnection +- (void) setConnection:(SPMySQLConnection *)theConnection { mySQLConnection = theConnection; [mySQLConnection retain]; @@ -476,7 +478,7 @@ [constraints removeAllObjects]; // Retrieve the CREATE TABLE syntax for the table - MCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW CREATE TABLE %@", [tableName backtickQuotedString]]]; + SPMySQLResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW CREATE TABLE %@", [tableName backtickQuotedString]]]; [theResult setReturnDataAsStrings:YES]; // Check for any errors, but only display them if a connection still exists @@ -485,10 +487,10 @@ SPBeginAlertSheet(NSLocalizedString(@"Error retrieving table information", @"error retrieving table information message"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [NSApp mainWindow], self, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"An error occurred while retrieving the information for table '%@'. Please try again.\n\nMySQL said: %@", @"error retrieving table information informative message"), - tableName, [mySQLConnection getLastErrorMessage]]); + tableName, [mySQLConnection lastErrorMessage]]); // If the current table doesn't exist anymore reload table list - if([mySQLConnection getLastErrorID] == 1146) { + if([mySQLConnection lastErrorID] == 1146) { // Release the table loading lock to allow reselection/reloading to requery the database. pthread_mutex_unlock(&dataProcessingLock); @@ -503,8 +505,8 @@ } // Retrieve the table syntax string - NSArray *syntaxResult = [theResult fetchRowAsArray]; - NSArray *resultFieldNames = [theResult fetchFieldNames]; + NSArray *syntaxResult = [theResult getRowAsArray]; + NSArray *resultFieldNames = [theResult fieldNames]; // Only continue if syntaxResult is not nil. This accommodates causes where the above query caused the // connection reconnect dialog to appear and the user chose to close the connection. @@ -798,7 +800,6 @@ NSMutableArray *tableColumns; NSDictionary *resultRow; NSMutableDictionary *tableColumn, *viewData; - NSUInteger i; BOOL changeEncoding = ![[mySQLConnection encoding] isEqualToString:@"utf8"]; // Catch unselected views and return nil @@ -811,7 +812,7 @@ } // Retrieve the CREATE TABLE syntax for the table - MCPResult *theResult = [mySQLConnection queryString: [NSString stringWithFormat: @"SHOW CREATE TABLE %@", + SPMySQLResult *theResult = [mySQLConnection queryString: [NSString stringWithFormat: @"SHOW CREATE TABLE %@", [viewName backtickQuotedString] ]]; [theResult setReturnDataAsStrings:YES]; @@ -822,7 +823,7 @@ 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 (changeEncoding) [mySQLConnection restoreStoredEncoding]; } return nil; @@ -830,7 +831,7 @@ // Retrieve the table syntax string if (tableCreateSyntax) [tableCreateSyntax release], tableCreateSyntax = nil; - NSString *syntaxString = [[theResult fetchRowAsArray] objectAtIndex:1]; + NSString *syntaxString = [[theResult getRowAsArray] objectAtIndex:1]; // A NULL value indicates that the user does not have permission to view the syntax if ([syntaxString isNSNull]) { @@ -856,20 +857,18 @@ 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 (changeEncoding) [mySQLConnection restoreStoredEncoding]; } return nil; } // Loop through the fields and capture details - if ([theResult numOfRows]) [theResult dataSeek:0]; tableColumns = [[NSMutableArray alloc] init]; tableColumn = [[NSMutableDictionary alloc] init]; fieldParser = [[SPSQLParser alloc] init]; - for ( i = 0; i < [theResult numOfRows] ; i++ ) { + for (resultRow in theResult) { [tableColumn removeAllObjects]; - resultRow = [theResult fetchRowAsDictionary]; // Add the column index and name [tableColumn setObject:[NSString stringWithFormat:@"%llu", (unsigned long long)[tableColumns count]] forKey:@"datacolumnindex"]; @@ -938,7 +937,7 @@ [escapedTableName replaceOccurrencesOfString:@"\\" withString:@"\\\\" options:0 range:NSMakeRange(0, [escapedTableName length])]; [escapedTableName replaceOccurrencesOfString:@"'" withString:@"\\\'" options:0 range:NSMakeRange(0, [escapedTableName length])]; - MCPResult *tableStatusResult = nil; + SPMySQLResult *tableStatusResult = nil; if ([tableListInstance tableType] == SPTableTypeProc) { NSMutableString *escapedDatabaseName = [NSMutableString stringWithString:[tableDocumentInstance database]]; @@ -967,7 +966,7 @@ SPBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [NSApp mainWindow], self, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"An error occured while retrieving status data.\nMySQL said: %@", @"message of panel when retrieving view information failed"), - [mySQLConnection getLastErrorMessage]]); + [mySQLConnection lastErrorMessage]]); if (changeEncoding) [mySQLConnection restoreStoredEncoding]; } pthread_mutex_unlock(&dataProcessingLock); @@ -975,7 +974,7 @@ } // Retrieve the status as a dictionary and set as the cache - [status setDictionary:[tableStatusResult fetchRowAsDictionary]]; + [status setDictionary:[tableStatusResult getRowAsDictionary]]; if ([tableListInstance tableType] == SPTableTypeTable) { @@ -1001,10 +1000,10 @@ // [status objectForKey:@"Rows"] is NULL then try to get the number of rows via SELECT COUNT(1) FROM `foo` // this happens e.g. for db "information_schema" - if([[status objectForKey:@"Rows"] isKindOfClass:[NSNull class]]) { + if([[status objectForKey:@"Rows"] isNSNull]) { tableStatusResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SELECT COUNT(1) FROM %@", [escapedTableName backtickQuotedString] ]]; if (![mySQLConnection queryErrored]) - [status setObject:[[tableStatusResult fetchRowAsArray] objectAtIndex:0] forKey:@"Rows"]; + [status setObject:[[tableStatusResult getRowAsArray] objectAtIndex:0] forKey:@"Rows"]; [status setObject:@"y" forKey:@"RowsCountAccurate"]; } @@ -1042,7 +1041,7 @@ [mySQLConnection setEncoding:@"utf8"]; } - MCPResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"/*!50003 SHOW TRIGGERS WHERE `Table` = %@ */", + SPMySQLResult *theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"/*!50003 SHOW TRIGGERS WHERE `Table` = %@ */", [[tableListInstance tableName] tickQuotedString]]]; [theResult setReturnDataAsStrings:YES]; @@ -1052,7 +1051,7 @@ SPBeginAlertSheet(NSLocalizedString(@"Error retrieving trigger information", @"error retrieving trigger information message"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [NSApp mainWindow], self, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"An error occurred while retrieving the trigger information for table '%@'. Please try again.\n\nMySQL said: %@", @"error retrieving table information informative message"), - [tableListInstance tableName], [mySQLConnection getLastErrorMessage]]); + [tableListInstance tableName], [mySQLConnection lastErrorMessage]]); if (triggers) [triggers release], triggers = nil; if (changeEncoding) [mySQLConnection restoreStoredEncoding]; } @@ -1061,11 +1060,8 @@ return NO; } - if (triggers) [triggers release]; - triggers = [[NSMutableArray alloc] init]; - for (NSUInteger i=0; i<[theResult numOfRows]; i++) { - [triggers addObject:[theResult fetchRowAsDictionary]]; - } + [triggers removeAllObjects]; + [triggers addObjectsFromArray:[theResult getAllRows]]; if (changeEncoding) [mySQLConnection restoreStoredEncoding]; @@ -1298,9 +1294,7 @@ NSString *selectedTable = [tableListInstance tableName]; if(![selectedTable length]) return nil; - MCPResult *r; - NSArray *resultRow; - NSUInteger i; + SPMySQLResult *r; NSMutableArray *keyColumns = [NSMutableArray array]; // select all columns that are primary keys @@ -1308,22 +1302,22 @@ r = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW COLUMNS FROM %@ /*!50003 WHERE `key` = 'PRI'*/", [selectedTable backtickQuotedString]]]; [r setReturnDataAsStrings:YES]; - if([r numOfRows] < 1) { + if ([r numberOfRows] < 1) { if (changeEncoding && [mySQLConnection isConnected]) [mySQLConnection restoreStoredEncoding]; return nil; } if ([mySQLConnection queryErrored]) { if ([mySQLConnection isConnected]) { - NSRunAlertPanel(@"Error", [NSString stringWithFormat:NSLocalizedString(@"An error occured while retrieving the PRIMARY KEY data:\n\n%@",@"message when the query that fetches the primary keys fails"), [mySQLConnection getLastErrorMessage]], @"OK", nil, nil); + NSRunAlertPanel(@"Error", [NSString stringWithFormat:NSLocalizedString(@"An error occured while retrieving the PRIMARY KEY data:\n\n%@",@"message when the query that fetches the primary keys fails"), [mySQLConnection lastErrorMessage]], @"OK", nil, nil); if (changeEncoding) [mySQLConnection restoreStoredEncoding]; } return nil; } - for( i = 0; i < [r numOfRows]; i++ ) { - resultRow = [r fetchRowAsArray]; + for (NSArray *resultRow in r) { + // check if the row is indeed a key (for MySQL servers before 5.0.3) if ([[NSArrayObjectAtIndex(resultRow ,3) description] isEqualToString:@"PRI"]) { [keyColumns addObject:[NSArrayObjectAtIndex(resultRow ,0) description]]; @@ -1382,4 +1376,4 @@ #endif -@end \ No newline at end of file +@end -- cgit v1.2.3 From a9f95273a69035e6be8249a42322350df695870f Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Wed, 14 Mar 2012 01:50:43 +0000 Subject: SPMySQL integration bugfixes: - Fix a bug where the socket path would not be autodetected if an empty string was supplied - Fix a bug fetching primary key column names for tables - Fix a bug building database structures on MySQL 3.x servers --- Source/SPTableData.m | 1 + 1 file changed, 1 insertion(+) (limited to 'Source/SPTableData.m') diff --git a/Source/SPTableData.m b/Source/SPTableData.m index 5f043548..582ad9d2 100644 --- a/Source/SPTableData.m +++ b/Source/SPTableData.m @@ -1301,6 +1301,7 @@ // MySQL before 5.0.3 does not support the WHERE syntax r = [mySQLConnection queryString:[NSString stringWithFormat:@"SHOW COLUMNS FROM %@ /*!50003 WHERE `key` = 'PRI'*/", [selectedTable backtickQuotedString]]]; [r setReturnDataAsStrings:YES]; + [r setDefaultRowReturnType:SPMySQLResultRowAsArray]; if ([r numberOfRows] < 1) { if (changeEncoding && [mySQLConnection isConnected]) [mySQLConnection restoreStoredEncoding]; -- cgit v1.2.3 From eab6df4de773259f90dd5a1d25e44ca4d2765bbf Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Fri, 16 Mar 2012 09:41:37 +0000 Subject: - Fix a bug in the SPMySQL integration branch causing the table triggers list never to be initialised - 10.7 magically fixed this and worked, but 10.6 didn't show any triggers. Thanks to schlabberdog for spotting and diagnosing this issue! --- Source/SPTableData.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Source/SPTableData.m') diff --git a/Source/SPTableData.m b/Source/SPTableData.m index 582ad9d2..0c0814d6 100644 --- a/Source/SPTableData.m +++ b/Source/SPTableData.m @@ -166,7 +166,7 @@ } } - return (NSArray *)triggers; + return triggers; } /** @@ -1060,8 +1060,8 @@ return NO; } - [triggers removeAllObjects]; - [triggers addObjectsFromArray:[theResult getAllRows]]; + if (triggers) [triggers release]; + triggers = [[NSArray alloc] initWithArray:[theResult getAllRows]]; if (changeEncoding) [mySQLConnection restoreStoredEncoding]; -- cgit v1.2.3