Skip to content

Commit 5128081

Browse files
authored
Merge branch 'taozhi8833998:master' into master
2 parents 840ed1a + c6303e7 commit 5128081

File tree

7 files changed

+322
-259
lines changed

7 files changed

+322
-259
lines changed

package-lock.json

Lines changed: 226 additions & 226 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-sql-parser",
3-
"version": "4.11.0",
3+
"version": "4.12.0",
44
"description": "simple node sql parser",
55
"main": "index.js",
66
"types": "types.d.ts",

pegjs/mariadb.pegjs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@
105105
'PERSIST_ONLY': true,
106106
};
107107

108+
const reservedFunctionName = {
109+
avg: true,
110+
sum: true,
111+
count: true,
112+
max: true,
113+
min: true,
114+
group_concat: true,
115+
std: true,
116+
variance: true,
117+
current_date: true,
118+
current_time: true,
119+
current_timestamp: true,
120+
current_user: true,
121+
user: true,
122+
session_user: true,
123+
system_user: true
124+
}
125+
108126
function createUnaryExpr(op, e) {
109127
return {
110128
type: 'unary_expr',
@@ -811,6 +829,7 @@ alter_action_list
811829

812830
alter_action
813831
= ALTER_ADD_COLUMN
832+
/ ALTER_DROP_KEY_INDEX
814833
/ ALTER_DROP_COLUMN
815834
/ ALTER_MODIFY_COLUMN
816835
/ ALTER_ADD_INDEX_OR_KEY
@@ -866,9 +885,7 @@ ALTER_MODIFY_COLUMN
866885
}
867886

868887
ALTER_DROP_COLUMN
869-
= KW_DROP __
870-
kc:KW_COLUMN __
871-
c:column_ref {
888+
= KW_DROP __ kc:KW_COLUMN __ c:column_ref {
872889
return {
873890
action: 'drop',
874891
column: c,
@@ -877,8 +894,7 @@ ALTER_DROP_COLUMN
877894
type: 'alter',
878895
}
879896
}
880-
/ KW_DROP __
881-
c:column_ref {
897+
/ KW_DROP __ c:column_ref {
882898
return {
883899
action: 'drop',
884900
column: c,
@@ -887,10 +903,29 @@ ALTER_DROP_COLUMN
887903
}
888904
}
889905

906+
ALTER_DROP_KEY_INDEX
907+
= KW_DROP __ 'PRIMARY'i __ KW_KEY {
908+
return {
909+
action: 'drop',
910+
key: '',
911+
keyword: 'primary key',
912+
resource: 'key',
913+
type: 'alter',
914+
}
915+
}
916+
/ KW_DROP __ k:(('FOREIGN'i? __ KW_KEY) / (KW_INDEX)) __ c:ident_name {
917+
const resource = Array.isArray(k) ? 'key' : 'index'
918+
return {
919+
action: 'drop',
920+
[resource]: c,
921+
keyword: Array.isArray(k) ? `${[k[0], k[2]].filter(v => v).join(' ').toLowerCase()}` : k.toLowerCase(),
922+
resource,
923+
type: 'alter',
924+
}
925+
}
926+
890927
ALTER_ADD_INDEX_OR_KEY
891-
= KW_ADD __
892-
id:create_index_definition
893-
{
928+
= KW_ADD __ id:create_index_definition {
894929
return {
895930
action: 'add',
896931
type: 'alter',
@@ -2914,7 +2949,7 @@ func_call
29142949
over: up
29152950
}
29162951
}
2917-
/ name:proc_func_name &{ return name.toLowerCase() !== 'convert' } __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
2952+
/ name:proc_func_name &{ return name.toLowerCase() !== 'convert' && !reservedFunctionName[name.toLowerCase()] } __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
29182953
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
29192954
if ((name.toUpperCase() === 'TIMESTAMPDIFF' || name.toUpperCase() === 'TIMESTAMPADD') && l.value && l.value[0]) l.value[0] = { type: 'origin', value: l.value[0].column }
29202955
return {

pegjs/mysql.pegjs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,24 @@
280280
'ZEROFILL': true,
281281
};
282282

283+
const reservedFunctionName = {
284+
avg: true,
285+
sum: true,
286+
count: true,
287+
max: true,
288+
min: true,
289+
group_concat: true,
290+
std: true,
291+
variance: true,
292+
current_date: true,
293+
current_time: true,
294+
current_timestamp: true,
295+
current_user: true,
296+
user: true,
297+
session_user: true,
298+
system_user: true
299+
}
300+
283301
function createUnaryExpr(op, e) {
284302
return {
285303
type: 'unary_expr',
@@ -1011,7 +1029,7 @@ alter_action_list
10111029
alter_action
10121030
= ALTER_ADD_CONSTRAINT
10131031
/ ALTER_DROP_CONSTRAINT
1014-
/ ALTER_DROP_KEY
1032+
/ ALTER_DROP_KEY_INDEX
10151033
/ ALTER_ENABLE_CONSTRAINT
10161034
/ ALTER_DISABLE_CONSTRAINT
10171035
/ ALTER_ADD_COLUMN
@@ -1175,7 +1193,7 @@ ALTER_ADD_CONSTRAINT
11751193
}
11761194
}
11771195

1178-
ALTER_DROP_KEY
1196+
ALTER_DROP_KEY_INDEX
11791197
= KW_DROP __ 'PRIMARY'i __ KW_KEY {
11801198
return {
11811199
action: 'drop',
@@ -1185,21 +1203,13 @@ ALTER_DROP_KEY
11851203
type: 'alter',
11861204
}
11871205
}
1188-
/ KW_DROP __ 'FOREIGN'i __ KW_KEY __ c:ident_name {
1206+
/ KW_DROP __ k:(('FOREIGN'i? __ KW_KEY) / (KW_INDEX)) __ c:ident_name {
1207+
const resource = Array.isArray(k) ? 'key' : 'index'
11891208
return {
11901209
action: 'drop',
1191-
key: c,
1192-
keyword: 'foreign key',
1193-
resource: 'key',
1194-
type: 'alter',
1195-
}
1196-
}
1197-
/ KW_DROP __ (KW_KEY / KW_INDEX) __ c:ident_name {
1198-
return {
1199-
action: 'drop',
1200-
index: c,
1201-
keyword: 'index',
1202-
resource: 'index',
1210+
[resource]: c,
1211+
keyword: Array.isArray(k) ? `${[k[0], k[2]].filter(v => v).join(' ').toLowerCase()}` : k.toLowerCase(),
1212+
resource,
12031213
type: 'alter',
12041214
}
12051215
}
@@ -1579,8 +1589,7 @@ lock_stmt
15791589
}
15801590

15811591
call_stmt
1582-
= KW_CALL __
1583-
e: proc_func_call {
1592+
= KW_CALL __ e:proc_func_call {
15841593
return {
15851594
tableList: Array.from(tableList),
15861595
columnList: columnListTableAlias(columnList),
@@ -3206,6 +3215,7 @@ trim_func_clause
32063215
args,
32073216
};
32083217
}
3218+
32093219
func_call
32103220
= extract_func / trim_func_clause
32113221
/ 'convert'i __ LPAREN __ l:convert_args __ RPAREN __ ca:collate_expr? {
@@ -3231,7 +3241,7 @@ func_call
32313241
over: up
32323242
}
32333243
}
3234-
/ name:proc_func_name &{ return name.toLowerCase() !== 'convert' } __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
3244+
/ name:proc_func_name &{ return name.toLowerCase() !== 'convert' && !reservedFunctionName[name.toLowerCase()] } __ LPAREN __ l:or_and_where_expr? __ RPAREN __ bc:over_partition? {
32353245
if (l && l.type !== 'expr_list') l = { type: 'expr_list', value: [l] }
32363246
if ((name.toUpperCase() === 'TIMESTAMPDIFF' || name.toUpperCase() === 'TIMESTAMPADD') && l.value && l.value[0]) l.value[0] = { type: 'origin', value: l.value[0].column }
32373247
return {

pegjs/postgresql.pegjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5213,9 +5213,9 @@ numeric_type_suffix
52135213
return result
52145214
}
52155215
numeric_type
5216-
= t:(KW_NUMERIC / KW_DECIMAL / KW_INT / KW_INTEGER / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL) __ LPAREN __ l:[0-9]+ __ r:(COMMA __ [0-9]+)? __ RPAREN __ s:numeric_type_suffix? { /* => data_type */ return { dataType: t, length: parseInt(l.join(''), 10), scale: r && parseInt(r[2].join(''), 10), parentheses: true, suffix: s }; }
5217-
/ t:(KW_NUMERIC / KW_DECIMAL / KW_INT / KW_INTEGER / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL)l:[0-9]+ __ s:numeric_type_suffix? { /* => data_type */ return { dataType: t, length: parseInt(l.join(''), 10), suffix: s }; }
5218-
/ t:(KW_NUMERIC / KW_DECIMAL / KW_INT / KW_INTEGER / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL) __ s:numeric_type_suffix? __{ /* => data_type */ return { dataType: t, suffix: s }; }
5216+
= t:(KW_NUMERIC / KW_DECIMAL / KW_INT / KW_INTEGER / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE __ 'PRECISION'i / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL) __ LPAREN __ l:[0-9]+ __ r:(COMMA __ [0-9]+)? __ RPAREN __ s:numeric_type_suffix? { /* => data_type */ return { dataType: Array.isArray(t) ? `${t[0].toUpperCase()} ${t[2].toUpperCase()}` : t, length: parseInt(l.join(''), 10), scale: r && parseInt(r[2].join(''), 10), parentheses: true, suffix: s }; }
5217+
/ t:(KW_NUMERIC / KW_DECIMAL / KW_INT / KW_INTEGER / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE __ 'PRECISION'i / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL)l:[0-9]+ __ s:numeric_type_suffix? { /* => data_type */ return { dataType: Array.isArray(t) ? `${t[0].toUpperCase()} ${t[2].toUpperCase()}` : t, length: parseInt(l.join(''), 10), suffix: s }; }
5218+
/ t:(KW_NUMERIC / KW_DECIMAL / KW_INT / KW_INTEGER / KW_SMALLINT / KW_TINYINT / KW_BIGINT / KW_FLOAT / KW_DOUBLE __ 'PRECISION'i / KW_DOUBLE / KW_SERIAL / KW_BIGSERIAL / KW_REAL) __ s:numeric_type_suffix? __{ /* => data_type */ return { dataType: Array.isArray(t) ? `${t[0].toUpperCase()} ${t[2].toUpperCase()}` : t, suffix: s }; }
52195219

52205220
oid_type
52215221
= t:(KW_OID / KW_REGCLASS / KW_REGCOLLATION / KW_REGCONFIG / KW_REGDICTIONARY / KW_REGNAMESPACE / KW_REGOPER / KW_REGOPERATOR / KW_REGPROC / KW_REGPROCEDURE / KW_REGROLE / KW_REGTYPE) { /* => data_type */ return { dataType: t }}

test/mysql-mariadb.spec.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,13 @@ describe('mysql', () => {
879879
'SELECT * FROM `test` WHERE `name` LIKE :pattern COLLATE UTF8MB4_GENERAL_CI'
880880
]
881881
},
882+
{
883+
title: 'alter drop index or key',
884+
sql: [
885+
'ALTER TABLE table_name DROP INDEX index_name',
886+
'ALTER TABLE `table_name` DROP INDEX index_name'
887+
]
888+
},
882889
]
883890
SQL_LIST.forEach(sqlInfo => {
884891
const { title, sql } = sqlInfo
@@ -890,10 +897,12 @@ describe('mysql', () => {
890897
})
891898
})
892899

893-
it('should throw error when covert args is not right', () => {
894-
const sql = `select convert(json_unquote(json_extract('{"thing": "252"}', "$.thing")));`
900+
it('should throw error when args is not right', () => {
901+
let sql = `select convert(json_unquote(json_extract('{"thing": "252"}', "$.thing")));`
895902
expect(parser.astify.bind(parser, sql)).to.throw('Expected "!=", "#", "%", "&", "&&", "*", "+", ",", "-", "--", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "AND", "BETWEEN", "IN", "IS", "LIKE", "NOT", "ON", "OR", "OVER", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", or [ \\t\\n\\r] but ")" found.')
896903
expect(parser.astify.bind(parser, 'select convert("");')).to.throw('Expected "!=", "#", "%", "&", "&&", "*", "+", ",", "-", "--", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "AND", "BETWEEN", "COLLATE", "IN", "IS", "LIKE", "NOT", "OR", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", or [ \\t\\n\\r] but ")" found.')
904+
sql = 'SELECT AVG(Quantity,age) FROM table1;'
905+
expect(parser.astify.bind(parser, sql)).to.throw('Expected "#", "%", "&", "(", ")", "*", "+", "-", "--", "->", "->>", ".", "/", "/*", "<<", ">>", "^", "div", "|", "||", [ \\t\\n\\r], [A-Za-z0-9_$\\x80-￿], or [A-Za-z0-9_:] but "," found.')
897906
})
898907

899908
it('should join multiple table with comma', () => {

test/postgres.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,15 @@ describe('Postgres', () => {
15701570
'SELECT "中文" FROM "t1"'
15711571
],
15721572
},
1573+
{
1574+
title: 'double precision type',
1575+
sql: [
1576+
`CREATE TABLE test (
1577+
amount double precision
1578+
);`,
1579+
'CREATE TABLE "test" ("amount" DOUBLE PRECISION)'
1580+
]
1581+
},
15731582
]
15741583
neatlyNestTestedSQL(SQL_LIST)
15751584
})

0 commit comments

Comments
 (0)