use super::{Column, ColumnId, Index, IndexId, PrimaryKey};
use crate::stmt;
use std::fmt;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Table {
pub id: TableId,
pub name: String,
pub columns: Vec<Column>,
pub primary_key: PrimaryKey,
pub indices: Vec<Index>,
}
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TableId(pub usize);
impl Table {
pub fn primary_key_column(&self, i: usize) -> &Column {
&self.columns[self.primary_key.columns[i].index]
}
pub fn primary_key_columns(&self) -> impl ExactSizeIterator<Item = &Column> + '_ {
self.primary_key
.columns
.iter()
.map(|column_id| &self.columns[column_id.index])
}
pub fn column(&self, id: impl Into<ColumnId>) -> &Column {
&self.columns[id.into().index]
}
pub fn resolve(&self, projection: &stmt::Projection) -> &Column {
let [first, rest @ ..] = projection.as_slice() else {
panic!("need at most one path step")
};
assert!(rest.is_empty());
&self.columns[*first]
}
pub(crate) fn new(id: TableId, name: String) -> Self {
Self {
id,
name,
columns: vec![],
primary_key: PrimaryKey {
columns: vec![],
index: IndexId {
table: id,
index: 0,
},
},
indices: vec![],
}
}
}
impl TableId {
pub(crate) fn placeholder() -> Self {
Self(usize::MAX)
}
}
impl fmt::Debug for TableId {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "TableId({})", self.0)
}
}