use std::collections::BTreeMap;
#[derive(Debug)]
pub enum ValueType {
Nothing,
Integer,
Float,
Text,
Boolean,
BigInt,
VarChar,
Unknown(u32),
}
impl From<ValueType> for u32 {
fn from(value_type: ValueType) -> u32 {
match value_type {
ValueType::Nothing => 0,
ValueType::Integer => 1,
ValueType::Float => 3,
ValueType::Text => 4,
ValueType::Boolean => 5,
ValueType::BigInt => 6,
ValueType::VarChar => 8,
ValueType::Unknown(key) => key,
}
}
}
impl From<u32> for ValueType {
fn from(value_type: u32) -> ValueType {
match value_type {
0 => ValueType::Nothing,
1 => ValueType::Integer,
3 => ValueType::Float,
4 => ValueType::Text,
5 => ValueType::Boolean,
6 => ValueType::BigInt,
8 => ValueType::VarChar,
k => ValueType::Unknown(k),
}
}
}
#[derive(Debug, Clone)]
pub enum Field {
Nothing,
Integer(i32),
Float(f32),
Text(String),
Boolean(bool),
BigInt(i64),
VarChar(String),
}
impl From<&Field> for ValueType {
fn from(val: &Field) -> Self {
match val {
Field::Nothing => ValueType::Nothing,
Field::Integer(_) => ValueType::Integer,
Field::Float(_) => ValueType::Float,
Field::Text(_) => ValueType::Text,
Field::Boolean(_) => ValueType::Boolean,
Field::BigInt(_) => ValueType::BigInt,
Field::VarChar(_) => ValueType::VarChar,
}
}
}
#[derive(Debug)]
pub struct Row(Vec<Field>);
impl From<Vec<Field>> for Row {
fn from(fields: Vec<Field>) -> Self {
Row(fields)
}
}
impl Row {
#[allow(dead_code)]
pub fn new() -> Row {
Row(Vec::new())
}
pub fn fields(self) -> Vec<Field> {
self.0
}
pub fn fields_ref(&self) -> &Vec<Field> {
&self.0
}
}
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,
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 }
}
}
pub struct TableDef {
pub columns: Vec<Column>,
pub name: String,
}
pub struct TableData {
pub buckets: Vec<Bucket>,
}
impl TableData {
pub fn new() -> Self {
TableData{buckets: Vec::new()}
}
}
#[allow(dead_code)]
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 buckets(self) -> Vec<Bucket> {
self.data.buckets
}
pub fn buckets_ref(&self) -> &Vec<Bucket> {
&self.data.buckets
}
pub fn columns(self) -> Vec<Column> {
self.definition.columns
}
pub fn columns_ref(&self) -> &Vec<Column> {
&self.definition.columns
}
pub fn name(&self) -> &str {
self.definition.name.as_ref()
}
}
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 }
}
}