aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPStringAdditions.m
diff options
context:
space:
mode:
authorBibiko <bibiko@eva.mpg.de>2009-04-23 14:38:47 +0000
committerBibiko <bibiko@eva.mpg.de>2009-04-23 14:38:47 +0000
commita16c4f07fb362c3dac9af24f8df8765ca1e61900 (patch)
treea25ae3ee0f0e832557c926bc6878614930ed3fac /Source/SPStringAdditions.m
parent985d03cb9980bc92078af8c6a6815efdeefd1377 (diff)
downloadsequelpro-a16c4f07fb362c3dac9af24f8df8765ca1e61900.tar.gz
sequelpro-a16c4f07fb362c3dac9af24f8df8765ca1e61900.tar.bz2
sequelpro-a16c4f07fb362c3dac9af24f8df8765ca1e61900.zip
• ADDED createViewSyntaxPrettifier method to a NSString to make the syntax a bit more readable
- used for show/copy create view syntax as well as for a MySQL dump
Diffstat (limited to 'Source/SPStringAdditions.m')
-rw-r--r--Source/SPStringAdditions.m71
1 files changed, 59 insertions, 12 deletions
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 <http://code.google.com/p/sequel-pro/>
#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
//