Skip to content

Commit

Permalink
feat: support set time zone in pg
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Nov 6, 2024
1 parent 1438d8c commit cb1225c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
22 changes: 21 additions & 1 deletion pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -5639,8 +5639,28 @@ assign_stmt_list
return createList(head, tail);
}

assign_stmt_timezone
= KW_TIME __ 'ZONE'i __ e:interval_expr __ KW_TO __ r:interval_unit {
// => { type: 'assign'; left: expr_list; symbol: 'to'; right: interval_unit; }
return {
type: 'assign',
left: { type: 'expr_list', value: [{ type: 'origin', value: 'time zone' }, e], separator: ' ' },
symbol: 'to',
right: { type: 'origin', value: r }
};
}
/ KW_TIME __ 'ZONE'i __ s:KW_TO? __ e:(literal_numeric / literal_string / KW_LOCAL / 'default'i) {
// => { type: 'assign'; left: literal_string; symbol?: 'to'; right: literal; }
return {
type: 'assign',
left: { type: 'origin', value: 'time zone' },
symbol: s ? 'to' : null,
right: typeof e === 'string' ? { type: 'origin', value: e } : e
};
}
assign_stmt
= va:(var_decl / without_prefix_var_decl) __ s:(KW_ASSIGN / KW_ASSIGIN_EQUAL / KW_TO) __ e:proc_expr {
= assign_stmt_timezone
/ 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',
Expand Down
4 changes: 2 additions & 2 deletions src/assign.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { exprToSQL } from './expr'
import { hasVal } from './util'
import { hasVal, toUpper } from './util'

function assignToSQL(expr) {
/** @type {Object} */
const { left, right, symbol, keyword } = expr
left.keyword = keyword
const leftVar = exprToSQL(left)
const rightVal = exprToSQL(right)
return [leftVar, symbol, rightVal].filter(hasVal).join(' ')
return [leftVar, toUpper(symbol), rightVal].filter(hasVal).join(' ')
}

export {
Expand Down
14 changes: 14 additions & 0 deletions test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,20 @@ describe('Postgres', () => {

neatlyNestTestedSQL(SQL_LIST)

describe('set time zone', () => {
it('should support set time zone', () => {
let sql = "SET TIME ZONE INTERVAL '00:00' HOUR TO MINUTE;"
expect(getParsedSql(sql, opt)).to.equal(sql.slice(0, -1))
sql = "SET TIME ZONE 'America/Los_Angeles';"
expect(getParsedSql(sql, opt)).to.equal(sql.slice(0, -1))
sql = 'SET TIME ZONE -8;'
expect(getParsedSql(sql, opt)).to.equal(sql.slice(0, -1))
sql = 'SET TIME ZONE LOCAL;'
expect(getParsedSql(sql, opt)).to.equal(sql.slice(0, -1))
sql = 'SET TIME ZONE DEFAULT;'
expect(getParsedSql(sql, opt)).to.equal(sql.slice(0, -1))
})
})
describe('tables to sql', () => {
it('should parse object tables', () => {
const ast = parser.astify(SQL_LIST[100].sql[0], opt)
Expand Down

0 comments on commit cb1225c

Please sign in to comment.