Skip to content

Commit

Permalink
Add support for SET LOCAL and SET SESSION statements (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
airhorns authored Jan 8, 2024
1 parent 8e9ba23 commit 5b7b81c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/syntax/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ export interface ExprWhen extends PGNode {
export interface SetGlobalStatement extends PGNode {
type: 'set';
variable: Name;
scope?: string;
set: SetGlobalValue;
}
export interface SetTimezone extends PGNode {
Expand Down
1 change: 1 addition & 0 deletions src/syntax/base.ne
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ kw_interval -> %word {% notReservedKw('interval') %}
kw_hour -> %word {% notReservedKw('hour') %}
kw_minute -> %word {% notReservedKw('minute') %}
kw_local -> %word {% notReservedKw('local') %}
kw_session -> %word {% notReservedKw('session') %}
kw_prepare -> %word {% notReservedKw('prepare') %}
kw_deallocate -> %word {% notReservedKw('deallocate') %}
kw_raise -> %word {% notReservedKw('raise') %}
Expand Down
7 changes: 4 additions & 3 deletions src/syntax/simple-statements.ne
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ simplestatements_set_names -> kw_names simplestatements_set_names_val {% x => tr
simplestatements_set_names_val
-> (string) {% x => track(x, { type: 'value', value: unwrap(x[0]) }) %}

simplestatements_set_simple -> ident (%op_eq | %kw_to) simplestatements_set_val {% x => track(x, {
simplestatements_set_simple -> (kw_local | kw_session):? ident (%op_eq | %kw_to) simplestatements_set_val {% x => track(x, {
type: 'set',
variable: asName(x[0]),
set: unbox(x[2]),
variable: asName(x[1]),
scope: unwrap(x[0])?.toLowerCase(),
set: unbox(x[3]),
}) %}

simplestatements_set_val
Expand Down
20 changes: 20 additions & 0 deletions src/syntax/simple-statements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ describe('Simple statements', () => {
}
})

checkStatement(`SET LOCAL lock_timeout = '50ms'`, {
type: 'set',
variable: { name: 'lock_timeout' },
scope: 'local',
set: {
type: 'value',
value: '50ms',
}
})

checkStatement(`SET SESSION lock_timeout = '50ms'`, {
type: 'set',
variable: { name: 'lock_timeout' },
scope: 'session',
set: {
type: 'value',
value: '50ms',
}
})

checkStatement(`SET TIME ZONE INTERVAL '+00:00' HOUR TO MINUTE`, {
type: 'set timezone',
to: {
Expand Down
6 changes: 5 additions & 1 deletion src/to-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,11 @@ const visitor = astVisitor<IAstFullVisitor>(m => ({


setGlobal: g => {
ret.push('SET ', name(g.variable), ' = ');
ret.push('SET ')
if (g.scope) {
ret.push(g.scope.toUpperCase() + ' ');
}
ret.push(name(g.variable), ' = ');
visitSetVal(g.set);
},

Expand Down

0 comments on commit 5b7b81c

Please sign in to comment.