From 966a8d31f4ea2253186cbf680e7440660592d560 Mon Sep 17 00:00:00 2001 From: HSunboy Date: Wed, 25 Sep 2019 17:04:43 +0800 Subject: [PATCH] fix error position --- lib/lib/flinkParser.js | 3 ++- package.json | 2 +- src/lib/flinkParser.ts | 3 ++- test/index.test.ts | 6 ++++++ test/t.js | 3 +-- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/lib/flinkParser.js b/lib/lib/flinkParser.js index 2312fa5..45b6868 100644 --- a/lib/lib/flinkParser.js +++ b/lib/lib/flinkParser.js @@ -37,10 +37,11 @@ function parserSyntax(sql) { let runSql = typeof sql == 'string' ? sql : sql.join(''); const sqls = utils_1.splitSql(runSql); for (let i = 0, index = 0; i < sqls.length; i++) { + let end = runSql[sqls[i]] == ';' ? sqls[i] : sqls[i] + 1; /** * 这边不取分号 */ - let sql = new Array(index).fill(' ').join('') + runSql.substring(index, sqls[i]); + let sql = new Array(index).fill(' ').join('') + runSql.substring(index, end); let err = parserSingle(sql); if (err) { return err; diff --git a/package.json b/package.json index 3384f01..2a654c7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dt-sql-parser", - "version": "2.0.2", + "version": "2.0.3", "description": "sql,hive,parser ", "keywords": [ "hive", diff --git a/src/lib/flinkParser.ts b/src/lib/flinkParser.ts index d7d568f..5d9b819 100644 --- a/src/lib/flinkParser.ts +++ b/src/lib/flinkParser.ts @@ -42,10 +42,11 @@ function parserSyntax (sql: sql): SyntaxError { let runSql = typeof sql == 'string' ? sql : sql.join(''); const sqls = splitSql(runSql); for (let i = 0, index = 0; i < sqls.length; i++) { + let end = runSql[sqls[i]] == ';' ? sqls[i] : sqls[i] + 1 /** * 这边不取分号 */ - let sql = new Array(index).fill(' ').join('') + runSql.substring(index, sqls[i]); + let sql = new Array(index).fill(' ').join('') + runSql.substring(index, end); let err = parserSingle(sql); if (err) { return err; diff --git a/test/index.test.ts b/test/index.test.ts index c1a13d9..11f83b7 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -76,6 +76,12 @@ describe('syntax test', () => { const result = flinksqlParser(sql); expect(result).toBeNull(); }); + test('position', () => { + const sql = `selec`; + const result = flinksqlParser(sql); + expect(result.token.start).toBe(0); + expect(result.token.stop).toBe(4); + }); test('syntax error', () => { const sql = 'select id from user.id; \nselect id from us*er.id; \nselect id from *user.id;'; const result = flinksqlParser(sql); diff --git a/test/t.js b/test/t.js index bc8e3be..53d0eae 100644 --- a/test/t.js +++ b/test/t.js @@ -2,8 +2,7 @@ const dtSqlParser = require('../lib/index'); const flinkParser = require('../lib/lib/flinkParser').default; const parser = dtSqlParser.parser; -console.log(flinkParser(`select * from user - wh1ere a`)) +console.log(flinkParser(`selec`)) console.time('t') const sql = `INSERT INTO TABLE STUDENT_SCORES VALUES ('1','111','68','69','90','CLASS1','DEPARTMENT1'),