pub mod iter;
use std::collections::BTreeMap;
use std::fmt;
use super::{
common::{Context, Value, ValueType},
mem::Field as MemField,
};
#[derive(Debug, PartialEq, Eq)]
pub struct OwnedContext;
impl Context for OwnedContext {
type String = String;
type I64 = i64;
type XML = String;
}
pub type Field = Value<OwnedContext>;
impl From<MemField<'_>> for Field {
fn from(src: MemField<'_>) -> Self {
match src {
MemField::Nothing => Field::Nothing,
MemField::Integer(v) => Field::Integer(v),
MemField::Float(v) => Field::Float(v),
MemField::Text(v) => Field::Text(v.decode().into_owned()),
MemField::Boolean(v) => Field::Boolean(v),
MemField::BigInt(v) => Field::BigInt(v),
MemField::VarChar(v) => Field::VarChar(v.decode().into_owned()),
}
}
}
impl PartialEq<MemField<'_>> for Field {
fn eq(&self, other: &MemField<'_>) -> bool {
match other {
Value::Nothing => matches!(self, Self::Nothing),
Value::Integer(x) => matches!(self, Self::Integer(y) if x == y),
Value::Float(x) => matches!(self, Self::Float(y) if x == y),
Value::Text(x) => matches!(self, Self::Text(y) if x.decode().as_ref() == y),
Value::Boolean(x) => matches!(self, Self::Boolean(y) if x == y),
Value::BigInt(x) => matches!(self, Self::BigInt(y) if x == y),
Value::VarChar(x) => matches!(self, Self::VarChar(y) if x.decode().as_ref() == y),
}
}
}
impl fmt::Display for Field {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Field::Nothing => write!(f, "NULL"),
Field::Integer(i) => write!(f, "{}", i),
Field::Float(v) => write!(f, "{}", v),
Field::Text(t) => write!(f, "{:?}", t),
Field::Boolean(b) => write!(f, "{}", b),
Field::BigInt(i) => write!(f, "{}", i),
Field::VarChar(v) => write!(f, "{:?}", v),
}
}
}
#[derive(Debug, Default)]
pub struct Row(Vec<Field>);
impl From<Vec<Field>> for Row {
fn from(fields: Vec<Field>) -> Self {
Row(fields)
}
}
impl Row {
pub fn new() -> Row {
Row(Vec::new())
}
pub fn into_fields(self) -> Vec<Field> {
self.0
}
pub fn fields(&self) -> &Vec<Field> {
&self.0
}
pub fn fields_mut(&mut self) -> &mut Vec<Field> {
&mut self.0
}
}
#[derive(Debug, Default)]
pub struct Bucket(pub Vec<Row>);
impl Bucket {
pub fn new() -> Bucket {
Bucket(Vec::new())
}
pub fn rows(self) -> Vec<Row> {
self.0
}
pub fn rows_ref(&self) -> &Vec<Row> {
&self.0
}
pub fn rows_mut(&mut self) -> &mut Vec<Row> {
&mut self.0
}
}
#[derive(Debug)]
pub struct Column {
pub name: String,
pub field_type: ValueType,
}
impl From<(&str, ValueType)> for Column {
fn from(data: (&str, ValueType)) -> Self {
Column {
name: String::from(data.0),
field_type: data.1,
}
}
}
#[derive(Debug)]
pub struct TableDef {
pub columns: Vec<Column>,
pub name: String,
}
#[derive(Debug, Default)]
pub struct TableData {
pub buckets: Vec<Bucket>,
}
impl TableData {
pub fn new() -> Self {
TableData {
buckets: Vec::new(),
}
}
}
#[derive(Debug)]
pub struct Table {
definition: TableDef,
data: TableData,
}
impl Table {
pub fn from(definition: TableDef, data: TableData) -> Self {
Table { definition, data }
}
pub fn new(definition: TableDef) -> Self {
let data = TableData::new();
Table { definition, data }
}
pub fn into_buckets(self) -> Vec<Bucket> {
self.data.buckets
}
pub fn buckets(&self) -> &[Bucket] {
&self.data.buckets
}
pub fn buckets_mut(&mut self) -> &mut Vec<Bucket> {
&mut self.data.buckets
}
pub fn into_columns(self) -> Vec<Column> {
self.definition.columns
}
pub fn columns(&self) -> &[Column] {
&self.definition.columns
}
pub fn columns_mut(&mut self) -> &mut Vec<Column> {
&mut self.definition.columns
}
pub fn name(&self) -> &str {
self.definition.name.as_ref()
}
}
#[derive(Debug, Default)]
pub struct Schema {
pub tables: BTreeMap<String, Table>,
}
impl Schema {
pub fn new() -> Schema {
Schema {
tables: BTreeMap::new(),
}
}
pub fn table(&self, name: &str) -> Option<&Table> {
self.tables.get(name)
}
pub fn table_mut(&mut self, name: &str) -> Option<&mut Table> {
self.tables.get_mut(name)
}
pub fn table_count(&self) -> usize {
self.tables.len()
}
}
impl From<Vec<Table>> for Schema {
fn from(tables: Vec<Table>) -> Self {
let mut tree = BTreeMap::new();
for table in tables {
tree.insert(table.name().to_string(), table);
}
Schema { tables: tree }
}
}