|
| 1 | +use std::collections::HashMap; |
1 | 2 | use std::num::NonZeroI32; |
2 | 3 |
|
3 | 4 | use etl_config::shared::{IntoConnectOptions, PgConnectionConfig}; |
@@ -37,38 +38,44 @@ pub async fn connect_to_source_database( |
37 | 38 | Ok(pool) |
38 | 39 | } |
39 | 40 |
|
40 | | -/// Retrieves table name from table OID by querying system catalogs. |
| 41 | +/// Retrieves table names for multiple table ids in a single query. |
41 | 42 | /// |
42 | | -/// Looks up the schema and table name for the given table OID using Postgres's |
43 | | -/// pg_class and pg_namespace system tables. |
44 | | -pub async fn get_table_name_from_oid( |
| 43 | +/// Looks up the schema and table names for all given table OIDs using Postgres's |
| 44 | +/// pg_class and pg_namespace system tables. Returns a HashMap mapping each TableId |
| 45 | +/// to its corresponding TableName. |
| 46 | +pub async fn get_table_names_from_table_ids( |
45 | 47 | pool: &PgPool, |
46 | | - table_id: TableId, |
47 | | -) -> Result<TableName, TableLookupError> { |
48 | | - let query = " |
49 | | - select n.nspname as schema_name, c.relname as table_name |
50 | | - from pg_class c |
51 | | - join pg_namespace n on c.relnamespace = n.oid |
52 | | - where c.oid = $1 |
| 48 | + table_ids: &[TableId], |
| 49 | +) -> Result<HashMap<TableId, TableName>, TableLookupError> { |
| 50 | + if table_ids.is_empty() { |
| 51 | + return Ok(HashMap::new()); |
| 52 | + } |
| 53 | + |
| 54 | + let query = "select c.oid::int as oid, n.nspname as schema_name, c.relname as table_name |
| 55 | + from pg_class c |
| 56 | + join pg_namespace n on c.relnamespace = n.oid |
| 57 | + where c.oid = any($1::oid[]) |
53 | 58 | "; |
54 | 59 |
|
55 | | - let row = sqlx::query(query) |
56 | | - .bind(table_id.into_inner() as i64) |
57 | | - .fetch_optional(pool) |
58 | | - .await?; |
| 60 | + let oids: Vec<i32> = table_ids.iter().map(|id| id.into_inner() as i32).collect(); |
| 61 | + let rows = sqlx::query(query).bind(&oids).fetch_all(pool).await?; |
59 | 62 |
|
60 | | - match row { |
61 | | - Some(row) => { |
62 | | - let schema_name: String = row.try_get("schema_name")?; |
63 | | - let table_name: String = row.try_get("table_name")?; |
| 63 | + let mut result = HashMap::with_capacity(rows.len()); |
| 64 | + for row in rows { |
| 65 | + let oid: i32 = row.try_get("oid")?; |
| 66 | + let schema_name: String = row.try_get("schema_name")?; |
| 67 | + let table_name: String = row.try_get("table_name")?; |
64 | 68 |
|
65 | | - Ok(TableName { |
| 69 | + result.insert( |
| 70 | + TableId::new(oid as u32), |
| 71 | + TableName { |
66 | 72 | schema: schema_name, |
67 | 73 | name: table_name, |
68 | | - }) |
69 | | - } |
70 | | - None => Err(TableLookupError::TableNotFound(table_id)), |
| 74 | + }, |
| 75 | + ); |
71 | 76 | } |
| 77 | + |
| 78 | + Ok(result) |
72 | 79 | } |
73 | 80 |
|
74 | 81 | /// Extracts the PostgreSQL server version from a version string. |
|
0 commit comments