Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select statement can have multiple table #2 #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/grammar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 12 additions & 3 deletions src/grammar/statement/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use grammar::{Buffer, Clause, Condition, Expression, Statement};
/// A `SELECT` statement.
#[derive(Debug, Default)]
pub struct Select {
table: Option<String>,
tables: Vec<String>,
columns: Option<Vec<String>>,
so_that: Option<Where>,
order_by: Option<OrderBy>,
Expand All @@ -21,7 +21,7 @@ impl Select {

/// Set the table.
pub fn table<T: ToString>(mut self, name: T) -> Self {
self.table = Some(name.to_string());
self.tables.push(name.to_string());
self
}

Expand Down Expand Up @@ -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()));
}
Expand All @@ -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"]);
Expand Down