use crate::SQLError;
const LATERAL_HINT: &str =
"You might be able to move the set-returning function into a LATERAL FROM item.";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SetPlacement {
Expanding,
FromFunction,
JoinCondition,
Where,
Having,
Filter,
WindowFrame,
Update,
Limit,
Offset,
Returning,
Values,
MergeWhen,
IndexExpression,
IndexPredicate,
ExecuteParameter,
TriggerWhen,
CallArgument,
}
impl SetPlacement {
pub fn admit(self, nested: bool) -> Result<(), SQLError> {
let context = match self {
Self::Expanding => return Ok(()),
Self::FromFunction if nested => return Err(top_level_from()),
Self::FromFunction => return Ok(()),
Self::JoinCondition => "JOIN conditions",
Self::Where => "WHERE",
Self::Having => "HAVING",
Self::Filter => "FILTER",
Self::WindowFrame => "window definitions",
Self::Update => "UPDATE",
Self::Limit => "LIMIT",
Self::Offset => "OFFSET",
Self::Returning => "RETURNING",
Self::Values => "VALUES",
Self::MergeWhen => "MERGE WHEN conditions",
Self::IndexExpression => "index expressions",
Self::IndexPredicate => "index predicates",
Self::ExecuteParameter => "EXECUTE parameters",
Self::TriggerWhen => "trigger WHEN conditions",
Self::CallArgument => "CALL arguments",
};
Err(feature_not_supported(format!(
"set-returning functions are not allowed in {context}"
)))
}
}
pub fn top_level_from() -> SQLError {
feature_not_supported("set-returning functions must appear at top level of FROM".into())
}
pub fn conditional_set_error(message: &str) -> SQLError {
SQLError::Diagnostic {
sqlstate: "0A000".into(),
message: message.into(),
detail: None,
hint: Some(LATERAL_HINT.into()),
}
}
pub fn set_argument_error(construct: &str) -> SQLError {
datatype_mismatch(format!("argument of {construct} must not return a set"))
}
pub fn row_comparison_set_error() -> SQLError {
datatype_mismatch("row comparison operator must not return a set".into())
}
pub fn set_valued_call_error() -> SQLError {
feature_not_supported("set-valued function called in context that cannot accept a set".into())
}
fn feature_not_supported(message: String) -> SQLError {
SQLError::Routine {
sqlstate: "0A000".into(),
message,
}
}
fn datatype_mismatch(message: String) -> SQLError {
SQLError::Routine {
sqlstate: "42804".into(),
message,
}
}