diff --git a/src/grammar/mod.rs b/src/grammar/mod.rs index a4275e8..315f073 100644 --- a/src/grammar/mod.rs +++ b/src/grammar/mod.rs @@ -103,6 +103,19 @@ macro_rules! some( ); ); + +macro_rules! first( + ($arr:expr, $name:expr) => ( + match $arr.first() { + Some(ref value) => value, + _ => raise!(concat!("expected “", stringify!($name), "” to have at least 1 specified")), + } + ); + ($this:ident.$field:ident) => ( + first!($this.$field, $field) + ); +); + macro_rules! push( ($collection:expr, $value:expr) => ( match $collection { diff --git a/src/grammar/statement/select.rs b/src/grammar/statement/select.rs index 88c480b..c32965e 100644 --- a/src/grammar/statement/select.rs +++ b/src/grammar/statement/select.rs @@ -5,7 +5,7 @@ use grammar::{Buffer, Clause, Condition, Expression, Statement}; /// A `SELECT` statement. #[derive(Debug, Default)] pub struct Select { - table: Option, + tables: Vec, columns: Option>, so_that: Option, order_by: Option, @@ -21,7 +21,7 @@ impl Select { /// Set the table. pub fn table(mut self, name: T) -> Self { - self.table = Some(name.to_string()); + self.tables.push(name.to_string()); self } @@ -80,7 +80,10 @@ impl Statement for Select { buffer.push("*"); } buffer.push("FROM"); - buffer.push(format!("`{}`", some!(self.table))); + buffer.push(format!("`{}`", first!(self.tables) )); + for table in self.tables.iter().skip(1){ + buffer.push(format!(",`{}`", table)); + } if let &Some(ref clause) = &self.so_that { buffer.push(try!(clause.compile())); } @@ -104,6 +107,12 @@ mod tests { assert_eq!(statement.compile().unwrap(), "SELECT * FROM `foo`"); } + #[test] + fn multiple_tables() { + let statement = select_from("foo").table("bar"); + assert_eq!(statement.compile().unwrap(), "SELECT * FROM `foo` ,`bar`"); + } + #[test] fn columns() { let statement = select_from("foo").columns(&["bar", "baz"]);