feat(structure): search and paginate source table picker for copy columns

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-24 18:47:13 +08:00
1 parent 5bafb68919
commit ff4667b93f
14 files changed
+460 -60

No files matched your search

+46 -2
View File
@@ -2831,6 +2831,29 @@ pub async fn list_tables_filtered(
filter: Option<&str>,
limit: Option<usize>,
offset: Option<usize>,
) -> Result<Vec<TableInfo>, String> {
list_tables_filtered_by_kind(pool, schema, filter, limit, offset, false).await
}
/// Lists only table-like relations, excluding views and materialized views while
/// retaining server-side filtering and pagination.
pub async fn list_table_objects_filtered(
pool: &Pool,
schema: &str,
filter: Option<&str>,
limit: Option<usize>,
offset: Option<usize>,
) -> Result<Vec<TableInfo>, String> {
list_tables_filtered_by_kind(pool, schema, filter, limit, offset, true).await
}
async fn list_tables_filtered_by_kind(
pool: &Pool,
schema: &str,
filter: Option<&str>,
limit: Option<usize>,
offset: Option<usize>,
table_objects_only: bool,
) -> Result<Vec<TableInfo>, String> {
let schema = if schema.is_empty() { "public" } else { schema };
let filter = filter.unwrap_or("").trim();
@@ -2840,7 +2863,11 @@ pub async fn list_tables_filtered(
let limit_param = limit.and_then(|value| i64::try_from(value).ok());
let offset_param = offset.and_then(|value| i64::try_from(value).ok()).unwrap_or(0);
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
let sql = postgres_tables_sql(limit_param, offset_param);
let sql = if table_objects_only {
postgres_table_objects_sql(limit_param, offset_param)
} else {
postgres_tables_sql(limit_param, offset_param)
};
let params: &[(&(dyn tokio_postgres::types::ToSql + Sync), Type)] =
&[(&schema, Type::VARCHAR), (&filter_pattern, Type::VARCHAR), (&fuzzy_filter_pattern, Type::VARCHAR)];
// The pagination literals make this SQL vary by page. Use an unnamed typed
@@ -4030,6 +4057,14 @@ fn postgres_table_comment_sql() -> &'static str {
}
fn postgres_tables_sql(limit: Option<i64>, offset: i64) -> String {
postgres_tables_sql_with_kind(limit, offset, false)
}
fn postgres_table_objects_sql(limit: Option<i64>, offset: i64) -> String {
postgres_tables_sql_with_kind(limit, offset, true)
}
fn postgres_tables_sql_with_kind(limit: Option<i64>, offset: i64, table_objects_only: bool) -> String {
// PostgreSQL-compatible servers do not agree on the inferred wire types
// or accepted expression grammar for LIMIT/OFFSET parameters. These values
// originate as usize and are converted to non-negative i64 literals.
@@ -4039,6 +4074,7 @@ fn postgres_tables_sql(limit: Option<i64>, offset: i64) -> String {
Some(limit) => format!("LIMIT {limit} OFFSET {offset}"),
None => format!("OFFSET {offset}"),
};
let relation_kinds = if table_objects_only { "'r','f','p'" } else { "'r','v','m','f','p'" };
format!(
"SELECT c.relname AS table_name, \
CASE c.relkind WHEN 'r' THEN 'BASE TABLE' WHEN 'v' THEN 'VIEW' \
@@ -4052,7 +4088,7 @@ fn postgres_tables_sql(limit: Option<i64>, offset: i64) -> String {
LEFT JOIN pg_catalog.pg_inherits i ON i.inhrelid = c.oid \
LEFT JOIN pg_catalog.pg_class pc ON pc.oid = i.inhparent \
LEFT JOIN pg_catalog.pg_namespace pn ON pn.oid = pc.relnamespace \
WHERE n.nspname = $1 AND c.relkind IN ('r','v','m','f','p') \
WHERE n.nspname = $1 AND c.relkind IN ({relation_kinds}) \
AND ($2 = '%%' OR c.relname ILIKE $2 OR ($3 <> '' AND c.relname ILIKE $3)) \
ORDER BY CASE WHEN pc.relkind = 'p' THEN 1 ELSE 0 END, c.relname \
{pagination}"
@@ -9831,6 +9867,14 @@ mod tests {
assert!(sql.contains("FOREIGN TABLE"));
}
#[test]
fn postgres_table_objects_sql_excludes_views_before_pagination() {
let sql = postgres_table_objects_sql(Some(101), 100);
assert!(sql.contains("c.relkind IN ('r','f','p')"));
assert!(sql.contains("LIMIT 101 OFFSET 100"));
}
#[test]
fn postgres_table_comment_sql_targets_single_table() {
let sql = postgres_table_comment_sql();