Sfoglia il codice sorgente

HUE-6361 [editor] Add statement parser support for ; in quoted values

Johan Ahlen 8 anni fa
parent
commit
5910ef7d42

+ 7 - 4
desktop/core/src/desktop/static/desktop/js/autocomplete/jison/sqlStatementsParser.jison

@@ -18,9 +18,12 @@
 %options flex
 %%
 
-\s                                              { /* skip whitespace */ }
-<<EOF>>                                         { return 'EOF'; }
-[^;]*[;]?                                       { return 'STATEMENT'; }
+\s                                                                    { /* skip whitespace */ }
+'--'.*                                                                { /* skip comments */ }
+[/][*][^*]*[*]+([^/*][^*]*[*]+)*[/]                                   { /* skip comments */ }
+
+<<EOF>>                                                               { return 'EOF'; }
+([^;"'`]|(["][^"]*["])|(['][^']*['])|([`][^`]*[`]))*[;]?              { return 'STATEMENT'; }
 
 /lex
 
@@ -40,7 +43,7 @@ SqlStatementsParser
  ;
 
 Statements
- : 'STATEMENT'                                  --> [{ type: 'statement', statement: $1, location: @1 }]
+ : 'STATEMENT'                                                        --> [{ type: 'statement', statement: $1, location: @1 }]
  | Statements 'STATEMENT'
    {
      $1.push({ type: 'statement', statement: $2, location: @2 });

+ 9 - 5
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlStatementsParser.js

@@ -599,16 +599,20 @@ var YYSTATE=YY_START;
 switch($avoiding_name_collisions) {
 case 0: /* skip whitespace */ 
 break;
-case 1: return 5; 
+case 1: /* skip comments */ 
 break;
-case 2: return 6; 
+case 2: /* skip comments */ 
 break;
-case 3:console.log(yy_.yytext);
+case 3: return 5; 
+break;
+case 4: return 6; 
+break;
+case 5:console.log(yy_.yytext);
 break;
 }
 },
-rules: [/^(?:\s)/,/^(?:$)/,/^(?:[^;]*[;]?)/,/^(?:.)/],
-conditions: {"INITIAL":{"rules":[0,1,2,3],"inclusive":true}}
+rules: [/^(?:\s)/,/^(?:--.*)/,/^(?:[\/][*][^*]*[*]+([^\/*][^*]*[*]+)*[\/])/,/^(?:$)/,/^(?:([^;"'`]|(["][^"]*["])|(['][^']*['])|([`][^`]*[`]))*[;]?)/,/^(?:.)/],
+conditions: {"INITIAL":{"rules":[0,1,2,3,4,5],"inclusive":true}}
 });
 return lexer;
 })();

+ 42 - 2
desktop/core/src/desktop/static/desktop/spec/autocomplete/sqlStatementsParserSpec.js

@@ -63,17 +63,57 @@
           { type: 'statement', statement: 'select * \nfrom bla;', location: { first_line: 1, last_line: 2, first_column: 0,  last_column: 9 } },
           { type: 'statement', statement: '\r\nselect * from ble;', location: { first_line: 2, last_line: 3, first_column: 9,  last_column: 18 } }
         ]
+      }, {
+        id: 7,
+        statements: 'select * from bla where x = ";";',
+        expectedResult: [
+          { type: 'statement', statement: 'select * from bla where x = ";";', location: { first_line: 1, last_line: 1, first_column: 0,  last_column: 32 } }
+        ]
+      }, {
+        id: 8,
+        statements: 'select * from bla where x = \';\';\n\nSELECT bla FROM foo WHERE y = `;` AND true = false;',
+        expectedResult: [
+          { type: 'statement', statement: 'select * from bla where x = \';\';', location: { first_line: 1, last_line: 1, first_column: 0,  last_column: 32 } },
+          { type: 'statement', statement: '\n\nSELECT bla FROM foo WHERE y = `;` AND true = false;', location: { first_line: 1, last_line: 3, first_column: 32,  last_column: 51 } }
+        ]
+      }, {
+        id: 9,
+        statements: 'select * from bla where x = "; AND boo = 1;\n\nUSE db',
+        expectedResult: [
+          { type: 'statement', statement: 'select * from bla where x = ', location: { first_line: 1, last_line: 1, first_column: 0,  last_column: 28 } },
+          { type: 'statement', statement: ';', location: { first_line: 1, last_line: 1, first_column: 29,  last_column: 30 } },
+          { type: 'statement', statement: ' AND boo = 1;', location: { first_line: 1, last_line: 1, first_column: 30,  last_column: 43 } },
+          { type: 'statement', statement: '\n\nUSE db', location: { first_line: 1, last_line: 3, first_column: 43,  last_column: 6 } }
+        ]
+      }, {
+        id: 10,
+        statements: '--- Some comment with ; ; \nselect * from bla where x = ";";',
+        expectedResult: [
+          { type: 'statement', statement: '\nselect * from bla where x = ";";', location: { first_line: 1, last_line: 2, first_column: 26,  last_column: 32 } }
+        ]
+      }, {
+        id: 11,
+        statements: 'select *\n-- bla\n from bla;',
+        expectedResult: [
+          { type: 'statement', statement: 'select *\n-- bla\n from bla;', location: { first_line: 1, last_line: 3, first_column: 0,  last_column: 10 } }
+        ]
+      }, {
+        id: 12,
+        statements: 'select *\n/* bla \n\n*/\n from bla;',
+        expectedResult: [
+          { type: 'statement', statement: 'select *\n/* bla \n\n*/\n from bla;', location: { first_line: 1, last_line: 5, first_column: 0,  last_column: 10 } }
+        ]
       }
     ];
 
     splitTests.forEach(function (splitTest) {
       it('should split correctly, test ' + splitTest.id, function () {
         try {
-        var result = sqlStatementsParser.parse(splitTest.statements);
+          var result = sqlStatementsParser.parse(splitTest.statements);
+          expect(result).toEqual(splitTest.expectedResult);
         } catch (error) {
           fail('Got error');
         }
-        expect(result).toEqual(splitTest.expectedResult);
       });
     });
   });