diff options
author | Bibiko <bibiko@eva.mpg.de> | 2010-11-12 10:10:28 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2010-11-12 10:10:28 +0000 |
commit | e65937dea0371319e3d1fd41493f0ba2d765f311 (patch) | |
tree | 37f5898787de7282834e801f866522b8ebd75e11 /Source | |
parent | b86e805f9b7325e4e4ce144069722f56a0a811b7 (diff) | |
download | sequelpro-e65937dea0371319e3d1fd41493f0ba2d765f311.tar.gz sequelpro-e65937dea0371319e3d1fd41493f0ba2d765f311.tar.bz2 sequelpro-e65937dea0371319e3d1fd41493f0ba2d765f311.zip |
• improved export of a query result via sequelpro:// scheme command
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPDatabaseDocument.m | 71 |
1 files changed, 61 insertions, 10 deletions
diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m index 96a6aa3e..abcd0b9f 100644 --- a/Source/SPDatabaseDocument.m +++ b/Source/SPDatabaseDocument.m @@ -4455,7 +4455,7 @@ SPFileHandle *fh = [SPFileHandle fileHandleForWritingAtPath:resultFileName]; if(!fh) NSLog(@"Couldn't create file handle to %@", resultFileName); - MCPResult *theResult = [mySQLConnection queryString:query]; + MCPStreamingResult *theResult = [mySQLConnection streamingQueryString:query]; [theResult setReturnDataAsStrings:YES]; if ([mySQLConnection queryErrored]) { [fh writeData:[[NSString stringWithFormat:@"MySQL said: %@", [mySQLConnection getLastErrorMessage]] dataUsingEncoding:NSUTF8StringEncoding]]; @@ -4469,17 +4469,68 @@ [fh writeData:[[[theResult fetchFieldNames] componentsJoinedByString:@"\t"] dataUsingEncoding:NSUTF8StringEncoding]]; [fh writeData:[[NSString stringWithString:@"\n"] dataUsingEncoding:NSUTF8StringEncoding]]; + NSArray *columnDefinition = [theResult fetchResultFieldsStructure]; + // write data - if ([theResult numOfRows]) [theResult dataSeek:0]; - NSInteger i; + NSInteger i, j; NSArray *theRow; - for ( i = 0 ; i < [theResult numOfRows] ; i++ ) { - theRow = [theResult fetchRowAsArray]; - if(writeAsCsv) - [fh writeData:[[theRow componentsJoinedAsCSV] dataUsingEncoding:NSUTF8StringEncoding]]; - else - [fh writeData:[[theRow componentsJoinedByString:@"\t"] dataUsingEncoding:NSUTF8StringEncoding]]; - [fh writeData:[[NSString stringWithString:@"\n"] dataUsingEncoding:NSUTF8StringEncoding]]; + NSMutableString *result = [NSMutableString string]; + + if(writeAsCsv) { + for ( i = 0 ; i < [theResult numOfRows] ; i++ ) { + [result setString:@""]; + theRow = [theResult fetchNextRowAsArray]; + for( j = 0 ; j < [theRow count] ; j++ ) { + if([result length]) [result appendString:@","]; + id cell = NSArrayObjectAtIndex(theRow, j); + if([cell isKindOfClass:[NSNull class]]) + [result appendString:@"\"NULL\""]; + else if([cell isKindOfClass:[MCPGeometryData class]]) + [result appendFormat:@"\"%@\"", [cell wktString]]; + else if([cell isKindOfClass:[NSData class]]) { + NSString *displayString = [[NSString alloc] initWithData:cell encoding:[mySQLConnection stringEncoding]]; + if (!displayString) displayString = [[NSString alloc] initWithData:cell encoding:NSASCIIStringEncoding]; + if (displayString) { + [result appendFormat:@"\"%@\"", [displayString stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]]; + [displayString release]; + } else { + [result appendString:@"\"\""]; + } + } + else + [result appendFormat:@"\"%@\"", [[cell description] stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""]]; + } + [result appendString:@"\n"]; + [fh writeData:[result dataUsingEncoding:NSUTF8StringEncoding]]; + } + } + else { + for ( i = 0 ; i < [theResult numOfRows] ; i++ ) { + [result setString:@""]; + theRow = [theResult fetchNextRowAsArray]; + for( j = 0 ; j < [theRow count] ; j++ ) { + if([result length]) [result appendString:@"\t"]; + id cell = NSArrayObjectAtIndex(theRow, j); + if([cell isKindOfClass:[NSNull class]]) + [result appendString:@"NULL"]; + else if([cell isKindOfClass:[MCPGeometryData class]]) + [result appendFormat:@"%@", [cell wktString]]; + else if([cell isKindOfClass:[NSData class]]) { + NSString *displayString = [[NSString alloc] initWithData:cell encoding:[mySQLConnection stringEncoding]]; + if (!displayString) displayString = [[NSString alloc] initWithData:cell encoding:NSASCIIStringEncoding]; + if (displayString) { + [result appendFormat:@"%@", [[displayString stringByReplacingOccurrencesOfString:@"\n" withString:@"↵"] stringByReplacingOccurrencesOfString:@"\t" withString:@"⇥"]]; + [displayString release]; + } else { + [result appendString:@""]; + } + } + else + [result appendString:[[[cell description] stringByReplacingOccurrencesOfString:@"\n" withString:@"↵"] stringByReplacingOccurrencesOfString:@"\t" withString:@"⇥"]]; + } + [result appendString:@"\n"]; + [fh writeData:[result dataUsingEncoding:NSUTF8StringEncoding]]; + } } } [fh closeFile]; |