123 lines
3.0 KiB
Rust
Raw Normal View History

/// A parsed `SELECT-FROM-WHERE-GROUP BY-ORDER BY-LIMIT` statement in the
/// current SQL subset.
2026-04-09 12:38:43 +02:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Select {
/// Output expressions requested by the query.
2026-04-09 12:38:43 +02:00
pub projection: Vec<SelectItem>,
2026-04-10 09:56:18 +02:00
/// Source tables and their optional aliases.
pub from: Vec<TableRef>,
/// Optional filter predicate.
2026-04-09 12:38:43 +02:00
pub selection: Option<Expr>,
/// Grouping columns. Empty means no `GROUP BY` clause.
pub group_by: Vec<Expr>,
2026-04-10 10:10:46 +02:00
/// Optional output ordering.
pub order_by: Vec<OrderByItem>,
/// Optional row limit.
pub limit: Option<usize>,
2026-04-09 12:38:43 +02:00
}
2026-04-10 09:56:18 +02:00
/// One source entry in a `FROM` list.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableRef {
/// The predicate-backed table name.
pub name: String,
/// Optional table alias used for qualification.
pub alias: Option<String>,
}
2026-04-10 10:10:46 +02:00
/// One item in an `ORDER BY` clause.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OrderByItem {
/// Output column name to sort by.
pub expr: Expr,
/// Sort direction.
pub direction: SortDirection,
}
/// One item in a `SELECT` projection list.
2026-04-09 12:38:43 +02:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SelectItem {
/// `*`
2026-04-09 12:38:43 +02:00
Wildcard,
/// A projected expression, optionally renamed with `AS`.
2026-04-09 12:38:43 +02:00
Expr { expr: Expr, alias: Option<String> },
}
/// A SQL expression in the current subset.
2026-04-09 12:38:43 +02:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expr {
/// A column reference.
2026-04-09 12:38:43 +02:00
Identifier(String),
/// A literal value.
2026-04-09 12:38:43 +02:00
Literal(Literal),
/// A binary expression.
2026-04-09 12:38:43 +02:00
Binary {
left: Box<Expr>,
op: BinaryOp,
right: Box<Expr>,
},
/// An aggregate function applied to an argument.
Aggregate {
func: AggregateFunc,
arg: AggregateArg,
},
}
/// An aggregate function in the current SQL subset.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregateFunc {
/// Row count (with `*`) or count of non-null values (with a column).
Count,
/// Sum of integer values.
Sum,
/// Minimum value.
Min,
/// Maximum value.
Max,
/// Arithmetic mean of integer values.
Avg,
}
/// The argument to an aggregate function: either `*` (only valid for
/// `COUNT`) or an expression.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AggregateArg {
/// `COUNT(*)` style argument.
Star,
/// An expression argument such as `SUM(col)`.
Expr(Box<Expr>),
2026-04-09 12:38:43 +02:00
}
/// A SQL literal in the current subset.
2026-04-09 12:38:43 +02:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Literal {
/// A string literal.
2026-04-09 12:38:43 +02:00
String(String),
/// An integer literal.
Integer(i64),
/// The `NULL` literal.
2026-04-09 12:38:43 +02:00
Null,
}
/// A binary operator in the current subset.
2026-04-09 12:38:43 +02:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
/// Equality.
2026-04-09 12:38:43 +02:00
Eq,
/// Inequality.
Ne,
/// Boolean conjunction.
And,
/// Boolean disjunction.
Or,
2026-04-09 12:38:43 +02:00
}
2026-04-10 10:10:46 +02:00
/// Sort direction for `ORDER BY`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortDirection {
/// Ascending order.
Asc,
/// Descending order.
Desc,
}