2023-09-08 11:28:36 +00:00
|
|
|
use crate::sql::error::ParseError;
|
2023-01-08 17:11:35 +00:00
|
|
|
use crate::sql::field::{Field, Fields};
|
|
|
|
use crate::sql::group::Groups;
|
|
|
|
use crate::sql::order::Orders;
|
|
|
|
use crate::sql::split::Splits;
|
|
|
|
use crate::sql::value::Value;
|
2023-06-10 20:23:22 +00:00
|
|
|
use crate::sql::Idiom;
|
2023-01-08 17:11:35 +00:00
|
|
|
use nom::Err;
|
|
|
|
use nom::Err::Failure;
|
|
|
|
|
2023-06-10 20:23:22 +00:00
|
|
|
/// Check to see whether the expression is in the SELECT clause
|
|
|
|
fn contains_idiom(fields: &Fields, idiom: &Idiom) -> bool {
|
|
|
|
fields.iter().any(|field| {
|
|
|
|
match field {
|
|
|
|
// There is a SELECT * expression, so presume everything is ok
|
|
|
|
Field::All => true,
|
|
|
|
// Check each field
|
|
|
|
Field::Single {
|
|
|
|
expr,
|
|
|
|
alias,
|
|
|
|
} => {
|
|
|
|
if let Some(i) = alias {
|
|
|
|
// This field is aliased, so check the alias name
|
|
|
|
i.as_ref() == idiom.as_ref()
|
|
|
|
} else {
|
|
|
|
// This field is not aliased, so check the field value
|
|
|
|
match expr {
|
|
|
|
// Use raw idiom (TODO: should this use `simplify`?)
|
|
|
|
Value::Idiom(i) => i.as_ref() == idiom.as_ref(),
|
|
|
|
// Check the expression
|
|
|
|
v => v.to_idiom().as_ref() == idiom.as_ref(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:11:35 +00:00
|
|
|
pub fn check_split_on_fields<'a>(
|
|
|
|
i: &'a str,
|
|
|
|
fields: &Fields,
|
|
|
|
splits: &Option<Splits>,
|
2023-09-08 11:28:36 +00:00
|
|
|
) -> Result<(), Err<ParseError<&'a str>>> {
|
2023-06-10 20:23:22 +00:00
|
|
|
// Check to see if a SPLIT ON clause has been defined
|
2023-01-08 17:11:35 +00:00
|
|
|
if let Some(splits) = splits {
|
|
|
|
// Loop over each of the expressions in the SPLIT ON clause
|
2023-06-10 20:23:22 +00:00
|
|
|
for split in splits.iter() {
|
|
|
|
if !contains_idiom(fields, &split.0) {
|
|
|
|
// If the expression isn't specified in the SELECT clause, then error
|
2023-09-08 11:28:36 +00:00
|
|
|
return Err(Failure(ParseError::Split(i, split.to_string())));
|
2023-01-08 17:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This query is ok to run
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_order_by_fields<'a>(
|
|
|
|
i: &'a str,
|
|
|
|
fields: &Fields,
|
|
|
|
orders: &Option<Orders>,
|
2023-09-08 11:28:36 +00:00
|
|
|
) -> Result<(), Err<ParseError<&'a str>>> {
|
2023-01-08 17:11:35 +00:00
|
|
|
// Check to see if a ORDER BY clause has been defined
|
|
|
|
if let Some(orders) = orders {
|
|
|
|
// Loop over each of the expressions in the ORDER BY clause
|
2023-06-10 20:23:22 +00:00
|
|
|
for order in orders.iter() {
|
2023-06-20 11:48:20 +00:00
|
|
|
if !contains_idiom(fields, order) {
|
2023-06-10 20:23:22 +00:00
|
|
|
// If the expression isn't specified in the SELECT clause, then error
|
2023-09-08 11:28:36 +00:00
|
|
|
return Err(Failure(ParseError::Order(i, order.to_string())));
|
2023-01-08 17:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This query is ok to run
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_group_by_fields<'a>(
|
|
|
|
i: &'a str,
|
|
|
|
fields: &Fields,
|
|
|
|
groups: &Option<Groups>,
|
2023-09-08 11:28:36 +00:00
|
|
|
) -> Result<(), Err<ParseError<&'a str>>> {
|
2023-01-08 17:11:35 +00:00
|
|
|
// Check to see if a GROUP BY clause has been defined
|
|
|
|
if let Some(groups) = groups {
|
|
|
|
// Loop over each of the expressions in the GROUP BY clause
|
2023-06-10 20:23:22 +00:00
|
|
|
for group in groups.iter() {
|
|
|
|
if !contains_idiom(fields, &group.0) {
|
|
|
|
// If the expression isn't specified in the SELECT clause, then error
|
2023-09-08 11:28:36 +00:00
|
|
|
return Err(Failure(ParseError::Group(i, group.to_string())));
|
2023-01-08 17:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if this is a GROUP ALL clause or a GROUP BY clause
|
2023-06-10 20:23:22 +00:00
|
|
|
if !groups.is_empty() {
|
2023-01-08 17:11:35 +00:00
|
|
|
// Loop over each of the expressions in the SELECT clause
|
|
|
|
'outer: for field in fields.iter() {
|
|
|
|
// Loop over each of the expressions in the GROUP BY clause
|
|
|
|
for group in groups.iter() {
|
2023-06-10 20:23:22 +00:00
|
|
|
// Check to see whether the expression is in the GROUP BY clause or is an aggregate
|
2023-06-20 11:48:20 +00:00
|
|
|
if let Field::Single {
|
|
|
|
expr,
|
|
|
|
alias,
|
|
|
|
} = field
|
|
|
|
{
|
|
|
|
if alias.as_ref().map(|i| i.as_ref() == group.as_ref()).unwrap_or(false) {
|
|
|
|
// This field is aliased, and the alias name matched
|
|
|
|
continue 'outer;
|
|
|
|
} else {
|
|
|
|
match expr {
|
|
|
|
// If the expression in the SELECT clause is a field, check to see if it exists in the GROUP BY
|
|
|
|
Value::Idiom(i) if i == &group.0 => continue 'outer,
|
|
|
|
// If the expression in the SELECT clause is a function, check to see if it is an aggregate function
|
|
|
|
Value::Function(f) if f.is_aggregate() => continue 'outer,
|
|
|
|
// Otherwise check if the expression itself exists in the GROUP BY clause
|
|
|
|
v if v.to_idiom() == group.0 => continue 'outer,
|
|
|
|
// Check if this is a static value which can be used in the GROUP BY clause
|
|
|
|
v if v.is_static() => continue 'outer,
|
|
|
|
// If not, then this query should fail
|
|
|
|
_ => (),
|
2023-06-10 20:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-08 17:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the expression isn't an aggregate function and isn't specified in the GROUP BY clause, then error
|
2023-09-08 11:28:36 +00:00
|
|
|
return Err(Failure(ParseError::Field(i, field.to_string())));
|
2023-01-08 17:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This query is ok to run
|
|
|
|
Ok(())
|
|
|
|
}
|