From 290b44ebed268cee8a0c16d1340c2ded015c2b87 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Sat, 7 Oct 2023 09:28:45 +0800 Subject: [PATCH] refactor: ast be object when single sql, to be array when multiple statements for all db --- pegjs/bigquery.pegjs | 7 ++++--- pegjs/db2.pegjs | 7 ++++--- pegjs/flinksql.pegjs | 8 ++++---- pegjs/hive.pegjs | 7 ++++--- pegjs/mariadb.pegjs | 7 ++++--- pegjs/mysql.pegjs | 7 ++++--- pegjs/postgresql.pegjs | 7 ++++--- pegjs/snowflake.pegjs | 8 ++++---- pegjs/sqlite.pegjs | 5 +++-- pegjs/transactsql.pegjs | 7 ++++--- test/select.spec.js | 10 +++++----- 11 files changed, 44 insertions(+), 36 deletions(-) diff --git a/pegjs/bigquery.pegjs b/pegjs/bigquery.pegjs index 07a59672..d363973a 100644 --- a/pegjs/bigquery.pegjs +++ b/pegjs/bigquery.pegjs @@ -222,13 +222,14 @@ } start - = __ n:(multiple_stmt / stmt) { + = __ n:(multiple_stmt) { return n } multiple_stmt - = head:stmt tail:(__ SEMICOLON __ stmt)+ { - const cur = [head && head.ast || head]; + = head:stmt tail:(__ SEMICOLON __ stmt)* { + const headAst = head && head.ast || head + const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst; for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]); diff --git a/pegjs/db2.pegjs b/pegjs/db2.pegjs index 17f037d5..53298564 100644 --- a/pegjs/db2.pegjs +++ b/pegjs/db2.pegjs @@ -199,7 +199,7 @@ } start - = __ n:(multiple_stmt / cmd_stmt / crud_stmt) { + = __ n:(multiple_stmt) { return n } @@ -233,8 +233,9 @@ crud_stmt / proc_stmts multiple_stmt - = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)+ { - const cur = [head && head.ast || head]; + = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)* { + const headAst = head && head.ast || head + const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst; for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]); diff --git a/pegjs/flinksql.pegjs b/pegjs/flinksql.pegjs index 2a8c6fbf..f30b4bac 100644 --- a/pegjs/flinksql.pegjs +++ b/pegjs/flinksql.pegjs @@ -495,8 +495,7 @@ } start - = __ n:(multiple_stmt / cmd_stmt / crud_stmt) { - // => multiple_stmt | cmd_stmt | crud_stmt + = __ n:(multiple_stmt) { return n } @@ -531,12 +530,13 @@ crud_stmt / proc_stmts multiple_stmt - = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)+ { + = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)* { /* // is in reality: { tableList: any[]; columnList: any[]; ast: T; } export type AstStatement = T; => AstStatement */ - const cur = [head && head.ast || head]; + const headAst = head && head.ast || head + const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst; for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]); diff --git a/pegjs/hive.pegjs b/pegjs/hive.pegjs index a5fbcf80..41fb9b21 100644 --- a/pegjs/hive.pegjs +++ b/pegjs/hive.pegjs @@ -200,7 +200,7 @@ } start - = __ n:(multiple_stmt / cmd_stmt / crud_stmt) { + = __ n:(multiple_stmt) { return n } @@ -233,8 +233,9 @@ crud_stmt / proc_stmts multiple_stmt - = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)+ { - const cur = [head && head.ast || head]; + = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)* { + const headAst = head && head.ast || head + const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst; for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]); diff --git a/pegjs/mariadb.pegjs b/pegjs/mariadb.pegjs index 311cf9d4..060e1a7d 100644 --- a/pegjs/mariadb.pegjs +++ b/pegjs/mariadb.pegjs @@ -208,7 +208,7 @@ } start - = __ n:(multiple_stmt / cmd_stmt / crud_stmt) { + = __ n:multiple_stmt { return n } @@ -248,8 +248,9 @@ crud_stmt / proc_stmts multiple_stmt - = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)+ { - const cur = [head && head.ast || head]; + = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)* { + const headAst = head && head.ast || head + const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst; for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]); diff --git a/pegjs/mysql.pegjs b/pegjs/mysql.pegjs index 86163ee6..928f3cbc 100644 --- a/pegjs/mysql.pegjs +++ b/pegjs/mysql.pegjs @@ -403,7 +403,7 @@ start } start_item - = __ n:(multiple_stmt / cmd_stmt / crud_stmt) { + = __ n:multiple_stmt { return n } @@ -443,8 +443,9 @@ crud_stmt / proc_stmts multiple_stmt - = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)+ { - const cur = [head && head.ast || head]; + = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)* { + const headAst = head && head.ast || head + const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst; for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]); diff --git a/pegjs/postgresql.pegjs b/pegjs/postgresql.pegjs index 138e452b..7840ce81 100644 --- a/pegjs/postgresql.pegjs +++ b/pegjs/postgresql.pegjs @@ -259,8 +259,9 @@ multiple_stmt /* // is in reality: { tableList: any[]; columnList: any[]; ast: T; } export type AstStatement = T; - => AstStatement */ - const cur = [head && head.ast || head]; + => AstStatement */ + const headAst = head && head.ast || head + const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]); @@ -632,7 +633,7 @@ create_func_opt type: 'as', declare: de && de.ast, begin: b, - expr: s.ast.flat(), + expr: Array.isArray(s.ast) ? s.ast.flat() : [s.ast], end: e && e[0], symbol: start, } diff --git a/pegjs/snowflake.pegjs b/pegjs/snowflake.pegjs index 35596347..bef425ca 100644 --- a/pegjs/snowflake.pegjs +++ b/pegjs/snowflake.pegjs @@ -203,8 +203,7 @@ } start - = __ n:(multiple_stmt / cmd_stmt / crud_stmt) { - // => multiple_stmt | cmd_stmt | crud_stmt + = __ n:multiple_stmt { return n } @@ -249,12 +248,13 @@ crud_stmt / proc_stmts multiple_stmt - = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)+ { + = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)* { /* // is in reality: { tableList: any[]; columnList: any[]; ast: T; } export type AstStatement = T; => AstStatement */ - const cur = [head && head.ast || head]; + const headAst = head && head.ast || head + const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst; for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]); diff --git a/pegjs/sqlite.pegjs b/pegjs/sqlite.pegjs index 080b71e4..a1c37bfb 100644 --- a/pegjs/sqlite.pegjs +++ b/pegjs/sqlite.pegjs @@ -199,7 +199,7 @@ } start - = __ n:(multiple_stmt / cmd_stmt / crud_stmt) { + = __ n:(multiple_stmt) { return n } @@ -239,7 +239,8 @@ crud_stmt multiple_stmt = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)* { - const cur = [head && head.ast || head]; + const headAst = head && head.ast || head + const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst; if (!tail) tail = [] for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; diff --git a/pegjs/transactsql.pegjs b/pegjs/transactsql.pegjs index d3d13368..6025469b 100644 --- a/pegjs/transactsql.pegjs +++ b/pegjs/transactsql.pegjs @@ -223,7 +223,7 @@ start } start_item - = __ n:(multiple_stmt / cmd_stmt / crud_stmt) __ SEMICOLON? { + = __ n:(multiple_stmt) __ SEMICOLON? { return n } @@ -261,8 +261,9 @@ crud_stmt / proc_stmts multiple_stmt - = head:crud_stmt tail:(__ SEMICOLON? __ crud_stmt)+ { - const cur = [head && head.ast || head]; + = head:crud_stmt tail:(__ SEMICOLON __ crud_stmt)* { + const headAst = head && head.ast || head + const cur = tail && tail.length && tail[0].length >= 4 ? [headAst] : headAst; for (let i = 0; i < tail.length; i++) { if(!tail[i][3] || tail[i][3].length === 0) continue; cur.push(tail[i][3] && tail[i][3].ast || tail[i][3]); diff --git a/test/select.spec.js b/test/select.spec.js index 9a9e6ddd..82486e19 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -495,8 +495,8 @@ describe('select', () => { { db: null, table: 't1', as: null }, { expr: { - tableList: ["select::null::t2", "select::null::t1"], - columnList: ["select::null::(.*)", "select::null::id", "select::null::col1", "select::t1::id", "select::someAlias::id"], + tableList: ["select::null::t2"], + columnList: ["select::null::(.*)", "select::null::id", "select::null::col1"], ast: { with: null, type: 'select', @@ -670,7 +670,7 @@ describe('select', () => { type: 'unary_expr', operator: operator.toUpperCase(), expr: { - tableList: ["select::null::t"], + tableList: [], columnList: ["select::null::(.*)"], ast: { with: null, @@ -811,7 +811,7 @@ describe('select', () => { database: 'postgresql' } const ast = parser.astify('SELECT DISTINCT a FROM b WHERE c = 0 GROUP BY d ORDER BY e limit all', opt) - expect(ast[0].limit).to.be.eql({ + expect(ast.limit).to.be.eql({ seperator: '', value: [ { type: 'origin', value: 'all' }, @@ -820,7 +820,7 @@ describe('select', () => { expect(parser.sqlify(ast)).to.be.equal('SELECT DISTINCT `a` FROM `b` WHERE `c` = 0 GROUP BY `d` ORDER BY `e` ASC LIMIT ALL') const offsetAst = parser.astify('SELECT DISTINCT a FROM b WHERE c = 0 GROUP BY d ORDER BY e limit all offset 100', opt); - expect(offsetAst[0].limit).eql({ + expect(offsetAst.limit).eql({ seperator: 'offset', value: [ { type: 'origin', value: 'all' },