Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you need to parse any Sql scripts with comments and string values with 'go' ("smth go smth") and etc. you can use <a href="http://gplex.codeplex.com/" rel="nofollow" title="gplex">gplex</a> tool. Gplex rules for sql script parsing:</p> <pre><code>%namespace LexScanner %option verbose, summary, noparser, unicode %x QUOTE %x COMMENT %{ static string line = ""; static List&lt;string&gt; butch = new List&lt;string&gt;(); enum TokenType { SL_COMMENT, ML_COMMENT, STRING, WORD, OTHER, ending }; %} dotchr [^\r\n] eol (\r\n?|\n) %% \-\-[^\n]*$ { add(yytext, TokenType.SL_COMMENT); } \/\* { add(yytext, TokenType.ML_COMMENT); BEGIN(COMMENT); } &lt;COMMENT&gt;\*\/ { add(yytext, TokenType.ML_COMMENT); BEGIN(INITIAL); } &lt;COMMENT&gt;[^\*]+ { add(yytext, TokenType.ML_COMMENT); } &lt;COMMENT&gt;\* { add(yytext, TokenType.ML_COMMENT); } \' { add(yytext, TokenType.STRING); BEGIN(QUOTE); } &lt;QUOTE&gt;\'\' { add(yytext, TokenType.STRING); } &lt;QUOTE&gt;[^\']+ { add(yytext, TokenType.STRING); } &lt;QUOTE&gt;\' { add(yytext, TokenType.STRING); BEGIN(INITIAL); } [gG][oO] { push(); } [a-zA-Z0-9]+ { add(yytext, TokenType.WORD); } . { add(yytext, TokenType.OTHER); } \r?\n { add(yytext, TokenType.OTHER); } &lt;&lt;EOF&gt;&gt; { push(); } %% </code></pre> <p>Then you generate C# class and use it.</p> <p><strong>EDIT:</strong><br/> Some more comments how to use it.<br/> Functions <code>add(string text, TokenType token)</code> and <code>push()</code> - what to do with parsed string. add() function collects parsed strings between GO keywords and writes results to output file (just to control): </p> <pre><code>private void add(string text, TokenType token) { //write to the file for output control (for test only) using (StreamWriter str = new StreamWriter("C:\\temp\\temp.txt", true)) { str.WriteLine(token + " : " + text); } line += text; } </code></pre> <p>push() collects butch strings for execution:</p> <pre><code>private void push() { //write to the file for output control (for test only) using (StreamWriter str = new StreamWriter("C:\\temp\\butch.txt", true)) { str.WriteLine("GO: " + line); } butch.Add(line); line = ""; } </code></pre> <p>To use this class from C# code your should specify entry point. For example:</p> <pre><code> public static List&lt;string&gt; ParseFile(String fileToParse) { int tok; Scanner scnr = new Scanner(); scnr.SetSource(fileToParse, 0); do { tok = scnr.yylex(); } while (tok &gt; (int)Tokens.EOF); return butch; } </code></pre> <p>Or define a Main function to use it as standalone application.</p> <p>All the above code should be placed in the .lex file. The file sqlparser.cs is created by invoking from command line:</p> <pre><code>gplex sqlparser.lex </code></pre> <p>Gplex has a good documentation and examples how to use it. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload