taozhi8833998 / node-sql-parser

Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList and convert it back to SQL
https://taozhi8833998.github.io/node-sql-parser/
Apache License 2.0
798 stars 180 forks source link

TSQL comment not supported #292

Closed esetnik closed 4 years ago

esetnik commented 4 years ago

Describe the bug When using a comment with transactsql the parser throws an error

Database Engine transactsql

To Reproduce -the SQL that be parsed

const { Parser } = require("node-sql-parser");
const parser = new Parser();
const ast = parser.astify(`-- +migrate Up`, { database: "transactsql" });
console.log(ast);

-the node-sql-parser version 1.9.2 -the node version 12.16.3

Expected behavior No error since this is valid tsql

Screenshots

Additional context

/Users/xxx/xxx/xxx/node-sql-parser/node_modules/node-sql-parser/build/transactsql.js:20031
      throw peg$buildStructuredError(
      ^
peg$SyntaxError: Expected "#", "$", "--", "/*", ";", "@", "@@", "SELECT", "WITH", "return", [ \t\n\r], or [A-Za-z_] but end of input found.
    at peg$buildStructuredError (/Users/ethan/work/projects/node-sql-parser/node_modules/node-sql-parser/build/transactsql.js:1953:14)
    at Object.peg$parse [as transactsql] (/Users/ethan/work/projects/node-sql-parser/node_modules/node-sql-parser/build/transactsql.js:20031:13)
    at Parser.parse (/Users/ethan/work/projects/node-sql-parser/node_modules/node-sql-parser/lib/parser.js:49:52)
    at Parser.astify (/Users/ethan/work/projects/node-sql-parser/node_modules/node-sql-parser/lib/parser.js:34:28)
    at Object.<anonymous> (/Users/ethan/work/projects/node-sql-parser/index.js:3:20)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
  expected: [
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: '/*', ignoreCase: false },
    { type: 'literal', text: '--', ignoreCase: false },
    { type: 'literal', text: '#', ignoreCase: false },
    { type: 'literal', text: 'WITH', ignoreCase: true },
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: '/*', ignoreCase: false },
    { type: 'literal', text: '--', ignoreCase: false },
    { type: 'literal', text: '#', ignoreCase: false },
    { type: 'literal', text: 'WITH', ignoreCase: true },
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: '/*', ignoreCase: false },
    { type: 'literal', text: '--', ignoreCase: false },
    { type: 'literal', text: '#', ignoreCase: false },
    { type: 'literal', text: 'SELECT', ignoreCase: true },
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: '/*', ignoreCase: false },
    { type: 'literal', text: '--', ignoreCase: false },
    { type: 'literal', text: '#', ignoreCase: false },
    { type: 'literal', text: '@@', ignoreCase: false },
    { type: 'literal', text: '@', ignoreCase: false },
    { type: 'literal', text: '$', ignoreCase: false },
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: 'return', ignoreCase: true },
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: '/*', ignoreCase: false },
    { type: 'literal', text: '--', ignoreCase: false },
    { type: 'literal', text: '#', ignoreCase: false },
    { type: 'literal', text: ';', ignoreCase: false },
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: '/*', ignoreCase: false },
    { type: 'literal', text: '--', ignoreCase: false },
    { type: 'literal', text: '#', ignoreCase: false },
    { type: 'literal', text: 'WITH', ignoreCase: true },
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: '/*', ignoreCase: false },
    { type: 'literal', text: '--', ignoreCase: false },
    { type: 'literal', text: '#', ignoreCase: false },
    { type: 'literal', text: 'WITH', ignoreCase: true },
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: '/*', ignoreCase: false },
    { type: 'literal', text: '--', ignoreCase: false },
    { type: 'literal', text: '#', ignoreCase: false },
    { type: 'literal', text: 'SELECT', ignoreCase: true },
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: '/*', ignoreCase: false },
    { type: 'literal', text: '--', ignoreCase: false },
    { type: 'literal', text: '#', ignoreCase: false },
    { type: 'literal', text: '@@', ignoreCase: false },
    { type: 'literal', text: '@', ignoreCase: false },
    { type: 'literal', text: '$', ignoreCase: false },
    {
      type: 'class',
      parts: [Array],
      inverted: false,
      ignoreCase: false
    },
    { type: 'literal', text: 'return', ignoreCase: true }
  ],
  found: null,
  location: {
    start: { offset: 14, line: 1, column: 15 },
    end: { offset: 14, line: 1, column: 15 }
  }
}
esetnik commented 4 years ago

Related to #174

taozhi8833998 commented 4 years ago

comment only???

taozhi8833998 commented 4 years ago

@esetnik sql is required in content.

try

-- +migrate Up .  
SELECT *  
FROM Person.Address  
ORDER BY PostalCode ASC; -- SQL is required in context
-- that is the default.
taozhi8833998 commented 4 years ago

@esetnik I will close the issue since the parsed content don't contain any valid SQL. If you have any more comments on this, you could reopen it, and discuss it.

esetnik commented 4 years ago

@taozhi8833998 thank you for your feedback. In Microsoft SQL Server management studio you can create a "comment-only" SQL statement and execute it without issue. So I believe this issue should be reopened as the error is not consistent with the official tooling built by Microsoft. Thanks again.

image

esetnik commented 4 years ago

Also I think it's still not working with the following CREATE TABLE. This is showing an error for me.

-- +migrate Up
CREATE TABLE test (
  id BIGINT NOT NULL PRIMARY KEY IDENTITY(1, 1),
);
taozhi8833998 commented 4 years ago

ok, I will check it again later. Thanks for ur feedback.

taozhi8833998 commented 4 years ago

try 1.10.1

esetnik commented 4 years ago

Working well, thanks!