2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2023-01-19 09:53:33 +00:00
|
|
|
use crate::sql::fmt::Pretty;
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::statement::{statements, Statement, Statements};
|
2022-05-21 00:35:59 +00:00
|
|
|
use derive::Store;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::combinator::all_consuming;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2023-01-19 09:53:33 +00:00
|
|
|
use std::fmt::Write;
|
2022-10-04 21:51:18 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2022-05-04 21:34:28 +00:00
|
|
|
use std::ops::Deref;
|
2020-06-29 15:36:01 +00:00
|
|
|
use std::str;
|
|
|
|
|
2022-10-27 12:23:24 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize, Store, Hash)]
|
2022-05-04 21:34:28 +00:00
|
|
|
pub struct Query(pub Statements);
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-05-04 21:34:28 +00:00
|
|
|
impl Deref for Query {
|
|
|
|
type Target = Vec<Statement>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0 .0
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 21:51:18 +00:00
|
|
|
impl Display for Query {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2023-01-19 09:53:33 +00:00
|
|
|
write!(Pretty::from(f), "{}", &self.0)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn query(i: &str) -> IResult<&str, Query> {
|
|
|
|
let (i, v) = all_consuming(statements)(i)?;
|
2022-05-04 21:34:28 +00:00
|
|
|
Ok((i, Query(v)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_query() {
|
|
|
|
let sql = "CREATE test";
|
|
|
|
let res = query(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("CREATE test;", format!("{}", out))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_query() {
|
|
|
|
let sql = "CREATE test; CREATE temp;";
|
|
|
|
let res = query(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("CREATE test;\nCREATE temp;", format!("{}", out))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_query_semicolons() {
|
|
|
|
let sql = "CREATE test;;;CREATE temp;;;";
|
|
|
|
let res = query(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("CREATE test;\nCREATE temp;", format!("{}", out))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_query_semicolons_comments() {
|
|
|
|
let sql = "CREATE test;;;CREATE temp;;;/* some comment */";
|
|
|
|
let res = query(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("CREATE test;\nCREATE temp;", format!("{}", out))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_query_semicolons_multi_comments() {
|
|
|
|
let sql = "CREATE test;;;CREATE temp;;;/* some comment */;;;/* other comment */";
|
|
|
|
let res = query(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("CREATE test;\nCREATE temp;", format!("{}", out))
|
|
|
|
}
|
|
|
|
}
|