cli/metadata/control/
selector.rs1use clap::Args;
16use client::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
17use common_catalog::format_full_table_name;
18use common_error::ext::BoxedError;
19use common_meta::key::table_name::TableNameManager;
20use store_api::storage::TableId;
21
22use crate::error::InvalidArgumentsSnafu;
23use crate::metadata::control::utils::get_table_id_by_name;
24
25#[derive(Debug, Clone, Default, Args)]
27pub(crate) struct TableSelector {
28 #[clap(long)]
30 table_id: Option<u32>,
31
32 #[clap(long)]
34 table_name: Option<String>,
35
36 #[clap(long, default_value = DEFAULT_SCHEMA_NAME)]
38 schema_name: String,
39
40 #[clap(long, default_value = DEFAULT_CATALOG_NAME)]
42 catalog_name: String,
43}
44
45impl TableSelector {
46 pub(crate) fn validate(&self) -> Result<(), BoxedError> {
47 if matches!(
48 (&self.table_id, &self.table_name),
49 (Some(_), Some(_)) | (None, None)
50 ) {
51 return Err(BoxedError::new(
52 InvalidArgumentsSnafu {
53 msg: "You must specify either --table-id or --table-name.",
54 }
55 .build(),
56 ));
57 }
58
59 Ok(())
60 }
61
62 pub(crate) async fn resolve_table_id(
63 &self,
64 table_name_manager: &TableNameManager,
65 ) -> Result<Option<TableId>, BoxedError> {
66 if let Some(table_id) = self.table_id {
67 return Ok(Some(table_id));
68 }
69
70 get_table_id_by_name(
71 table_name_manager,
72 &self.catalog_name,
73 &self.schema_name,
74 self.table_name
75 .as_deref()
76 .expect("validated table selector"),
77 )
78 .await
79 }
80
81 pub(crate) fn formatted_table_name(&self) -> String {
82 format_full_table_name(
83 &self.catalog_name,
84 &self.schema_name,
85 self.table_name.as_deref().unwrap_or_default(),
86 )
87 }
88}
89
90#[cfg(test)]
91impl TableSelector {
92 pub(crate) fn with_table_id(table_id: u32) -> Self {
93 Self {
94 table_id: Some(table_id),
95 table_name: None,
96 schema_name: DEFAULT_SCHEMA_NAME.to_string(),
97 catalog_name: DEFAULT_CATALOG_NAME.to_string(),
98 }
99 }
100}