Skip to content

Commit

Permalink
Merge pull request #1761 from taozhi8833998/feat-set-to-pg
Browse files Browse the repository at this point in the history
feat: support set to value in pg
  • Loading branch information
taozhi8833998 authored Jan 19, 2024
2 parents b042c3d + f416e21 commit d2a31a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 6 additions & 3 deletions pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,13 @@
}

start
= __ n:(create_function_stmt / multiple_stmt) {
= b:('begin'i __ SEMICOLON) __ n:(create_function_stmt / multiple_stmt) __ c:('commit'i __ SEMICOLON) {
// => multiple_stmt
n.ast.transactions = true
return n
}
/ create_function_stmt
/ multiple_stmt

cmd_stmt
= drop_stmt
Expand Down Expand Up @@ -5033,12 +5036,12 @@ proc_stmt
}

assign_stmt
= va:(var_decl / without_prefix_var_decl) __ s: (KW_ASSIGN / KW_ASSIGIN_EQUAL) __ e:proc_expr {
= va:(var_decl / without_prefix_var_decl) __ s:(KW_ASSIGN / KW_ASSIGIN_EQUAL / KW_TO) __ e:proc_expr {
// => { type: 'assign'; left: var_decl | without_prefix_var_decl; symbol: ':=' | '='; right: proc_expr; }
return {
type: 'assign',
left: va,
symbol: s,
symbol: Array.isArray(s) ? s[0] : s,
right: e
};
}
Expand Down
5 changes: 3 additions & 2 deletions src/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function goToSQL(stmt) {
}

export default function astToSQL(ast) {
if (ast.go === 'go') return goToSQL(ast)
return toSQL(ast)
const sql = ast.go === 'go' ? goToSQL(ast) : toSQL(ast)
if (ast.transactions) return `BEGIN;\n${sql};\nCOMMIT;`
return sql
}
11 changes: 11 additions & 0 deletions test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,17 @@ describe('Postgres', () => {
'SELECT "item" FROM "public"."orders" WHERE "ID" = 1 INTERSECT SELECT "sku" FROM "public"."inventory" WHERE "ID" = 1'
]
},
{
title: 'set to in transactions',
sql: [
`BEGIN;
SET search_path TO ht_hyt;
COMMIT;`,
`BEGIN;
SET search_path TO ht_hyt;
COMMIT;`,
]
}
]
function neatlyNestTestedSQL(sqlList){
sqlList.forEach(sqlInfo => {
Expand Down

0 comments on commit d2a31a8

Please sign in to comment.