1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::collections::HashSet;

/// Data for `select`
#[derive(Debug)]
pub struct QueryData {
    pub fields: Vec<String>,
    pub tables: Vec<String>,
    pub joins: Vec<Join>,
    pub predicate: NodePtr,
    pub group_fields: Vec<String>,
    pub aggregation_fn: Vec<String>,
    pub sort_fields: Vec<String>,
    pub sort_dir: SortDirection,
    pub is_distinct: bool,
    pub top: TopType,
}

impl QueryData {
    pub fn new() -> QueryData {
        QueryData {
            fields: vec![],
            tables: vec![],
            joins: vec![],
            predicate: None,
            group_fields: vec![],
            aggregation_fn: vec![],
            sort_fields: vec![],
            sort_dir: SortDirection::None,
            is_distinct: false,
            top: TopType::None,
        }
    }
}

#[derive(Debug, PartialEq)]
#[allow(dead_code)]
pub enum SortDirection {
    Asc,
    Desc,
    None,
}

#[derive(Debug, PartialEq)]
pub enum TopType {
    Percent(f32),
    Number(u32),
    None,
}

#[derive(Debug)]
pub struct Join {
    pub join_type: JoinType,
    pub table: String,
    pub condition: NodePtr,
}

impl Join {
    pub fn new(name: &str) -> Join {
        Join {
            join_type: JoinType::get(name).unwrap(),
            table: "".to_string(),
            condition: None,
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum JoinType {
    InnerJoin,
    FullOuterJoin,
    RightJoin,
    LeftJoin,
}

impl JoinType {
    fn get(name: &str) -> Option<JoinType> {
        let t = match name {
            "inner join" => JoinType::InnerJoin,
            "full outer join" => JoinType::FullOuterJoin,
            "left join" => JoinType::LeftJoin,
            "right join" => JoinType::RightJoin,
            _ => return None,
        };
        Some(t)
    }
}

pub type NodePtr = Option<Box<Node>>;

#[derive(Default, Debug, Clone)]
pub struct Node {
    pub root: String,
    pub set: HashSet<usize>,
    pub left: NodePtr,
    pub right: NodePtr,
}

impl Node {
    pub fn new(root: String) -> Node {
        Node {
            root: root,
            ..Default::default()
        }
    }

    pub fn left(mut self, leaf: Node) -> Self {
        self.left = Some(Box::new(leaf));
        self
    }

    pub fn right(mut self, leaf: Node) -> Self {
        self.right = Some(Box::new(leaf));
        self
    }
}