From 0612652448f32e620a96e923a44b463fa1b9e0dc Mon Sep 17 00:00:00 2001 From: Bibiko Date: Tue, 14 Jul 2009 19:34:40 +0000 Subject: =?UTF-8?q?=E2=80=A2=20fix=20for=20completion=20in=20CustomQuery?= =?UTF-8?q?=20editor=20-=20now=20the=20gathering=20of=20suggestions=20does?= =?UTF-8?q?=20not=20query=20the=20MySQL=20connection,=20instead=20it=20use?= =?UTF-8?q?s=20the=20TableList/TableDocument=20tableView=20data=20as=20a?= =?UTF-8?q?=20kind=20of=20cache=20-=20this=20approach=20should=20improve?= =?UTF-8?q?=20the=20speed=20for=20slow=20server=20connections?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: auto-update for TableList and Database List resp. (it could happen that an other user changed the name of a table/db meanwhile) --- Source/CMTextView.m | 104 +++++++++++++------------------------------------ Source/TableDocument.h | 3 ++ Source/TableDocument.m | 32 +++++++++++---- Source/TablesList.h | 6 +++ Source/TablesList.m | 72 ++++++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 86 deletions(-) diff --git a/Source/CMTextView.m b/Source/CMTextView.m index 3f9a0d57..c99baa1a 100644 --- a/Source/CMTextView.m +++ b/Source/CMTextView.m @@ -129,60 +129,36 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) NSMutableArray *compl = [[NSMutableArray alloc] initWithCapacity:32]; NSMutableArray *possibleCompletions = [[NSMutableArray alloc] initWithCapacity:32]; - unsigned i; if([mySQLConnection isConnected] && !isDictMode) { // Add table names to completions list - MCPResult *queryResult = [mySQLConnection listTables]; - if ([queryResult numOfRows]) - [queryResult dataSeek:0]; - for (i = 0 ; i < [queryResult numOfRows] ; i++) - { - [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSArrayObjectAtIndex([queryResult fetchRowAsArray], 0), @"display", @"table-small-square", @"image", nil]]; - //[possibleCompletions addObject:[[queryResult fetchRowAsArray] objectAtIndex:0]]; - } + for (id obj in [[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"allTableAndViewNames"]) + [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:obj, @"display", @"table-small-square", @"image", nil]]; + + // Add view names to completions list + // for (id obj in [[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"allViewNames"]) + // [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:obj, @"display", @"table-view-small", @"image", nil]]; // Add field names to completions list for currently selected table - if ([[[self window] delegate] table] != nil) { - id columnNames = [[[[self window] delegate] valueForKeyPath:@"tableDataInstance"] valueForKey:@"columnNames"]; - // [possibleCompletions addObjectsFromArray:columnNames]; - NSString *s; - enumerate(columnNames, s) - [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:s, @"display", @"dummy-small", @"image", nil]]; - } + if ([[[self window] delegate] table] != nil) + for (id obj in [[[[self window] delegate] valueForKeyPath:@"tableDataInstance"] valueForKey:@"columnNames"]) + [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:obj, @"display", @"dummy-small", @"image", nil]]; + // Add all database names to completions list - queryResult = [mySQLConnection listDBs]; - if ([queryResult numOfRows]) - [queryResult dataSeek:0]; - for (i = 0 ; i < [queryResult numOfRows] ; i++) - { - // [possibleCompletions addObject:[[queryResult fetchRowAsArray] objectAtIndex:0]]; - [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSArrayObjectAtIndex([queryResult fetchRowAsArray], 0), @"display", @"database-small", @"image", nil]]; - } + for (id obj in [[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"allDatabaseNames"]) + [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:obj, @"display", @"database-small", @"image", nil]]; // Add proc/func only for MySQL version 5 or higher if(mySQLmajorVersion > 4) { // Add all procedures to completions list for currently selected table - queryResult = [mySQLConnection queryString:@"SHOW PROCEDURE STATUS"]; - if ([queryResult numOfRows]) - [queryResult dataSeek:0]; - for (i = 0 ; i < [queryResult numOfRows] ; i++) - { - // [possibleCompletions addObject:[[queryResult fetchRowAsArray] objectAtIndex:1]]; - [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSArrayObjectAtIndex([queryResult fetchRowAsArray], 1), @"display", @"proc-small", @"image", nil]]; - } + for (id obj in [[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"allProcedureNames"]) + [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:obj, @"display", @"proc-small", @"image", nil]]; // Add all function to completions list for currently selected table - queryResult = [mySQLConnection queryString:@"SHOW FUNCTION STATUS"]; - if ([queryResult numOfRows]) - [queryResult dataSeek:0]; - for (i = 0 ; i < [queryResult numOfRows] ; i++) - { - // [possibleCompletions addObject:[[queryResult fetchRowAsArray] objectAtIndex:1]]; - [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSArrayObjectAtIndex([queryResult fetchRowAsArray], 1), @"display", @"func-small", @"image", nil]]; - } + for (id obj in [[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"allFunctionNames"]) + [possibleCompletions addObject:[NSDictionary dictionaryWithObjectsAndKeys:obj, @"display", @"func-small", @"image", nil]]; } } @@ -927,51 +903,23 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) if([mySQLConnection isConnected]) { + + // Add all database names to completions list + [possibleCompletions addObjectsFromArray:[[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"allDatabaseNames"]]; + // Add table names to completions list - MCPResult *queryResult = [mySQLConnection listTables]; - if ([queryResult numOfRows]) - [queryResult dataSeek:0]; - for (i = 0 ; i < [queryResult numOfRows] ; i++) - { - [possibleCompletions addObject:[[queryResult fetchRowAsArray] objectAtIndex:0]]; - } + [possibleCompletions addObjectsFromArray:[[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"allTableAndViewNames"]]; // Add field names to completions list for currently selected table - if ([[[self window] delegate] table] != nil) { - id columnNames = [[[[self window] delegate] valueForKeyPath:@"tableDataInstance"] valueForKey:@"columnNames"]; - [possibleCompletions addObjectsFromArray:columnNames]; - } - - // Add all database names to completions list - queryResult = [mySQLConnection listDBs]; - if ([queryResult numOfRows]) - [queryResult dataSeek:0]; - for (i = 0 ; i < [queryResult numOfRows] ; i++) - { - [possibleCompletions addObject:[[queryResult fetchRowAsArray] objectAtIndex:0]]; - } + if ([[[self window] delegate] table] != nil) + [possibleCompletions addObjectsFromArray:[[[[self window] delegate] valueForKeyPath:@"tableDataInstance"] valueForKey:@"columnNames"]]; // Add proc/func only for MySQL version 5 or higher if(mySQLmajorVersion > 4) { - // Add all procedures to completions list for currently selected table - queryResult = [mySQLConnection queryString:@"SHOW PROCEDURE STATUS"]; - if ([queryResult numOfRows]) - [queryResult dataSeek:0]; - for (i = 0 ; i < [queryResult numOfRows] ; i++) - { - [possibleCompletions addObject:[[queryResult fetchRowAsArray] objectAtIndex:1]]; - } - - // Add all function to completions list for currently selected table - queryResult = [mySQLConnection queryString:@"SHOW FUNCTION STATUS"]; - if ([queryResult numOfRows]) - [queryResult dataSeek:0]; - for (i = 0 ; i < [queryResult numOfRows] ; i++) - { - [possibleCompletions addObject:[[queryResult fetchRowAsArray] objectAtIndex:1]]; - } + [possibleCompletions addObjectsFromArray:[[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"allProcedureNames"]]; + [possibleCompletions addObjectsFromArray:[[[[self window] delegate] valueForKeyPath:@"tablesListInstance"] valueForKey:@"allFunctionNames"]]; } - + } // If caret is not inside backticks add keywords and all words coming from the view. if(![[[self textStorage] attribute:kBTQuote atIndex:charRange.location effectiveRange:nil] isEqualToString:kBTQuoteValue] ) diff --git a/Source/TableDocument.h b/Source/TableDocument.h index 4816b4a3..8272cd62 100644 --- a/Source/TableDocument.h +++ b/Source/TableDocument.h @@ -100,6 +100,8 @@ NSToolbarItem *chooseDatabaseToolbarItem; WebView *printWebView; + + NSMutableArray *allDatabases; } - (NSString *)getHTMLforPrint; @@ -117,6 +119,7 @@ - (IBAction)removeDatabase:(id)sender; - (IBAction)showMySQLHelp:(id)sender; - (IBAction)saveServerVariables:(id)sender; +- (NSArray *)allDatabaseNames; //encoding methods - (void)setConnectionEncoding:(NSString *)mysqlEncoding reloadingViews:(BOOL)reloadViews; diff --git a/Source/TableDocument.m b/Source/TableDocument.m index f4383535..cbcc13b7 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -70,8 +70,9 @@ [printWebView setFrameLoadDelegate:self]; prefs = [NSUserDefaults standardUserDefaults]; + } - + return self; } @@ -349,12 +350,19 @@ [queryResult dataSeek:0]; } - int i; - - for (i = 0 ; i < [queryResult numOfRows] ; i++) - { - [chooseDatabaseButton addItemWithTitle:NSArrayObjectAtIndex([queryResult fetchRowAsArray], 0)]; - } + // if([allDatabases count]) + // [allDatabases removeAllObjects]; + + if(allDatabases) + [allDatabases release]; + + allDatabases = [[NSMutableArray alloc] initWithCapacity:[queryResult numOfRows]]; + + for (int i = 0 ; i < [queryResult numOfRows] ; i++) + [allDatabases addObject:NSArrayObjectAtIndex([queryResult fetchRowAsArray], 0)]; + + for (id db in allDatabases) + [chooseDatabaseButton addItemWithTitle:db]; (![self database]) ? [chooseDatabaseButton selectItemAtIndex:0] : [chooseDatabaseButton selectItemWithTitle:[self database]]; } @@ -494,6 +502,14 @@ [alert beginSheetModalForWindow:tableWindow modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:@"removedatabase"]; } +/* + * Returns an array of all available database names + */ +- (NSArray *)allDatabaseNames +{ + return allDatabases; +} + /** * alert sheets method * invoked when alertSheet get closed @@ -2127,7 +2143,7 @@ [selectedDatabase release]; [mySQLVersion release]; [connectionController release]; - + [allDatabases release]; [super dealloc]; } diff --git a/Source/TablesList.h b/Source/TablesList.h index 8a13e487..965f00be 100644 --- a/Source/TablesList.h +++ b/Source/TablesList.h @@ -110,6 +110,12 @@ enum sp_table_types - (int)tableType; - (NSArray *)tables; - (NSArray *)tableTypes; +- (NSArray *)allTableAndViewNames; +- (NSArray *)allTableNames; +- (NSArray *)allViewNames; +- (NSArray *)allFunctionNames; +- (NSArray *)allProcedureNames; +- (NSArray *)allDatabaseNames; - (BOOL)structureLoaded; - (BOOL)contentLoaded; - (BOOL)statusLoaded; diff --git a/Source/TablesList.m b/Source/TablesList.m index ec330d9a..49b97008 100644 --- a/Source/TablesList.m +++ b/Source/TablesList.m @@ -929,6 +929,78 @@ return tables; } +/** + * Database tables accessors for a given table type + */ +- (NSArray *)allTableAndViewNames +{ + NSMutableArray *returnArray = [NSMutableArray array]; + int i; + int cnt = [[self tables] count]; + for(i=0; i