use crate::edb::io::Writer;
use crate::error::Result;
use crate::{Collection, Relation};
use std::fs::File;
use std::io::Write;
use std::path::Path;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Options {}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TextTables {}
pub const PRAGMA_ID: &str = "text";
impl Writer for TextTables {
type Options = Options;
fn write_to_with_options(
&self,
file_name: &Path,
relation: &Relation,
_: &Self::Options,
) -> Result<()> {
let mut file = File::create(file_name)?;
write(&mut file, relation)
}
fn print_with_options(&self, relation: &Relation, _: &Self::Options) -> Result<()> {
write(&mut std::io::stdout(), relation)
}
}
fn write(w: &mut impl Write, relation: &Relation) -> Result<()> {
use prettytable::format::Alignment;
use prettytable::Table;
use prettytable::{Attr, Cell};
let mut table = Table::new();
table.set_titles(
relation
.schema()
.iter()
.map(|attr| {
Cell::new_align(&attr.to_column_decl(true), Alignment::CENTER)
.with_style(Attr::Bold)
})
.collect(),
);
for row in relation.iter() {
table.add_row(row.iter().map(|c| Cell::new(&c.to_string())).collect());
}
write!(w, "{}", table)?;
Ok(())
}