Skip to content

Commit e400a71

Browse files
committed
allow nested compound statements wherever permitted
1 parent 23e2b18 commit e400a71

14 files changed

Lines changed: 415 additions & 220 deletions

src/arithmetic.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,13 @@ mod tests {
459459
}
460460

461461
#[test]
462-
fn arithmetic_scalar(){
462+
fn arithmetic_scalar() {
463463
let qs = "56";
464464
let res = arithmetic(qs.as_bytes());
465465
assert!(res.is_err());
466-
assert_eq!(nom::Err::Error(nom::error::Error::new(qs.as_bytes(), ErrorKind::Tag)), res.err().unwrap());
466+
assert_eq!(
467+
nom::Err::Error(nom::error::Error::new(qs.as_bytes(), ErrorKind::Tag)),
468+
res.err().unwrap()
469+
);
467470
}
468471
}

src/common.rs

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ where
388388
let (inp, _) = first.parse(inp)?;
389389
let (inp, o2) = second.parse(inp)?;
390390
third.parse(inp).map(|(i, _)| (i, o2))
391-
},
391+
}
392392
}
393393
}
394394
}
@@ -641,7 +641,8 @@ pub fn function_argument_parser(i: &[u8]) -> IResult<&[u8], FunctionArgument> {
641641
// present.
642642
pub fn function_arguments(i: &[u8]) -> IResult<&[u8], (FunctionArgument, bool)> {
643643
let distinct_parser = opt(tuple((tag_no_case("distinct"), multispace1)));
644-
let (remaining_input, (distinct, args)) = tuple((distinct_parser, function_argument_parser))(i)?;
644+
let (remaining_input, (distinct, args)) =
645+
tuple((distinct_parser, function_argument_parser))(i)?;
645646
Ok((remaining_input, (args, distinct.is_some())))
646647
}
647648

@@ -695,12 +696,25 @@ pub fn column_function(i: &[u8]) -> IResult<&[u8], FunctionExpression> {
695696
FunctionExpression::GroupConcat(FunctionArgument::Column(col.clone()), sep)
696697
},
697698
),
698-
map(tuple((sql_identifier, multispace0, tag("("), separated_list0(tag(","), delimited(multispace0, function_argument_parser, multispace0)), tag(")"))), |tuple| {
699-
let (name, _, _, arguments, _) = tuple;
700-
FunctionExpression::Generic(
701-
str::from_utf8(name).unwrap().to_string(),
702-
FunctionArguments::from(arguments))
703-
})
699+
map(
700+
tuple((
701+
sql_identifier,
702+
multispace0,
703+
tag("("),
704+
separated_list0(
705+
tag(","),
706+
delimited(multispace0, function_argument_parser, multispace0),
707+
),
708+
tag(")"),
709+
)),
710+
|tuple| {
711+
let (name, _, _, arguments, _) = tuple;
712+
FunctionExpression::Generic(
713+
str::from_utf8(name).unwrap().to_string(),
714+
FunctionArguments::from(arguments),
715+
)
716+
},
717+
),
704718
))(i)
705719
}
706720

@@ -1021,22 +1035,23 @@ pub fn value_list(i: &[u8]) -> IResult<&[u8], Vec<Literal>> {
10211035
// Parse a reference to a named schema.table, with an optional alias
10221036
pub fn schema_table_reference(i: &[u8]) -> IResult<&[u8], Table> {
10231037
map(
1024-
tuple((
1025-
opt(pair(sql_identifier, tag("."))),
1026-
sql_identifier,
1027-
opt(as_alias)
1028-
)),
1029-
|tup| Table {
1030-
name: String::from(str::from_utf8(tup.1).unwrap()),
1031-
alias: match tup.2 {
1032-
Some(a) => Some(String::from(a)),
1033-
None => None,
1034-
},
1035-
schema: match tup.0 {
1036-
Some((schema, _)) => Some(String::from(str::from_utf8(schema).unwrap())),
1037-
None => None,
1038+
tuple((
1039+
opt(pair(sql_identifier, tag("."))),
1040+
sql_identifier,
1041+
opt(as_alias),
1042+
)),
1043+
|tup| Table {
1044+
name: String::from(str::from_utf8(tup.1).unwrap()),
1045+
alias: match tup.2 {
1046+
Some(a) => Some(String::from(a)),
1047+
None => None,
1048+
},
1049+
schema: match tup.0 {
1050+
Some((schema, _)) => Some(String::from(str::from_utf8(schema).unwrap())),
1051+
None => None,
1052+
},
10381053
},
1039-
})(i)
1054+
)(i)
10401055
}
10411056

10421057
// Parse a reference to a named table, with an optional alias
@@ -1047,7 +1062,7 @@ pub fn table_reference(i: &[u8]) -> IResult<&[u8], Table> {
10471062
Some(a) => Some(String::from(a)),
10481063
None => None,
10491064
},
1050-
schema: None,
1065+
schema: None,
10511066
})(i)
10521067
}
10531068

@@ -1137,25 +1152,31 @@ mod tests {
11371152
name: String::from("max(addr_id)"),
11381153
alias: None,
11391154
table: None,
1140-
function: Some(Box::new(FunctionExpression::Max(
1141-
FunctionArgument::Column(Column::from("addr_id")),
1142-
))),
1155+
function: Some(Box::new(FunctionExpression::Max(FunctionArgument::Column(
1156+
Column::from("addr_id"),
1157+
)))),
11431158
};
11441159
assert_eq!(res.unwrap().1, expected);
11451160
}
11461161

11471162
#[test]
11481163
fn simple_generic_function() {
1149-
let qlist = ["coalesce(a,b,c)".as_bytes(), "coalesce (a,b,c)".as_bytes(), "coalesce(a ,b,c)".as_bytes(), "coalesce(a, b,c)".as_bytes()];
1164+
let qlist = [
1165+
"coalesce(a,b,c)".as_bytes(),
1166+
"coalesce (a,b,c)".as_bytes(),
1167+
"coalesce(a ,b,c)".as_bytes(),
1168+
"coalesce(a, b,c)".as_bytes(),
1169+
];
11501170
for q in qlist.iter() {
11511171
let res = column_function(q);
1152-
let expected = FunctionExpression::Generic("coalesce".to_string(),
1153-
FunctionArguments::from(
1154-
vec!(
1155-
FunctionArgument::Column(Column::from("a")),
1156-
FunctionArgument::Column(Column::from("b")),
1157-
FunctionArgument::Column(Column::from("c"))
1158-
)));
1172+
let expected = FunctionExpression::Generic(
1173+
"coalesce".to_string(),
1174+
FunctionArguments::from(vec![
1175+
FunctionArgument::Column(Column::from("a")),
1176+
FunctionArgument::Column(Column::from("b")),
1177+
FunctionArgument::Column(Column::from("c")),
1178+
]),
1179+
);
11591180
assert_eq!(res, Ok((&b""[..], expected)));
11601181
}
11611182
}

0 commit comments

Comments
 (0)