Skip to main content

cli/metadata/control/
selector.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use 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/// Selects a table by id or by fully qualified name.
26#[derive(Debug, Clone, Default, Args)]
27pub(crate) struct TableSelector {
28    /// The table id to select from the metadata store.
29    #[clap(long)]
30    table_id: Option<u32>,
31
32    /// The table name to select from the metadata store.
33    #[clap(long)]
34    table_name: Option<String>,
35
36    /// The schema name of the table.
37    #[clap(long, default_value = DEFAULT_SCHEMA_NAME)]
38    schema_name: String,
39
40    /// The catalog name of the table.
41    #[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}