Skip to content

Commit

Permalink
feat: support transactions in pg
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Jan 18, 2024
1 parent a5be053 commit f416e21
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
5 changes: 4 additions & 1 deletion 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
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
}
12 changes: 8 additions & 4 deletions test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1379,10 +1379,14 @@ describe('Postgres', () => {
]
},
{
title: 'set to',
sql: [
'SET search_path TO ht_hyt',
'SET search_path TO ht_hyt'
title: 'set to in transactions',
sql: [
`BEGIN;
SET search_path TO ht_hyt;
COMMIT;`,
`BEGIN;
SET search_path TO ht_hyt;
COMMIT;`,
]
}
]
Expand Down

0 comments on commit f416e21

Please sign in to comment.