Lightweight 0.20251201.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 // clang-format off
160 LIGHTWEIGHT_FORCE_INLINE SqlSearchCondition& SearchCondition() noexcept // NOLINT(bugprone-derived-method-shadowing-base-method)
161 {
162 // clang-format on
163 return _query.searchCondition;
164 }
165
166 // clang-format off
167 [[nodiscard]] LIGHTWEIGHT_FORCE_INLINE SqlQueryFormatter const& Formatter() const noexcept // NOLINT(bugprone-derived-method-shadowing-base-method)
168 {
169 // clang-format on
170 return _formatter;
171 }
172
173 private:
174 SqlQueryFormatter const& _formatter;
175 SqlQueryBuilderMode _mode = SqlQueryBuilderMode::Fluent;
176 bool _aliasAllowed = false;
177};
178
179template <typename... MoreFields>
180SqlSelectQueryBuilder& SqlSelectQueryBuilder::Fields(std::string_view const& firstField, MoreFields&&... moreFields)
181{
182 using namespace std::string_view_literals;
183
184 std::ostringstream fragment;
185
186 if (!_query.fields.empty())
187 fragment << ", "sv;
188
189 fragment << '"' << firstField << '"';
190
191 if constexpr (sizeof...(MoreFields) > 0)
192 ((fragment << R"(, ")"sv << std::forward<MoreFields>(moreFields) << '"') << ...);
193
194 _query.fields += fragment.str();
195 return *this;
196}
197
198template <typename Callable>
199inline LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder& SqlSelectQueryBuilder::Build(Callable const& callable)
200{
201 callable(*this);
202 return *this;
203}
204
205template <typename FirstRecord, typename... MoreRecords>
206inline LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder& SqlSelectQueryBuilder::Fields()
207{
208 if constexpr (sizeof...(MoreRecords) == 0)
209 {
210 Reflection::EnumerateMembers<FirstRecord>(
211 [&]<size_t FieldIndex, typename FieldType>() { Field(FieldNameAt<FieldIndex, FirstRecord>); });
212 }
213 else
214 {
215 Reflection::EnumerateMembers<FirstRecord>([&]<size_t FieldIndex, typename FieldType>() {
216 Field(SqlQualifiedTableColumnName {
217 .tableName = RecordTableName<FirstRecord>,
218 .columnName = FieldNameAt<FieldIndex, FirstRecord>,
219 });
220 });
221
222 (Reflection::EnumerateMembers<MoreRecords>([&]<size_t FieldIndex, typename FieldType>() {
223 Field(SqlQualifiedTableColumnName {
224 .tableName = RecordTableName<MoreRecords>,
225 .columnName = FieldNameAt<FieldIndex, MoreRecords>,
226 });
227 }),
228 ...);
229 }
230 return *this;
231}
232
233} // 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.