|
| 1 | +use std::cmp::Ordering; |
| 2 | +use std::fmt::{self, Display}; |
| 3 | +use std::str; |
| 4 | + |
| 5 | +use common::{Literal, SqlType}; |
| 6 | + |
| 7 | +#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] |
| 8 | +pub enum FunctionExpression { |
| 9 | + Avg(Column, bool), |
| 10 | + Count(Column, bool), |
| 11 | + CountStar, |
| 12 | + Sum(Column, bool), |
| 13 | + Max(Column), |
| 14 | + Min(Column), |
| 15 | + GroupConcat(Column, String), |
| 16 | +} |
| 17 | + |
| 18 | +impl Display for FunctionExpression { |
| 19 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 20 | + match *self { |
| 21 | + FunctionExpression::Avg(ref col, d) if d => { |
| 22 | + write!(f, "avg(distinct {})", col.name.as_str()) |
| 23 | + } |
| 24 | + FunctionExpression::Count(ref col, d) if d => { |
| 25 | + write!(f, "count(distinct {})", col.name.as_str()) |
| 26 | + } |
| 27 | + FunctionExpression::Sum(ref col, d) if d => { |
| 28 | + write!(f, "sum(distinct {})", col.name.as_str()) |
| 29 | + } |
| 30 | + |
| 31 | + FunctionExpression::Avg(ref col, _) => write!(f, "avg({})", col.name.as_str()), |
| 32 | + FunctionExpression::Count(ref col, _) => write!(f, "count({})", col.name.as_str()), |
| 33 | + FunctionExpression::CountStar => write!(f, "count(all)"), |
| 34 | + FunctionExpression::Sum(ref col, _) => write!(f, "sum({})", col.name.as_str()), |
| 35 | + FunctionExpression::Max(ref col) => write!(f, "max({})", col.name.as_str()), |
| 36 | + FunctionExpression::Min(ref col) => write!(f, "min({})", col.name.as_str()), |
| 37 | + FunctionExpression::GroupConcat(ref col, ref s) => { |
| 38 | + write!(f, "group_concat({}, {})", col.name.as_str(), s) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] |
| 45 | +pub struct Column { |
| 46 | + pub name: String, |
| 47 | + pub alias: Option<String>, |
| 48 | + pub table: Option<String>, |
| 49 | + pub function: Option<Box<FunctionExpression>>, |
| 50 | +} |
| 51 | + |
| 52 | +impl fmt::Display for Column { |
| 53 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 54 | + if let Some(ref table) = self.table { |
| 55 | + write!(f, "{}.{}", table, self.name)?; |
| 56 | + } else { |
| 57 | + write!(f, "{}", self.name)?; |
| 58 | + } |
| 59 | + if let Some(ref alias) = self.alias { |
| 60 | + write!(f, " AS {}", alias)?; |
| 61 | + } |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl<'a> From<&'a str> for Column { |
| 67 | + fn from(c: &str) -> Column { |
| 68 | + match c.find(".") { |
| 69 | + None => Column { |
| 70 | + name: String::from(c), |
| 71 | + alias: None, |
| 72 | + table: None, |
| 73 | + function: None, |
| 74 | + }, |
| 75 | + Some(i) => Column { |
| 76 | + name: String::from(&c[i + 1..]), |
| 77 | + alias: None, |
| 78 | + table: Some(String::from(&c[0..i])), |
| 79 | + function: None, |
| 80 | + }, |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl Ord for Column { |
| 86 | + fn cmp(&self, other: &Column) -> Ordering { |
| 87 | + if self.table.is_some() && other.table.is_some() { |
| 88 | + match self.table.cmp(&other.table) { |
| 89 | + Ordering::Equal => self.name.cmp(&other.name), |
| 90 | + x => x, |
| 91 | + } |
| 92 | + } else { |
| 93 | + self.name.cmp(&other.name) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl PartialOrd for Column { |
| 99 | + fn partial_cmp(&self, other: &Column) -> Option<Ordering> { |
| 100 | + if self.table.is_some() && other.table.is_some() { |
| 101 | + match self.table.cmp(&other.table) { |
| 102 | + Ordering::Equal => Some(self.name.cmp(&other.name)), |
| 103 | + x => Some(x), |
| 104 | + } |
| 105 | + } else if self.table.is_none() && other.table.is_none() { |
| 106 | + Some(self.name.cmp(&other.name)) |
| 107 | + } else { |
| 108 | + None |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] |
| 114 | +pub enum ColumnConstraint { |
| 115 | + NotNull, |
| 116 | + DefaultValue(Literal), |
| 117 | + AutoIncrement, |
| 118 | +} |
| 119 | + |
| 120 | +impl fmt::Display for ColumnConstraint { |
| 121 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 122 | + match *self { |
| 123 | + ColumnConstraint::NotNull => write!(f, "NOT NULL"), |
| 124 | + ColumnConstraint::DefaultValue(ref literal) => { |
| 125 | + write!(f, "DEFAULT {}", literal.to_string()) |
| 126 | + } |
| 127 | + ColumnConstraint::AutoIncrement => write!(f, "AUTOINCREMENT"), |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] |
| 133 | +pub struct ColumnSpecification { |
| 134 | + pub column: Column, |
| 135 | + pub sql_type: SqlType, |
| 136 | + pub constraints: Vec<ColumnConstraint>, |
| 137 | +} |
| 138 | + |
| 139 | +impl fmt::Display for ColumnSpecification { |
| 140 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 141 | + write!(f, "{} {}", self.column, self.sql_type)?; |
| 142 | + for constraint in self.constraints.iter() { |
| 143 | + write!(f, " {}", constraint)?; |
| 144 | + } |
| 145 | + Ok(()) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl ColumnSpecification { |
| 150 | + pub fn new(c: Column, t: SqlType) -> ColumnSpecification { |
| 151 | + ColumnSpecification { |
| 152 | + column: c, |
| 153 | + sql_type: t, |
| 154 | + constraints: vec![], |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + pub fn with_constraints( |
| 159 | + c: Column, |
| 160 | + t: SqlType, |
| 161 | + ccs: Vec<ColumnConstraint>, |
| 162 | + ) -> ColumnSpecification { |
| 163 | + ColumnSpecification { |
| 164 | + column: c, |
| 165 | + sql_type: t, |
| 166 | + constraints: ccs, |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +#[cfg(test)] |
| 172 | +mod tests { |
| 173 | + use super::*; |
| 174 | + |
| 175 | + #[test] |
| 176 | + fn column_from_str() { |
| 177 | + let s = "table.col"; |
| 178 | + let c = Column::from(s); |
| 179 | + |
| 180 | + assert_eq!( |
| 181 | + c, |
| 182 | + Column { |
| 183 | + name: String::from("col"), |
| 184 | + alias: None, |
| 185 | + table: Some(String::from("table")), |
| 186 | + function: None, |
| 187 | + } |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
0 commit comments