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.m | 71 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 12 deletions(-) (limited to 'Source/SPStringAdditions.m') 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 // -- cgit v1.2.3