Skip to content
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
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ pub fn field_definition_expr(i: &[u8]) -> IResult<&[u8], Vec<FieldDefinitionExpr
// Parse list of table names.
// XXX(malte): add support for aliases
pub fn table_list(i: &[u8]) -> IResult<&[u8], Vec<Table>> {
many0(terminated(schema_table_reference, opt(ws_sep_comma)))(i)
many1(terminated(schema_table_reference, opt(ws_sep_comma)))(i)
}

// Integer literal value
Expand Down
51 changes: 31 additions & 20 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,36 +274,40 @@ pub fn selection(i: &[u8]) -> IResult<&[u8], SelectStatement> {
}

pub fn nested_selection(i: &[u8]) -> IResult<&[u8], SelectStatement> {
let (
remaining_input,
(_, _, distinct, _, fields, _, tables, join, where_clause, group_by, order, limit),
) = tuple((
let (remaining_input, (_, _, distinct, _, fields)) = tuple((
tag_no_case("select"),
multispace1,
opt(tag_no_case("distinct")),
multispace0,
field_definition_expr,
))(i)?;

let (remaining_input, from_clause) = opt(tuple((
delimited(multispace0, tag_no_case("from"), multispace0),
table_list,
many0(join_clause),
opt(where_clause),
opt(group_by_clause),
opt(order_clause),
opt(limit_clause),
))(i)?;
Ok((
remaining_input,
SelectStatement {
tables,
distinct: distinct.is_some(),
fields,
join,
where_clause,
group_by,
order,
limit,
},
))
)))(remaining_input)?;

let mut result = SelectStatement {
distinct: distinct.is_some(),
fields,
..Default::default()
};

if let Some((_, tables, join, where_clause, group_by, order, limit)) = from_clause {
result.tables = tables;
result.join = join;
result.where_clause = where_clause;
result.group_by = group_by;
result.order = order;
result.limit = limit;
}

Ok((remaining_input, result))
}

#[cfg(test)]
Expand Down Expand Up @@ -341,6 +345,13 @@ mod tests {
);
}

#[test]
fn select_without_table() {
let qstring = "SELECT * FROM;";
let res = selection(qstring.as_bytes());
assert!(res.is_err(), "!{:?}.is_err()", res);
}

#[test]
fn more_involved_select() {
let qstring = "SELECT users.id, users.name FROM users;";
Expand Down Expand Up @@ -533,7 +544,7 @@ mod tests {
tables: vec![Table {
name: String::from("PaperTag"),
alias: Some(String::from("t")),
schema: None,
schema: None,
},],
fields: vec![FieldDefinitionExpression::All],
..Default::default()
Expand All @@ -554,7 +565,7 @@ mod tests {
tables: vec![Table {
name: String::from("PaperTag"),
alias: Some(String::from("t")),
schema: Some(String::from("db1")),
schema: Some(String::from("db1")),
},],
fields: vec![FieldDefinitionExpression::All],
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,6 @@ fn parse_autoincrement() {
#[test]
fn parse_select() {
let (ok, fail) = parse_file("tests/select.txt");
assert_eq!(fail, 0);
assert_eq!(fail, 1);
assert_eq!(ok, 27);
}
1 change: 1 addition & 0 deletions tests/select.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
select * from;

select a + b from c;
select a + 2 from c;
Expand Down