Extract ASTNode::SQLSelect to a separate struct (1/5)

This will allow re-using it for SQLStatement in a later commit.

(Also split the new struct into a separate file, other query-related
types will be moved here in a follow-up commit.)
This commit is contained in:
Nickolay Ponomarev 2019-01-21 02:28:39 +03:00
parent 50b5724c39
commit d8173d4196
5 changed files with 97 additions and 96 deletions

View file

@ -14,11 +14,13 @@
//! SQL Abstract Syntax Tree (AST) types
mod query;
mod sql_operator;
mod sqltype;
mod table_key;
mod value;
pub use self::query::SQLSelect;
pub use self::sqltype::SQLType;
pub use self::table_key::{AlterOperation, Key, TableKey};
pub use self::value::Value;
@ -78,24 +80,7 @@ pub enum ASTNode {
alias: Option<SQLIdent>,
},
/// SELECT
SQLSelect {
/// projection expressions
projection: Vec<ASTNode>,
/// FROM
relation: Option<Box<ASTNode>>, // TableFactor
// JOIN
joins: Vec<Join>,
/// WHERE
selection: Option<Box<ASTNode>>,
/// ORDER BY
order_by: Option<Vec<SQLOrderByExpr>>,
/// GROUP BY
group_by: Option<Vec<ASTNode>>,
/// HAVING
having: Option<Box<ASTNode>>,
/// LIMIT
limit: Option<Box<ASTNode>>,
},
SQLSelect(SQLSelect),
/// INSERT
SQLInsert {
/// TABLE
@ -203,61 +188,7 @@ impl ToString for ASTNode {
relation.to_string()
}
}
ASTNode::SQLSelect {
projection,
relation,
joins,
selection,
order_by,
group_by,
having,
limit,
} => {
let mut s = format!(
"SELECT {}",
projection
.iter()
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(", ")
);
if let Some(relation) = relation {
s += &format!(" FROM {}", relation.as_ref().to_string());
}
for join in joins {
s += &join.to_string();
}
if let Some(selection) = selection {
s += &format!(" WHERE {}", selection.as_ref().to_string());
}
if let Some(group_by) = group_by {
s += &format!(
" GROUP BY {}",
group_by
.iter()
.map(|g| g.to_string())
.collect::<Vec<String>>()
.join(", ")
);
}
if let Some(having) = having {
s += &format!(" HAVING {}", having.as_ref().to_string());
}
if let Some(order_by) = order_by {
s += &format!(
" ORDER BY {}",
order_by
.iter()
.map(|o| o.to_string())
.collect::<Vec<String>>()
.join(", ")
);
}
if let Some(limit) = limit {
s += &format!(" LIMIT {}", limit.as_ref().to_string());
}
s
}
ASTNode::SQLSelect(s) => s.to_string(),
ASTNode::SQLInsert {
table_name,
columns,

70
src/sqlast/query.rs Normal file
View file

@ -0,0 +1,70 @@
use super::*;
#[derive(Debug, Clone, PartialEq)]
pub struct SQLSelect {
/// projection expressions
pub projection: Vec<ASTNode>,
/// FROM
pub relation: Option<Box<ASTNode>>, // TableFactor
// JOIN
pub joins: Vec<Join>,
/// WHERE
pub selection: Option<Box<ASTNode>>,
/// ORDER BY
pub order_by: Option<Vec<SQLOrderByExpr>>,
/// GROUP BY
pub group_by: Option<Vec<ASTNode>>,
/// HAVING
pub having: Option<Box<ASTNode>>,
/// LIMIT
pub limit: Option<Box<ASTNode>>,
}
impl ToString for SQLSelect {
fn to_string(&self) -> String {
let mut s = format!(
"SELECT {}",
self.projection
.iter()
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(", ")
);
if let Some(ref relation) = self.relation {
s += &format!(" FROM {}", relation.as_ref().to_string());
}
for join in &self.joins {
s += &join.to_string();
}
if let Some(ref selection) = self.selection {
s += &format!(" WHERE {}", selection.as_ref().to_string());
}
if let Some(ref group_by) = self.group_by {
s += &format!(
" GROUP BY {}",
group_by
.iter()
.map(|g| g.to_string())
.collect::<Vec<String>>()
.join(", ")
);
}
if let Some(ref having) = self.having {
s += &format!(" HAVING {}", having.as_ref().to_string());
}
if let Some(ref order_by) = self.order_by {
s += &format!(
" ORDER BY {}",
order_by
.iter()
.map(|o| o.to_string())
.collect::<Vec<String>>()
.join(", ")
);
}
if let Some(ref limit) = self.limit {
s += &format!(" LIMIT {}", limit.as_ref().to_string());
}
s
}
}

View file

@ -1142,7 +1142,7 @@ impl Parser {
next_token
))
} else {
Ok(ASTNode::SQLSelect {
Ok(ASTNode::SQLSelect(SQLSelect {
projection,
selection,
relation,
@ -1151,7 +1151,7 @@ impl Parser {
order_by,
group_by,
having,
})
}))
}
}

View file

@ -11,7 +11,7 @@ fn parse_simple_select() {
let sql = String::from("SELECT id, fname, lname FROM customer WHERE id = 1");
let ast = parse_sql(&sql);
match ast {
ASTNode::SQLSelect { projection, .. } => {
ASTNode::SQLSelect(SQLSelect { projection, .. }) => {
assert_eq!(3, projection.len());
}
_ => assert!(false),

View file

@ -62,9 +62,9 @@ fn parse_where_delete_statement() {
fn parse_simple_select() {
let sql = String::from("SELECT id, fname, lname FROM customer WHERE id = 1 LIMIT 5");
match verified(&sql) {
ASTNode::SQLSelect {
ASTNode::SQLSelect(SQLSelect {
projection, limit, ..
} => {
}) => {
assert_eq!(3, projection.len());
assert_eq!(Some(Box::new(ASTNode::SQLValue(Value::Long(5)))), limit);
}
@ -76,7 +76,7 @@ fn parse_simple_select() {
fn parse_select_wildcard() {
let sql = String::from("SELECT * FROM customer");
match verified(&sql) {
ASTNode::SQLSelect { projection, .. } => {
ASTNode::SQLSelect(SQLSelect { projection, .. }) => {
assert_eq!(1, projection.len());
assert_eq!(ASTNode::SQLWildcard, projection[0]);
}
@ -88,7 +88,7 @@ fn parse_select_wildcard() {
fn parse_select_count_wildcard() {
let sql = String::from("SELECT COUNT(*) FROM customer");
match verified(&sql) {
ASTNode::SQLSelect { projection, .. } => {
ASTNode::SQLSelect(SQLSelect { projection, .. }) => {
assert_eq!(1, projection.len());
assert_eq!(
ASTNode::SQLFunction {
@ -191,7 +191,7 @@ fn parse_is_not_null() {
fn parse_like() {
let sql = String::from("SELECT * FROM customers WHERE name LIKE '%a'");
match verified(&sql) {
ASTNode::SQLSelect { selection, .. } => {
ASTNode::SQLSelect(SQLSelect { selection, .. }) => {
assert_eq!(
ASTNode::SQLBinaryExpr {
left: Box::new(ASTNode::SQLIdentifier("name".to_string())),
@ -211,7 +211,7 @@ fn parse_like() {
fn parse_not_like() {
let sql = String::from("SELECT * FROM customers WHERE name NOT LIKE '%a'");
match verified(&sql) {
ASTNode::SQLSelect { selection, .. } => {
ASTNode::SQLSelect(SQLSelect { selection, .. }) => {
assert_eq!(
ASTNode::SQLBinaryExpr {
left: Box::new(ASTNode::SQLIdentifier("name".to_string())),
@ -231,7 +231,7 @@ fn parse_not_like() {
fn parse_select_order_by() {
fn chk(sql: &str) {
match verified(&sql) {
ASTNode::SQLSelect { order_by, .. } => {
ASTNode::SQLSelect(SQLSelect { order_by, .. }) => {
assert_eq!(
Some(vec![
SQLOrderByExpr {
@ -265,9 +265,9 @@ fn parse_select_order_by_limit() {
);
let ast = parse_sql(&sql);
match ast {
ASTNode::SQLSelect {
ASTNode::SQLSelect(SQLSelect {
order_by, limit, ..
} => {
}) => {
assert_eq!(
Some(vec![
SQLOrderByExpr {
@ -291,7 +291,7 @@ fn parse_select_order_by_limit() {
fn parse_select_group_by() {
let sql = String::from("SELECT id, fname, lname FROM customer GROUP BY lname, fname");
match verified(&sql) {
ASTNode::SQLSelect { group_by, .. } => {
ASTNode::SQLSelect(SQLSelect { group_by, .. }) => {
assert_eq!(
Some(vec![
ASTNode::SQLIdentifier("lname".to_string()),
@ -316,7 +316,7 @@ fn parse_limit_accepts_all() {
fn parse_cast() {
let sql = String::from("SELECT CAST(id AS bigint) FROM customer");
match verified(&sql) {
ASTNode::SQLSelect { projection, .. } => {
ASTNode::SQLSelect(SQLSelect { projection, .. }) => {
assert_eq!(1, projection.len());
assert_eq!(
ASTNode::SQLCast {
@ -377,7 +377,7 @@ fn parse_create_table() {
fn parse_scalar_function_in_projection() {
let sql = String::from("SELECT sqrt(id) FROM foo");
match verified(&sql) {
ASTNode::SQLSelect { projection, .. } => {
ASTNode::SQLSelect(SQLSelect { projection, .. }) => {
assert_eq!(
vec![ASTNode::SQLFunction {
id: String::from("sqrt"),
@ -401,7 +401,7 @@ fn parse_aggregate_with_group_by() {
fn parse_literal_string() {
let sql = "SELECT 'one'";
match verified(&sql) {
ASTNode::SQLSelect { ref projection, .. } => {
ASTNode::SQLSelect(SQLSelect { ref projection, .. }) => {
assert_eq!(
projection[0],
ASTNode::SQLValue(Value::SingleQuotedString("one".to_string()))
@ -427,7 +427,7 @@ fn parse_simple_math_expr_minus() {
fn parse_select_version() {
let sql = "SELECT @@version";
match verified(&sql) {
ASTNode::SQLSelect { ref projection, .. } => {
ASTNode::SQLSelect(SQLSelect { ref projection, .. }) => {
assert_eq!(
projection[0],
ASTNode::SQLIdentifier("@@version".to_string())
@ -466,10 +466,10 @@ fn parse_case_expression() {
let sql = "SELECT CASE WHEN bar IS NULL THEN 'null' WHEN bar = 0 THEN '=0' WHEN bar >= 0 THEN '>=0' ELSE '<0' END FROM foo";
let ast = parse_sql(&sql);
assert_eq!(sql, ast.to_string());
use self::ASTNode::*;
use self::ASTNode::{SQLBinaryExpr, SQLCase, SQLIdentifier, SQLIsNull, SQLValue};
use self::SQLOperator::*;
match ast {
ASTNode::SQLSelect { projection, .. } => {
ASTNode::SQLSelect(SQLSelect { projection, .. }) => {
assert_eq!(1, projection.len());
assert_eq!(
SQLCase {
@ -507,7 +507,7 @@ fn parse_select_with_semi_colon() {
let sql = String::from("SELECT id, fname, lname FROM customer WHERE id = 1;");
let ast = parse_sql(&sql);
match ast {
ASTNode::SQLSelect { projection, .. } => {
ASTNode::SQLSelect(SQLSelect { projection, .. }) => {
assert_eq!(3, projection.len());
}
_ => assert!(false),
@ -536,7 +536,7 @@ fn parse_implicit_join() {
let sql = "SELECT * FROM t1, t2";
match verified(sql) {
ASTNode::SQLSelect { joins, .. } => {
ASTNode::SQLSelect(SQLSelect { joins, .. }) => {
assert_eq!(joins.len(), 1);
assert_eq!(
joins[0],
@ -558,7 +558,7 @@ fn parse_cross_join() {
let sql = "SELECT * FROM t1 CROSS JOIN t2";
match verified(sql) {
ASTNode::SQLSelect { joins, .. } => {
ASTNode::SQLSelect(SQLSelect { joins, .. }) => {
assert_eq!(joins.len(), 1);
assert_eq!(
joins[0],
@ -711,7 +711,7 @@ fn parses_to(from: &str, to: &str) {
fn joins_from(ast: ASTNode) -> Vec<Join> {
match ast {
ASTNode::SQLSelect { joins, .. } => joins,
ASTNode::SQLSelect(SQLSelect { joins, .. }) => joins,
_ => panic!("Expected SELECT"),
}
}