Conversation started Sep 6, 2012 at 9:56.
Sep 6, 2012 09:56
anyone know how hard splitting a sql batch file up into individual statements is (given the possibility of strings with various delimiters containing ';'s and 'GO' etc) - and if there are any libraries around that do this?
@JackDouglas have you ever used a lexer like flex?
nope
is it spiffing?
If you could tokenise the SQL with flex then it shouldn't be too hard to pick out the statement delimiters. Making lexers isn't all that hard.
I've used flex as a sort of super grep on several occasions. It's easier than it looks once you understand the tool.
You shouldn't need to implement a grammar to find the delimiter tokens, because flex will be able to recognise comments and string literals.
I did a lexer for a whole language (cecil) in about a day or two for a computer science assignment. I think a lexer for PL/SQL or T-SQL wouldn't be all that grim.
But, I can't think of anything easier than that. It might be quicker to load it into vim and pick it apart by hand.
@ConcernedOfTunbridgeWells would things like dollar quoted strings be a major stumbling block?
This is for SQLFiddle btw
If they can be picked out by a regular expression they should be OK. Flex uses regexes for token matching, but it's a little bit cleverer than just a regular language.
It has a stack, so you can push and pop states and have state dependent token matching patterns.
Sep 6, 2012 10:05
@ConcernedOfTunbridgeWells not simply - there might dollars in a quoted string within the dollar delimited string
@ConcernedOfTunbridgeWells does that solve the problem I just mentioned?
@JackDouglas How are they quoted?
$foo\$bar$ or something like that?
$$this is a dollar quoted string - it is delimited by '$$'$$
is $delim$ a valid delimiter in your case?
Sep 6, 2012 10:07
Flex should be able to cope with that. If the internal $$ is quoted then you can have pattern matching within the state for lexing the comments.
As long as you can tell the difference between a quoted $$ in the string and the delimiter.
@ConcernedOfTunbridgeWells oops my example is wrong
you cant have $$ inside the string
you use another delimiter instead: stackoverflow.com/a/9272066/533832
so, yes, you can tell the difference :)
The default output of flex is just to print the tokens back to stdout. You can add specific actions to tag a statement delimiter with something that can be picked up with a dumber tool.
Depending on the scale of your problem it might be overkill, but it is quite a bit smarter than grep.
@ConcernedOfTunbridgeWells can the 'token' be defined as a statement?
@JackDouglas You can define custom actions to implement when you match specific tokens.
What you would do is to match statement delimiter tokens and take a specific action like redirecting the output to another file when you encounter them.
Sep 6, 2012 10:13
gotcha
how easy is it to use flex from eg PHP, C or whatever?
Or doing it in two hops and tagging the tokens with something that can be picked up with something dumber like a little perl or awk script that switches the output file when it picks up the gag.
@JackDouglas Dead easy from C. It generates C code for the lexer that you can compile and embed into a C program.
I could dig out my old COSC202 assignment and send it to you if you want a worked example of a lexer.
Plus the O'reilly lex & yacc book actually has an example of a YACC brammar for SQL (presumably a standard dialect) if you really want to go to town on parsing.
@ConcernedOfTunbridgeWells thanks but don't worry :)
I think SQLFiddle is Java based
ok, thanks its nice to know it is possible and wouldn't be infinitely difficult at least
 
Conversation ended Sep 6, 2012 at 10:20.