refactor(workspace): split SQL and database driver crates

This commit is contained in:
t8y2 committed 2026-09-25 15:51:58 +08:00
1 parent b99c60aff6
commit e0da2d09d3
180 files changed
+1422 -538

No files matched your search

+236
View File
@@ -0,0 +1,236 @@
use mysql_async::prelude::Queryable;
use std::collections::HashSet;
use crate::models::connection::{ConnectionConfig, DatabaseType};
use crate::types::{DatabaseInfo, TableInfo};
use super::mysql::{get_conn_with_timeout, list_table_names_show_filtered_with_conn, quote_identifier, MySqlPool};
const ENABLE_BRANCH_DATABASES_SQL: &str = "SET @@dolt_show_branch_databases = 1";
// 与设置对称地复位 session 变量:连接从池中取出后是单条会被复用的 session 连接,
// 若不在归还前复位,后续无关的 SHOW DATABASES(其他元数据路径复用同一条连接时)
// 会意外看到 branch 数据库。复位是 best-effort,失败不应让 list_databases 整体失败。
const DISABLE_BRANCH_DATABASES_SQL: &str = "SET @@dolt_show_branch_databases = 0";
const SHOW_DATABASES_SQL: &str = "SHOW DATABASES";
const ENABLE_SYSTEM_TABLES_SQL: &str = "SET @@dolt_show_system_tables = 1";
const DISABLE_SYSTEM_TABLES_SQL: &str = "SET @@dolt_show_system_tables = 0";
const SHOW_SYSTEM_TABLES_CONFIG_KEY: &str = "doltShowSystemTables";
const SYSTEM_TABLE_PREFIX: &str = "dolt";
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn system_tables_visible(config: &ConnectionConfig) -> bool {
is_config(config)
&& config
.external_config
.as_ref()
.and_then(|value| value.get(SHOW_SYSTEM_TABLES_CONFIG_KEY))
.and_then(serde_json::Value::as_bool)
.unwrap_or(false)
}
pub fn requests_system_tables(include_patterns: Option<&[String]>) -> bool {
include_patterns.is_some_and(|patterns| patterns.iter().any(|pattern| pattern.trim().eq_ignore_ascii_case("dolt%")))
}
fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::Mysql && driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case("dolt"))
}
pub async fn list_databases(pool: &MySqlPool) -> Result<Vec<DatabaseInfo>, String> {
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
if let Err(error) = conn.query_drop(ENABLE_BRANCH_DATABASES_SQL).await {
log::debug!("Dolt branch database system variable is unavailable; falling back to dolt_branches: {error}");
return list_databases_from_branches(&mut conn).await;
}
let databases = query_database_names(&mut conn).await?;
// 主路径成功读完 SHOW DATABASES 后,归还连接前复位 session 变量,
// 避免该连接被复用于无关元数据查询时仍残留 branch 数据库可见性。
if let Err(error) = conn.query_drop(DISABLE_BRANCH_DATABASES_SQL).await {
log::warn!("Failed to reset Dolt branch database session variable before returning connection: {error}");
}
Ok(database_infos(databases))
}
pub async fn list_system_tables(
pool: &MySqlPool,
database: &str,
filter: Option<&str>,
) -> Result<Vec<TableInfo>, String> {
// The visibility flag is session-scoped, so setup, metadata lookup, and
// cleanup must use the same pooled connection.
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
conn.query_drop(ENABLE_SYSTEM_TABLES_SQL).await.map_err(|error| error.to_string())?;
let server_filter = filter.map(str::trim).filter(|filter| !filter.is_empty()).unwrap_or(SYSTEM_TABLE_PREFIX);
let tables = list_table_names_show_filtered_with_conn(&mut conn, database, Some(server_filter), &[])
.await
.map(retain_system_tables);
if let Err(error) = conn.query_drop(DISABLE_SYSTEM_TABLES_SQL).await {
log::warn!("Failed to reset Dolt system table session variable before returning connection: {error}");
}
tables
}
fn retain_system_tables(mut tables: Vec<TableInfo>) -> Vec<TableInfo> {
tables.retain(|table| table.name.to_ascii_lowercase().starts_with(SYSTEM_TABLE_PREFIX));
tables
}
async fn list_databases_from_branches(conn: &mut mysql_async::Conn) -> Result<Vec<DatabaseInfo>, String> {
let base_names = query_database_names(conn).await?;
let discovery_names: Vec<String> =
base_names.iter().filter(|name| is_branch_discovery_database(name)).cloned().collect();
let mut seen: HashSet<String> = base_names.iter().cloned().collect();
let mut names = base_names;
for database in discovery_names {
let branches: Vec<String> = match conn.query(list_branches_sql(&database)).await {
Ok(branches) => branches,
Err(error) => {
// fallback 进入这里说明实例不支持 dolt_show_branch_databases 系统变量。
// 首个非系统库的 dolt_branches 查询报错即可判定该实例并非 Dolt
// (可能是同一实例上混用的纯 MySQL 库),逐库试探只会对每个普通库
// 产生一次无效报错噪音。首次失败即整体降级为纯 SHOW DATABASES 结果,
// 不再继续试探剩余库。注意区分「查询报错(非 Dolt)」与「查询成功但为空
// (是 Dolt 但无 branch)」:只有前者触发降级,后者继续遍历后续库。
log::debug!(
"Dolt branch discovery failed on `{database}`, treating instance as non-Dolt and falling back to plain SHOW DATABASES: {error}"
);
return Ok(database_infos(names));
}
};
for branch in branches {
let revision = format!("{database}/{}", branch.trim());
if !branch.trim().is_empty() && seen.insert(revision.clone()) {
names.push(revision);
}
}
}
Ok(database_infos(names))
}
async fn query_database_names(conn: &mut mysql_async::Conn) -> Result<Vec<String>, String> {
conn.query(SHOW_DATABASES_SQL).await.map_err(|error| error.to_string())
}
fn database_infos(names: Vec<String>) -> Vec<DatabaseInfo> {
names
.into_iter()
.map(|name| name.trim().to_string())
.filter(|name| !name.is_empty())
.map(|name| DatabaseInfo { name, ..Default::default() })
.collect()
}
fn is_branch_discovery_database(name: &str) -> bool {
let normalized = name.trim().to_ascii_lowercase();
!normalized.is_empty()
&& !normalized.contains('/')
&& !matches!(normalized.as_str(), "information_schema" | "mysql" | "performance_schema" | "sys")
}
fn list_branches_sql(database: &str) -> String {
format!("SELECT name FROM {}.dolt_branches ORDER BY name", quote_identifier(database))
}
#[cfg(test)]
mod tests {
use super::*;
fn dolt_config() -> ConnectionConfig {
serde_json::from_value(serde_json::json!({
"id": "dolt",
"name": "Dolt",
"db_type": "mysql",
"driver_profile": "dolt",
"host": "127.0.0.1",
"port": 3306,
"username": "root",
"password": "",
"database": "app"
}))
.unwrap()
}
#[test]
fn profile_is_isolated_from_standard_mysql() {
assert!(is_profile(&DatabaseType::Mysql, Some("Dolt")));
assert!(!is_profile(&DatabaseType::Mysql, None));
assert!(!is_profile(&DatabaseType::Postgres, Some("dolt")));
}
#[test]
fn system_table_visibility_uses_dolt_profile_config_only() {
let mut config = dolt_config();
assert!(!system_tables_visible(&config));
config.external_config = Some(serde_json::json!({ "doltShowSystemTables": true }));
assert!(system_tables_visible(&config));
config.driver_profile = Some("mysql".to_string());
assert!(!system_tables_visible(&config));
}
#[test]
fn system_table_requests_require_the_dedicated_include_pattern() {
assert!(requests_system_tables(Some(&["dolt%".to_string()])));
assert!(requests_system_tables(Some(&[" DOLT% ".to_string()])));
assert!(!requests_system_tables(Some(&["%dolt%".to_string()])));
assert!(!requests_system_tables(Some(&[])));
assert!(!requests_system_tables(None));
}
#[test]
fn system_table_results_exclude_ordinary_tables_after_show_fallbacks() {
let tables = vec![
TableInfo {
name: "dolt_log".to_string(),
table_type: "BASE TABLE".to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
},
TableInfo {
name: "DOLT_BRANCHES".to_string(),
table_type: "BASE TABLE".to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
},
TableInfo {
name: "orders".to_string(),
table_type: "BASE TABLE".to_string(),
valid: None,
comment: Some("ordinary".to_string()),
parent_schema: None,
parent_name: None,
},
];
assert_eq!(
retain_system_tables(tables).into_iter().map(|table| table.name).collect::<Vec<_>>(),
vec!["dolt_log", "DOLT_BRANCHES"]
);
}
#[test]
fn fallback_only_discovers_base_user_databases() {
assert!(is_branch_discovery_database("inventory"));
assert!(!is_branch_discovery_database("inventory/main"));
assert!(!is_branch_discovery_database("information_schema"));
assert!(!is_branch_discovery_database("mysql"));
}
#[test]
fn fallback_quotes_database_identifier() {
assert_eq!(
list_branches_sql("inventory`archive"),
"SELECT name FROM `inventory``archive`.dolt_branches ORDER BY name"
);
}
}
+32
View File
@@ -0,0 +1,32 @@
use crate::models::connection::{ConnectionConfig, DatabaseType};
pub use super::mysql_compatible::{
get_columns_show_from as get_catalog_columns, list_catalog_indexes, list_catalogs,
list_databases_show_from as list_catalog_databases, list_indexes_with_ddl_fallback as list_indexes,
list_tables_show_from as list_catalog_tables, show_create_table_ddl_from as get_catalog_table_ddl,
};
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::Doris
|| (*db_type == DatabaseType::Mysql
&& driver_profile
.is_some_and(|profile| matches!(profile.to_ascii_lowercase().as_str(), "doris" | "selectdb")))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_matches_doris_and_selectdb_only() {
assert!(is_profile(&DatabaseType::Doris, None));
assert!(is_profile(&DatabaseType::Mysql, Some("DORIS")));
assert!(is_profile(&DatabaseType::Mysql, Some("selectdb")));
assert!(!is_profile(&DatabaseType::Mysql, Some("starrocks")));
assert!(!is_profile(&DatabaseType::Postgres, Some("doris")));
}
}
+17
View File
@@ -0,0 +1,17 @@
#![recursion_limit = "256"]
pub use dbx_driver_support::db::*;
pub use dbx_driver_support::{db, execution, file_validator, wkb};
pub use dbx_sql_core::{mysql_event_sql, sql};
pub use dbx_sql_dialect::sql_dialect;
pub use dbx_types::{models, types};
pub mod dolt;
pub mod doris;
pub mod manticoresearch;
pub mod mysql;
pub mod mysql_compatible;
pub mod ob_oracle;
pub mod oceanbase_mysql;
pub mod starrocks;
pub mod tidb;
@@ -0,0 +1,270 @@
use mysql_async::prelude::*;
use std::collections::HashMap;
use crate::models::connection::{ConnectionConfig, DatabaseType};
use crate::types::{ColumnInfo, IndexInfo, ObjectInfo};
use super::mysql::{self, MySqlPool};
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::ManticoreSearch
|| (*db_type == DatabaseType::Mysql
&& driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case("manticoresearch")))
}
fn quote_identifier(value: &str) -> String {
format!("`{}`", value.replace('`', "``"))
}
fn row_get<T, I>(row: &mysql_async::Row, index: I) -> Option<T>
where
T: mysql_async::prelude::FromValue,
I: mysql_async::prelude::ColumnIndex,
{
row.get_opt::<T, I>(index).and_then(|result| result.ok())
}
fn get_str_by_name(row: &mysql_async::Row, name: &str) -> String {
row_get::<String, _>(row, name)
.or_else(|| row_get::<Vec<u8>, _>(row, name).map(|bytes| String::from_utf8_lossy(&bytes).to_string()))
.unwrap_or_default()
}
fn get_opt_str(row: &mysql_async::Row, name: &str) -> Option<String> {
row_get::<String, _>(row, name)
.or_else(|| row_get::<Vec<u8>, _>(row, name).map(|bytes| String::from_utf8_lossy(&bytes).to_string()))
}
fn preferred_metadata(primary: Option<String>, fallback: Option<String>) -> Option<String> {
primary.filter(|value| !value.trim().is_empty()).or_else(|| fallback.filter(|value| !value.trim().is_empty()))
}
pub async fn list_objects(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectInfo>, String> {
let (tables, functions) = tokio::join!(mysql::list_tables_show(pool, database), list_udf_objects(pool, database));
let mut objects: Vec<ObjectInfo> = tables?
.into_iter()
.map(|table| ObjectInfo {
name: table.name,
object_type: "TABLE".to_string(),
schema: Some(database.to_string()),
valid: None,
signature: None,
custom_type_kind: None,
has_members: None,
comment: table.comment,
created_at: None,
updated_at: None,
parent_schema: table.parent_schema,
parent_name: table.parent_name,
trigger: None,
xugu_type_members_expandable: None,
})
.collect();
match functions {
Ok(functions) => objects.extend(functions),
Err(err) => log::warn!("Skipping UDFs for Manticore Search database `{}` in object browser: {}", database, err),
}
Ok(objects)
}
async fn list_udf_objects(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectInfo>, String> {
let mut conn = pool.get_conn().await.map_err(|err| err.to_string())?;
let result = conn.query_iter("SHOW PLUGINS").await.map_err(|err| err.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|err| err.to_string())?;
Ok(rows
.iter()
.filter_map(|row| {
plugin_object(
&get_str_by_name(row, "Type"),
&get_str_by_name(row, "Name"),
get_opt_str(row, "Library").as_deref(),
get_opt_str(row, "Extra").as_deref(),
database,
)
})
.collect())
}
fn plugin_object(
plugin_type: &str,
name: &str,
library: Option<&str>,
extra: Option<&str>,
database: &str,
) -> Option<ObjectInfo> {
if !plugin_type.eq_ignore_ascii_case("udf") {
return None;
}
let name = name.trim();
if name.is_empty() {
return None;
}
let mut comment_parts = Vec::new();
if let Some(library) = library.map(str::trim).filter(|value| !value.is_empty()) {
comment_parts.push(format!("SONAME {library}"));
}
if let Some(return_type) = extra.map(str::trim).filter(|value| !value.is_empty()) {
comment_parts.push(format!("RETURNS {return_type}"));
}
Some(ObjectInfo {
name: name.to_string(),
object_type: "FUNCTION".to_string(),
schema: Some(database.to_string()),
valid: None,
signature: None,
custom_type_kind: None,
has_members: None,
comment: if comment_parts.is_empty() { None } else { Some(comment_parts.join(", ")) },
created_at: None,
updated_at: None,
parent_schema: None,
parent_name: None,
trigger: None,
xugu_type_members_expandable: None,
})
}
pub async fn get_columns(pool: &MySqlPool, database: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
let mut columns = mysql::get_columns_show(pool, database, table).await?;
let properties = column_properties(pool, database, table).await.unwrap_or_default();
for column in &mut columns {
if let Some(property) = properties.get(&column.name.to_lowercase()) {
column.extra = preferred_metadata(column.extra.take(), Some(property.clone()));
}
}
Ok(columns)
}
async fn column_properties(pool: &MySqlPool, database: &str, table: &str) -> Result<HashMap<String, String>, String> {
let sql = show_columns_sql(database, table, true);
let mut conn = pool.get_conn().await.map_err(|err| err.to_string())?;
let rows: Vec<mysql_async::Row> = match conn.query_iter(&sql).await {
Ok(result) => result.collect_and_drop().await.map_err(|err| err.to_string())?,
Err(_) => {
let sql = show_columns_sql(database, table, false);
let result = conn.query_iter(&sql).await.map_err(|err| err.to_string())?;
result.collect_and_drop().await.map_err(|err| err.to_string())?
}
};
Ok(rows
.iter()
.filter_map(|row| {
let name = get_str_by_name(row, "Field").trim().to_string();
if name.is_empty() {
return None;
}
get_opt_str(row, "Properties")
.filter(|value| !value.trim().is_empty())
.map(|properties| (name.to_lowercase(), properties))
})
.collect())
}
fn show_columns_sql(database: &str, table: &str, full: bool) -> String {
let prefix = if full { "SHOW FULL COLUMNS FROM" } else { "SHOW COLUMNS FROM" };
if database.trim().is_empty() {
format!("{prefix} {}", quote_identifier(table))
} else {
format!("{prefix} {}.{}", quote_identifier(database), quote_identifier(table))
}
}
pub async fn list_indexes(pool: &MySqlPool, table: &str) -> Result<Vec<IndexInfo>, String> {
let sql = list_indexes_sql(table);
let mut conn = pool.get_conn().await.map_err(|err| err.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|err| err.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|err| err.to_string())?;
Ok(rows.iter().map(index_info_from_row).collect())
}
fn list_indexes_sql(table: &str) -> String {
format!("SHOW TABLE INDEXES FROM {}", quote_identifier(table))
}
fn index_info_from_row(row: &mysql_async::Row) -> IndexInfo {
let name = get_str_by_name(row, "Name");
let enabled = get_str_by_name(row, "Enabled");
let percent = get_str_by_name(row, "Percent");
let comment_parts: Vec<String> = [
(!enabled.is_empty()).then(|| format!("Enabled: {enabled}")),
(!percent.is_empty()).then(|| format!("Percent: {percent}")),
]
.into_iter()
.flatten()
.collect();
IndexInfo {
name: name.clone(),
columns: vec![name],
is_unique: false,
is_primary: false,
filter: None,
index_type: Some(get_str_by_name(row, "Type")),
included_columns: None,
comment: (!comment_parts.is_empty()).then(|| comment_parts.join(", ")),
key_is_expression: Vec::new(),
column_opclasses: vec![],
key_options: Vec::new(),
constraint_backed: false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_matches_native_and_mysql_profile_connections() {
assert!(is_profile(&DatabaseType::ManticoreSearch, None));
assert!(is_profile(&DatabaseType::Mysql, Some("MANTICORESEARCH")));
assert!(!is_profile(&DatabaseType::Mysql, None));
assert!(!is_profile(&DatabaseType::Postgres, Some("manticoresearch")));
}
#[test]
fn show_metadata_uses_unqualified_table_names_when_database_is_empty() {
assert_eq!(show_columns_sql("", "idx", true), "SHOW FULL COLUMNS FROM `idx`");
}
#[test]
fn column_extra_falls_back_to_properties() {
assert_eq!(
preferred_metadata(Some("auto_increment".to_string()), Some("indexed attribute".to_string())),
Some("auto_increment".to_string())
);
assert_eq!(
preferred_metadata(Some(" ".to_string()), Some("indexed attribute".to_string())),
Some("indexed attribute".to_string())
);
assert_eq!(preferred_metadata(None, Some(" ".to_string())), None);
}
#[test]
fn indexes_sql_uses_show_table_indexes() {
assert_eq!(list_indexes_sql("materials"), "SHOW TABLE INDEXES FROM `materials`");
assert_eq!(list_indexes_sql("odd`name"), "SHOW TABLE INDEXES FROM `odd``name`");
}
#[test]
fn udf_plugin_maps_to_function_object() {
let object = plugin_object("udf", "sequence", Some("udfexample.dll"), Some("INT"), "app").expect("udf plugin");
assert_eq!(object.name, "sequence");
assert_eq!(object.object_type, "FUNCTION");
assert_eq!(object.schema.as_deref(), Some("app"));
assert_eq!(object.comment.as_deref(), Some("SONAME udfexample.dll, RETURNS INT"));
}
#[test]
fn non_udf_plugins_are_not_sidebar_functions() {
assert!(plugin_object("ranker", "bm25", Some("ranker.so"), None, "app").is_none());
assert!(plugin_object("udf", " ", Some("udf.so"), Some("INT"), "app").is_none());
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,646 @@
use mysql_async::prelude::*;
use std::collections::HashSet;
use crate::models::connection::ConnectionConfig;
use crate::types::{ColumnInfo, DatabaseInfo, IndexInfo, TableInfo};
use super::mysql::{
bytes_to_string_lossy, database_infos_from_names, first_nonempty_str_by_name, fix_potential_double_encoding,
get_conn_with_health_check, get_conn_with_timeout, get_opt_str, get_str, get_str_by_name, is_mysql_identifier_byte,
list_indexes, mysql_keyword_at, quote_identifier, show_create_table_ddl, skip_mysql_quoted, MySqlPool,
};
pub fn uses_show_metadata(config: &ConnectionConfig) -> bool {
super::doris::is_config(config) || super::starrocks::is_config(config) || super::manticoresearch::is_config(config)
}
pub fn supports_external_catalogs(config: &ConnectionConfig) -> bool {
super::doris::is_config(config) || super::starrocks::is_config(config)
}
// Doris and StarRocks reuse the MySQL wire protocol, but catalog addressing and DDL/index
// metadata semantics are MySQL-compatible distributed behavior and stay isolated in this module.
pub async fn list_indexes_with_ddl_fallback(
pool: &MySqlPool,
database: &str,
table: &str,
) -> Result<Vec<IndexInfo>, String> {
let statistics_result = list_indexes(pool, database, table).await;
let mut indexes = match &statistics_result {
Ok(indexes) => indexes.clone(),
Err(err) => {
log::debug!(
"Falling back to SHOW CREATE TABLE for MySQL-compatible distributed indexes on `{database}`.`{table}` after information_schema.STATISTICS failed: {err}"
);
Vec::new()
}
};
match show_create_table_ddl(pool, database, table).await {
Ok(ddl) => {
merge_index_infos(&mut indexes, indexes_from_create_table_ddl(&ddl));
Ok(indexes)
}
Err(ddl_err) => {
if indexes.is_empty() {
if let Err(statistics_err) = statistics_result {
return Err(format!(
"{statistics_err}; SHOW CREATE TABLE fallback failed for MySQL-compatible distributed indexes: {ddl_err}"
));
}
}
Ok(indexes)
}
}
}
// ---------------------------------------------------------------------------
// Doris / StarRocks multi-catalog support.
//
// These engines expose external catalogs (iceberg, hive, jdbc, ...) alongside
// the native `internal` catalog via `SHOW CATALOGS`. The functions below address
// objects in a specific catalog using 3-part qualified names
// (`<catalog>.<database>.<table>`), which the engines accept directly without
// needing to `SWITCH` the session catalog.
// ---------------------------------------------------------------------------
/// Build a 2-part qualified identifier `` `<catalog>`.`<database>` ``.
fn catalog_database_ref(catalog: &str, database: &str) -> String {
format!("{}.{}", quote_identifier(catalog), quote_identifier(database))
}
/// Build a 3-part qualified identifier `` `<catalog>`.`<database>`.`<table>` ``.
fn catalog_table_ref(catalog: &str, database: &str, table: &str) -> String {
format!("{}.{}.{}", quote_identifier(catalog), quote_identifier(database), quote_identifier(table))
}
/// `SHOW CATALOGS` → list of catalogs visible to the current user.
///
/// Column layouts differ between engines: Doris exposes `CatalogName` (with
/// `CatalogId`/`IsCurrent`/`CreateTime`/`LastUpdateTime`), while StarRocks
/// exposes `Catalog` (only `Type`/`Comment`, no `IsCurrent`). The name is read
/// from either column; missing trailing columns degrade gracefully to
/// empty/None. The built-in catalog is named `internal` in Doris and
/// `default_catalog` in StarRocks (both with `Type=internal`); detection is
/// type-based (see `CatalogInfo::is_internal`), not name-based.
pub async fn list_catalogs(pool: &MySqlPool) -> Result<Vec<crate::db::CatalogInfo>, String> {
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let result = conn.query_iter("SHOW CATALOGS").await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
let catalogs: Vec<crate::db::CatalogInfo> = rows
.iter()
.filter_map(|row| {
// Doris column is `CatalogName`; StarRocks column is `Catalog`.
let name = first_nonempty_str_by_name(row, &["CatalogName", "Catalog"]).trim().to_string();
if name.is_empty() {
return None;
}
let catalog_type = get_str_by_name(row, "Type").trim().to_string();
let is_current = {
let value = get_str_by_name(row, "IsCurrent").trim().to_ascii_lowercase();
!value.is_empty() && value != "no" && value != "false" && value != "0"
};
let comment = get_opt_str(row, "Comment").map(|s| s.trim().to_string()).filter(|s| !s.is_empty());
Some(crate::db::CatalogInfo { name, catalog_type, is_current, comment })
})
.collect();
Ok(normalize_catalogs(catalogs))
}
/// Sort with the built-in catalog first, then the rest alphabetically by name.
/// The built-in catalog is identified by `CatalogInfo::is_internal` (type-based)
/// rather than by name, so StarRocks `default_catalog` sorts first just like
/// Doris `internal`. No synthetic catalog is injected: `SHOW CATALOGS` always
/// lists the built-in catalog on both engines, and a single-catalog result is
/// handled by the flat-sidebar fallback in the caller.
fn normalize_catalogs(mut catalogs: Vec<crate::db::CatalogInfo>) -> Vec<crate::db::CatalogInfo> {
catalogs.sort_by(|a, b| match (a.is_internal(), b.is_internal()) {
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => a.name.cmp(&b.name),
});
catalogs
}
/// `SHOW DATABASES FROM <catalog>` → databases in the given catalog.
pub async fn list_databases_show_from(pool: &MySqlPool, catalog: &str) -> Result<Vec<DatabaseInfo>, String> {
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let sql = format!("SHOW DATABASES FROM {}", quote_identifier(catalog));
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(database_infos_from_names(rows.iter().map(|row| get_str(row, 0)), false))
}
/// `SHOW TABLES FROM <catalog>.<database>` → tables in an external catalog.
///
/// External catalogs do not support `SHOW TABLE STATUS`, so comments/status are
/// not fetched (the caller only needs names + types for browsing).
pub async fn list_tables_show_from(pool: &MySqlPool, catalog: &str, database: &str) -> Result<Vec<TableInfo>, String> {
let sql = format!("SHOW TABLES FROM {}", catalog_database_ref(catalog, database));
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
let mut tables: Vec<TableInfo> = rows
.iter()
.filter_map(|row| {
let name = get_str(row, 0).trim().to_string();
if name.is_empty() {
return None;
}
// SHOW FULL TABLES exposes a type column; plain SHOW TABLES does not.
let table_type = get_str(row, 1);
Some(TableInfo {
name,
table_type: if table_type.trim().is_empty() { "TABLE".to_string() } else { table_type },
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
})
})
.collect();
tables.sort_by(|a, b| a.name.cmp(&b.name));
Ok(tables)
}
/// `SHOW COLUMNS FROM <catalog>.<database>.<table>` → columns of an external
/// catalog table. Falls back to `DESCRIBE` if `SHOW COLUMNS` is rejected.
pub async fn get_columns_show_from(
pool: &MySqlPool,
catalog: &str,
database: &str,
table: &str,
) -> Result<Vec<ColumnInfo>, String> {
let qualified = catalog_table_ref(catalog, database, table);
let full_sql = format!("SHOW FULL COLUMNS FROM {qualified}");
let plain_sql = format!("SHOW COLUMNS FROM {qualified}");
let describe_sql = format!("DESCRIBE {qualified}");
let mut conn = get_conn_with_health_check(pool).await?;
let rows: Vec<mysql_async::Row> = match conn.query_iter(&full_sql).await {
Ok(result) => result.collect_and_drop().await.map_err(|e| e.to_string())?,
Err(_) => match conn.query_iter(&plain_sql).await {
Ok(result) => result.collect_and_drop().await.map_err(|e| e.to_string())?,
Err(_) => {
let result = conn.query_iter(&describe_sql).await.map_err(|e| e.to_string())?;
result.collect_and_drop().await.map_err(|e| e.to_string())?
}
},
};
Ok(rows
.iter()
.filter_map(|row| {
// MySQL allows leading/trailing spaces in column names, so keep the name verbatim
// and only drop rows whose name is blank, matching `mysql_column_name` in mysql.rs.
let name = get_str_by_name(row, "Field");
if name.trim().is_empty() {
return None;
}
let key = get_str_by_name(row, "Key");
let collation = get_opt_str(row, "Collation").filter(|s| !s.is_empty());
Some(ColumnInfo {
name,
data_type: get_str_by_name(row, "Type"),
resolved_schema: None,
is_nullable: get_str_by_name(row, "Null").eq_ignore_ascii_case("YES"),
column_default: get_opt_str(row, "Default"),
is_primary_key: key.eq_ignore_ascii_case("PRI"),
is_unique: key.eq_ignore_ascii_case("UNI"),
extra: get_opt_str(row, "Extra"),
comment: get_opt_str(row, "Comment")
.map(|s| fix_potential_double_encoding(&s))
.filter(|s| !s.is_empty()),
numeric_precision: None,
numeric_scale: None,
character_maximum_length: None,
enum_values: None,
character_set: collation
.as_deref()
.and_then(|c| c.split_once('_').map(|(charset, _)| charset.to_string()))
.filter(|s| !s.is_empty()),
collation,
metadata_capabilities: None,
})
})
.collect())
}
/// `SHOW CREATE TABLE <catalog>.<database>.<table>` → DDL for an external
/// catalog table.
pub async fn show_create_table_ddl_from(
pool: &MySqlPool,
catalog: &str,
database: &str,
table: &str,
) -> Result<String, String> {
let sql = format!("SHOW CREATE TABLE {}", catalog_table_ref(catalog, database, table));
let mut conn = get_conn_with_health_check(pool).await?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
let row = rows.first().ok_or("DDL not found")?;
row.get_opt::<String, usize>(1)
.and_then(|result| result.ok())
.or_else(|| row.get_opt::<Vec<u8>, usize>(1).and_then(|result| result.ok()).map(bytes_to_string_lossy))
.ok_or_else(|| "Failed to read DDL".to_string())
}
/// Best-effort index listing for an external catalog table. External catalogs
/// generally do not expose MySQL-style index metadata via `information_schema`
/// (that view is scoped to the internal catalog), so indexes are derived from
/// `SHOW CREATE TABLE` parsing. Returns empty on failure (graceful degradation
/// — indexes are informational for external tables).
pub async fn list_catalog_indexes(
pool: &MySqlPool,
catalog: &str,
database: &str,
table: &str,
) -> Result<Vec<IndexInfo>, String> {
let ddl = show_create_table_ddl_from(pool, catalog, database, table).await?;
Ok(indexes_from_create_table_ddl(&ddl))
}
fn merge_index_infos(target: &mut Vec<IndexInfo>, parsed: Vec<IndexInfo>) {
let mut seen_names: HashSet<String> = target.iter().map(|index| index.name.to_ascii_lowercase()).collect();
for index in parsed {
if index.columns.is_empty() {
continue;
}
if seen_names.contains(&index.name.to_ascii_lowercase())
|| target.iter().any(|existing| {
existing.is_unique == index.is_unique
&& existing.is_primary == index.is_primary
&& existing.columns == index.columns
})
{
continue;
}
seen_names.insert(index.name.to_ascii_lowercase());
target.push(index);
}
}
fn indexes_from_create_table_ddl(ddl: &str) -> Vec<IndexInfo> {
let mut indexes = Vec::new();
for raw_line in ddl.lines() {
let line = trim_ddl_definition_line(raw_line);
if line.is_empty() {
continue;
}
let upper = line.to_ascii_uppercase();
if upper.starts_with("PRIMARY KEY") {
if let Some(index) = table_key_index("PRIMARY", line, true, true, "PRIMARY KEY") {
indexes.push(index);
}
} else if upper.starts_with("UNIQUE KEY") {
if let Some(index) = table_key_index("UNIQUE KEY", line, true, false, "UNIQUE KEY") {
indexes.push(index);
}
} else if upper.starts_with("INDEX ") {
if let Some(index) = secondary_index(line) {
indexes.push(index);
}
}
}
indexes
}
fn trim_ddl_definition_line(line: &str) -> &str {
let mut trimmed = line.trim();
if let Some(rest) = trimmed.strip_prefix(',') {
trimmed = rest.trim_start();
}
while let Some(rest) = trimmed.strip_suffix(',') {
trimmed = rest.trim_end();
}
trimmed
}
fn table_key_index(name: &str, line: &str, is_unique: bool, is_primary: bool, index_type: &str) -> Option<IndexInfo> {
let columns = parse_mysql_index_columns(first_parenthesized_content(line)?);
if columns.is_empty() {
return None;
}
Some(IndexInfo {
name: name.to_string(),
columns,
is_unique,
is_primary,
filter: None,
index_type: Some(index_type.to_string()),
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
column_opclasses: vec![],
key_options: Vec::new(),
constraint_backed: false,
})
}
fn secondary_index(line: &str) -> Option<IndexInfo> {
let (_, rest) = split_keyword_prefix(line, "INDEX")?;
let (name, after_name) = read_mysql_identifier(rest.trim_start())?;
let columns = parse_mysql_index_columns(first_parenthesized_content(after_name)?);
if columns.is_empty() {
return None;
}
Some(IndexInfo {
name,
columns,
is_unique: false,
is_primary: false,
filter: None,
index_type: mysql_keyword_argument(after_name, "USING").or_else(|| Some("INDEX".to_string())),
included_columns: None,
comment: mysql_quoted_string_argument(after_name, "COMMENT"),
key_is_expression: Vec::new(),
column_opclasses: vec![],
key_options: Vec::new(),
constraint_backed: false,
})
}
fn split_keyword_prefix<'a>(line: &'a str, keyword: &str) -> Option<(&'a str, &'a str)> {
if line.len() < keyword.len() || !line[..keyword.len()].eq_ignore_ascii_case(keyword) {
return None;
}
let rest = &line[keyword.len()..];
if !rest.is_empty() && is_mysql_identifier_byte(rest.as_bytes()[0]) {
return None;
}
Some((&line[..keyword.len()], rest))
}
fn read_mysql_identifier(input: &str) -> Option<(String, &str)> {
let input = input.trim_start();
if input.is_empty() {
return None;
}
let bytes = input.as_bytes();
if bytes[0] == b'`' {
let mut i = 1;
let mut value = String::new();
while i < bytes.len() {
if bytes[i] == b'`' {
if i + 1 < bytes.len() && bytes[i + 1] == b'`' {
value.push('`');
i += 2;
continue;
}
return Some((value, &input[i + 1..]));
}
let ch = input[i..].chars().next()?;
value.push(ch);
i += ch.len_utf8();
}
return None;
}
let end = input.find(|ch: char| ch.is_whitespace() || matches!(ch, '(' | ')' | ',')).unwrap_or(input.len());
if end == 0 {
return None;
}
Some((input[..end].to_string(), &input[end..]))
}
fn first_parenthesized_content(input: &str) -> Option<&str> {
let bytes = input.as_bytes();
let mut depth = 0usize;
let mut start = None;
let mut i = 0usize;
while i < bytes.len() {
match bytes[i] {
b'\'' | b'"' | b'`' => {
i = skip_mysql_quoted(input, i, bytes[i]);
continue;
}
b'(' => {
if depth == 0 {
start = Some(i + 1);
}
depth += 1;
}
b')' if depth > 0 => {
depth -= 1;
if depth == 0 {
return start.map(|start| &input[start..i]);
}
}
_ => {}
}
i += 1;
}
None
}
fn split_top_level_csv(input: &str) -> Vec<&str> {
let bytes = input.as_bytes();
let mut parts = Vec::new();
let mut depth = 0usize;
let mut start = 0usize;
let mut i = 0usize;
while i < bytes.len() {
match bytes[i] {
b'\'' | b'"' | b'`' => {
i = skip_mysql_quoted(input, i, bytes[i]);
continue;
}
b'(' => depth += 1,
b')' if depth > 0 => depth -= 1,
b',' if depth == 0 => {
parts.push(input[start..i].trim());
start = i + 1;
}
_ => {}
}
i += 1;
}
parts.push(input[start..].trim());
parts
}
fn parse_mysql_index_columns(input: &str) -> Vec<String> {
split_top_level_csv(input)
.into_iter()
.filter_map(|part| read_mysql_identifier(part).map(|(column, _)| column))
.filter(|column| !column.is_empty())
.collect()
}
fn mysql_keyword_argument(input: &str, keyword: &str) -> Option<String> {
let bytes = input.as_bytes();
let mut i = 0usize;
while i < bytes.len() {
match bytes[i] {
b'\'' | b'"' | b'`' => {
i = skip_mysql_quoted(input, i, bytes[i]);
continue;
}
_ if mysql_keyword_at(input, i, keyword) => {
return read_mysql_identifier(&input[i + keyword.len()..]).map(|(value, _)| value);
}
_ => i += 1,
}
}
None
}
fn mysql_quoted_string_argument(input: &str, keyword: &str) -> Option<String> {
let bytes = input.as_bytes();
let mut i = 0usize;
while i < bytes.len() {
match bytes[i] {
b'\'' | b'"' | b'`' => {
i = skip_mysql_quoted(input, i, bytes[i]);
continue;
}
_ if mysql_keyword_at(input, i, keyword) => {
let rest = input[i + keyword.len()..].trim_start();
if rest.as_bytes().first().copied() != Some(b'\'') {
return None;
}
let end = skip_mysql_quoted(rest, 0, b'\'');
if end <= 1 || end > rest.len() {
return None;
}
return Some(rest[1..end - 1].replace("\\'", "'").replace("''", "'"));
}
_ => i += 1,
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn doris_create_table_ddl_indexes_include_unique_key_and_inverted_indexes() {
let ddl = r#"
CREATE TABLE `bfm_org` (
`org_id` bigint NULL,
`ORG_CODE` varchar(255) NULL,
`ORG_NAME` varchar(255) NULL,
INDEX org_id_idx (`org_id`) USING INVERTED,
INDEX org_code_idx (`ORG_CODE`) USING INVERTED,
INDEX org_name_idx (`ORG_NAME`) USING INVERTED
) ENGINE=OLAP
UNIQUE KEY(`org_id`)
COMMENT '部门信息表'
DISTRIBUTED BY HASH(`org_id`) BUCKETS 4
"#;
let indexes = indexes_from_create_table_ddl(ddl);
assert_eq!(indexes.len(), 4);
assert_eq!(indexes[0].name, "org_id_idx");
assert_eq!(indexes[0].columns, vec!["org_id"]);
assert!(!indexes[0].is_unique);
assert_eq!(indexes[0].index_type.as_deref(), Some("INVERTED"));
assert_eq!(indexes[3].name, "UNIQUE KEY");
assert_eq!(indexes[3].columns, vec!["org_id"]);
assert!(indexes[3].is_unique);
assert!(!indexes[3].is_primary);
assert_eq!(indexes[3].index_type.as_deref(), Some("UNIQUE KEY"));
}
#[test]
fn doris_create_table_ddl_index_parser_handles_quoted_names_and_comments() {
let ddl = r#"
CREATE TABLE `search_test` (
`name``part` varchar(64) NULL,
INDEX `idx``name` (`name``part`) USING NGRAM_BF COMMENT 'name''s index'
) ENGINE=OLAP
UNIQUE KEY(`tenant_id`, `name``part`)
"#;
let indexes = indexes_from_create_table_ddl(ddl);
assert_eq!(indexes.len(), 2);
assert_eq!(indexes[0].name, "idx`name");
assert_eq!(indexes[0].columns, vec!["name`part"]);
assert_eq!(indexes[0].index_type.as_deref(), Some("NGRAM_BF"));
assert_eq!(indexes[0].comment.as_deref(), Some("name's index"));
assert_eq!(indexes[1].columns, vec!["tenant_id", "name`part"]);
assert!(indexes[1].is_unique);
}
fn catalog_info(name: &str, catalog_type: &str, is_current: bool) -> crate::db::CatalogInfo {
crate::db::CatalogInfo {
name: name.to_string(),
catalog_type: catalog_type.to_string(),
is_current,
comment: None,
}
}
#[test]
fn catalog_database_ref_backtick_qualifies_two_parts() {
assert_eq!(catalog_database_ref("iceberg_catalog", "sales"), "`iceberg_catalog`.`sales`");
}
#[test]
fn catalog_table_ref_backtick_qualifies_three_parts() {
assert_eq!(catalog_table_ref("iceberg_catalog", "sales", "orders"), "`iceberg_catalog`.`sales`.`orders`");
}
#[test]
fn doris_catalog_refs_escape_embedded_backticks() {
assert_eq!(catalog_database_ref("a`b", "c`d"), "`a``b`.`c``d`");
assert_eq!(catalog_table_ref("a`b", "c`d", "e`f"), "`a``b`.`c``d`.`e``f`");
}
#[test]
fn normalize_catalogs_does_not_inject_missing_internal() {
// SHOW CATALOGS always lists the built-in catalog on both engines, so a
// missing internal catalog is not synthesized — the caller's flat-sidebar
// fallback handles a single/empty result instead.
let catalogs =
vec![catalog_info("iceberg_catalog", "iceberg", true), catalog_info("hive_catalog", "hive", false)];
let normalized = normalize_catalogs(catalogs);
let names: Vec<&str> = normalized.iter().map(|c| c.name.as_str()).collect();
assert_eq!(names, vec!["hive_catalog", "iceberg_catalog"]);
assert!(!normalized.iter().any(|c| c.is_internal()));
}
#[test]
fn normalize_catalogs_keeps_existing_internal_first() {
let catalogs = vec![
catalog_info("iceberg_catalog", "iceberg", false),
catalog_info("internal", "internal", true),
catalog_info("hive_catalog", "hive", false),
];
let normalized = normalize_catalogs(catalogs);
let names: Vec<&str> = normalized.iter().map(|c| c.name.as_str()).collect();
assert_eq!(names, vec!["internal", "hive_catalog", "iceberg_catalog"]);
}
#[test]
fn normalize_catalogs_sorts_starrocks_default_catalog_first() {
// StarRocks names its built-in catalog `default_catalog` (Type=Internal);
// detection is type-based, so it sorts first just like Doris `internal`.
let catalogs =
vec![catalog_info("hive_catalog", "hive", false), catalog_info("default_catalog", "Internal", true)];
let normalized = normalize_catalogs(catalogs);
let names: Vec<&str> = normalized.iter().map(|c| c.name.as_str()).collect();
assert_eq!(names, vec!["default_catalog", "hive_catalog"]);
assert!(normalized[0].is_internal());
assert!(!normalized[1].is_internal());
}
#[test]
fn normalize_catalogs_detects_internal_by_type_not_name() {
// A catalog literally named `internal` but with an external type is NOT
// the built-in catalog and must not sort first.
let catalogs = vec![catalog_info("internal", "iceberg", false), catalog_info("hive_catalog", "hive", false)];
let normalized = normalize_catalogs(catalogs);
let names: Vec<&str> = normalized.iter().map(|c| c.name.as_str()).collect();
assert_eq!(names, vec!["hive_catalog", "internal"]);
assert!(!normalized.iter().any(|c| c.is_internal()));
}
#[test]
fn normalize_catalogs_handles_empty_input() {
let normalized = normalize_catalogs(Vec::new());
assert!(normalized.is_empty());
}
}
+401
View File
@@ -0,0 +1,401 @@
use mysql_async::prelude::*;
use crate::types::{ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, ObjectInfo, TableInfo, TriggerInfo};
const CURRENT_SCHEMA_EXPRESSION: &str = "SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')";
fn row_get<T, I>(row: &mysql_async::Row, index: I) -> Option<T>
where
T: mysql_async::prelude::FromValue,
I: mysql_async::prelude::ColumnIndex,
{
row.get_opt::<T, I>(index).and_then(|result| result.ok())
}
fn quote_value(s: &str) -> String {
format!("'{}'", s.replace('\\', "\\\\").replace('\'', "\\'"))
}
fn metadata_owner_sql(schema: &str) -> String {
if schema.trim().is_empty() {
// Unqualified Oracle objects resolve in the session's current schema. Keep metadata lookup on the same session
// semantics without an extra round trip that could observe a different pooled connection.
CURRENT_SCHEMA_EXPRESSION.to_string()
} else {
quote_value(schema)
}
}
fn get_str(row: &mysql_async::Row, idx: usize) -> String {
row_get::<String, _>(row, idx)
.or_else(|| row_get::<Vec<u8>, _>(row, idx).map(|b| String::from_utf8_lossy(&b).to_string()))
.unwrap_or_default()
}
fn get_opt_str(row: &mysql_async::Row, idx: usize) -> Option<String> {
row_get::<String, _>(row, idx)
.or_else(|| row_get::<Vec<u8>, _>(row, idx).map(|b| String::from_utf8_lossy(&b).to_string()))
}
fn get_opt_i32(row: &mysql_async::Row, idx: usize) -> Option<i32> {
row_get::<i32, _>(row, idx).or_else(|| row_get::<i64, _>(row, idx).and_then(|v| i32::try_from(v).ok()))
}
fn list_user_schemas_sql() -> &'static str {
"SELECT USERNAME FROM ALL_USERS \
WHERE USERNAME NOT IN ('SYS','LBACSYS','__public') \
OR USERNAME = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') \
ORDER BY USERNAME"
}
pub async fn list_databases(pool: &mysql_async::Pool) -> Result<Vec<DatabaseInfo>, String> {
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(list_user_schemas_sql()).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows.iter().map(|row| DatabaseInfo { name: get_str(row, 0), ..Default::default() }).collect())
}
pub async fn list_schemas(pool: &mysql_async::Pool) -> Result<Vec<String>, String> {
list_databases(pool).await.map(|databases| databases.into_iter().map(|database| database.name).collect())
}
fn list_tables_sql(schema: &str) -> String {
format!(
"SELECT TABLE_NAME, 'TABLE' AS TABLE_TYPE FROM ALL_TABLES WHERE OWNER = {s} \
UNION ALL \
SELECT VIEW_NAME, 'VIEW' AS TABLE_TYPE FROM ALL_VIEWS WHERE OWNER = {s} \
ORDER BY 1",
s = metadata_owner_sql(schema),
)
}
pub async fn list_tables(pool: &mysql_async::Pool, schema: &str) -> Result<Vec<TableInfo>, String> {
let sql = list_tables_sql(schema);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| TableInfo {
name: get_str(row, 0),
table_type: get_str(row, 1),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
})
.collect())
}
fn list_objects_sql(schema: &str) -> String {
format!(
"SELECT TABLE_NAME AS OBJECT_NAME, 'TABLE' AS OBJECT_TYPE, OWNER AS OBJECT_SCHEMA, 0 AS SORT_ORDER \
FROM ALL_TABLES WHERE OWNER = {s} \
UNION ALL \
SELECT VIEW_NAME AS OBJECT_NAME, 'VIEW' AS OBJECT_TYPE, OWNER AS OBJECT_SCHEMA, 1 AS SORT_ORDER \
FROM ALL_VIEWS WHERE OWNER = {s} \
UNION ALL \
SELECT OBJECT_NAME, OBJECT_TYPE, OWNER AS OBJECT_SCHEMA, \
CASE WHEN OBJECT_TYPE = 'PROCEDURE' THEN 2 ELSE 3 END AS SORT_ORDER \
FROM ALL_PROCEDURES \
WHERE OWNER = {s} AND OBJECT_TYPE IN ('PROCEDURE', 'FUNCTION') AND PROCEDURE_NAME IS NULL \
UNION ALL \
SELECT OBJECT_NAME, CASE OBJECT_TYPE WHEN 'PACKAGE BODY' THEN 'PACKAGE_BODY' ELSE OBJECT_TYPE END AS OBJECT_TYPE, \
OWNER AS OBJECT_SCHEMA, CASE WHEN OBJECT_TYPE = 'PACKAGE' THEN 4 ELSE 5 END AS SORT_ORDER \
FROM ALL_OBJECTS \
WHERE OWNER = {s} AND OBJECT_TYPE IN ('PACKAGE', 'PACKAGE BODY') \
ORDER BY SORT_ORDER, OBJECT_NAME",
s = metadata_owner_sql(schema),
)
}
pub async fn list_objects(pool: &mysql_async::Pool, schema: &str) -> Result<Vec<ObjectInfo>, String> {
let sql = list_objects_sql(schema);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| ObjectInfo {
name: get_str(row, 0),
object_type: get_str(row, 1),
schema: get_opt_str(row, 2)
.filter(|schema| !schema.is_empty())
.or_else(|| (!schema.trim().is_empty()).then(|| schema.to_string())),
valid: None,
signature: None,
custom_type_kind: None,
has_members: None,
comment: None,
created_at: None,
updated_at: None,
parent_schema: None,
parent_name: None,
trigger: None,
xugu_type_members_expandable: None,
})
.collect())
}
fn get_columns_sql(schema: &str, table: &str) -> String {
format!(
"SELECT c.COLUMN_NAME, c.DATA_TYPE, c.NULLABLE, c.DATA_DEFAULT, \
c.DATA_LENGTH, c.DATA_PRECISION, c.DATA_SCALE, c.COLUMN_ID, \
CASE WHEN cc.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END AS IS_PK \
FROM ALL_TAB_COLUMNS c \
LEFT JOIN ( \
SELECT cols.OWNER, cols.TABLE_NAME, cols.COLUMN_NAME \
FROM ALL_CONS_COLUMNS cols \
JOIN ALL_CONSTRAINTS con ON con.CONSTRAINT_NAME = cols.CONSTRAINT_NAME AND con.OWNER = cols.OWNER \
WHERE con.CONSTRAINT_TYPE = 'P' \
) cc ON cc.OWNER = c.OWNER AND cc.TABLE_NAME = c.TABLE_NAME AND cc.COLUMN_NAME = c.COLUMN_NAME \
WHERE c.OWNER = {s} AND c.TABLE_NAME = {t} \
ORDER BY c.COLUMN_ID",
s = metadata_owner_sql(schema),
t = quote_value(table),
)
}
pub async fn get_columns(pool: &mysql_async::Pool, schema: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
let sql = get_columns_sql(schema, table);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| {
let data_type = get_str(row, 1);
let precision = get_opt_i32(row, 5);
let scale = get_opt_i32(row, 6);
let length = get_opt_i32(row, 4);
let display_type = format_oracle_type(&data_type, precision, scale, length);
ColumnInfo {
name: get_str(row, 0),
data_type: display_type,
is_nullable: get_str(row, 2) == "Y",
column_default: get_opt_str(row, 3).map(|s| s.trim().to_string()).filter(|s| !s.is_empty()),
is_primary_key: row_get::<i32, _>(row, 8).unwrap_or(0) == 1,
extra: None,
comment: None,
numeric_precision: precision,
numeric_scale: scale,
character_maximum_length: length,
enum_values: None,
..Default::default()
}
})
.collect())
}
fn format_oracle_type(data_type: &str, precision: Option<i32>, scale: Option<i32>, length: Option<i32>) -> String {
match data_type.to_uppercase().as_str() {
"NUMBER" => match (precision, scale) {
(Some(p), Some(s)) if s > 0 => format!("NUMBER({p},{s})"),
(Some(p), _) => format!("NUMBER({p})"),
_ => "NUMBER".to_string(),
},
"VARCHAR2" | "NVARCHAR2" | "CHAR" | "NCHAR" | "RAW" => match length {
Some(l) => format!("{data_type}({l})"),
None => data_type.to_string(),
},
_ => data_type.to_string(),
}
}
fn list_indexes_sql(schema: &str, table: &str) -> String {
format!(
"SELECT ai.INDEX_NAME, \
LISTAGG(aic.COLUMN_NAME, ',') WITHIN GROUP (ORDER BY aic.COLUMN_POSITION) AS COLUMNS, \
ai.UNIQUENESS, \
CASE WHEN ac.CONSTRAINT_TYPE = 'P' THEN 1 ELSE 0 END AS IS_PRIMARY \
FROM ALL_INDEXES ai \
JOIN ALL_IND_COLUMNS aic ON ai.INDEX_NAME = aic.INDEX_NAME AND ai.TABLE_OWNER = aic.TABLE_OWNER \
LEFT JOIN ALL_CONSTRAINTS ac ON ac.INDEX_NAME = ai.INDEX_NAME AND ac.OWNER = ai.TABLE_OWNER AND ac.CONSTRAINT_TYPE = 'P' \
WHERE ai.TABLE_OWNER = {s} AND ai.TABLE_NAME = {t} \
GROUP BY ai.INDEX_NAME, ai.UNIQUENESS, ac.CONSTRAINT_TYPE \
ORDER BY ai.INDEX_NAME",
s = metadata_owner_sql(schema),
t = quote_value(table),
)
}
pub async fn list_indexes(pool: &mysql_async::Pool, schema: &str, table: &str) -> Result<Vec<IndexInfo>, String> {
let sql = list_indexes_sql(schema, table);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| {
let cols_str = get_str(row, 1);
IndexInfo {
name: get_str(row, 0),
columns: cols_str.split(',').filter(|s| !s.is_empty()).map(|s| s.to_string()).collect(),
is_unique: get_str(row, 2) == "UNIQUE",
is_primary: row_get::<i32, _>(row, 3).unwrap_or(0) == 1,
filter: None,
index_type: None,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
column_opclasses: vec![],
key_options: Vec::new(),
constraint_backed: false,
}
})
.collect())
}
pub async fn list_foreign_keys(
pool: &mysql_async::Pool,
schema: &str,
table: &str,
) -> Result<Vec<ForeignKeyInfo>, String> {
let sql = list_foreign_keys_sql(schema, table);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| ForeignKeyInfo {
name: get_str(row, 0),
column: get_str(row, 1),
ref_schema: Some(get_str(row, 2)),
ref_table: get_str(row, 3),
ref_column: get_str(row, 4),
on_update: None,
on_delete: None,
})
.collect())
}
fn list_foreign_keys_sql(schema: &str, table: &str) -> String {
format!(
"SELECT ac.CONSTRAINT_NAME, acc.COLUMN_NAME, \
ac2.OWNER AS R_OWNER, ac2.TABLE_NAME AS R_TABLE, acc2.COLUMN_NAME AS R_COLUMN \
FROM ALL_CONSTRAINTS ac \
JOIN ALL_CONS_COLUMNS acc ON ac.CONSTRAINT_NAME = acc.CONSTRAINT_NAME AND ac.OWNER = acc.OWNER \
JOIN ALL_CONSTRAINTS ac2 ON ac.R_CONSTRAINT_NAME = ac2.CONSTRAINT_NAME AND ac.R_OWNER = ac2.OWNER \
JOIN ALL_CONS_COLUMNS acc2 ON ac2.CONSTRAINT_NAME = acc2.CONSTRAINT_NAME AND ac2.OWNER = acc2.OWNER \
AND acc.POSITION = acc2.POSITION \
WHERE ac.CONSTRAINT_TYPE = 'R' AND ac.OWNER = {s} AND ac.TABLE_NAME = {t} \
ORDER BY ac.CONSTRAINT_NAME, acc.POSITION",
s = metadata_owner_sql(schema),
t = quote_value(table),
)
}
fn list_triggers_sql(schema: &str, table: &str) -> String {
format!(
"SELECT TRIGGER_NAME, TRIGGERING_EVENT, TRIGGER_TYPE \
FROM ALL_TRIGGERS \
WHERE TABLE_OWNER = {s} AND TABLE_NAME = {t} \
ORDER BY TRIGGER_NAME",
s = metadata_owner_sql(schema),
t = quote_value(table),
)
}
pub async fn list_triggers(pool: &mysql_async::Pool, schema: &str, table: &str) -> Result<Vec<TriggerInfo>, String> {
let sql = list_triggers_sql(schema, table);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| {
let trigger_type = get_str(row, 2);
let timing = if trigger_type.contains("BEFORE") {
"BEFORE"
} else if trigger_type.contains("AFTER") {
"AFTER"
} else {
"INSTEAD OF"
};
TriggerInfo {
name: get_str(row, 0),
event: get_str(row, 1),
timing: timing.to_string(),
level: None,
condition: None,
language: None,
enabled: None,
valid: None,
comment: None,
created_at: None,
statement: None,
}
})
.collect())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ob_oracle_list_objects_sql_includes_routines() {
let sql = list_objects_sql("DLJPM");
assert!(sql.contains("ALL_TABLES"));
assert!(sql.contains("ALL_VIEWS"));
assert!(sql.contains("ALL_PROCEDURES"));
assert!(sql.contains("'PROCEDURE'"));
assert!(sql.contains("'FUNCTION'"));
assert!(sql.contains("ALL_OBJECTS"));
assert!(sql.contains("'PACKAGE'"));
assert!(sql.contains("'PACKAGE BODY'"));
assert!(sql.contains("OWNER AS OBJECT_SCHEMA"));
}
#[test]
fn ob_oracle_user_schema_sql_filters_system_schemas_but_keeps_current_schema() {
let sql = list_user_schemas_sql();
assert!(sql.contains("USERNAME NOT IN ('SYS','LBACSYS','__public')"));
assert!(sql.contains("OR USERNAME = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')"));
assert!(!sql.contains("ORAAUDITOR"));
}
#[test]
fn ob_oracle_empty_schema_uses_current_schema_for_all_metadata_sql() {
let statements = [
list_tables_sql(""),
list_objects_sql(""),
get_columns_sql("", "PERF_PROV_CONFIG"),
list_indexes_sql("", "PERF_PROV_CONFIG"),
list_foreign_keys_sql("", "PERF_PROV_CONFIG"),
list_triggers_sql("", "PERF_PROV_CONFIG"),
];
for sql in statements {
assert!(sql.contains(CURRENT_SCHEMA_EXPRESSION), "missing current schema fallback in: {sql}");
assert!(!sql.contains("OWNER = ''"), "empty owner remained in: {sql}");
assert!(!sql.contains("TABLE_OWNER = ''"), "empty table owner remained in: {sql}");
}
}
#[test]
fn ob_oracle_explicit_schema_does_not_query_current_schema() {
let statements = [
list_tables_sql("SYS"),
list_objects_sql("SYS"),
get_columns_sql("SYS", "PERF_PROV_CONFIG"),
list_indexes_sql("SYS", "PERF_PROV_CONFIG"),
list_foreign_keys_sql("SYS", "PERF_PROV_CONFIG"),
list_triggers_sql("SYS", "PERF_PROV_CONFIG"),
];
for sql in statements {
assert!(sql.contains("'SYS'"), "missing explicit schema in: {sql}");
assert!(!sql.contains(CURRENT_SCHEMA_EXPRESSION), "unexpected current schema lookup in: {sql}");
}
}
}
@@ -0,0 +1,237 @@
use mysql_async::prelude::*;
use std::collections::{HashMap, HashSet};
use crate::models::connection::{ConnectionConfig, DatabaseType};
use crate::types::{ObjectInfo, TableInfo};
use super::mysql::{
get_conn_with_timeout, get_str_by_name, list_routine_objects, list_tables_show_with_status, quote_value,
table_infos_to_objects, MySqlPool, TableStatusMeta,
};
pub const DRIVER_PROFILE: &str = "oceanbase";
fn materialized_views_sql(database: &str) -> String {
format!("SELECT MVIEW_NAME FROM oceanbase.DBA_MVIEWS WHERE OWNER = {}", quote_value(database))
}
async fn list_materialized_view_names(pool: &MySqlPool, database: &str) -> Result<HashSet<String>, String> {
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let result = conn.query_iter(materialized_views_sql(database)).await.map_err(|error| error.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|error| error.to_string())?;
Ok(rows
.iter()
.filter_map(|row| {
let name = get_str_by_name(row, "MVIEW_NAME").trim().to_string();
(!name.is_empty()).then_some(name)
})
.collect())
}
fn is_materialized_view_type(table_type: &str) -> bool {
let normalized = table_type.to_ascii_uppercase().replace('_', " ");
normalized.contains("MATERIALIZED") && normalized.contains("VIEW")
}
fn merge_materialized_views(
tables: &mut Vec<TableInfo>,
materialized_view_names: Result<HashSet<String>, String>,
database: &str,
) {
let materialized_view_names = match materialized_view_names {
Ok(names) => names,
Err(error) => {
log::warn!("Skipping materialized view classification for OceanBase database `{database}`: {error}");
HashSet::new()
}
};
for table in tables.iter_mut() {
if is_materialized_view_type(&table.table_type)
|| materialized_view_names.iter().any(|name| name.eq_ignore_ascii_case(&table.name))
{
table.table_type = "VIEW".to_string();
}
}
let mut known_names: HashSet<String> = tables.iter().map(|table| table.name.to_ascii_lowercase()).collect();
let mut missing_names = materialized_view_names.into_iter().collect::<Vec<_>>();
missing_names.sort();
for name in missing_names {
if known_names.insert(name.to_ascii_lowercase()) {
tables.push(TableInfo {
name,
table_type: "VIEW".to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
});
}
}
tables.sort_by(|left, right| left.name.cmp(&right.name));
}
async fn list_tables_with_status(
pool: &MySqlPool,
database: &str,
) -> Result<(Vec<TableInfo>, HashMap<String, TableStatusMeta>), String> {
let (tables, materialized_view_names) =
tokio::join!(list_tables_show_with_status(pool, database), list_materialized_view_names(pool, database));
let (mut tables, status) = tables?;
merge_materialized_views(&mut tables, materialized_view_names, database);
Ok((tables, status))
}
pub async fn list_tables(pool: &MySqlPool, database: &str) -> Result<Vec<TableInfo>, String> {
list_tables_with_status(pool, database).await.map(|(tables, _)| tables)
}
pub async fn list_objects(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectInfo>, String> {
let (tables, routines) =
tokio::join!(list_tables_with_status(pool, database), list_routine_objects(pool, database));
let (tables, status) = tables?;
let mut objects = table_infos_to_objects(tables, &status, database);
match routines {
Ok(routines) => objects.extend(routines),
Err(error) => log::warn!("Skipping routines for OceanBase database `{database}` in object browser: {error}"),
}
Ok(objects)
}
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::Mysql
&& driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case(DRIVER_PROFILE))
}
pub fn query_timeout_sql(config: &ConnectionConfig, timeout_secs: u64) -> Option<String> {
if !is_config(config) || timeout_secs == 0 {
return None;
}
Some(format!("SET ob_query_timeout = {}", timeout_secs.saturating_mul(1_000_000)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_is_isolated_from_oracle_mode_and_standard_mysql() {
assert!(is_profile(&DatabaseType::Mysql, Some("OceanBase")));
assert!(!is_profile(&DatabaseType::Mysql, None));
assert!(!is_profile(&DatabaseType::OceanbaseOracle, Some("oceanbase")));
}
#[test]
fn query_timeout_uses_microseconds_and_saturates() {
let mut config = test_config();
assert_eq!(query_timeout_sql(&config, 30), Some("SET ob_query_timeout = 30000000".to_string()));
assert_eq!(query_timeout_sql(&config, u64::MAX), Some(format!("SET ob_query_timeout = {}", u64::MAX)));
config.driver_profile = None;
assert_eq!(query_timeout_sql(&config, 30), None);
}
#[test]
fn materialized_views_are_visible_as_mysql_views_without_retyping_regular_objects() {
let mut tables =
vec![table("orders", "BASE TABLE"), table("orders_view", "VIEW"), table("orders_mv", "BASE TABLE")];
merge_materialized_views(
&mut tables,
Ok(HashSet::from(["orders_mv".to_string(), "daily_orders_mv".to_string()])),
"analytics",
);
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![("daily_orders_mv", "VIEW"), ("orders", "BASE TABLE"), ("orders_mv", "VIEW"), ("orders_view", "VIEW"),]
);
let objects = table_infos_to_objects(tables, &HashMap::new(), "analytics");
assert_eq!(
objects.iter().map(|object| (object.name.as_str(), object.object_type.as_str())).collect::<Vec<_>>(),
vec![("daily_orders_mv", "VIEW"), ("orders", "TABLE"), ("orders_mv", "VIEW"), ("orders_view", "VIEW"),]
);
}
#[test]
fn materialized_view_names_are_case_insensitive_and_not_duplicated() {
let mut tables = vec![table("Orders_MV", "BASE TABLE")];
merge_materialized_views(
&mut tables,
Ok(HashSet::from(["orders_mv".to_string(), "daily_mv".to_string()])),
"analytics",
);
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![("Orders_MV", "VIEW"), ("daily_mv", "VIEW")]
);
}
#[test]
fn materialized_view_catalog_failures_preserve_show_rows() {
let mut tables = vec![table("orders", "BASE TABLE"), table("orders_mv", "MATERIALIZED VIEW")];
merge_materialized_views(&mut tables, Err("permission denied".to_string()), "analytics");
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![("orders", "BASE TABLE"), ("orders_mv", "VIEW")]
);
}
#[test]
fn empty_materialized_view_metadata_does_not_retype_regular_objects() {
let mut tables = vec![table("orders", "BASE TABLE"), table("orders_view", "VIEW")];
merge_materialized_views(&mut tables, Ok(HashSet::new()), "analytics");
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![("orders", "BASE TABLE"), ("orders_view", "VIEW")]
);
}
#[test]
fn materialized_view_catalog_sql_escapes_database_names() {
assert_eq!(
materialized_views_sql("tenant's analytics"),
"SELECT MVIEW_NAME FROM oceanbase.DBA_MVIEWS WHERE OWNER = 'tenant\\'s analytics'"
);
}
fn test_config() -> ConnectionConfig {
serde_json::from_value(serde_json::json!({
"id": "oceanbase",
"name": "OceanBase",
"db_type": "mysql",
"driver_profile": "oceanbase",
"host": "127.0.0.1",
"port": 2883,
"username": "root",
"password": "",
"database": "test"
}))
.unwrap()
}
fn table(name: &str, table_type: &str) -> TableInfo {
TableInfo {
name: name.to_string(),
table_type: table_type.to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
}
}
}
+255
View File
@@ -0,0 +1,255 @@
use mysql_async::prelude::*;
use std::collections::HashSet;
use crate::models::connection::{ConnectionConfig, DatabaseType};
use crate::types::{ObjectInfo, TableInfo};
use super::mysql::{
get_conn_with_timeout, get_str_by_name, list_routine_objects, list_tables_show_with_status, quote_value,
table_infos_to_objects, MySqlPool, TableStatusMeta,
};
pub use super::mysql_compatible::{
get_columns_show_from as get_catalog_columns, list_catalog_indexes, list_catalogs,
list_databases_show_from as list_catalog_databases, list_indexes_with_ddl_fallback as list_indexes,
show_create_table_ddl_from as get_catalog_table_ddl,
};
pub async fn list_catalog_tables(pool: &MySqlPool, catalog: &str, database: &str) -> Result<Vec<TableInfo>, String> {
match super::mysql_compatible::list_tables_show_from(pool, catalog, database).await {
Ok(tables) => Ok(tables),
Err(error) if should_retry_catalog_table_listing(&error) => {
match super::mysql_compatible::list_databases_show_from(pool, catalog).await {
Ok(databases) if catalog_database_exists(&databases, database) => {
log::debug!(
"Treating StarRocks external database `{catalog}`.`{database}` as empty after its qualified table lookup failed: {error}"
);
Ok(Vec::new())
}
_ => Err(error),
}
}
Err(error) => Err(error),
}
}
fn should_retry_catalog_table_listing(error: &str) -> bool {
error.to_ascii_lowercase().contains("unknown database")
}
fn catalog_database_exists(databases: &[crate::types::DatabaseInfo], database: &str) -> bool {
databases.iter().any(|candidate| candidate.name == database)
}
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::StarRocks
|| (*db_type == DatabaseType::Mysql
&& driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case("starrocks")))
}
fn materialized_views_sql(database: &str) -> String {
format!(
"SELECT TABLE_NAME FROM information_schema.materialized_views WHERE TABLE_SCHEMA = {}",
quote_value(database)
)
}
pub fn materialized_view_definition_sql(database: &str, name: &str) -> String {
format!(
"SELECT MATERIALIZED_VIEW_DEFINITION \
FROM information_schema.materialized_views \
WHERE TABLE_SCHEMA = {} AND TABLE_NAME = {} \
LIMIT 1",
quote_value(database),
quote_value(name)
)
}
async fn list_materialized_view_names(pool: &MySqlPool, database: &str) -> Result<HashSet<String>, String> {
let sql = materialized_views_sql(database);
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let result = conn.query_iter(&sql).await.map_err(|error| error.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|error| error.to_string())?;
Ok(rows
.iter()
.filter_map(|row| {
let name = get_str_by_name(row, "TABLE_NAME").trim().to_string();
(!name.is_empty()).then_some(name)
})
.collect())
}
fn merge_materialized_views(
tables: &mut Vec<TableInfo>,
materialized_view_names: Result<HashSet<String>, String>,
database: &str,
) {
let materialized_view_names = match materialized_view_names {
Ok(names) => names,
Err(error) => {
log::warn!("Skipping materialized view classification for StarRocks database `{database}`: {error}");
return;
}
};
let known_names: HashSet<String> = tables.iter().map(|table| table.name.clone()).collect();
for table in tables.iter_mut() {
if materialized_view_names.contains(&table.name) {
table.table_type = "MATERIALIZED_VIEW".to_string();
}
}
let mut sorted_names: Vec<&String> = materialized_view_names.iter().collect();
sorted_names.sort();
for name in sorted_names {
if !known_names.contains(name.as_str()) {
tables.push(TableInfo {
name: name.clone(),
table_type: "MATERIALIZED_VIEW".to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
});
}
}
}
async fn list_tables_with_status(
pool: &MySqlPool,
database: &str,
) -> Result<(Vec<TableInfo>, std::collections::HashMap<String, TableStatusMeta>), String> {
let (tables, materialized_view_names) =
tokio::join!(list_tables_show_with_status(pool, database), list_materialized_view_names(pool, database));
let (mut tables, status) = tables?;
merge_materialized_views(&mut tables, materialized_view_names, database);
Ok((tables, status))
}
pub async fn list_tables(pool: &MySqlPool, database: &str) -> Result<Vec<TableInfo>, String> {
list_tables_with_status(pool, database).await.map(|(tables, _)| tables)
}
pub async fn list_table_objects(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectInfo>, String> {
let (tables, routines) =
tokio::join!(list_tables_with_status(pool, database), list_routine_objects(pool, database));
let (tables, status) = tables?;
let mut objects = table_infos_to_objects(tables, &status, database);
match routines {
Ok(routines) => objects.extend(routines),
Err(error) => log::warn!("Skipping routines for database `{database}` in object browser: {error}"),
}
Ok(objects)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_matches_starrocks_only() {
assert!(is_profile(&DatabaseType::StarRocks, None));
assert!(is_profile(&DatabaseType::Mysql, Some("STARROCKS")));
assert!(!is_profile(&DatabaseType::Mysql, Some("doris")));
assert!(!is_profile(&DatabaseType::Postgres, Some("starrocks")));
}
#[test]
fn catalog_table_listing_retries_only_unknown_database_errors() {
assert!(should_retry_catalog_table_listing(
"ERROR 5501 (3F000): Getting analyzing error. Detail message: Unknown database 'edw_dwt'."
));
assert!(!should_retry_catalog_table_listing("Access denied on database edw_dwt"));
assert!(!should_retry_catalog_table_listing("Connection timed out"));
}
#[test]
fn catalog_database_existence_requires_an_exact_name_match() {
let databases = vec![crate::types::DatabaseInfo { name: "edw_dwt".to_string(), ..Default::default() }];
assert!(catalog_database_exists(&databases, "edw_dwt"));
assert!(!catalog_database_exists(&databases, "EDW_DWT"));
assert!(!catalog_database_exists(&databases, "missing"));
}
#[test]
fn materialized_views_are_classified_without_duplicates() {
let mut tables = vec![table("orders", "BASE TABLE"), table("orders_view", "VIEW"), table("orders_mv", "VIEW")];
merge_materialized_views(&mut tables, Ok(HashSet::from(["orders_mv".to_string()])), "analytics");
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![("orders", "BASE TABLE"), ("orders_view", "VIEW"), ("orders_mv", "MATERIALIZED_VIEW")]
);
}
#[test]
fn missing_materialized_views_are_appended_deterministically() {
let mut tables = vec![table("orders", "BASE TABLE")];
let names = HashSet::from(["orders_mv".to_string(), "daily_orders_mv".to_string()]);
merge_materialized_views(&mut tables, Ok(names), "analytics");
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![
("orders", "BASE TABLE"),
("daily_orders_mv", "MATERIALIZED_VIEW"),
("orders_mv", "MATERIALIZED_VIEW"),
]
);
}
#[test]
fn lookup_failure_preserves_base_table_types() {
let mut tables = vec![table("orders_mv", "VIEW")];
merge_materialized_views(&mut tables, Err("permission denied".to_string()), "analytics");
assert_eq!(tables[0].table_type, "VIEW");
}
#[test]
fn materialized_view_queries_escape_database_and_name() {
assert_eq!(
materialized_views_sql("tenant's analytics"),
"SELECT TABLE_NAME FROM information_schema.materialized_views WHERE TABLE_SCHEMA = 'tenant\\'s analytics'"
);
assert_eq!(
materialized_view_definition_sql("tenant's analytics", "weird'name"),
"SELECT MATERIALIZED_VIEW_DEFINITION FROM information_schema.materialized_views WHERE TABLE_SCHEMA = 'tenant\\'s analytics' AND TABLE_NAME = 'weird\\'name' LIMIT 1"
);
}
#[test]
fn object_conversion_preserves_materialized_view_type() {
let objects = table_infos_to_objects(
vec![table("orders", "BASE TABLE"), table("orders_view", "VIEW"), table("orders_mv", "MATERIALIZED_VIEW")],
&std::collections::HashMap::new(),
"analytics",
);
assert_eq!(
objects.iter().map(|object| (object.name.as_str(), object.object_type.as_str())).collect::<Vec<_>>(),
vec![("orders", "TABLE"), ("orders_view", "VIEW"), ("orders_mv", "MATERIALIZED_VIEW")]
);
}
fn table(name: &str, table_type: &str) -> TableInfo {
TableInfo {
name: name.to_string(),
table_type: table_type.to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
}
}
}
+24
View File
@@ -0,0 +1,24 @@
use crate::models::connection::{ConnectionConfig, DatabaseType};
pub const DRIVER_PROFILE: &str = "tidb";
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::Mysql
&& driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case(DRIVER_PROFILE))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_is_isolated_from_standard_mysql() {
assert!(is_profile(&DatabaseType::Mysql, Some("TiDB")));
assert!(!is_profile(&DatabaseType::Mysql, None));
assert!(!is_profile(&DatabaseType::Postgres, Some("tidb")));
}
}