Lightweight 0.20250904.0
Loading...
Searching...
No Matches
Select.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../SqlQueryFormatter.hpp"
6#include "../Utils.hpp"
7#include "Core.hpp"
8
9#include <reflection-cpp/reflection.hpp>
10
11namespace Lightweight
12{
13
14enum class SqlQueryBuilderMode : uint8_t
15{
16 Fluent,
17 Varying
18};
19
20struct [[nodiscard]] SqlFieldExpression final
21{
22 std::string expression;
23};
24
25namespace Aggregate
26{
27
28 inline SqlFieldExpression Count(std::string const& field = "*") noexcept
29 {
30 return SqlFieldExpression { .expression = std::format("COUNT(\"{}\")", field) };
31 }
32
33 inline SqlFieldExpression Count(SqlQualifiedTableColumnName const& field) noexcept
34 {
35 return SqlFieldExpression { .expression = std::format(R"(COUNT("{}"."{}"))", field.tableName, field.columnName) };
36 }
37
38 inline SqlFieldExpression Sum(std::string const& field) noexcept
39 {
40 return SqlFieldExpression { .expression = std::format("SUM(\"{}\")", field) };
41 }
42
43 inline SqlFieldExpression Sum(SqlQualifiedTableColumnName const& field) noexcept
44 {
45 return SqlFieldExpression { .expression = std::format(R"(SUM("{}"."{}"))", field.tableName, field.columnName) };
46 }
47
48 inline SqlFieldExpression Avg(std::string const& field) noexcept
49 {
50 return SqlFieldExpression { .expression = std::format("AVG(\"{}\")", field) };
51 }
52
53 inline SqlFieldExpression Avg(SqlQualifiedTableColumnName const& field) noexcept
54 {
55 return SqlFieldExpression { .expression = std::format(R"(AVG("{}"."{}"))", field.tableName, field.columnName) };
56 }
57
58 inline SqlFieldExpression Min(std::string const& field) noexcept
59 {
60 return SqlFieldExpression { .expression = std::format("MIN(\"{}\")", field) };
61 }
62
63 inline SqlFieldExpression Min(SqlQualifiedTableColumnName const& field) noexcept
64 {
65 return SqlFieldExpression { .expression = std::format(R"(MIN("{}"."{}"))", field.tableName, field.columnName) };
66 }
67
68 inline SqlFieldExpression Max(std::string const& field) noexcept
69 {
70 return SqlFieldExpression { .expression = std::format("MAX(\"{}\")", field) };
71 }
72
73 inline SqlFieldExpression Max(SqlQualifiedTableColumnName const& field) noexcept
74 {
75 return SqlFieldExpression { .expression = std::format(R"(MAX("{}"."{}"))", field.tableName, field.columnName) };
76 }
77
78} // namespace Aggregate
79
80/// @brief Query builder for building SELECT ... queries.
81///
82/// @see SqlQueryBuilder
83class [[nodiscard]] SqlSelectQueryBuilder final: public SqlBasicSelectQueryBuilder<SqlSelectQueryBuilder>
84{
85 public:
86 using SelectType = detail::SelectType;
87
88 explicit SqlSelectQueryBuilder(SqlQueryFormatter const& formatter, std::string table, std::string tableAlias) noexcept:
89 SqlBasicSelectQueryBuilder<SqlSelectQueryBuilder> {},
90 _formatter { formatter }
91 {
92 _query.formatter = &formatter;
93 _query.searchCondition.tableName = std::move(table);
94 _query.searchCondition.tableAlias = std::move(tableAlias);
95 _query.fields.reserve(256);
96 }
97
98 /// Sets the builder mode to Varying, allowing varying final query types.
99 constexpr LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder& Varying() noexcept
100 {
101 _mode = SqlQueryBuilderMode::Varying;
102 return *this;
103 }
104
105 /// Adds a sequence of columns to the SELECT clause.
106 template <typename... MoreFields>
107 SqlSelectQueryBuilder& Fields(std::string_view const& firstField, MoreFields&&... moreFields);
108
109 /// Adds a single column to the SELECT clause.
110 LIGHTWEIGHT_API SqlSelectQueryBuilder& Field(std::string_view const& fieldName);
111
112 /// Adds a single column to the SELECT clause.
113 LIGHTWEIGHT_API SqlSelectQueryBuilder& Field(SqlQualifiedTableColumnName const& fieldName);
114
115 /// Adds an aggregate function call to the SELECT clause.
116 LIGHTWEIGHT_API SqlSelectQueryBuilder& Field(SqlFieldExpression const& fieldExpression);
117
118 /// Aliases the last added field (a column or an aggregate call) in the SELECT clause.
119 LIGHTWEIGHT_API SqlSelectQueryBuilder& As(std::string_view alias);
120
121 /// Adds a sequence of columns to the SELECT clause.
122 LIGHTWEIGHT_API SqlSelectQueryBuilder& Fields(std::vector<std::string_view> const& fieldNames);
123
124 /// Adds a sequence of columns from the given table to the SELECT clause.
125 LIGHTWEIGHT_API SqlSelectQueryBuilder& Fields(std::vector<std::string_view> const& fieldNames,
126 std::string_view tableName);
127
128 LIGHTWEIGHT_API SqlSelectQueryBuilder& Fields(std::initializer_list<std::string_view> const& fieldNames,
129 std::string_view tableName);
130
131 /// Adds a sequence of columns from the given tables to the SELECT clause.
132 template <typename FirstRecord, typename... MoreRecords>
134
135 /// Adds a single column with an alias to the SELECT clause.
136 [[deprecated("Use Field(...).As(\"alias\") instead.")]]
137 LIGHTWEIGHT_API SqlSelectQueryBuilder& FieldAs(std::string_view const& fieldName, std::string_view const& alias);
138
139 /// Adds a single column with an alias to the SELECT clause.
140 [[deprecated("Use Field(...).As(\"alias\") instead.")]]
141 LIGHTWEIGHT_API SqlSelectQueryBuilder& FieldAs(SqlQualifiedTableColumnName const& fieldName,
142 std::string_view const& alias);
143
144 template <typename Callable>
145 SqlSelectQueryBuilder& Build(Callable const& callable);
146
147 /// Finalizes building the query as SELECT COUNT(*) ... query.
148 LIGHTWEIGHT_API ComposedQuery Count();
149
150 /// Finalizes building the query as SELECT field names FROM ... query.
151 LIGHTWEIGHT_API ComposedQuery All();
152
153 /// Finalizes building the query as SELECT TOP n field names FROM ... query.
154 LIGHTWEIGHT_API ComposedQuery First(size_t count = 1);
155
156 /// Finalizes building the query as SELECT field names FROM ... query with a range.
157 LIGHTWEIGHT_API ComposedQuery Range(std::size_t offset, std::size_t limit);
158
159 LIGHTWEIGHT_FORCE_INLINE SqlSearchCondition& SearchCondition() noexcept
160 {
161 return _query.searchCondition;
162 }
163
164 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE SqlQueryFormatter const& Formatter() const noexcept
165 {
166 return _formatter;
167 }
168
169 private:
170 SqlQueryFormatter const& _formatter;
171 SqlQueryBuilderMode _mode = SqlQueryBuilderMode::Fluent;
172 bool _aliasAllowed = false;
173};
174
175template <typename... MoreFields>
176SqlSelectQueryBuilder& SqlSelectQueryBuilder::Fields(std::string_view const& firstField, MoreFields&&... moreFields)
177{
178 using namespace std::string_view_literals;
179
180 std::ostringstream fragment;
181
182 if (!_query.fields.empty())
183 fragment << ", "sv;
184
185 fragment << '"' << firstField << '"';
186
187 if constexpr (sizeof...(MoreFields) > 0)
188 ((fragment << R"(, ")"sv << std::forward<MoreFields>(moreFields) << '"') << ...);
189
190 _query.fields += fragment.str();
191 return *this;
192}
193
194template <typename Callable>
195inline LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder& SqlSelectQueryBuilder::Build(Callable const& callable)
196{
197 callable(*this);
198 return *this;
199}
200
201template <typename FirstRecord, typename... MoreRecords>
202inline LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder& SqlSelectQueryBuilder::Fields()
203{
204 if constexpr (sizeof...(MoreRecords) == 0)
205 {
206 Reflection::EnumerateMembers<FirstRecord>(
207 [&]<size_t FieldIndex, typename FieldType>() { Field(FieldNameAt<FieldIndex, FirstRecord>); });
208 }
209 else
210 {
211 Reflection::EnumerateMembers<FirstRecord>([&]<size_t FieldIndex, typename FieldType>() {
212 Field(SqlQualifiedTableColumnName {
213 .tableName = RecordTableName<FirstRecord>,
214 .columnName = FieldNameAt<FieldIndex, FirstRecord>,
215 });
216 });
217
218 (Reflection::EnumerateMembers<MoreRecords>([&]<size_t FieldIndex, typename FieldType>() {
219 Field(SqlQualifiedTableColumnName {
220 .tableName = RecordTableName<MoreRecords>,
221 .columnName = FieldNameAt<FieldIndex, MoreRecords>,
222 });
223 }),
224 ...);
225 }
226 return *this;
227}
228
229} // namespace Lightweight
API to format SQL queries for different SQL dialects.
Query builder for building SELECT ... queries.
Definition Select.hpp:84
LIGHTWEIGHT_API SqlSelectQueryBuilder & FieldAs(std::string_view const &fieldName, std::string_view const &alias)
Adds a single column with an alias to the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder & Fields(std::vector< std::string_view > const &fieldNames, std::string_view tableName)
Adds a sequence of columns from the given table to the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder & Field(SqlQualifiedTableColumnName const &fieldName)
Adds a single column to the SELECT clause.
SqlSelectQueryBuilder & Fields()
Adds a sequence of columns from the given tables to the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder & Fields(std::vector< std::string_view > const &fieldNames)
Adds a sequence of columns to the SELECT clause.
LIGHTWEIGHT_API ComposedQuery First(size_t count=1)
Finalizes building the query as SELECT TOP n field names FROM ... query.
LIGHTWEIGHT_API ComposedQuery All()
Finalizes building the query as SELECT field names FROM ... query.
LIGHTWEIGHT_API SqlSelectQueryBuilder & Field(SqlFieldExpression const &fieldExpression)
Adds an aggregate function call to the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder & FieldAs(SqlQualifiedTableColumnName const &fieldName, std::string_view const &alias)
Adds a single column with an alias to the SELECT clause.
LIGHTWEIGHT_API ComposedQuery Count()
Finalizes building the query as SELECT COUNT(*) ... query.
LIGHTWEIGHT_API SqlSelectQueryBuilder & As(std::string_view alias)
Aliases the last added field (a column or an aggregate call) in the SELECT clause.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder & Varying() noexcept
Sets the builder mode to Varying, allowing varying final query types.
Definition Select.hpp:99
LIGHTWEIGHT_API SqlSelectQueryBuilder & Field(std::string_view const &fieldName)
Adds a single column to the SELECT clause.
LIGHTWEIGHT_API ComposedQuery Range(std::size_t offset, std::size_t limit)
Finalizes building the query as SELECT field names FROM ... query with a range.