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('');
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;

View File

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

View File

@ -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;

View File

@ -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);

View File

@ -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'),