sql/parsers/
query_parser.rsuse snafu::prelude::*;
use crate::error::{self, Result};
use crate::parser::ParserContext;
use crate::statements::query::Query;
use crate::statements::statement::Statement;
impl ParserContext<'_> {
pub(crate) fn parse_query(&mut self) -> Result<Statement> {
let spquery = self.parser.parse_query().context(error::SyntaxSnafu)?;
Ok(Statement::Query(Box::new(Query::try_from(spquery)?)))
}
}
#[cfg(test)]
mod tests {
use common_error::ext::ErrorExt;
use crate::dialect::GreptimeDbDialect;
use crate::parser::{ParseOptions, ParserContext};
#[test]
pub fn test_parse_query() {
let sql = "SELECT a, b, 123, myfunc(b) \
FROM table_1 \
WHERE a > b AND b < 100 \
ORDER BY a DESC, b";
let _ =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();
}
#[test]
pub fn test_parse_invalid_query() {
let sql = "SELECT * FROM table_1 WHERE";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
assert!(result.is_err());
assert!(result
.unwrap_err()
.output_msg()
.contains("Expected an expression"));
}
}