athena_cli/commands/database/utils/
display.rs1use aws_sdk_athena::types::Column;
2use prettytable::Cell;
3
4pub struct ColumnDisplay {
6 name: String,
8 column_type: String,
10 comment: String,
12}
13
14impl From<&Column> for ColumnDisplay {
15 fn from(column: &Column) -> Self {
16 Self {
17 name: column.name().to_string(),
18 column_type: column.r#type().unwrap_or("").to_string(),
19 comment: column.comment().unwrap_or("").to_string(),
20 }
21 }
22}
23
24impl From<Column> for ColumnDisplay {
25 fn from(column: Column) -> Self {
26 Self::from(&column)
27 }
28}
29
30impl ColumnDisplay {
31 pub fn to_row(&self) -> prettytable::Row {
33 prettytable::Row::new(vec![
34 Cell::new(&self.name),
35 Cell::new(&self.column_type),
36 Cell::new(&self.comment),
37 ])
38 }
39
40 pub fn create_columns_table(columns: &[Column]) -> prettytable::Table {
42 let mut table = prettytable::Table::new();
43
44 table.add_row(header_row(&["Name", "Type", "Description"]));
46
47 for column in columns {
49 let display = ColumnDisplay::from(column);
50 table.add_row(display.to_row());
51 }
52
53 table
54 }
55}
56
57pub struct ParameterDisplay {
59 name: String,
61 value: String,
63}
64
65impl ParameterDisplay {
66 pub fn to_row(&self) -> prettytable::Row {
68 prettytable::Row::new(vec![Cell::new(&self.name), Cell::new(&self.value)])
69 }
70
71 pub fn create_parameters_table(
73 parameters: &std::collections::HashMap<String, String>,
74 exclude_keys: &[&str],
75 ) -> prettytable::Table {
76 let mut table = prettytable::Table::new();
77
78 table.add_row(header_row(&["Parameter", "Value"]));
80
81 for (key, value) in parameters {
83 if exclude_keys.contains(&key.as_str()) {
85 continue;
86 }
87
88 let display = Self {
89 name: key.clone(),
90 value: value.clone(),
91 };
92 table.add_row(display.to_row());
93 }
94
95 table
96 }
97}
98
99pub struct DatabaseDisplay {
101 name: String,
103 description: String,
105}
106
107impl DatabaseDisplay {
108 pub fn from_database(db: &aws_sdk_athena::types::Database) -> Self {
110 Self {
111 name: db.name().to_string(),
112 description: db.description().unwrap_or("").to_string(),
113 }
114 }
115
116 pub fn to_row(&self) -> prettytable::Row {
118 prettytable::Row::new(vec![Cell::new(&self.name), Cell::new(&self.description)])
119 }
120
121 pub fn create_databases_table(
123 databases: &[aws_sdk_athena::types::Database],
124 ) -> prettytable::Table {
125 let mut table = prettytable::Table::new();
126
127 table.add_row(header_row(&["Name", "Description"]));
129
130 for db in databases {
132 let display = Self::from_database(db);
133 table.add_row(display.to_row());
134 }
135
136 table
137 }
138}
139
140pub struct TableMetadataDisplay {
142 name: String,
144 table_type: String,
146 column_count: usize,
148}
149
150impl TableMetadataDisplay {
151 pub fn from_table_metadata(table: &aws_sdk_athena::types::TableMetadata) -> Self {
153 Self {
154 name: table.name().to_string(),
155 table_type: table.table_type().unwrap_or("").to_string(),
156 column_count: table.columns().len(),
157 }
158 }
159
160 pub fn to_row(&self) -> prettytable::Row {
162 prettytable::Row::new(vec![
163 Cell::new(&self.name),
164 Cell::new(&self.table_type),
165 Cell::new(&self.column_count.to_string()),
166 ])
167 }
168
169 pub fn create_table_metadata_table(
171 tables: &[&aws_sdk_athena::types::TableMetadata],
172 ) -> prettytable::Table {
173 let mut table = prettytable::Table::new();
174
175 table.add_row(prettytable::Row::new(vec![
177 Cell::new("Name"),
178 Cell::new("Type"),
179 Cell::new("Columns"),
180 ]));
181
182 for table_meta in tables {
184 let display = Self::from_table_metadata(table_meta);
185 table.add_row(display.to_row());
186 }
187
188 table
189 }
190}
191
192pub fn header_cell(text: &str) -> Cell {
194 Cell::new(text).style_spec("Fb")
195}
196
197pub fn header_row(headers: &[&str]) -> prettytable::Row {
199 let cells = headers.iter().map(|&text| header_cell(text)).collect();
200 prettytable::Row::new(cells)
201}