aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPTableData.m
diff options
context:
space:
mode:
authorrowanbeentje <rowan@beent.je>2012-03-17 15:32:00 +0000
committerrowanbeentje <rowan@beent.je>2012-03-17 15:32:00 +0000
commitac8ebfe4bba6d7da6edad87c75b174156d919621 (patch)
tree08a3c2843d3c520090f40e6341b26043a7ccf014 /Source/SPTableData.m
parent8886d935e933c6239aa9b35e900d96f7d07527c7 (diff)
parenteab6df4de773259f90dd5a1d25e44ca4d2765bbf (diff)
downloadsequelpro-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/SPTableData.m')
-rw-r--r--Source/SPTableData.m63
1 files changed, 29 insertions, 34 deletions
diff --git a/Source/SPTableData.m b/Source/SPTableData.m
index 297718b9..0c0814d6 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 <pthread.h>
@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];
@@ -164,7 +166,7 @@
}
}
- return (NSArray *)triggers;
+ return triggers;
}
/**
@@ -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];
}
@@ -1062,10 +1061,7 @@
}
if (triggers) [triggers release];
- triggers = [[NSMutableArray alloc] init];
- for (NSUInteger i=0; i<[theResult numOfRows]; i++) {
- [triggers addObject:[theResult fetchRowAsDictionary]];
- }
+ triggers = [[NSArray alloc] initWithArray:[theResult getAllRows]];
if (changeEncoding) [mySQLConnection restoreStoredEncoding];
@@ -1298,32 +1294,31 @@
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
// 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 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 +1377,4 @@
#endif
-@end \ No newline at end of file
+@end