Skip to content

Commit 09c26a7

Browse files
committed
fix: set and show support quoted string in pg
1 parent 0fd19f4 commit 09c26a7

5 files changed

Lines changed: 51 additions & 14 deletions

File tree

ast/postgresql.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ export type call_stmt = AstStatement<call_stmt_node>;
452452

453453
export interface show_stmt_node {
454454
type: 'show';
455-
keyword: 'tables';
455+
keyword: 'tables' | 'var';
456+
var?: without_prefix_var_decl;
456457
}
457458

458459
export type show_stmt = AstStatement<show_stmt_node>;
@@ -507,7 +508,9 @@ export type array_index = { brackets: boolean, number: number };
507508

508509
export type expr_item = (expr || binary_expr) & { array_index: array_index };
509510

510-
export type column_list_item = { expr: expr; as: null; } | { type: 'cast'; expr: expr; symbol: '::'; target: data_type; as?: null; } | { type: 'star_ref'; expr: column_ref; as: null; } | { type: 'expr'; expr: expr; as?: alias_clause; };
511+
export type cast_data_type = data_type & { quoted?: string };
512+
513+
export type column_list_item = { expr: expr; as: null; } | { type: 'cast'; expr: expr; symbol: '::'; target: cast_data_type; as?: null; } | { type: 'star_ref'; expr: column_ref; as: null; } | { type: 'expr'; expr: expr; as?: alias_clause; };
511514

512515

513516

@@ -1399,7 +1402,7 @@ export type var_decl_list = var_decl[];
13991402

14001403
export type var_decl = { type: 'var'; name: string; prefix: string; suffix: string; }; | without_prefix_var_decl & { type: 'var'; prefix: string; };;
14011404

1402-
export type without_prefix_var_decl = { type: 'var'; prefix: string; name: ident_name; members: mem_chain; };
1405+
export type without_prefix_var_decl = { type: 'var'; prefix: string; name: ident_name; members: mem_chain; quoted: string | null };
14031406

14041407
export type mem_chain = ident_name[];;
14051408

pegjs/postgresql.pegjs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,8 @@ show_stmt
15651565
/*
15661566
export interface show_stmt_node {
15671567
type: 'show';
1568-
keyword: 'tables';
1568+
keyword: 'tables' | 'var';
1569+
var?: without_prefix_var_decl;
15691570
}
15701571
=> AstStatement<show_stmt_node>
15711572
*/
@@ -1577,6 +1578,18 @@ show_stmt
15771578
}
15781579
}
15791580
}
1581+
/ KW_SHOW __ c:without_prefix_var_decl {
1582+
return {
1583+
// => AstStatement<show_stmt_node>
1584+
tableList: Array.from(tableList),
1585+
columnList: columnListTableAlias(columnList),
1586+
ast: {
1587+
type: 'show',
1588+
keyword: 'var',
1589+
var: c,
1590+
}
1591+
}
1592+
}
15801593

15811594
deallocate_stmt
15821595
= KW_DEALLOCATE __ p:('PREPARE'i)? __ i:(ident_name / KW_ALL) {
@@ -1776,12 +1789,12 @@ expr_item
17761789
}
17771790

17781791
cast_data_type
1779-
// => data_type & { quoted?: string }
1780-
= '"' t: data_type '"' {
1781-
t.quoted = '"'
1792+
= p:'"'? t: data_type s:'"'? {
1793+
// => data_type & { quoted?: string }
1794+
if ((p && !s) || (!p && s)) throw new Error('double quoted not match')
1795+
if (p && s) t.quoted = '"'
17821796
return t
17831797
}
1784-
/ data_type
17851798

17861799
column_list_item
17871800
= c:string_constants_escape {
@@ -3926,14 +3939,16 @@ var_decl
39263939
}
39273940

39283941
without_prefix_var_decl
3929-
= name:ident_name m:mem_chain {
3930-
// => { type: 'var'; prefix: string; name: ident_name; members: mem_chain; }
3942+
= p:'"'? name:ident_name m:mem_chain s:'"'? {
3943+
// => { type: 'var'; prefix: string; name: ident_name; members: mem_chain; quoted: string | null }
39313944
//push for analysis
3945+
if ((p && !s) || (!p && s)) throw new Error('double quoted not match')
39323946
varList.push(name);
39333947
return {
39343948
type: 'var',
39353949
name: name,
39363950
members: m,
3951+
quoted: p && s ? '"' : null,
39373952
prefix: null,
39383953
};
39393954
}

src/expr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ const exprToSQLConvertFn = {
3535
}
3636

3737
function varToSQL(expr) {
38-
const { prefix = '@', name, members, keyword, suffix } = expr
38+
const { prefix = '@', name, members, keyword, quoted, suffix } = expr
3939
const val = []
4040
if (keyword) val.push(keyword)
4141
const varName = members && members.length > 0 ? `${name}.${members.join('.')}` : name
4242
let result = `${prefix || ''}${varName}`
4343
if (suffix) result += suffix
4444
val.push(result)
45-
return val.join(' ')
45+
return [quoted, val.join(' '), quoted].filter(hasVal).join('')
4646
}
4747

4848
exprToSQLConvertFn.var = varToSQL

src/show.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exprToSQL } from './expr'
1+
import { exprToSQL, varToSQL } from './expr'
22
import { limitToSQL } from './limit'
33
import { tableToSQL, tablesToSQL } from './tables'
44
import { commonOptionConnector, hasVal, literalToSQL, toUpper } from './util'
@@ -30,7 +30,8 @@ function showGrantsForUser(showGrantsForExpr) {
3030
}
3131

3232
function showToSQL(showExpr) {
33-
const { suffix, keyword } = showExpr
33+
let { keyword } = showExpr
34+
const { suffix } = showExpr
3435
let str = ''
3536
switch (toUpper(keyword)) {
3637
case 'BINLOG':
@@ -46,6 +47,10 @@ function showToSQL(showExpr) {
4647
case 'CREATE':
4748
str = commonOptionConnector('', tableToSQL, showExpr.view)
4849
break
50+
case 'VAR':
51+
str = varToSQL(showExpr.var)
52+
keyword = ''
53+
break
4954
default:
5055
break
5156
}

test/postgres.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,20 @@ describe('Postgres', () => {
781781
`SELECT 'a'::"CHAR" AS "b"`
782782
]
783783
},
784+
{
785+
title: 'set with quoted string',
786+
sql: [
787+
`set "foo.bar" = 'a';`,
788+
`SET "foo.bar" = 'a'`
789+
]
790+
},
791+
{
792+
title: 'show stmt',
793+
sql: [
794+
'show "foo.bar";',
795+
'SHOW "foo.bar"'
796+
]
797+
},
784798
]
785799
function neatlyNestTestedSQL(sqlList){
786800
sqlList.forEach(sqlInfo => {

0 commit comments

Comments
 (0)