diff --git a/lib/utils/index.js b/lib/utils/index.js index adb240f..396114a 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -25,6 +25,16 @@ function splitSql(sql) { sql += ';'; haveEnd = false; } + function pushSql(parser, sql) { + if (!haveEnd && parser.index == sql.length - 1) { + parser.sqls.push(parser.index - 1); + parser.queue = ''; + } + else { + parser.sqls.push(parser.index); + parser.queue = ''; + } + } // 处理引号 function quoteToken(parser, sql) { let queue = parser.queue; @@ -81,14 +91,7 @@ function splitSql(sql) { function splitToken(parser, sql) { let queue = parser.queue; if (queue.endsWith(';')) { - if (!haveEnd && parser.index == sql.length - 1) { - parser.sqls.push(parser.index - 1); - queue = ''; - } - else { - parser.sqls.push(parser.index); - queue = ''; - } + pushSql(parser, sql); } else { return null; @@ -106,6 +109,9 @@ function splitSql(sql) { for (let i = 0; i < tokenFuncs.length; i++) { tokenFuncs[i](parser, sql); } + if (parser.index == sql.length - 1 && parser.queue) { + pushSql(parser, sql); + } } return parser.sqls; } diff --git a/package.json b/package.json index 2a654c7..29f1ab6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dt-sql-parser", - "version": "2.0.3", + "version": "2.0.4", "description": "sql,hive,parser ", "keywords": [ "hive", @@ -13,7 +13,7 @@ "build:parse": "pegjs -o core/astParser.js peg/nquery.pegjs ", "build:filter": "pegjs -o core/comment.js peg/comment.pegjs ", "build:syntax": "sh ./jison/hue-sql-syntax.sh ", - "build": "rm -rf lib && tsc && jest", + "build": "npm test && rm -rf lib && tsc", "test": "jest" }, "author": "xiaokang", diff --git a/src/utils/index.ts b/src/utils/index.ts index 865c635..2c20a6c 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -31,6 +31,15 @@ function splitSql (sql: string) { queue: string; sqls: number[]; } + function pushSql (parser: splitParser, sql: string) { + if (!haveEnd && parser.index == sql.length - 1) { + parser.sqls.push(parser.index - 1); + parser.queue = ''; + } else { + parser.sqls.push(parser.index); + parser.queue = ''; + } + } // 处理引号 function quoteToken (parser: splitParser, sql: string): string { let queue = parser.queue; @@ -81,13 +90,7 @@ function splitSql (sql: string) { function splitToken (parser: splitParser, sql: string): string { let queue = parser.queue; if (queue.endsWith(';')) { - if (!haveEnd && parser.index == sql.length - 1) { - parser.sqls.push(parser.index - 1); - queue = ''; - } else { - parser.sqls.push(parser.index); - queue = ''; - } + pushSql(parser, sql) } else { return null; @@ -105,6 +108,9 @@ function splitSql (sql: string) { for (let i = 0; i < tokenFuncs.length; i++) { tokenFuncs[i](parser, sql); } + if (parser.index == sql.length - 1 && parser.queue) { + pushSql(parser, sql) + } } return parser.sqls; } diff --git a/test/index.test.ts b/test/index.test.ts index 11f83b7..536b110 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -91,6 +91,39 @@ describe('syntax test', () => { }); expect(result.token.start).toBe(42); expect(result.token.stop).toBe(42); - }) + const sql2 = `CREATE TABLE MyTable( + message.after.id int AS id, + message.after.userid varchar AS userid, + message.after.username varchar AS username, + message.after.prodid varchar AS prodid, + message.after.price double AS price, + message.after.amount int AS amount, + message.after.discount double AS discount, + message.after.tm timestamp AS tm, + WATERMARK FOR tm AS withOffset(tm,1000) + )WITH( + 'type' ='kafka11, + topic ='1' + ); + + CREATE TABLE MyResult( + a double, + b timestamp, + c timestamp + )WITH( + type ='mysql', + url ='jdbc:mysql://1:3306/yanxi?charset=utf8' + ); + + insert into MyResult + select + sum(price * amount * discount) as a, + TUMBLE_START( ROWTIME, INTERVAL '30' SECOND) as b + from MyTable + group by + TUMBLE( ROWTIME, INTERVAL '30' SECOND);`; + const result2 = flinksqlParser(sql2); + expect(result2).not.toBeNull(); + }); }) }) \ No newline at end of file diff --git a/test/utils/index.test.ts b/test/utils/index.test.ts index 83fd77f..85f1ba6 100644 --- a/test/utils/index.test.ts +++ b/test/utils/index.test.ts @@ -16,5 +16,33 @@ describe('utils', () => { const result = utils.splitSql(sql); expect(result).toEqual([34, 65]) }); + test('error sql', () => { + const sql = `CREATE TABLE MyResult( + a double, + b timestamp, + c timestamp + )WITH( + type ='mysql, + url ='jdbc:mysql://1.1.1.1:3306/hi?charset=utf8', + userName ='name', + password ='123', + tableName ='user' + );`; + const result = utils.splitSql(sql); + expect(result).toEqual([337]) + const sql2 = `CREATE TABLE MyResult( + a double, + b timestamp, + c timestamp + )WITH( + type ='mysql, + url ='jdbc:mysql://1.1.1.1:3306/hi?charset=utf8', + userName ='name', + password ='123', + tableName ='user' + )`; + const result2 = utils.splitSql(sql2); + expect(result2).toEqual([336]) + }); }) }) \ No newline at end of file