diff options
author | Bibiko <bibiko@eva.mpg.de> | 2009-05-19 19:53:26 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2009-05-19 19:53:26 +0000 |
commit | 641eeff363a2864456a985b246576bfd5d41b144 (patch) | |
tree | be9353ad60b0c282cf0a2014a78ab63e61b3101d /Source/SPSQLParser.m | |
parent | 2ff722a0de334e4fc4df1dc6219734e85326637c (diff) | |
download | sequelpro-641eeff363a2864456a985b246576bfd5d41b144.tar.gz sequelpro-641eeff363a2864456a985b246576bfd5d41b144.tar.bz2 sequelpro-641eeff363a2864456a985b246576bfd5d41b144.zip |
• implemented a new approach to split a string into single SQL statements by using the lexer SPTokenizer
- the new method is called splitStringIntoRangesOfSQLQueries: in SPSQLParser
- in CustomQuery's method queryAtPosition: can be found a test case which is as default not activated
- must be improved further
Diffstat (limited to 'Source/SPSQLParser.m')
-rw-r--r-- | Source/SPSQLParser.m | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/Source/SPSQLParser.m b/Source/SPSQLParser.m index ce7cd07e..281c1b3d 100644 --- a/Source/SPSQLParser.m +++ b/Source/SPSQLParser.m @@ -24,6 +24,17 @@ // More info at <http://code.google.com/p/sequel-pro/> #import "SPSQLParser.h" +#import "RegexKitLite.h" + +/* + * Include all the extern variables and prototypes required for flex (used for syntax highlighting) + */ +#import "SPSQLTokenizer.h" +extern int tolex(); +extern int yyuoffset, yyuleng; +typedef struct to_buffer_state *TO_BUFFER_STATE; +void to_switch_to_buffer(TO_BUFFER_STATE); +TO_BUFFER_STATE to_scan_string (const char *); /* * Please see the header files for a general description of the purpose of this class, @@ -426,6 +437,77 @@ return resultsArray; } +/* + * As splitStringByCharacter: ..., but allows control over quoting + * - it recognises CREATE ... BEGIN ... END statements + * - it can detect a SINGLE SQL statement in between + * delimiter foo ... foo delimiter ; + * ['delimiter ;' MUST be given!] + * - it returns an array of ranges (as NSString "{loc, length}"). + * FromPosition: is needed if a subrange is passed to sync the ranges + * according to the CQ textView ones. + */ +- (NSArray *) splitStringIntoRangesOfSQLQueries +{ + return [self splitStringIntoRangesOfSQLQueriesFromPosition:0]; +} +- (NSArray *) splitStringIntoRangesOfSQLQueriesFromPosition:(long)position +{ + NSMutableArray *resultsArray = [NSMutableArray array]; + + //initialise flex + yyuoffset = 0; yyuleng = 0; + to_switch_to_buffer(to_scan_string([string UTF8String])); + + unsigned long token; + unsigned long lastFoundToken = 0; + unsigned long delimLength = 0; + unsigned long commentStart = 0; + unsigned long commentLength = 0; + + NSString *delimString; + + //now loop through all queries + while (token=tolex()){ + switch (token) { + case SP_SQL_TOKEN_SEMICOLON: + [resultsArray addObject:[NSString stringWithFormat:@"{%d,%d}", lastFoundToken+position, yyuoffset-lastFoundToken]]; + break; + case SP_SQL_TOKEN_SINGLE_LINE_COMMENT: + commentStart = yyuoffset+position; + commentLength = yyuleng; + break; + case SP_SQL_TOKEN_DELIM_END: + [resultsArray addObject:[NSString stringWithFormat:@"{%d,%d}", lastFoundToken+position, yyuoffset-lastFoundToken-delimLength]]; + delimLength = 0; + delimString = nil; + break; + case SP_SQL_TOKEN_DELIM_VALUE: + delimString = [string substringWithRange:NSMakeRange(yyuoffset,yyuleng)]; + // NSLog(@"del: %@", delimString); + delimLength = yyuleng; + break; + case SP_SQL_TOKEN_COMPOUND_END: + [resultsArray addObject:[NSString stringWithFormat:@"{%d,%d}", lastFoundToken+position, yyuoffset+yyuleng-lastFoundToken]]; + break; + default: + continue; + } + if(token<SP_SQL_TOKEN_IGNORE) + { + lastFoundToken = yyuoffset+yyuleng; + // ignore sinlge comment lines at the very beginning of a query + if(commentStart == lastFoundToken) + lastFoundToken += commentLength; + } + } + + // add the last text chunk as query + if(lastFoundToken+1<[self length]) + [resultsArray addObject:[NSString stringWithFormat:@"{%d,%d}", lastFoundToken+position, [self length]-lastFoundToken-delimLength]]; + + return resultsArray; +} /* * A method intended for use by the functions above. |