From a16c4f07fb362c3dac9af24f8df8765ca1e61900 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Thu, 23 Apr 2009 14:38:47 +0000 Subject: =?UTF-8?q?=E2=80=A2=20ADDED=20createViewSyntaxPrettifier=20method?= =?UTF-8?q?=20to=20a=20NSString=20to=20make=20the=20syntax=20a=20bit=20mor?= =?UTF-8?q?e=20readable=20-=20used=20for=20show/copy=20create=20view=20syn?= =?UTF-8?q?tax=20as=20well=20as=20for=20a=20MySQL=20dump?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPStringAdditions.h | 1 + Source/SPStringAdditions.m | 71 ++++++++++++++++++++++++++++++++++++++-------- Source/TableDocument.m | 19 +++++++++---- Source/TableDump.m | 2 +- 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/Source/SPStringAdditions.h b/Source/SPStringAdditions.h index 1d706666..1b16dfd9 100644 --- a/Source/SPStringAdditions.h +++ b/Source/SPStringAdditions.h @@ -29,6 +29,7 @@ - (NSString *)backtickQuotedString; - (NSArray *)lineRangesForRange:(NSRange)aRange; +- (NSString *)createViewSyntaxPrettifier; #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5 - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)set; diff --git a/Source/SPStringAdditions.m b/Source/SPStringAdditions.m index e0bf65cf..fdad0cff 100644 --- a/Source/SPStringAdditions.m +++ b/Source/SPStringAdditions.m @@ -21,6 +21,7 @@ // More info at #import "SPStringAdditions.h" +#import "RegexKitLite.h" @implementation NSString (SPStringAdditions) @@ -124,24 +125,70 @@ // ------------------------------------------------------------------------------- - (NSString *)backtickQuotedString { - // mutableCopy automatically retains the returned string, so don't forget to release it later... - NSMutableString *workingCopy = [self mutableCopy]; - - // First double all backticks in the string to escape them - // I don't want to use "stringByReplacingOccurrencesOfString:withString:" because it's only available in 10.5 - [workingCopy replaceOccurrencesOfString: @"`" + // mutableCopy automatically retains the returned string, so don't forget to release it later... + NSMutableString *workingCopy = [self mutableCopy]; + + // First double all backticks in the string to escape them + // I don't want to use "stringByReplacingOccurrencesOfString:withString:" because it's only available in 10.5 + [workingCopy replaceOccurrencesOfString: @"`" withString: @"``" options: NSLiteralSearch range: NSMakeRange(0, [workingCopy length]) ]; - // Add the quotes around the string - NSString *quotedString = [NSString stringWithFormat: @"`%@`", workingCopy]; - - [workingCopy release]; - - return quotedString; + // Add the quotes around the string + NSString *quotedString = [NSString stringWithFormat: @"`%@`", workingCopy]; + + [workingCopy release]; + + return quotedString; +} + + +// ------------------------------------------------------------------------------- +// createViewSyntaxPrettifier +// +// Returns a 'CREATE VIEW SYNTAX' string a bit more readable +// If the string doesn't match it returns the unchanged string. +// ------------------------------------------------------------------------------- +- (NSString *)createViewSyntaxPrettifier +{ + NSRange searchRange = NSMakeRange(0, [self length]); + NSRange matchedRange; + NSError *err = NULL; + NSMutableString *tblSyntax = [NSMutableString stringWithCapacity:[self length]]; + NSString * re = @"(.*?) AS select (.*?) (from.*)"; + + // create view syntax + matchedRange = [self rangeOfRegex:re options:(RKLMultiline|RKLDotAll) inRange:searchRange capture:1 error:&err]; + + if(!matchedRange.length || matchedRange.length > [self length]) return([self description]); + + [tblSyntax appendString:[self substringWithRange:matchedRange]]; + [tblSyntax appendString:@"\nAS select\n "]; + + // match all column definitions, split them by ',', and rejoin them by '\n' + matchedRange = [self rangeOfRegex:re options:(RKLMultiline|RKLDotAll) inRange:searchRange capture:2 error:&err]; + + if(!matchedRange.length || matchedRange.length > [self length]) return([self description]); + + [tblSyntax appendString: + [[[self substringWithRange:matchedRange] componentsSeparatedByString:@"`,`"] componentsJoinedByString:@"`,\n `"]]; + + // from ... at a new line + matchedRange = [self rangeOfRegex:re options:(RKLMultiline|RKLDotAll) inRange:searchRange capture:3 error:&err]; + + if(!matchedRange.length || matchedRange.length > [self length]) return([self description]); + + [tblSyntax appendString:@"\n"]; + [tblSyntax appendString:[self substringWithRange:matchedRange]]; + + // where clause at a new line if given + [tblSyntax replaceOccurrencesOfString:@" where (" withString:@"\nwhere (" options:NSLiteralSearch range:NSMakeRange(0, [tblSyntax length])]; + + return(tblSyntax); } + // ------------------------------------------------------------------------------- // lineRangesForRange // diff --git a/Source/TableDocument.m b/Source/TableDocument.m index 851213e7..3acb53b4 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -936,8 +936,12 @@ NSString *TableDocumentFavoritesControllerSelectionIndexDidChange = @"TableDocum if ([tableSyntax isKindOfClass:[NSData class]]) tableSyntax = [[NSString alloc] initWithData:tableSyntax encoding:[mySQLConnection encoding]]; - - [syntaxViewContent setString:tableSyntax]; + + if([tablesListInstance tableType] == SP_TABLETYPE_VIEW) + [syntaxViewContent setString:[tableSyntax createViewSyntaxPrettifier]]; + else + [syntaxViewContent setString:tableSyntax]; + [createTableSyntaxWindow makeKeyAndOrderFront:self]; } @@ -966,10 +970,13 @@ NSString *TableDocumentFavoritesControllerSelectionIndexDidChange = @"TableDocum // copy to the clipboard NSPasteboard *pb = [NSPasteboard generalPasteboard]; [pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self]; - [pb setString:tableSyntax forType:NSStringPboardType]; - - // Table syntax copied Growl notification - [[SPGrowlController sharedGrowlController] notifyWithTitle:@"Table Syntax Copied" + if([tablesListInstance tableType] == SP_TABLETYPE_VIEW) + [pb setString:[tableSyntax createViewSyntaxPrettifier] forType:NSStringPboardType]; + else + [pb setString:tableSyntax forType:NSStringPboardType]; + + // Table syntax copied Growl notification + [[SPGrowlController sharedGrowlController] notifyWithTitle:@"Table Syntax Copied" description:[NSString stringWithFormat:NSLocalizedString(@"Syntax for %@ table copied",@"description for table syntax copied growl notification"), [self table]] notificationName:@"Table Syntax Copied"]; } diff --git a/Source/TableDump.m b/Source/TableDump.m index 03e98bd6..e44e01ae 100644 --- a/Source/TableDump.m +++ b/Source/TableDump.m @@ -882,7 +882,7 @@ if ( [queryResult numOfRows] ) { tableDetails = [[NSDictionary alloc] initWithDictionary:[queryResult fetchRowAsDictionary]]; if ([tableDetails objectForKey:@"Create View"]) { - createTableSyntax = [[[tableDetails objectForKey:@"Create View"] copy] autorelease]; + createTableSyntax = [[[[tableDetails objectForKey:@"Create View"] copy] autorelease] createViewSyntaxPrettifier]; tableType = SP_TABLETYPE_VIEW; } else { createTableSyntax = [[[tableDetails objectForKey:@"Create Table"] copy] autorelease]; -- cgit v1.2.3