diff options
author | stuconnolly <stuart02@gmail.com> | 2012-05-08 14:23:31 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2012-05-08 14:23:31 +0000 |
commit | 343e4ed439ba0ccc255460df4fc69e25c4a3f91e (patch) | |
tree | fb1d7a981f2a371785d7008bf6563a9caf188cd0 | |
parent | cb29bcb923804e844411fb4872f55993bf29ee91 (diff) | |
download | sequelpro-343e4ed439ba0ccc255460df4fc69e25c4a3f91e.tar.gz sequelpro-343e4ed439ba0ccc255460df4fc69e25c4a3f91e.tar.bz2 sequelpro-343e4ed439ba0ccc255460df4fc69e25c4a3f91e.zip |
Improve formatting of SQL keywords in view syntax prettifier and add an associated test.
-rw-r--r-- | Source/SPSQLExporter.m | 8 | ||||
-rw-r--r-- | Source/SPStringAdditions.m | 32 | ||||
-rw-r--r-- | UnitTests/SPMenuAdditionsTests.h | 7 | ||||
-rw-r--r-- | UnitTests/SPStringAdditionsTest.h | 1 | ||||
-rw-r--r-- | UnitTests/SPStringAdditionsTest.m | 20 |
5 files changed, 49 insertions, 19 deletions
diff --git a/Source/SPSQLExporter.m b/Source/SPSQLExporter.m index 907360ea..4def00d3 100644 --- a/Source/SPSQLExporter.m +++ b/Source/SPSQLExporter.m @@ -251,10 +251,10 @@ } // Add a 'DROP TABLE' command if required - if (sqlOutputIncludeDropSyntax) + if (sqlOutputIncludeDropSyntax) { [[self exportOutputFile] writeData:[[NSString stringWithFormat:@"DROP %@ IF EXISTS %@;\n\n", ((tableType == SPTableTypeTable) ? @"TABLE" : @"VIEW"), [tableName backtickQuotedString]] - dataUsingEncoding:[self exportOutputEncoding]]]; - + dataUsingEncoding:[self exportOutputEncoding]]]; + } // Add the create syntax for the table if specified in the export dialog if (sqlOutputIncludeStructure && createTableSyntax) { @@ -575,7 +575,7 @@ [metaString setString:@"\n\n"]; // Add the name of table [metaString appendFormat:@"# Replace placeholder table for %@ with correct view syntax\n# ------------------------------------------------------------\n\n", tableName]; - [metaString appendFormat:@"DROP TABLE %@;\n", [tableName backtickQuotedString]]; + [metaString appendFormat:@"DROP TABLE %@;\n\n", [tableName backtickQuotedString]]; [metaString appendFormat:@"%@;\n", [viewSyntaxes objectForKey:tableName]]; [[self exportOutputFile] writeData:[metaString dataUsingEncoding:NSUTF8StringEncoding]]; diff --git a/Source/SPStringAdditions.m b/Source/SPStringAdditions.m index 84c5fa86..55d83a76 100644 --- a/Source/SPStringAdditions.m +++ b/Source/SPStringAdditions.m @@ -265,37 +265,43 @@ { NSRange searchRange = NSMakeRange(0, [self length]); NSRange matchedRange; - NSError *err = NULL; NSMutableString *tblSyntax = [NSMutableString stringWithCapacity:[self length]]; - NSString * re = @"(.*?) AS select (.*?) (from.*)"; + NSString *re = @"(.*?) AS select (.*?) (from.*)"; // Create view syntax - matchedRange = [self rangeOfRegex:re options:(RKLMultiline|RKLDotAll) inRange:searchRange capture:1 error:&err]; + matchedRange = [self rangeOfRegex:re options:(RKLMultiline|RKLDotAll) inRange:searchRange capture:1 error:nil]; - if (!matchedRange.length || matchedRange.length > [self length]) return([self description]); + if (!matchedRange.length || matchedRange.length > [self length]) return [self description]; [tblSyntax appendString:[self substringWithRange:matchedRange]]; - [tblSyntax appendString:@"\nAS select\n "]; + [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]; + matchedRange = [self rangeOfRegex:re options:(RKLMultiline|RKLDotAll) inRange:searchRange capture:2 error:nil]; - if (!matchedRange.length || matchedRange.length > [self length]) return([self description]); + 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]; + // FROM ... on a new line + matchedRange = [self rangeOfRegex:re options:(RKLMultiline|RKLDotAll) inRange:searchRange capture:3 error:nil]; - if (!matchedRange.length || matchedRange.length > [self length]) return([self description]); + if (!matchedRange.length || matchedRange.length > [self length]) return [self description]; + + NSMutableString *from = [[NSMutableString alloc] initWithString:[self substringWithRange:matchedRange]]; + + // Uppercase FROM + [from replaceCharactersInRange:NSMakeRange(0, 4) withString:@"FROM"]; [tblSyntax appendString:@"\n"]; - [tblSyntax appendString:[self substringWithRange:matchedRange]]; + [tblSyntax appendString:from]; + + [from release]; // Where clause at a new line if given - [tblSyntax replaceOccurrencesOfString:@" where (" withString:@"\nwhere (" options:NSLiteralSearch range:NSMakeRange(0, [tblSyntax length])]; + [tblSyntax replaceOccurrencesOfString:@" WHERE (" withString:@"\nWHERE (" options:NSLiteralSearch range:NSMakeRange(0, [tblSyntax length])]; - return(tblSyntax); + return tblSyntax; } /** diff --git a/UnitTests/SPMenuAdditionsTests.h b/UnitTests/SPMenuAdditionsTests.h index 1ff2ab61..f2999405 100644 --- a/UnitTests/SPMenuAdditionsTests.h +++ b/UnitTests/SPMenuAdditionsTests.h @@ -25,6 +25,13 @@ #import <SenTestingKit/SenTestingKit.h> +/** + * @class SPMenuAdditionsTests SPMenuAdditionsTests.h + * + * @author Stuart Connolly http://stuconnolly.com/ + * + * SPMenuAdditionsTests tests class. + */ @interface SPMenuAdditionsTests : SenTestCase { NSMenu *menu; diff --git a/UnitTests/SPStringAdditionsTest.h b/UnitTests/SPStringAdditionsTest.h index 25d418f4..9599b9a6 100644 --- a/UnitTests/SPStringAdditionsTest.h +++ b/UnitTests/SPStringAdditionsTest.h @@ -29,5 +29,6 @@ - (void)testStringByRemovingCharactersInSet; - (void)testStringWithNewUUID; +- (void)testCreateViewSyntaxPrettifier; @end diff --git a/UnitTests/SPStringAdditionsTest.m b/UnitTests/SPStringAdditionsTest.m index 0571ff3c..2d0cb0fb 100644 --- a/UnitTests/SPStringAdditionsTest.m +++ b/UnitTests/SPStringAdditionsTest.m @@ -34,8 +34,8 @@ */ - (void)testStringByRemovingCharactersInSet { - static NSString *SPASCIITestString = @"this is a big, crazy test st'ring with som'e random spaces and quot'es"; - static NSString *SPUTFTestString = @"In der Kürze liegt die Würz"; + NSString *SPASCIITestString = @"this is a big, crazy test st'ring with som'e random spaces and quot'es"; + NSString *SPUTFTestString = @"In der Kürze liegt die Würz"; NSString *charsToRemove = @"abc',ü"; @@ -58,6 +58,9 @@ charsToRemove); } +/** + * stringWithNewUUID test case. + */ - (void)testStringWithNewUUID { NSString *uuid = [NSString stringWithNewUUID]; @@ -65,4 +68,17 @@ STAssertTrue([uuid isMatchedByRegex:@"[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}"], @"UUID %@ doesn't match regex", uuid); } +/** + * createViewSyntaxPrettifier test case. + */ +- (void)testCreateViewSyntaxPrettifier +{ + NSString *originalSyntax = @"CREATE VIEW `test_view` AS select `test_table`.`id` AS `id` from `test_table`;"; + NSString *expectedSyntax = @"CREATE VIEW `test_view`\nAS SELECT\n `test_table`.`id` AS `id`\nFROM `test_table`;"; + + NSString *actualSyntax = [originalSyntax createViewSyntaxPrettifier]; + + STAssertEqualObjects([actualSyntax description], [expectedSyntax description], @"Actual view syntax '%@' does not equal expected syntax '%@'", actualSyntax, expectedSyntax); +} + @end |