use aws_sdk_athena::types::Column;
use prettytable::Cell;
pub struct ColumnDisplay {
name: String,
column_type: String,
comment: String,
}
impl From<&Column> for ColumnDisplay {
fn from(column: &Column) -> Self {
Self {
name: column.name().to_string(),
column_type: column.r#type().unwrap_or("").to_string(),
comment: column.comment().unwrap_or("").to_string(),
}
}
}
impl From<Column> for ColumnDisplay {
fn from(column: Column) -> Self {
Self::from(&column)
}
}
impl ColumnDisplay {
pub fn to_row(&self) -> prettytable::Row {
prettytable::Row::new(vec![
Cell::new(&self.name),
Cell::new(&self.column_type),
Cell::new(&self.comment),
])
}
pub fn create_columns_table(columns: &[Column]) -> prettytable::Table {
let mut table = prettytable::Table::new();
table.add_row(header_row(&["Name", "Type", "Description"]));
for column in columns {
let display = ColumnDisplay::from(column);
table.add_row(display.to_row());
}
table
}
}
pub struct ParameterDisplay {
name: String,
value: String,
}
impl ParameterDisplay {
pub fn to_row(&self) -> prettytable::Row {
prettytable::Row::new(vec![Cell::new(&self.name), Cell::new(&self.value)])
}
pub fn create_parameters_table(
parameters: &std::collections::HashMap<String, String>,
exclude_keys: &[&str],
) -> prettytable::Table {
let mut table = prettytable::Table::new();
table.add_row(header_row(&["Parameter", "Value"]));
for (key, value) in parameters {
if exclude_keys.contains(&key.as_str()) {
continue;
}
let display = Self {
name: key.clone(),
value: value.clone(),
};
table.add_row(display.to_row());
}
table
}
}
pub struct DatabaseDisplay {
name: String,
description: String,
}
impl DatabaseDisplay {
pub fn from_database(db: &aws_sdk_athena::types::Database) -> Self {
Self {
name: db.name().to_string(),
description: db.description().unwrap_or("").to_string(),
}
}
pub fn to_row(&self) -> prettytable::Row {
prettytable::Row::new(vec![Cell::new(&self.name), Cell::new(&self.description)])
}
pub fn create_databases_table(
databases: &[aws_sdk_athena::types::Database],
) -> prettytable::Table {
let mut table = prettytable::Table::new();
table.add_row(header_row(&["Name", "Description"]));
for db in databases {
let display = Self::from_database(db);
table.add_row(display.to_row());
}
table
}
}
pub struct TableMetadataDisplay {
name: String,
table_type: String,
column_count: usize,
}
impl TableMetadataDisplay {
pub fn from_table_metadata(table: &aws_sdk_athena::types::TableMetadata) -> Self {
Self {
name: table.name().to_string(),
table_type: table.table_type().unwrap_or("").to_string(),
column_count: table.columns().len(),
}
}
pub fn to_row(&self) -> prettytable::Row {
prettytable::Row::new(vec![
Cell::new(&self.name),
Cell::new(&self.table_type),
Cell::new(&self.column_count.to_string()),
])
}
pub fn create_table_metadata_table(
tables: &[&aws_sdk_athena::types::TableMetadata],
) -> prettytable::Table {
let mut table = prettytable::Table::new();
table.add_row(prettytable::Row::new(vec![
Cell::new("Name"),
Cell::new("Type"),
Cell::new("Columns"),
]));
for table_meta in tables {
let display = Self::from_table_metadata(table_meta);
table.add_row(display.to_row());
}
table
}
}
pub fn header_cell(text: &str) -> Cell {
Cell::new(text).style_spec("Fb")
}
pub fn header_row(headers: &[&str]) -> prettytable::Row {
let cells = headers.iter().map(|&text| header_cell(text)).collect();
prettytable::Row::new(cells)
}