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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
use std::cmp::Ordering; use std::fmt::{self, Display}; use std::str; use common::{Literal, SqlType}; use keywords::escape_if_keyword; #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub enum FunctionExpression { Avg(Column, bool), Count(Column, bool), CountStar, Sum(Column, bool), Max(Column), Min(Column), GroupConcat(Column, String), } impl Display for FunctionExpression { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { FunctionExpression::Avg(ref col, d) if d => { write!(f, "avg(distinct {})", col.name.as_str()) } FunctionExpression::Count(ref col, d) if d => { write!(f, "count(distinct {})", col.name.as_str()) } FunctionExpression::Sum(ref col, d) if d => { write!(f, "sum(distinct {})", col.name.as_str()) } FunctionExpression::Avg(ref col, _) => write!(f, "avg({})", col.name.as_str()), FunctionExpression::Count(ref col, _) => write!(f, "count({})", col.name.as_str()), FunctionExpression::CountStar => write!(f, "count(*)"), FunctionExpression::Sum(ref col, _) => write!(f, "sum({})", col.name.as_str()), FunctionExpression::Max(ref col) => write!(f, "max({})", col.name.as_str()), FunctionExpression::Min(ref col) => write!(f, "min({})", col.name.as_str()), FunctionExpression::GroupConcat(ref col, ref s) => { write!(f, "group_concat({}, {})", col.name.as_str(), s) } } } } #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct Column { pub name: String, pub alias: Option<String>, pub table: Option<String>, pub function: Option<Box<FunctionExpression>>, } impl fmt::Display for Column { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if let Some(ref table) = self.table { write!( f, "{}.{}", escape_if_keyword(table), escape_if_keyword(&self.name) )?; } else { write!(f, "{}", escape_if_keyword(&self.name))?; } if let Some(ref alias) = self.alias { write!(f, " AS {}", escape_if_keyword(alias))?; } Ok(()) } } impl<'a> From<&'a str> for Column { fn from(c: &str) -> Column { match c.find(".") { None => Column { name: String::from(c), alias: None, table: None, function: None, }, Some(i) => Column { name: String::from(&c[i + 1..]), alias: None, table: Some(String::from(&c[0..i])), function: None, }, } } } impl Ord for Column { fn cmp(&self, other: &Column) -> Ordering { if self.table.is_some() && other.table.is_some() { match self.table.cmp(&other.table) { Ordering::Equal => self.name.cmp(&other.name), x => x, } } else { self.name.cmp(&other.name) } } } impl PartialOrd for Column { fn partial_cmp(&self, other: &Column) -> Option<Ordering> { if self.table.is_some() && other.table.is_some() { match self.table.cmp(&other.table) { Ordering::Equal => Some(self.name.cmp(&other.name)), x => Some(x), } } else if self.table.is_none() && other.table.is_none() { Some(self.name.cmp(&other.name)) } else { None } } } #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub enum ColumnConstraint { NotNull, Collation(String), DefaultValue(Literal), AutoIncrement, PrimaryKey, Unique, } impl fmt::Display for ColumnConstraint { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { ColumnConstraint::NotNull => write!(f, "NOT NULL"), ColumnConstraint::Collation(ref collation) => write!(f, "COLLATE {}", collation), ColumnConstraint::DefaultValue(ref literal) => { write!(f, "DEFAULT {}", literal.to_string()) } ColumnConstraint::AutoIncrement => write!(f, "AUTO_INCREMENT"), ColumnConstraint::PrimaryKey => write!(f, "PRIMARY KEY"), ColumnConstraint::Unique => write!(f, "UNIQUE"), } } } #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct ColumnSpecification { pub column: Column, pub sql_type: SqlType, pub constraints: Vec<ColumnConstraint>, } impl fmt::Display for ColumnSpecification { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "{} {}", escape_if_keyword(&self.column.name), self.sql_type )?; for constraint in self.constraints.iter() { write!(f, " {}", constraint)?; } Ok(()) } } impl ColumnSpecification { pub fn new(c: Column, t: SqlType) -> ColumnSpecification { ColumnSpecification { column: c, sql_type: t, constraints: vec![], } } pub fn with_constraints( c: Column, t: SqlType, ccs: Vec<ColumnConstraint>, ) -> ColumnSpecification { ColumnSpecification { column: c, sql_type: t, constraints: ccs, } } } #[cfg(test)] mod tests { use super::*; #[test] fn column_from_str() { let s = "table.col"; let c = Column::from(s); assert_eq!( c, Column { name: String::from("col"), alias: None, table: Some(String::from("table")), function: None, } ); } }