Skip to main content

gluesql_core/query_builder/
select.rs

1mod distinct;
2mod filter;
3mod group_by;
4mod having;
5mod join;
6mod limit;
7mod offset;
8mod offset_limit;
9mod order_by;
10mod project;
11mod root;
12mod values;
13
14use {
15    super::Build,
16    crate::{
17        ast::{Query, Select, SetExpr},
18        plan::{
19            AggregationInputPlan, AggregationPlan, FilterInputPlan, FilterPlan, HavingPlan,
20            ProjectInputPlan, ProjectPlan, ProjectionPlan, QueryPlan, SelectItemPlan, SourcePlan,
21            StatementPlan,
22        },
23        result::Result,
24    },
25};
26pub use {
27    distinct::DistinctNode,
28    filter::FilterNode,
29    group_by::GroupByNode,
30    having::HavingNode,
31    join::{
32        InnerHashJoinNode, InnerJoinConditionNode, InnerNestedLoopJoinNode, LeftOuterHashJoinNode,
33        LeftOuterJoinConditionNode, LeftOuterNestedLoopJoinNode,
34    },
35    limit::LimitNode,
36    offset::OffsetNode,
37    offset_limit::OffsetLimitNode,
38    order_by::{SelectOrderByNode, ValuesOrderByNode},
39    project::ProjectNode,
40    root::{SelectNode, select},
41    values::{ValuesNode, values},
42};
43
44pub(super) trait BuildSourcePlan {
45    fn build_source_plan(self) -> Result<SourcePlan>;
46}
47
48pub(super) trait BuildFilterInputPlan {
49    fn build_filter_input_plan(self) -> Result<FilterInputPlan>;
50}
51
52pub(super) trait BuildFilterPlan {
53    fn build_filter_plan(self) -> Result<FilterPlan>;
54}
55
56pub(super) trait BuildAggregationInputPlan {
57    fn build_aggregation_input_plan(self) -> Result<AggregationInputPlan>;
58}
59
60pub(super) trait BuildAggregationPlan {
61    fn build_aggregation_plan(self) -> Result<AggregationPlan>;
62}
63
64pub(super) trait BuildHavingPlan {
65    fn build_having_plan(self) -> Result<HavingPlan>;
66}
67
68pub(super) trait BuildProjectInputPlan {
69    fn build_project_input_plan(self) -> Result<ProjectInputPlan>;
70}
71
72pub(super) trait BuildProjectPlan {
73    fn build_project_plan(self) -> Result<ProjectPlan>;
74}
75
76pub(super) trait BuildSelect {
77    fn build_select(self) -> Result<Select>;
78}
79
80pub(super) trait BuildQueryPlan {
81    fn build_query_plan(self) -> Result<QueryPlan>;
82}
83
84pub(super) trait BuildQuery {
85    fn build_query(self) -> Result<Query>;
86}
87
88impl<T: BuildProjectInputPlan> BuildProjectPlan for T {
89    fn build_project_plan(self) -> Result<ProjectPlan> {
90        self.build_project_input_plan().map(|input| ProjectPlan {
91            input,
92            projection: ProjectionPlan::SelectItems(vec![SelectItemPlan::Wildcard]),
93        })
94    }
95}
96
97impl<T: BuildProjectPlan> BuildQueryPlan for T {
98    fn build_query_plan(self) -> Result<QueryPlan> {
99        self.build_project_plan().map(QueryPlan::Project)
100    }
101}
102
103impl<T: BuildSelect> BuildQuery for T {
104    fn build_query(self) -> Result<Query> {
105        let select = self.build_select()?;
106        let body = SetExpr::Select(Box::new(select));
107        let query = Query {
108            body,
109            order_by: Vec::new(),
110            limit: None,
111            offset: None,
112        };
113
114        Ok(query)
115    }
116}
117
118impl<T: BuildQueryPlan> Build for T {
119    fn build(self) -> Result<StatementPlan> {
120        let query = self.build_query_plan()?;
121
122        Ok(StatementPlan::Query(query))
123    }
124}