diff options
author | Bibiko <bibiko@eva.mpg.de> | 2010-11-11 22:47:42 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2010-11-11 22:47:42 +0000 |
commit | e83978c06efee794faa0603c6e6653fd1647b26a (patch) | |
tree | 95314beabcdc0dceb8840d9ac524450fd15a5adb | |
parent | c7b35d8ce013ad4cfd02c7ab250f5864b49980bd (diff) | |
download | sequelpro-e83978c06efee794faa0603c6e6653fd1647b26a.tar.gz sequelpro-e83978c06efee794faa0603c6e6653fd1647b26a.tar.bz2 sequelpro-e83978c06efee794faa0603c6e6653fd1647b26a.zip |
• some sequelpro://...@passToDoc/ExecuteQuery/csv improvements
-rw-r--r-- | Source/SPArrayAdditions.h | 1 | ||||
-rw-r--r-- | Source/SPArrayAdditions.m | 15 | ||||
-rw-r--r-- | Source/SPDatabaseDocument.m | 28 | ||||
-rw-r--r-- | Source/SPTextView.m | 38 |
4 files changed, 58 insertions, 24 deletions
diff --git a/Source/SPArrayAdditions.h b/Source/SPArrayAdditions.h index 89ea53e4..206f15f8 100644 --- a/Source/SPArrayAdditions.h +++ b/Source/SPArrayAdditions.h @@ -44,6 +44,7 @@ static inline void NSMutableArrayReplaceObject(NSArray *self, CFIndex idx, id an - (NSString *)componentsJoinedBySpacesAndQuoted; - (NSString *)componentsJoinedByPeriodAndBacktickQuoted; - (NSString *)componentsJoinedByPeriodAndBacktickQuotedAndIgnoreFirst; +- (NSString *)componentsJoinedAsCSV; - (NSArray *)subarrayWithIndexes:(NSIndexSet *)indexes; @end diff --git a/Source/SPArrayAdditions.m b/Source/SPArrayAdditions.m index 9f9e7705..66a0ce1b 100644 --- a/Source/SPArrayAdditions.m +++ b/Source/SPArrayAdditions.m @@ -70,7 +70,7 @@ if ([result length]) [result appendString:@" "]; - [result appendString:[NSString stringWithFormat:@"\"%@\"", [component stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]]; + [result appendFormat:@"\"%@\"", [component stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]; } return result; } @@ -108,6 +108,19 @@ return result; } +- (NSString *)componentsJoinedAsCSV +{ + NSMutableString *result = [NSMutableString string]; + [result setString:@""]; + + for (NSString *component in self) + { + if ([result length]) + [result appendString: @","]; + [result appendFormat:@"\"%@\"", [[component description] stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]]; + } + return result; +} - (NSArray *)subarrayWithIndexes:(NSIndexSet *)indexes { diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m index 04660b28..96a6aa3e 100644 --- a/Source/SPDatabaseDocument.m +++ b/Source/SPDatabaseDocument.m @@ -4423,11 +4423,16 @@ } else if([command isEqualToString:@"ExecuteQuery"]) { + + // Bail if document is busy if (_isWorkingLevel) return; + NSString *outputFormat = @"tab"; if([params count] == 2) outputFormat = [params objectAtIndex:1]; + BOOL writeAsCsv = ([outputFormat isEqualToString:@"csv"]) ? YES : NO; + NSString *queryFileName = [NSString stringWithFormat:@"/private/tmp/SP_QUERY_%@", docProcessID]; NSString *resultFileName = [NSString stringWithFormat:@"/private/tmp/SP_QUERY_RESULT_%@", docProcessID]; NSString *metaFileName = [NSString stringWithFormat:@"/private/tmp/SP_QUERY_RESULT_META_%@", docProcessID]; @@ -4446,27 +4451,42 @@ [fm removeItemAtPath:statusFileName error:nil]; if(inError == nil && query && [query length]) { + SPFileHandle *fh = [SPFileHandle fileHandleForWritingAtPath:resultFileName]; - if(!fh) - NSLog(@"Couldn't create file handle to %@", resultFileName); + if(!fh) NSLog(@"Couldn't create file handle to %@", resultFileName); + MCPResult *theResult = [mySQLConnection queryString:query]; [theResult setReturnDataAsStrings:YES]; if ([mySQLConnection queryErrored]) { - [fh writeData:[[NSString stringWithFormat:@"Error.\nMySQL said: %@", [mySQLConnection getLastErrorMessage]] dataUsingEncoding:NSUTF8StringEncoding]]; + [fh writeData:[[NSString stringWithFormat:@"MySQL said: %@", [mySQLConnection getLastErrorMessage]] dataUsingEncoding:NSUTF8StringEncoding]]; status = @"1"; } else { + + // write header + if(writeAsCsv) + [fh writeData:[[[theResult fetchFieldNames] componentsJoinedAsCSV] dataUsingEncoding:NSUTF8StringEncoding]]; + else + [fh writeData:[[[theResult fetchFieldNames] componentsJoinedByString:@"\t"] dataUsingEncoding:NSUTF8StringEncoding]]; + [fh writeData:[[NSString stringWithString:@"\n"] dataUsingEncoding:NSUTF8StringEncoding]]; + + // write data if ([theResult numOfRows]) [theResult dataSeek:0]; NSInteger i; NSArray *theRow; for ( i = 0 ; i < [theResult numOfRows] ; i++ ) { theRow = [theResult fetchRowAsArray]; - [fh writeData:[[theRow componentsJoinedByString:@"\t"] dataUsingEncoding:NSUTF8StringEncoding]]; + if(writeAsCsv) + [fh writeData:[[theRow componentsJoinedAsCSV] dataUsingEncoding:NSUTF8StringEncoding]]; + else + [fh writeData:[[theRow componentsJoinedByString:@"\t"] dataUsingEncoding:NSUTF8StringEncoding]]; [fh writeData:[[NSString stringWithString:@"\n"] dataUsingEncoding:NSUTF8StringEncoding]]; } } [fh closeFile]; } } + + // write status file as notification that query was finished BOOL succeed = [status writeToFile:statusFileName atomically:YES encoding:NSUTF8StringEncoding error:nil]; if(!succeed) { NSBeep(); diff --git a/Source/SPTextView.m b/Source/SPTextView.m index 34c87822..da4c3f04 100644 --- a/Source/SPTextView.m +++ b/Source/SPTextView.m @@ -3510,13 +3510,13 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) if (cmdData) [cmdData release]; return; } else { - if([cmdData objectForKey:@"command"] && [[cmdData objectForKey:@"command"] length]) { + if([cmdData objectForKey:@"command"] && [[cmdData objectForKey:@"command"] length] && [cmdData objectForKey:@"scope"] && [[cmdData objectForKey:@"scope"] isEqualToString:@"editor"]) { NSString *cmd = [cmdData objectForKey:@"command"]; NSString *inputAction = @""; NSString *inputFallBackAction = @""; NSError *err = nil; - NSString *inputTempFileName = @"/tmp/sp_bundle_task_input"; + NSString *inputTempFileName = @"/tmp/SP_BUNDLE_TASK_INPUT"; NSRange currentWordRange, currentQueryRange, currentSelectionRange, currentLineRange; [[NSFileManager defaultManager] removeItemAtPath:inputTempFileName error:nil]; @@ -3555,21 +3555,6 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) replaceRange = NSMakeRange(0,[[self string] length]); } - NSError *inputFileError = nil; - NSString *input = [NSString stringWithString:[[self string] substringWithRange:replaceRange]]; - [input writeToFile:inputTempFileName - atomically:YES - encoding:NSUTF8StringEncoding - error:&inputFileError]; - - if(inputFileError != nil) { - NSString *errorMessage = [inputFileError localizedDescription]; - SPBeginAlertSheet(NSLocalizedString(@"Bundle Error", @"bundle error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [self window], self, nil, nil, - [NSString stringWithFormat:@"%@ “%@”:\n%@", NSLocalizedString(@"Error for", @"error for message"), [cmdData objectForKey:@"name"], errorMessage]); - if (cmdData) [cmdData release]; - return; - } - NSMutableDictionary *env = [NSMutableDictionary dictionary]; [env setObject:[infoPath stringByDeletingLastPathComponent] forKey:@"SP_BUNDLE_PATH"]; [env setObject:inputTempFileName forKey:@"SP_BUNDLE_INPUT_FILE"]; @@ -3613,8 +3598,25 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) if(currentLineRange.length) [env setObject:[[self string] substringWithRange:currentLineRange] forKey:@"SP_CURRENT_LINE"]; + NSError *inputFileError = nil; + NSString *input = [NSString stringWithString:[[self string] substringWithRange:replaceRange]]; + [input writeToFile:inputTempFileName + atomically:YES + encoding:NSUTF8StringEncoding + error:&inputFileError]; + + if(inputFileError != nil) { + NSString *errorMessage = [inputFileError localizedDescription]; + SPBeginAlertSheet(NSLocalizedString(@"Bundle Error", @"bundle error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, [self window], self, nil, nil, + [NSString stringWithFormat:@"%@ “%@”:\n%@", NSLocalizedString(@"Error for", @"error for message"), [cmdData objectForKey:@"name"], errorMessage]); + if (cmdData) [cmdData release]; + return; + } + NSString *output = [cmd runBashCommandWithEnvironment:env atCurrentDirectoryPath:nil error:&err]; + [[NSFileManager defaultManager] removeItemAtPath:inputTempFileName error:nil]; + if(err == nil && [cmdData objectForKey:@"output"]) { if([[cmdData objectForKey:@"output"] length] && ![[cmdData objectForKey:@"output"] isEqualToString:@"nop"]) { NSString *action = [[cmdData objectForKey:@"output"] lowercaseString]; @@ -3652,8 +3654,6 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) [NSString stringWithFormat:@"%@ “%@”:\n%@", NSLocalizedString(@"Error for", @"error for message"), [cmdData objectForKey:@"name"], errorMessage]); } - [[NSFileManager defaultManager] removeItemAtPath:inputTempFileName error:nil]; - } if (cmdData) [cmdData release]; |