Skip to content

Commit 7fda7cf

Browse files
Merge pull request #2213 from taozhi8833998/feat-set-time-zone-pg
feat: support set time zone in pg
2 parents ee0121a + cb1225c commit 7fda7cf

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

pegjs/postgresql.pegjs

+21-1
Original file line numberDiff line numberDiff line change
@@ -5639,8 +5639,28 @@ assign_stmt_list
56395639
return createList(head, tail);
56405640
}
56415641

5642+
assign_stmt_timezone
5643+
= KW_TIME __ 'ZONE'i __ e:interval_expr __ KW_TO __ r:interval_unit {
5644+
// => { type: 'assign'; left: expr_list; symbol: 'to'; right: interval_unit; }
5645+
return {
5646+
type: 'assign',
5647+
left: { type: 'expr_list', value: [{ type: 'origin', value: 'time zone' }, e], separator: ' ' },
5648+
symbol: 'to',
5649+
right: { type: 'origin', value: r }
5650+
};
5651+
}
5652+
/ KW_TIME __ 'ZONE'i __ s:KW_TO? __ e:(literal_numeric / literal_string / KW_LOCAL / 'default'i) {
5653+
// => { type: 'assign'; left: literal_string; symbol?: 'to'; right: literal; }
5654+
return {
5655+
type: 'assign',
5656+
left: { type: 'origin', value: 'time zone' },
5657+
symbol: s ? 'to' : null,
5658+
right: typeof e === 'string' ? { type: 'origin', value: e } : e
5659+
};
5660+
}
56425661
assign_stmt
5643-
= va:(var_decl / without_prefix_var_decl) __ s:(KW_ASSIGN / KW_ASSIGIN_EQUAL / KW_TO) __ e:proc_expr {
5662+
= assign_stmt_timezone
5663+
/ va:(var_decl / without_prefix_var_decl) __ s:(KW_ASSIGN / KW_ASSIGIN_EQUAL / KW_TO) __ e:proc_expr {
56445664
// => { type: 'assign'; left: var_decl | without_prefix_var_decl; symbol: ':=' | '='; right: proc_expr; }
56455665
return {
56465666
type: 'assign',

src/assign.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { exprToSQL } from './expr'
2-
import { hasVal } from './util'
2+
import { hasVal, toUpper } from './util'
33

44
function assignToSQL(expr) {
55
/** @type {Object} */
66
const { left, right, symbol, keyword } = expr
77
left.keyword = keyword
88
const leftVar = exprToSQL(left)
99
const rightVal = exprToSQL(right)
10-
return [leftVar, symbol, rightVal].filter(hasVal).join(' ')
10+
return [leftVar, toUpper(symbol), rightVal].filter(hasVal).join(' ')
1111
}
1212

1313
export {

test/postgres.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,20 @@ describe('Postgres', () => {
16981698

16991699
neatlyNestTestedSQL(SQL_LIST)
17001700

1701+
describe('set time zone', () => {
1702+
it('should support set time zone', () => {
1703+
let sql = "SET TIME ZONE INTERVAL '00:00' HOUR TO MINUTE;"
1704+
expect(getParsedSql(sql, opt)).to.equal(sql.slice(0, -1))
1705+
sql = "SET TIME ZONE 'America/Los_Angeles';"
1706+
expect(getParsedSql(sql, opt)).to.equal(sql.slice(0, -1))
1707+
sql = 'SET TIME ZONE -8;'
1708+
expect(getParsedSql(sql, opt)).to.equal(sql.slice(0, -1))
1709+
sql = 'SET TIME ZONE LOCAL;'
1710+
expect(getParsedSql(sql, opt)).to.equal(sql.slice(0, -1))
1711+
sql = 'SET TIME ZONE DEFAULT;'
1712+
expect(getParsedSql(sql, opt)).to.equal(sql.slice(0, -1))
1713+
})
1714+
})
17011715
describe('tables to sql', () => {
17021716
it('should parse object tables', () => {
17031717
const ast = parser.astify(SQL_LIST[100].sql[0], opt)

0 commit comments

Comments
 (0)