sql/statements/
set_variables.rsuse std::fmt::Display;
use serde::Serialize;
use sqlparser::ast::{Expr, ObjectName};
use sqlparser_derive::{Visit, VisitMut};
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)]
pub struct SetVariables {
pub variable: ObjectName,
pub value: Vec<Expr>,
}
impl Display for SetVariables {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let variable = &self.variable;
let value = &self
.value
.iter()
.map(|expr| format!("{}", expr))
.collect::<Vec<_>>()
.join(", ");
write!(f, r#"SET {variable} = {value}"#)
}
}
#[cfg(test)]
mod tests {
use std::assert_matches::assert_matches;
use crate::dialect::GreptimeDbDialect;
use crate::parser::{ParseOptions, ParserContext};
use crate::statements::statement::Statement;
#[test]
fn test_display_show_variables() {
let sql = r"set delayed_insert_timeout=300;";
let stmts =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();
assert_eq!(1, stmts.len());
assert_matches!(&stmts[0], Statement::SetVariables { .. });
match &stmts[0] {
Statement::SetVariables(set) => {
let new_sql = format!("\n{}", set);
assert_eq!(
r#"
SET delayed_insert_timeout = 300"#,
&new_sql
);
}
_ => {
unreachable!();
}
}
}
}