Skip to content

Commit 6193f04

Browse files
Merge pull request taozhi8833998#784 from taozhi8833998/feat-string-constants-pg
feat: support string constants
2 parents 7b09356 + 3b5da6c commit 6193f04

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

pegjs/postgresql.pegjs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,10 @@ expr_item
17071707
return e
17081708
}
17091709
column_list_item
1710-
= e:expr_item s:KW_DOUBLE_COLON t:data_type __ alias:alias_clause? {
1710+
= c:string_constants_escape {
1711+
return { expr: c, as: null }
1712+
}
1713+
/ e:expr_item s:KW_DOUBLE_COLON t:data_type __ alias:alias_clause? {
17111714
// => { type: 'cast'; expr: expr; symbol: '::'; target: data_type; as?: null; }
17121715
return {
17131716
as: alias,
@@ -2592,8 +2595,17 @@ primary
25922595
}
25932596
}
25942597

2598+
string_constants_escape
2599+
= 'E'i"'" __ n:single_char* __ "'" {
2600+
return {
2601+
type: 'origin',
2602+
value: `E'${n.join('')}'`
2603+
}
2604+
}
2605+
25952606
column_ref
2596-
= tbl:ident __ DOT __ STAR {
2607+
= string_constants_escape
2608+
/ tbl:ident __ DOT __ STAR {
25972609
// => IGNORE
25982610
columnList.add(`select::${tbl}::(.*)`);
25992611
return {
@@ -3121,6 +3133,7 @@ escape_char
31213133
return String.fromCharCode(parseInt("0x" + h1 + h2 + h3 + h4));
31223134
}
31233135
/ "\\" { return "\\"; }
3136+
/ "''" { return "''" }
31243137

31253138
line_terminator
31263139
= [\n\r]

test/postgres.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,35 @@ describe('Postgres', () => {
408408
`SHOW TABLES`
409409
]
410410
},
411+
{
412+
title: 'String Constants',
413+
sql: [
414+
`select ''''`,
415+
`SELECT ''''`
416+
]
417+
},
418+
{
419+
title: 'String Constants',
420+
sql: [
421+
`SELECT '''To be, or not'', it starts.' AS x;`,
422+
`SELECT '''To be, or not'', it starts.' AS "x"`
423+
]
424+
},
425+
{
426+
title: 'String Constants',
427+
sql: [
428+
`SELECT 'foo'
429+
'bar';`,
430+
`SELECT 'foobar'`
431+
]
432+
},
433+
{
434+
title: 'String Constants with C-Style Escapes',
435+
sql: [
436+
`SELECT E'\\''`,
437+
`SELECT E'\''`
438+
]
439+
},
411440
]
412441
function neatlyNestTestedSQL(sqlList){
413442
sqlList.forEach(sqlInfo => {

0 commit comments

Comments
 (0)