use crate::query::expr::Expr;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NullsOrder {
First,
Last,
}
#[derive(Debug, Clone)]
pub struct IndexColumn {
pub name: String,
pub expression: Option<Expr>,
pub descending: bool,
pub nulls: Option<NullsOrder>,
pub collation: Option<String>,
pub opclass: Option<String>,
}
impl IndexColumn {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
expression: None,
descending: false,
nulls: None,
collation: None,
opclass: None,
}
}
pub fn expression(expression: Expr) -> Self {
Self {
name: String::new(),
expression: Some(expression),
descending: false,
nulls: None,
collation: None,
opclass: None,
}
}
pub fn asc(mut self) -> Self {
self.descending = false;
self
}
pub fn desc(mut self) -> Self {
self.descending = true;
self
}
pub fn nulls_first(mut self) -> Self {
self.nulls = Some(NullsOrder::First);
self
}
pub fn nulls_last(mut self) -> Self {
self.nulls = Some(NullsOrder::Last);
self
}
pub fn collate(mut self, collation: impl Into<String>) -> Self {
self.collation = Some(collation.into());
self
}
pub fn opclass(mut self, opclass: impl Into<String>) -> Self {
self.opclass = Some(opclass.into());
self
}
}
#[derive(Debug, Clone)]
pub struct IndexDef {
pub name: String,
pub columns: Vec<IndexColumn>,
pub unique: bool,
pub predicate: Option<Expr>,
pub method: Option<String>,
pub include: Vec<String>,
}
impl IndexDef {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
columns: Vec::new(),
unique: false,
predicate: None,
method: None,
include: Vec::new(),
}
}
}