feat(mysql): optimize table structure loading
This commit is contained in:
10 files changed
+469
-96
No files matched your search
@@ -1025,6 +1025,29 @@ pub async fn list_tables(pool: &MySqlPool, database: &str) -> Result<Vec<TableIn
|
||||
Ok(tables)
|
||||
}
|
||||
|
||||
fn table_comment_sql(database: &str, table: &str) -> String {
|
||||
format!(
|
||||
"SELECT TABLE_COMMENT \
|
||||
FROM information_schema.TABLES \
|
||||
WHERE TABLE_SCHEMA = {} AND TABLE_NAME = {} AND TABLE_TYPE <> 'VIEW' \
|
||||
LIMIT 1",
|
||||
quote_value(database),
|
||||
quote_value(table),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn get_table_comment(pool: &MySqlPool, database: &str, table: &str) -> Result<Option<String>, String> {
|
||||
let sql = table_comment_sql(database, 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
|
||||
.first()
|
||||
.and_then(|row| get_opt_str(row, "TABLE_COMMENT"))
|
||||
.map(|s| fix_potential_double_encoding(&s))
|
||||
.filter(|s| !s.is_empty()))
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct TableStatusMeta {
|
||||
comment: Option<String>,
|
||||
@@ -2074,6 +2097,18 @@ mod tests {
|
||||
assert!(sql.contains("UPDATE_TIME"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_table_comment_sql_targets_single_table() {
|
||||
let sql = table_comment_sql("app", "users");
|
||||
|
||||
assert!(sql.contains("SELECT TABLE_COMMENT"));
|
||||
assert!(sql.contains("TABLE_SCHEMA = 'app'"));
|
||||
assert!(sql.contains("TABLE_NAME = 'users'"));
|
||||
assert!(sql.contains("TABLE_TYPE <> 'VIEW'"));
|
||||
assert!(sql.contains("LIMIT 1"));
|
||||
assert!(!sql.contains("ORDER BY"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_database_infos_filter_blank_names_and_keep_catalogless_marker() {
|
||||
let regular = database_infos_from_names(vec!["".to_string(), " app ".to_string(), "mysql".to_string()], true);
|
||||
|
||||
@@ -964,6 +964,22 @@ pub async fn list_tables_filtered(
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn get_table_comment(pool: &Pool, schema: &str, table: &str) -> Result<Option<String>, String> {
|
||||
let schema = if schema.is_empty() { "public" } else { schema };
|
||||
let client = pool.get().await.map_err(|e| e.to_string())?;
|
||||
let stmt = client.prepare_cached(postgres_table_comment_sql()).await.map_err(|e| e.to_string())?;
|
||||
let rows = client.query(&stmt, &[&schema, &table]).await.map_err(|e| e.to_string())?;
|
||||
Ok(rows.first().and_then(|row| row.try_get::<_, Option<String>>(0).ok().flatten()).filter(|s| !s.is_empty()))
|
||||
}
|
||||
|
||||
fn postgres_table_comment_sql() -> &'static str {
|
||||
"SELECT obj_description(c.oid) AS table_comment \
|
||||
FROM pg_catalog.pg_class c \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
|
||||
WHERE n.nspname = $1 AND c.relname = $2 AND c.relkind IN ('r','m','f','p') \
|
||||
LIMIT 1"
|
||||
}
|
||||
|
||||
fn postgres_tables_sql() -> &'static str {
|
||||
"SELECT c.relname AS table_name, \
|
||||
CASE c.relkind WHEN 'r' THEN 'BASE TABLE' WHEN 'v' THEN 'VIEW' \
|
||||
@@ -2292,6 +2308,17 @@ mod tests {
|
||||
assert!(sql.contains("FOREIGN TABLE"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_table_comment_sql_targets_single_table() {
|
||||
let sql = postgres_table_comment_sql();
|
||||
|
||||
assert!(sql.contains("obj_description(c.oid)"));
|
||||
assert!(sql.contains("n.nspname = $1"));
|
||||
assert!(sql.contains("c.relname = $2"));
|
||||
assert!(sql.contains("LIMIT 1"));
|
||||
assert!(!sql.contains("ORDER BY"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_column_metadata_reads_identity_extra() {
|
||||
assert!(POSTGRES_COLUMNS_SQL.contains("a.attidentity"));
|
||||
|
||||
@@ -494,6 +494,46 @@ pub async fn list_tables_core(
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_table_comment_core(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
schema: &str,
|
||||
table: &str,
|
||||
) -> Result<Option<String>, String> {
|
||||
if crate::sql_dialect::parse_sqlserver_linked_schema_ref(schema).is_some() {
|
||||
return Err("Table comments are not available for linked server tables".to_string());
|
||||
}
|
||||
|
||||
retry_metadata_connection(state, connection_id, Some(database), || async {
|
||||
let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
|
||||
let db_config = connection_config(state, connection_id).await;
|
||||
|
||||
{
|
||||
let connections = state.connections.read().await;
|
||||
try_sqlserver!(connections, &pool_key, get_table_comment, schema, table);
|
||||
}
|
||||
|
||||
let connections = state.connections.read().await;
|
||||
let pool = connections.get(&pool_key).ok_or("Pool not found")?;
|
||||
|
||||
match pool {
|
||||
PoolKind::Mysql(p, mode)
|
||||
if *mode != MysqlMode::OceanBaseOracle
|
||||
&& !db_config.as_ref().is_some_and(is_doris_family_config)
|
||||
&& !db_config.as_ref().is_some_and(is_manticoresearch_config) =>
|
||||
{
|
||||
db::mysql::get_table_comment(p, schema, table).await
|
||||
}
|
||||
PoolKind::Postgres(p) if !db_config.as_ref().is_some_and(is_questdb_config) => {
|
||||
db::postgres::get_table_comment(p, schema, table).await
|
||||
}
|
||||
_ => Err("Table comment lookup is not supported for this connection".to_string()),
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn list_tables_once(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
|
||||
Reference in new issue
Block a user