Skip to content

Commit

Permalink
Merge pull request #1981 from taozhi8833998/fix-character-set-quoted-…
Browse files Browse the repository at this point in the history
…ident

fix: character set quoted ident
  • Loading branch information
taozhi8833998 authored Jun 27, 2024
2 parents 44bed0a + 3254d62 commit 312396e
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
],
"complexity": [
"error",
30
35
],
"space-infix-ops": 2,
"consistent-return": 0,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/athena.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ create_option_character_set_kw
return 'CHARACTER SET'
}
create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/bigquery.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ create_like_table_simple
}

create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:literal_string {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/db2.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ create_option_character_set_kw
return 'CHARACTER SET'
}
create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/flinksql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ create_option_character_set_kw
return 'CHARACTER SET'
}
create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
Expand Down
2 changes: 1 addition & 1 deletion pegjs/hive.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ create_option_character_set_kw
return 'CHARACTER SET'
}
create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
Expand Down
19 changes: 12 additions & 7 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ column_definition_opt
/ ck:check_constraint_definition {
return { check: ck }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_without_kw_type {
return { character_set: { type: t, value: v, symbol: s }}
}

Expand Down Expand Up @@ -1394,7 +1394,7 @@ create_option_character_set_kw
return 'CHARACTER SET'
}
create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
Expand Down Expand Up @@ -3106,7 +3106,7 @@ count_arg
star_expr
= "*" { return { type: 'star', value: '*' }; }
convert_args
= c:proc_primary __ COMMA __ ch:character_string_type __ cs:create_option_character_set_kw __ v:ident_name {
= c:proc_primary __ COMMA __ ch:(character_string_type / datetime_type) __ cs:create_option_character_set_kw __ v:ident_without_kw_type {
const { dataType, length } = ch
let dataTypeStr = dataType
if (length !== undefined) dataTypeStr = `${dataTypeStr}(${length})`
Expand All @@ -3116,8 +3116,12 @@ convert_args
c,
{
type: 'origin',
value: `${dataTypeStr} ${cs} ${v}`
}
value: dataTypeStr,
suffix: {
prefix: cs,
...v,
}
},
]
}
}
Expand Down Expand Up @@ -3243,7 +3247,7 @@ scalar_func
/ KW_SYSTEM_USER

cast_expr
= c:KW_CAST __ LPAREN __ e:expr __ KW_AS __ ch:character_string_type __ cs:create_option_character_set_kw __ v:ident_name __ RPAREN __ ca:collate_expr? {
= c:KW_CAST __ LPAREN __ e:expr __ KW_AS __ ch:character_string_type __ cs:create_option_character_set_kw __ v:ident_without_kw_type __ RPAREN __ ca:collate_expr? {
const { dataType, length } = ch
let dataTypeStr = dataType
if (length !== undefined) dataTypeStr = `${dataTypeStr}(${length})`
Expand All @@ -3253,7 +3257,8 @@ cast_expr
expr: e,
symbol: 'as',
target: {
dataType: `${dataTypeStr} ${cs} ${v.toUpperCase()}`
dataType: dataTypeStr,
suffix: [{ type: 'origin', value: cs }, v],
},
collate: ca,
};
Expand Down
19 changes: 12 additions & 7 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ column_definition_opt
/ ck:check_constraint_definition {
return { check: ck }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_without_kw_type {
return { character_set: { type: t, value: v, symbol: s }}
}
/ g:generated {
Expand Down Expand Up @@ -1615,7 +1615,7 @@ create_option_character_set_kw
return 'CHARACTER SET'
}
create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
Expand Down Expand Up @@ -3391,7 +3391,7 @@ star_expr
= "*" { return { type: 'star', value: '*' }; }

convert_args
= c:proc_primary __ COMMA __ ch:(character_string_type / datetime_type) __ cs:create_option_character_set_kw __ v:ident_name {
= c:proc_primary __ COMMA __ ch:(character_string_type / datetime_type) __ cs:create_option_character_set_kw __ v:ident_without_kw_type {
const { dataType, length } = ch
let dataTypeStr = dataType
if (length !== undefined) dataTypeStr = `${dataTypeStr}(${length})`
Expand All @@ -3401,8 +3401,12 @@ convert_args
c,
{
type: 'origin',
value: `${dataTypeStr} ${cs} ${v}`
}
value: dataTypeStr,
suffix: {
prefix: cs,
...v,
}
},
]
}
}
Expand Down Expand Up @@ -3528,7 +3532,7 @@ scalar_func
/ KW_SYSTEM_USER

cast_expr
= c:KW_CAST __ LPAREN __ e:expr __ KW_AS __ ch:character_string_type __ cs:create_option_character_set_kw __ v:ident_name __ RPAREN __ ca:collate_expr? {
= c:KW_CAST __ LPAREN __ e:expr __ KW_AS __ ch:character_string_type __ cs:create_option_character_set_kw __ v:ident_without_kw_type __ RPAREN __ ca:collate_expr? {
const { dataType, length } = ch
let dataTypeStr = dataType
if (length !== undefined) dataTypeStr = `${dataTypeStr}(${length})`
Expand All @@ -3538,7 +3542,8 @@ cast_expr
expr: e,
symbol: 'as',
target: {
dataType: `${dataTypeStr} ${cs} ${v.toUpperCase()}`
dataType: dataTypeStr,
suffix: [{ type: 'origin', value: cs }, v],
},
collate: ca,
};
Expand Down
7 changes: 3 additions & 4 deletions pegjs/noql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,8 +1144,7 @@ column_definition_opt
// => { reference_definition: reference_definition; }
return { reference_definition: re }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
// => { character_set: collate_expr }
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_without_kw_type {
return { character_set: { type: t, value: v, symbol: s }}
}

Expand Down Expand Up @@ -2037,11 +2036,11 @@ create_option_character_set_kw
}

create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
/* => {
keyword: 'character set' | 'charset' | 'collate' | 'default character set' | 'default charset' | 'default collate';
symbol: '=';
value: ident_name;
value: ident_without_kw_type;
} */
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
Expand Down
8 changes: 4 additions & 4 deletions pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,8 +1275,8 @@ column_definition_opt
// => { check: check_constraint_definition; }
return { check: ck }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
// => { character_set: collate_expr }
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_without_kw_type {
// => { character_set: { type: 'CHARACTER SET'; symbol: '=' | null; value: ident_without_kw_type; } }
return { character_set: { type: t, value: v, symbol: s }}
}

Expand Down Expand Up @@ -2211,11 +2211,11 @@ create_option_character_set_kw
}

create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
/* => {
keyword: 'character set' | 'charset' | 'collate' | 'default character set' | 'default charset' | 'default collate';
symbol: '=';
value: ident_name;
value: ident_without_kw_type;
} */
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
Expand Down
7 changes: 3 additions & 4 deletions pegjs/redshift.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,8 +1156,7 @@ column_definition_opt
// => { reference_definition: reference_definition; }
return { reference_definition: re }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
// => { character_set: collate_expr }
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_without_kw_type {
return { character_set: { type: t, value: v, symbol: s }}
}

Expand Down Expand Up @@ -2049,11 +2048,11 @@ create_option_character_set_kw
}

create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
/* => {
keyword: 'character set' | 'charset' | 'collate' | 'default character set' | 'default charset' | 'default collate';
symbol: '=';
value: ident_name;
value: ident_without_kw_type;
} */
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
Expand Down
4 changes: 2 additions & 2 deletions pegjs/snowflake.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ column_definition_opt
// => { reference_definition: reference_definition; }
return { reference_definition: re }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_without_kw_type {
// => { character_set: collate_expr }
return { character_set: { type: t, value: v, symbol: s }}
}
Expand Down Expand Up @@ -1777,7 +1777,7 @@ create_option_character_set_kw
}

create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
/* => {
keyword: 'character set' | 'charset' | 'collate' | 'default character set' | 'default charset' | 'default collate';
symbol: '=';
Expand Down
4 changes: 2 additions & 2 deletions pegjs/sqlite.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ column_definition_opt
/ ck:check_constraint_definition {
return { check: ck }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_without_kw_type {
return { character_set: { type: t, value: v, symbol: s }}
}

Expand Down Expand Up @@ -1092,7 +1092,7 @@ create_option_character_set_kw
return 'CHARACTER SET'
}
create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
Expand Down
4 changes: 2 additions & 2 deletions pegjs/transactsql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ column_definition_opt
/ re:reference_definition {
return { reference_definition: re }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_without_kw_type {
return { character_set: { type: t, value: v, symbol: s }}
}

Expand Down Expand Up @@ -1155,7 +1155,7 @@ create_option_character_set_kw
return 'CHARACTER SET'
}
create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
symbol: s,
Expand Down
6 changes: 3 additions & 3 deletions pegjs/trino.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ column_definition_opt
// => { reference_definition: reference_definition; }
return { reference_definition: re }
}
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_name {
/ t:create_option_character_set_kw __ s:KW_ASSIGIN_EQUAL? __ v:ident_without_kw_type {
// => { character_set: collate_expr }
return { character_set: { type: t, value: v, symbol: s }}
}
Expand Down Expand Up @@ -1788,11 +1788,11 @@ create_option_character_set_kw
}

create_option_character_set
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_name {
= kw:KW_DEFAULT? __ t:(create_option_character_set_kw / 'CHARSET'i / 'COLLATE'i) __ s:(KW_ASSIGIN_EQUAL)? __ v:ident_without_kw_type {
/* => {
keyword: 'character set' | 'charset' | 'collate' | 'default character set' | 'default charset' | 'default collate';
symbol: '=';
value: ident_name;
value: ident_without_kw_type;
} */
return {
keyword: kw && `${kw[0].toLowerCase()} ${t.toLowerCase()}` || t.toLowerCase(),
Expand Down
2 changes: 1 addition & 1 deletion src/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function castToSQL(expr) {
let str = targetExpr ? exprToSQL(targetExpr) : ''
if (length != null) str = scale ? `${length}, ${scale}` : length
if (parentheses) str = `(${str})`
if (dataTypeSuffix && dataTypeSuffix.length) str += ` ${dataTypeSuffix.join(' ')}`
if (dataTypeSuffix && dataTypeSuffix.length) str += ` ${dataTypeSuffix.map(literalToSQL).join(' ')}`
let prefix = exprToSQL(expression)
let symbolChar = '::'
let suffix = ''
Expand Down
2 changes: 1 addition & 1 deletion src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function tableOptionToSQL(tableOption) {
const { keyword, symbol, value } = tableOption
const sql = [keyword.toUpperCase()]
if (symbol) sql.push(symbol)
let val = value
let val = literalToSQL(value)
switch (keyword) {
case 'partition by':
case 'default collate':
Expand Down
26 changes: 14 additions & 12 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,6 @@ function identifierToSql(ident, isDual) {
}
}

function commonTypeValue(opt) {
const result = []
if (!opt) return result
const { type, symbol, value } = opt
result.push(type.toUpperCase())
if (symbol) result.push(symbol)
result.push(value.toUpperCase())
return result
}

function toUpper(val) {
if (!val) return
return val.toUpperCase()
Expand All @@ -197,7 +187,7 @@ function literalToSQL(literal) {
if (!literal) return
let { prefix } = literal
const { type, parentheses, suffix, value } = literal
let str = typeof literal === 'string' ? literal : value
let str = typeof literal === 'object' ? value : literal
switch (type) {
case 'backticks_quote_string':
str = `\`${escape(value)}\``
Expand Down Expand Up @@ -258,11 +248,23 @@ function literalToSQL(literal) {
const result = []
if (prefix) result.push(toUpper(prefix))
result.push(str)
if (suffix) result.push(typeof suffix === 'object' && suffix.collate ? commonTypeValue(suffix.collate).join(' ') : toUpper(suffix))
if (suffix) {
if (typeof suffix === 'string') result.push(suffix)
if (typeof suffix === 'object') {
if (suffix.collate) result.push([toUpper(suffix.collate.type), suffix.collate.symbol, toUpper(suffix.collate.value)].filter(hasVal).join(' '))
else result.push(literalToSQL(suffix))
}
}
str = result.join(' ')
return parentheses ? `(${str})` : str
}

function commonTypeValue(opt) {
if (!opt) return []
const { type, symbol, value } = opt
return [type.toUpperCase(), symbol, typeof value === 'string' ? value.toUpperCase() : literalToSQL(value)].filter(hasVal)
}

function replaceParams(ast, params) {
return replaceParamsInner(JSON.parse(JSON.stringify(ast)), params)
}
Expand Down
Loading

0 comments on commit 312396e

Please sign in to comment.