IronQuery
A SQL-first query builder for C++
Loading...
Searching...
No Matches
select_expr.hpp
1#pragma once
2
3#include <initializer_list>
4#include <iron_query/condition.hpp>
5#include <iron_query/order_by.hpp>
6#include <iron_query/select_item.hpp>
7#include <iron_query/virtual_table.hpp>
8#include <optional>
9#include <string>
10#include <vector>
11
12namespace iron_query {
13
17class [[nodiscard]] SelectExpr final : public VirtualTable {
18public:
23
26
31
37 SelectExpr DistinctOn(std::initializer_list<Expr> exps) &&;
38
42
45 SelectExpr Select(std::initializer_list<SelectItem> items) &&;
46
50
54
57 SelectExpr OrderBy(std::initializer_list<OrderByTerm> terms) &&;
58
64
70 SelectExpr GroupBy(std::initializer_list<Expr> exps) &&;
71
77
80 SelectExpr Limit(int limit) &&;
81
84 SelectExpr Offset(int offset) &&;
85
88 std::string ToString() const override;
89
102 std::string ToStringFormatted() const override;
103
104private:
105 void EnsureValid() const;
106
107 bool distinct_{false};
108 std::optional<std::vector<Expr>> distinct_on_;
109 std::string from_;
110 std::optional<std::vector<SelectItem>> select_;
111 std::optional<Condition> where_;
112 std::optional<std::vector<Expr>> group_by_;
113 std::optional<Condition> having_;
114 std::optional<std::vector<OrderByTerm>> order_by_;
115 std::optional<int> limit_;
116 std::optional<int> offset_;
117};
118
122
123} // namespace iron_query
A boolean-valued SQL predicate, e.g. the result of a comparison, LIKE/IN/BETWEEN/IS [NOT] NULL,...
Definition condition.hpp:25
Arbitrary SQL expression.
Definition expr.hpp:46
Transitional representation for SELECT query.
Definition select_expr.hpp:17
SelectExpr Limit(int limit) &&
Sets the LIMIT clause.
std::string ToString() const override
SelectExpr Select(std::initializer_list< SelectItem > items) &&
Sets the SELECT list to a comma-separated list of entries.
SelectExpr GroupBy(Expr exp) &&
Sets the GROUP BY clause to a single expression.
SelectExpr Having(Condition exp) &&
Sets the HAVING clause.
SelectExpr OrderBy(OrderByTerm term) &&
Sets the ORDER BY clause to a single term.
std::string ToStringFormatted() const override
Renders the query with each clause on its own line, indented, e.g.:
SelectExpr GroupBy(std::initializer_list< Expr > exps) &&
Sets the GROUP BY clause to a comma-separated list of expressions.
SelectExpr Where(Condition exp) &&
Sets the WHERE clause.
SelectExpr Distinct() &&
Adds DISTINCT to the SELECT list, eliminating duplicate rows.
SelectExpr Select(SelectItem item) &&
Sets the SELECT list to a single entry.
SelectExpr(const VirtualTable &tbl)
Starts a SELECT ... FROM tbl query. tbl may be a table, a join, or anything VirtualTable::As has alia...
SelectExpr DistinctOn(Expr exp) &&
Sets DISTINCT ON to a single expression. Mutually exclusive with Distinct.
SelectExpr DistinctOn(std::initializer_list< Expr > exps) &&
Sets DISTINCT ON to a comma-separated list of expressions. Mutually exclusive with Distinct.
SelectExpr OrderBy(std::initializer_list< OrderByTerm > terms) &&
Sets the ORDER BY clause to a comma-separated list of terms.
SelectExpr Offset(int offset) &&
Sets the OFFSET clause.
A single entry of a SELECT list: an expression, optionally renamed with Expr::As. Kept distinct from ...
Definition select_item.hpp:15
Any table value, including physical table, table result, materialized view, etc.
Definition virtual_table.hpp:14
SelectExpr From(const VirtualTable &tbl)
Handy fabric for SelectExpr.
A single ORDER BY term: an expression plus its sort direction and NULL placement. Implicitly construc...
Definition order_by.hpp:31