84 lines
1.9 KiB
Rust
84 lines
1.9 KiB
Rust
|
use crate::sql::statement::{statements, Statement, Statements};
|
||
|
use nom::combinator::all_consuming;
|
||
|
use nom::IResult;
|
||
|
use serde::{Deserialize, Serialize};
|
||
|
use std::fmt;
|
||
|
use std::str;
|
||
|
|
||
|
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
|
||
|
pub struct Query {
|
||
|
statements: Statements,
|
||
|
}
|
||
|
|
||
|
impl Query {
|
||
|
pub fn empty(&self) -> bool {
|
||
|
self.statements.len() == 0
|
||
|
}
|
||
|
|
||
|
pub fn statements(&self) -> &Vec<Statement> {
|
||
|
&self.statements.0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl fmt::Display for Query {
|
||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||
|
write!(f, "{}", self.statements)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn query(i: &str) -> IResult<&str, Query> {
|
||
|
let (i, v) = all_consuming(statements)(i)?;
|
||
|
Ok((i, Query { statements: v }))
|
||
|
}
|
||
|
|
||
|
#[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))
|
||
|
}
|
||
|
}
|