use std::sync::Arc;
use crate::tree::ast::clause::TableReference;
use crate::tree::ast::command::Command;
use crate::tree::ast::dataset_identifier::DatasetIdentifier;
use crate::tree::ast::identifier::{Identifier, SimpleIdentifier};
use crate::tree::ast::node::Span;
use crate::tree::ast::pipeline::Pipeline;
use super::command::*;
use super::IntoExpressionBuilder;
impl From<SetCommandBuilder> for Command {
fn from(value: SetCommandBuilder) -> Self {
value.build()
}
}
impl From<WhereCommandBuilder> for Command {
fn from(value: WhereCommandBuilder) -> Self {
value.build()
}
}
impl From<SelectCommandBuilder> for Command {
fn from(value: SelectCommandBuilder) -> Self {
value.build()
}
}
impl From<DropCommandBuilder> for Command {
fn from(value: DropCommandBuilder) -> Self {
value.build()
}
}
impl From<FromCommandBuilder> for Command {
fn from(value: FromCommandBuilder) -> Self {
value.build()
}
}
impl From<UnionCommandBuilder> for Command {
fn from(value: UnionCommandBuilder) -> Self {
value.build()
}
}
impl From<LimitCommandBuilder> for Command {
fn from(value: LimitCommandBuilder) -> Self {
value.build()
}
}
impl From<WithinCommandBuilder> for Command {
fn from(value: WithinCommandBuilder) -> Self {
value.build()
}
}
impl From<SortCommandBuilder> for Command {
fn from(value: SortCommandBuilder) -> Self {
value.build()
}
}
impl From<ValidParseCommandBuilder> for Command {
fn from(value: ValidParseCommandBuilder) -> Self {
value.build()
}
}
impl From<AggCommandBuilder> for Command {
fn from(value: AggCommandBuilder) -> Self {
value.build()
}
}
impl From<WindowCommandBuilder> for Command {
fn from(value: WindowCommandBuilder) -> Self {
value.build()
}
}
impl From<SuppressCommandBuilder> for Command {
fn from(value: SuppressCommandBuilder) -> Self {
value.build()
}
}
impl From<AppendCommandBuilder> for Command {
fn from(value: AppendCommandBuilder) -> Self {
value.build()
}
}
impl From<JoinCommandBuilderWithOn> for Command {
fn from(value: JoinCommandBuilderWithOn) -> Self {
value.build()
}
}
impl From<LookupCommandBuilderWithOn> for Command {
fn from(value: LookupCommandBuilderWithOn) -> Self {
value.build()
}
}
impl From<UnnestCommandBuilder> for Command {
fn from(value: UnnestCommandBuilder) -> Self {
value.build()
}
}
impl From<RowsCommandBuilder> for Command {
fn from(value: RowsCommandBuilder) -> Self {
value.build()
}
}
impl From<NestCommandBuilder> for Command {
fn from(value: NestCommandBuilder) -> Self {
value.build()
}
}
impl From<MatchCommandBuilder> for Command {
fn from(value: MatchCommandBuilder) -> Self {
value.build()
}
}
#[derive(Debug)]
pub struct PipelineBuilder {
commands: Vec<Command>,
span: Span,
}
impl PipelineBuilder {
pub fn new() -> Self {
Self {
commands: Vec::new(),
span: Span::NONE,
}
}
pub fn at(mut self, span: Span) -> Self {
self.span = span;
self
}
pub fn command(mut self, cmd: impl Into<Command>) -> Self {
self.commands.push(cmd.into());
self
}
pub fn set_cmd<F>(self, f: F) -> Self
where
F: FnOnce(SetCommandBuilder) -> SetCommandBuilder,
{
self.command(f(set_command()))
}
pub fn where_cmd(self, condition: impl IntoExpressionBuilder) -> Self {
self.command(where_command(condition))
}
pub fn select<F>(self, f: F) -> Self
where
F: FnOnce(SelectCommandBuilder) -> SelectCommandBuilder,
{
self.command(f(select_command()))
}
pub fn drop<F>(self, f: F) -> Self
where
F: FnOnce(DropCommandBuilder) -> DropCommandBuilder,
{
self.command(f(drop_command()))
}
pub fn from<F>(self, f: F) -> Self
where
F: FnOnce(FromCommandBuilder) -> FromCommandBuilder,
{
self.command(f(from_command()))
}
pub fn union<F>(self, f: F) -> Self
where
F: FnOnce(UnionCommandBuilder) -> UnionCommandBuilder,
{
self.command(f(union_command()))
}
pub fn limit(self, count: impl IntoExpressionBuilder) -> Self {
self.command(limit_command(count))
}
pub fn within(self, duration: impl IntoExpressionBuilder) -> Self {
self.command(within_command(duration))
}
pub fn sort<F>(self, f: F) -> Self
where
F: FnOnce(SortCommandBuilder) -> SortCommandBuilder,
{
self.command(f(sort_command()))
}
pub fn parse<F>(self, f: F) -> Self
where
F: FnOnce(ParseCommandBuilder) -> ValidParseCommandBuilder,
{
self.command(f(parse_command()))
}
pub fn agg<F>(self, f: F) -> Self
where
F: FnOnce(AggCommandBuilder) -> AggCommandBuilder,
{
self.command(f(agg_command()))
}
pub fn window<F>(self, f: F) -> Self
where
F: FnOnce(WindowCommandBuilder) -> WindowCommandBuilder,
{
self.command(f(window_command()))
}
pub fn suppress<F>(self, interval: impl IntoExpressionBuilder, f: F) -> Self
where
F: FnOnce(SuppressCommandBuilder) -> SuppressCommandBuilder,
{
self.command(f(suppress_command(interval)))
}
pub fn append(
self,
table: impl Into<TableReference>,
f: impl FnOnce(AppendCommandBuilder) -> AppendCommandBuilder,
) -> Self {
self.command(f(append_command(table)))
}
pub fn join_dataset(
self,
dataset: DatasetIdentifier,
condition: impl IntoExpressionBuilder,
) -> Self {
self.command(join_dataset_reference(dataset).on(condition))
}
pub fn join_dataset_aliased(
self,
alias: impl Into<SimpleIdentifier>,
dataset: DatasetIdentifier,
condition: impl IntoExpressionBuilder,
) -> Self {
self.command(JoinCommandBuilder::table_alias_dataset(alias, dataset).on(condition))
}
pub fn lookup_dataset(
self,
dataset: DatasetIdentifier,
condition: impl IntoExpressionBuilder,
) -> Self {
self.command(lookup_dataset_reference(dataset).on(condition))
}
pub fn join(
self,
table: impl Into<TableReference>,
condition: impl IntoExpressionBuilder,
) -> Self {
self.command(join_table_reference(table).on(condition))
}
pub fn join_aliased(
self,
alias: impl Into<SimpleIdentifier>,
table: impl Into<TableReference>,
condition: impl IntoExpressionBuilder,
) -> Self {
self.command(join_table_alias(alias, table).on(condition))
}
pub fn lookup(
self,
table: impl Into<TableReference>,
condition: impl IntoExpressionBuilder,
) -> Self {
self.command(lookup_table_reference(table).on(condition))
}
pub fn lookup_aliased(
self,
alias: impl Into<SimpleIdentifier>,
table: impl Into<TableReference>,
condition: impl IntoExpressionBuilder,
) -> Self {
self.command(lookup_table_alias(alias, table).on(condition))
}
pub fn explode<F>(self, f: F) -> Self
where
F: FnOnce(ExplodeCommandBuilder<NoClause>) -> ExplodeCommandBuilder<WithClause>,
{
self.command(f(explode_command()))
}
pub fn unnest(self, expression: impl IntoExpressionBuilder) -> Self {
self.command(unnest_command(expression))
}
pub fn rows(self, expression: impl IntoExpressionBuilder) -> Self {
self.command(rows_command(expression))
}
pub fn nest(self, identifier: impl Into<Identifier>) -> Self {
self.command(nest_command(identifier))
}
pub fn match_cmd<F>(self, f: F) -> Self
where
F: FnOnce(MatchCommandBuilder) -> MatchCommandBuilder,
{
self.command(f(match_command()))
}
pub fn build(&self) -> Pipeline {
Pipeline {
span: self.span,
commands: self.commands.iter().cloned().map(Arc::new).collect(),
}
}
}
impl From<Arc<Command>> for Command {
fn from(cmd: Arc<Command>) -> Self {
(*cmd).clone()
}
}
impl From<PipelineBuilder> for Pipeline {
fn from(value: PipelineBuilder) -> Self {
value.build()
}
}
impl Default for PipelineBuilder {
fn default() -> Self {
Self::new()
}
}
pub fn pipeline() -> PipelineBuilder {
PipelineBuilder::new()
}
pub fn pipeline_from_expression(expr: impl IntoExpressionBuilder) -> Pipeline {
Pipeline::from(expr.into_expression_builder().build())
}