Skip to content

Commit a030048

Browse files
Merge pull request taozhi8833998#2595 from taozhi8833998/refactor-table-name-snowflake
refactor: change table name quoted in snowflake
2 parents 35134ab + cdf022d commit a030048

File tree

3 files changed

+68
-46
lines changed

3 files changed

+68
-46
lines changed

pegjs/snowflake.pegjs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@
177177
function commonStrToLiteral(strOrLiteral) {
178178
return typeof strOrLiteral === 'string' ? { type: 'same', value: strOrLiteral } : strOrLiteral
179179
}
180+
181+
function getSurroundFromLiteralType(literal) {
182+
switch (literal.type) {
183+
case 'double_quote_string':
184+
return '"'
185+
case 'single_quote_string':
186+
return "'"
187+
case 'backticks_quote_string':
188+
return '`'
189+
default:
190+
return ''
191+
}
192+
}
180193

181194
const cmpPrefixMap = {
182195
'+': true,
@@ -2524,13 +2537,14 @@ join_op
25242537
/ (KW_INNER __)? KW_JOIN { /* => 'INNER JOIN' */ return 'INNER JOIN'; }
25252538

25262539
table_name
2527-
= dt:ident schema:(__ DOT __ ident) tail:(__ DOT __ ident) {
2528-
// => { db?: ident; schema?: ident, table: ident | '*'; }
2529-
const obj = { db: null, table: dt, ...getLocationObject(), };
2540+
= db:ident_without_kw_type schema:(__ DOT __ ident_without_kw_type) tail:(__ DOT __ ident_without_kw_type) {
2541+
const obj = { db: null, table: db.value };
25302542
if (tail !== null) {
2531-
obj.db = dt;
2532-
obj.schema = schema[3];
2533-
obj.table = tail[3];
2543+
obj.db = db.value;
2544+
obj.catalog = db.value;
2545+
obj.schema = schema[3].value;
2546+
obj.table = tail[3].value;
2547+
obj.surround = { table: getSurroundFromLiteralType(tail[3]), db: getSurroundFromLiteralType(db), schema: getSurroundFromLiteralType(schema[3]) };
25342548
}
25352549
return obj;
25362550
}
@@ -2542,21 +2556,22 @@ table_name
25422556
...getLocationObject(),
25432557
}
25442558
}
2545-
/ dt:ident tail:(__ DOT __ ident)? {
2546-
// => IGNORE
2547-
const obj = { db: null, table: dt, ...getLocationObject(), };
2559+
/ dt:ident_without_kw_type tail:(__ DOT __ ident_without_kw_type)? {
2560+
const obj = { db: null, table: dt.value, surround: { table: getSurroundFromLiteralType(dt) } };
25482561
if (tail !== null) {
2549-
obj.db = dt;
2550-
obj.table = tail[3];
2562+
obj.db = dt.value;
2563+
obj.table = tail[3].value;
2564+
obj.surround = { table: getSurroundFromLiteralType(tail[3]), db: getSurroundFromLiteralType(dt) };
25512565
}
25522566
return obj;
25532567
}
2554-
/ v:var_decl {
2568+
/ v:var_decl {
25552569
// => IGNORE
25562570
v.db = null;
25572571
v.table = v.name;
25582572
return v;
25592573
}
2574+
25602575

25612576
or_and_expr
25622577
= head:expr tail:(__ (KW_AND / KW_OR) __ expr)* {

src/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function identifierToSql(ident, isDual, surround) {
155155
if (isDual === true) return `'${ident}'`
156156
if (!ident) return
157157
if (ident === '*') return ident
158-
if (surround) return `${surround}${ident}${surround}`
158+
if (surround != null) return `${surround}${ident}${surround}`
159159
const { database } = getParserOpt()
160160
switch (database && database.toLowerCase()) {
161161
case 'mysql':

0 commit comments

Comments
 (0)