feat(query): add MySQL standard explain table view
This commit is contained in:
1 parent
830e3c2183
commit
54632f4c16
16 files changed
+480
-28
No files matched your search
@@ -615,7 +615,8 @@ async fn execute_explain_query(
|
||||
}
|
||||
|
||||
// Build the database-specific EXPLAIN SQL
|
||||
let explain_result = build_explain_sql(ExplainSqlOptions { database_type: Some(*db_type), sql: sql.to_string() });
|
||||
let explain_result =
|
||||
build_explain_sql(ExplainSqlOptions { database_type: Some(*db_type), format: None, sql: sql.to_string() });
|
||||
|
||||
let explain_sql = match (explain_result.ok, explain_result.sql) {
|
||||
(true, Some(sql)) => sql,
|
||||
|
||||
@@ -2,11 +2,22 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum ExplainFormat {
|
||||
Json,
|
||||
Standard,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ExplainSqlOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
/// MySQL supports both a structured JSON plan and the traditional tabular plan.
|
||||
/// Omitted formats retain the existing JSON behavior.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub format: Option<ExplainFormat>,
|
||||
pub sql: String,
|
||||
}
|
||||
|
||||
@@ -49,6 +60,10 @@ pub fn build_explain_sql(options: ExplainSqlOptions) -> ExplainSqlBuildResult {
|
||||
format!("EXPLAIN {source}")
|
||||
}
|
||||
Some(DatabaseType::Oracle) => format!("EXPLAIN PLAN FOR {source}"),
|
||||
Some(DatabaseType::Mysql) if options.format == Some(ExplainFormat::Standard) => {
|
||||
// MySQL 8.0.32+ may otherwise inherit TREE or JSON from the session-level explain_format.
|
||||
format!("EXPLAIN FORMAT=TRADITIONAL {source}")
|
||||
}
|
||||
_ => format!("EXPLAIN FORMAT=JSON {source}"),
|
||||
};
|
||||
ExplainSqlBuildResult { ok: true, sql: Some(sql), reason: None }
|
||||
@@ -405,6 +420,7 @@ mod tests {
|
||||
fn builds_postgres_json_explain_sql() {
|
||||
let result = build_explain_sql(ExplainSqlOptions {
|
||||
database_type: Some(DatabaseType::Postgres),
|
||||
format: None,
|
||||
sql: " select * from users where id = 1; ".to_string(),
|
||||
});
|
||||
|
||||
@@ -422,6 +438,7 @@ mod tests {
|
||||
fn builds_dameng_explain_sql() {
|
||||
let result = build_explain_sql(ExplainSqlOptions {
|
||||
database_type: Some(DatabaseType::Dameng),
|
||||
format: None,
|
||||
sql: "SELECT * FROM t1 WHERE id = 1".to_string(),
|
||||
});
|
||||
|
||||
@@ -439,6 +456,7 @@ mod tests {
|
||||
fn builds_oracle_explain_plan_sql() {
|
||||
let result = build_explain_sql(ExplainSqlOptions {
|
||||
database_type: Some(DatabaseType::Oracle),
|
||||
format: None,
|
||||
sql: "WITH rows AS (SELECT 1 AS id FROM dual) SELECT * FROM rows;".to_string(),
|
||||
});
|
||||
|
||||
@@ -467,6 +485,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_explain_sql(ExplainSqlOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
format: None,
|
||||
sql: "SELECT * FROM users;".to_string(),
|
||||
}),
|
||||
ExplainSqlBuildResult {
|
||||
@@ -479,6 +498,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_explain_sql(ExplainSqlOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
format: None,
|
||||
sql: "delete from users".to_string(),
|
||||
}),
|
||||
ExplainSqlBuildResult { ok: false, sql: None, reason: Some("unsafe".to_string()) }
|
||||
@@ -487,10 +507,24 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_explain_sql(ExplainSqlOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
format: None,
|
||||
sql: "SELECT * FROM users; DELETE FROM users".to_string(),
|
||||
}),
|
||||
ExplainSqlBuildResult { ok: false, sql: None, reason: Some("unsafe".to_string()) }
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
build_explain_sql(ExplainSqlOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
format: Some(ExplainFormat::Standard),
|
||||
sql: "SELECT * FROM users;".to_string(),
|
||||
}),
|
||||
ExplainSqlBuildResult {
|
||||
ok: true,
|
||||
sql: Some("EXPLAIN FORMAT=TRADITIONAL SELECT * FROM users".to_string()),
|
||||
reason: None,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user