Skip to content

Commit

Permalink
refactor: ast be object when single sql, to be array when multiple st…
Browse files Browse the repository at this point in the history
…atements for all db
  • Loading branch information
taozhi8833998 committed Oct 7, 2023
1 parent 734ee38 commit 290b44e
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 36 deletions.
7 changes: 4 additions & 3 deletions pegjs/bigquery.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
7 changes: 4 additions & 3 deletions pegjs/db2.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
}

start
= __ n:(multiple_stmt / cmd_stmt / crud_stmt) {
= __ n:(multiple_stmt) {
return n
}

Expand Down Expand Up @@ -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]);
Expand Down
8 changes: 4 additions & 4 deletions pegjs/flinksql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,7 @@
}

start
= __ n:(multiple_stmt / cmd_stmt / crud_stmt) {
// => multiple_stmt | cmd_stmt | crud_stmt
= __ n:(multiple_stmt) {
return n
}

Expand Down Expand Up @@ -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> = T;
=> AstStatement<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;
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]);
Expand Down
7 changes: 4 additions & 3 deletions pegjs/hive.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
}

start
= __ n:(multiple_stmt / cmd_stmt / crud_stmt) {
= __ n:(multiple_stmt) {
return n
}

Expand Down Expand Up @@ -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]);
Expand Down
7 changes: 4 additions & 3 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
}

start
= __ n:(multiple_stmt / cmd_stmt / crud_stmt) {
= __ n:multiple_stmt {
return n
}

Expand Down Expand Up @@ -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]);
Expand Down
7 changes: 4 additions & 3 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ start
}

start_item
= __ n:(multiple_stmt / cmd_stmt / crud_stmt) {
= __ n:multiple_stmt {
return n
}

Expand Down Expand Up @@ -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]);
Expand Down
7 changes: 4 additions & 3 deletions pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ multiple_stmt
/*
// is in reality: { tableList: any[]; columnList: any[]; ast: T; }
export type AstStatement<T> = T;
=> AstStatement<crud_stmt[]> */
const cur = [head && head.ast || head];
=> AstStatement<curd_stmt | 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]);
Expand Down Expand Up @@ -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,
}
Expand Down
8 changes: 4 additions & 4 deletions pegjs/snowflake.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@
}

start
= __ n:(multiple_stmt / cmd_stmt / crud_stmt) {
// => multiple_stmt | cmd_stmt | crud_stmt
= __ n:multiple_stmt {
return n
}

Expand Down Expand Up @@ -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> = T;
=> AstStatement<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;
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]);
Expand Down
5 changes: 3 additions & 2 deletions pegjs/sqlite.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
}

start
= __ n:(multiple_stmt / cmd_stmt / crud_stmt) {
= __ n:(multiple_stmt) {
return n
}

Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions pegjs/transactsql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ start
}

start_item
= __ n:(multiple_stmt / cmd_stmt / crud_stmt) __ SEMICOLON? {
= __ n:(multiple_stmt) __ SEMICOLON? {
return n
}

Expand Down Expand Up @@ -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]);
Expand Down
10 changes: 5 additions & 5 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -670,7 +670,7 @@ describe('select', () => {
type: 'unary_expr',
operator: operator.toUpperCase(),
expr: {
tableList: ["select::null::t"],
tableList: [],
columnList: ["select::null::(.*)"],
ast: {
with: null,
Expand Down Expand Up @@ -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' },
Expand 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' },
Expand Down

0 comments on commit 290b44e

Please sign in to comment.