Skip to content

Commit

Permalink
Merge pull request #2001 from taozhi8833998/feat-explain-stmt-mysql
Browse files Browse the repository at this point in the history
feat: support explain stmt in mysql
  • Loading branch information
taozhi8833998 authored Jul 12, 2024
2 parents 7bfe03b + 79d4a87 commit cd9ddfd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
13 changes: 13 additions & 0 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ cmd_stmt
/ show_stmt
/ desc_stmt
/ grant_stmt
/ explain_stmt

create_stmt
= create_table_stmt
Expand Down Expand Up @@ -1663,6 +1664,18 @@ desc_stmt
};
}

explain_stmt
= KW_EXPLAIN __ t:select_stmt_nake {
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'explain',
expr: t
}
}
}

lock_type
= "READ"i __ s:("LOCAL"i)? {
return {
Expand Down
13 changes: 13 additions & 0 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ cmd_stmt
/ show_stmt
/ desc_stmt
/ grant_stmt
/ explain_stmt

create_stmt
= create_table_stmt
Expand Down Expand Up @@ -1938,6 +1939,18 @@ desc_stmt
};
}

explain_stmt
= KW_EXPLAIN __ t:select_stmt_nake {
return {
tableList: Array.from(tableList),
columnList: columnListTableAlias(columnList),
ast: {
type: 'explain',
expr: t
}
}
}

priv_type_table
= p:(KW_ALL / KW_ALTER / KW_CREATE __ 'VIEW'i / KW_CREATE / KW_DELETE / KW_DROP / 'GRANT'i __ 'OPTION'i / KW_INDEX / KW_INSERT / KW_REFERENCES / KW_SELECT / KW_SHOW __ KW_VIEW / KW_TRIGGER / KW_UPDATE) {
return {
Expand Down
11 changes: 11 additions & 0 deletions src/explain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { selectToSQL } from './select'
import { toUpper } from './util'

function explainToSQL(stmt) {
const { type, expr } = stmt
return [toUpper(type), selectToSQL(expr)].join(' ')
}

export {
explainToSQL,
}
2 changes: 1 addition & 1 deletion src/sql.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { unionToSQL, multipleToSQL } from './union'

const supportedTypes = ['analyze', 'attach', 'select', 'deallocate', 'delete', 'exec', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'desc', 'use', 'alter', 'set', 'create', 'lock', 'unlock', 'declare', 'show', 'replace', 'if', 'grant', 'revoke', 'proc', 'raise', 'execute', 'transaction']
const supportedTypes = ['analyze', 'attach', 'select', 'deallocate', 'delete', 'exec', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'desc', 'use', 'alter', 'set', 'create', 'lock', 'unlock', 'declare', 'show', 'replace', 'if', 'grant', 'revoke', 'proc', 'raise', 'execute', 'transaction', 'explain']

function checkSupported(expr) {
const ast = expr && expr.ast ? expr.ast : expr
Expand Down
2 changes: 2 additions & 0 deletions src/union.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { alterToSQL } from './alter'
import { analyzeToSQL, attachToSQL } from './analyze'
import { createToSQL } from './create'
import { explainToSQL } from './explain'
import { selectToSQL } from './select'
import { deleteToSQL } from './delete'
import { updateToSQL } from './update'
Expand Down Expand Up @@ -39,6 +40,7 @@ const typeToSQLFn = {
delete : deleteToSQL,
exec : execToSQL,
execute : executeToSQL,
explain : explainToSQL,
for : forLoopToSQL,
update : updateToSQL,
if : ifToSQL,
Expand Down
9 changes: 8 additions & 1 deletion test/mysql-mariadb.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1064,12 +1064,19 @@ describe('mysql', () => {
]
},
{
title: 'collate2',
title: 'collate with symbol and value',
sql: [
'select * from test where id COLLATE utf8mb4_unicode_ci = abc',
'SELECT * FROM `test` WHERE `id` COLLATE utf8mb4_unicode_ci = abc'
]
},
{
title: 'explain stmt',
sql: [
'EXPLAIN SELECT * FROM incidents where id > 10 and is_delete = 0',
'EXPLAIN SELECT * FROM `incidents` WHERE `id` > 10 AND `is_delete` = 0'
]
},
]
SQL_LIST.forEach(sqlInfo => {
const { title, sql } = sqlInfo
Expand Down

0 comments on commit cd9ddfd

Please sign in to comment.