fix error position

This commit is contained in:
HSunboy 2019-09-25 17:04:43 +08:00
parent 9a9ccd197a
commit 966a8d31f4
5 changed files with 12 additions and 5 deletions

View File

@ -37,10 +37,11 @@ function parserSyntax(sql) {
let runSql = typeof sql == 'string' ? sql : sql.join(''); let runSql = typeof sql == 'string' ? sql : sql.join('');
const sqls = utils_1.splitSql(runSql); const sqls = utils_1.splitSql(runSql);
for (let i = 0, index = 0; i < sqls.length; i++) { 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); let err = parserSingle(sql);
if (err) { if (err) {
return err; return err;

View File

@ -1,6 +1,6 @@
{ {
"name": "dt-sql-parser", "name": "dt-sql-parser",
"version": "2.0.2", "version": "2.0.3",
"description": "sql,hive,parser ", "description": "sql,hive,parser ",
"keywords": [ "keywords": [
"hive", "hive",

View File

@ -42,10 +42,11 @@ function parserSyntax (sql: sql): SyntaxError {
let runSql = typeof sql == 'string' ? sql : sql.join(''); let runSql = typeof sql == 'string' ? sql : sql.join('');
const sqls = splitSql(runSql); const sqls = splitSql(runSql);
for (let i = 0, index = 0; i < sqls.length; i++) { 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); let err = parserSingle(sql);
if (err) { if (err) {
return err; return err;

View File

@ -76,6 +76,12 @@ describe('syntax test', () => {
const result = flinksqlParser(sql); const result = flinksqlParser(sql);
expect(result).toBeNull(); 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', () => { test('syntax error', () => {
const sql = 'select id from user.id; \nselect id from us*er.id; \nselect id from *user.id;'; const sql = 'select id from user.id; \nselect id from us*er.id; \nselect id from *user.id;';
const result = flinksqlParser(sql); const result = flinksqlParser(sql);

View File

@ -2,8 +2,7 @@ const dtSqlParser = require('../lib/index');
const flinkParser = require('../lib/lib/flinkParser').default; const flinkParser = require('../lib/lib/flinkParser').default;
const parser = dtSqlParser.parser; const parser = dtSqlParser.parser;
console.log(flinkParser(`select * from user console.log(flinkParser(`selec`))
wh1ere a`))
console.time('t') console.time('t')
const sql = `INSERT INTO TABLE STUDENT_SCORES VALUES const sql = `INSERT INTO TABLE STUDENT_SCORES VALUES
('1','111','68','69','90','CLASS1','DEPARTMENT1'), ('1','111','68','69','90','CLASS1','DEPARTMENT1'),