diff --git a/pegjs/postgresql.pegjs b/pegjs/postgresql.pegjs index e90d6cea..91234548 100644 --- a/pegjs/postgresql.pegjs +++ b/pegjs/postgresql.pegjs @@ -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', diff --git a/src/assign.js b/src/assign.js index c78c9b02..4678ebca 100644 --- a/src/assign.js +++ b/src/assign.js @@ -1,5 +1,5 @@ import { exprToSQL } from './expr' -import { hasVal } from './util' +import { hasVal, toUpper } from './util' function assignToSQL(expr) { /** @type {Object} */ @@ -7,7 +7,7 @@ function assignToSQL(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 { diff --git a/test/postgres.spec.js b/test/postgres.spec.js index ce0252b1..8d7710b2 100644 --- a/test/postgres.spec.js +++ b/test/postgres.spec.js @@ -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)