5#include "../SqlQueryFormatter.hpp"
9#include <reflection-cpp/reflection.hpp>
17enum class SqlQueryBuilderMode : uint8_t
23struct [[nodiscard]] SqlFieldExpression final
25 std::string expression;
31 inline SqlFieldExpression Count(std::string_view field =
"*") noexcept
34 return SqlFieldExpression { .expression =
"COUNT(*)" };
35 return SqlFieldExpression { .expression = std::format(
"COUNT(\"{}\")", field) };
38 inline SqlFieldExpression Count(SqlQualifiedTableColumnName
const& field)
noexcept
40 if (field.columnName ==
"*")
41 return SqlFieldExpression { .expression = std::format(R
"(COUNT("{}".*))", field.tableName) };
42 return SqlFieldExpression { .expression = std::format(R
"(COUNT("{}"."{}"))", field.tableName, field.columnName) };
45 inline SqlFieldExpression Sum(std::string_view field)
noexcept
47 return SqlFieldExpression { .expression = std::format(
"SUM(\"{}\")", field) };
50 inline SqlFieldExpression Sum(SqlQualifiedTableColumnName
const& field)
noexcept
52 return SqlFieldExpression { .expression = std::format(R
"(SUM("{}"."{}"))", field.tableName, field.columnName) };
55 inline SqlFieldExpression Avg(std::string_view field)
noexcept
57 return SqlFieldExpression { .expression = std::format(
"AVG(\"{}\")", field) };
60 inline SqlFieldExpression Avg(SqlQualifiedTableColumnName
const& field)
noexcept
62 return SqlFieldExpression { .expression = std::format(R
"(AVG("{}"."{}"))", field.tableName, field.columnName) };
65 inline SqlFieldExpression Min(std::string_view field)
noexcept
67 return SqlFieldExpression { .expression = std::format(
"MIN(\"{}\")", field) };
70 inline SqlFieldExpression Min(SqlQualifiedTableColumnName
const& field)
noexcept
72 return SqlFieldExpression { .expression = std::format(R
"(MIN("{}"."{}"))", field.tableName, field.columnName) };
75 inline SqlFieldExpression Max(std::string_view field)
noexcept
77 return SqlFieldExpression { .expression = std::format(
"MAX(\"{}\")", field) };
80 inline SqlFieldExpression Max(SqlQualifiedTableColumnName
const& field)
noexcept
82 return SqlFieldExpression { .expression = std::format(R
"(MAX("{}"."{}"))", field.tableName, field.columnName) };
102 using SelectType = detail::SelectType;
106 SqlBasicSelectQueryBuilder<SqlSelectQueryBuilder> {},
107 _formatter { formatter }
109 _query.formatter = &formatter;
110 _query.searchCondition.tableName = std::move(table);
111 _query.searchCondition.tableAlias = std::move(tableAlias);
112 _query.fields.reserve(256);
118 _mode = SqlQueryBuilderMode::Varying;
123 template <
typename... MoreFields>
163 std::string_view tableName);
168 std::string_view tableName)
const;
172 std::string_view tableName);
177 std::initializer_list<std::string_view>
const& fieldNames, std::string_view tableName)
const;
185 std::span<SqlQualifiedTableColumnName const> fieldNames)
const;
193 std::initializer_list<SqlQualifiedTableColumnName const> fieldNames)
const;
196 template <
typename FirstRecord,
typename... MoreRecords>
200 [[deprecated(
"Use Field(...).As(\"alias\") instead.")]]
204 [[deprecated(
"Use Field(...).As(\"alias\") instead.")]]
206 std::string_view
const& alias);
209 template <
typename Callable>
213 LIGHTWEIGHT_API ComposedQuery
Count();
218 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery
Count()
const;
221 LIGHTWEIGHT_API ComposedQuery
All();
225 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery
All()
const;
228 LIGHTWEIGHT_API ComposedQuery
First(
size_t count = 1);
232 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery
First(
size_t count = 1)
const;
235 LIGHTWEIGHT_API ComposedQuery
Range(std::size_t offset, std::size_t limit);
239 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery
Range(std::size_t offset, std::size_t limit)
const;
246 return _query.searchCondition;
259 SqlQueryBuilderMode _mode = SqlQueryBuilderMode::Fluent;
264 mutable bool _aliasAllowed =
false;
267template <
typename... MoreFields>
270 using namespace std::string_view_literals;
272 std::ostringstream fragment;
274 if (!_query.fields.empty())
277 fragment <<
'"' << firstField <<
'"';
279 if constexpr (
sizeof...(MoreFields) > 0)
280 ((fragment << R
"(, ")"sv << std::forward<MoreFields>(moreFields) << '"') << ...);
282 _query.fields += fragment.str();
287template <
typename Callable>
295template <
typename FirstRecord,
typename... MoreRecords>
298 if constexpr (
sizeof...(MoreRecords) == 0)
300 EnumerateRecordMembers<FirstRecord>(
301 [&]<
size_t FieldIndex,
typename FieldType>() {
Field(FieldNameAt<FieldIndex, FirstRecord>); });
305 EnumerateRecordMembers<FirstRecord>([&]<
size_t FieldIndex,
typename FieldType>() {
307 .
tableName = RecordTableName<FirstRecord>,
308 .columnName = FieldNameAt<FieldIndex, FirstRecord>,
312 (EnumerateRecordMembers<MoreRecords>([&]<
size_t FieldIndex,
typename FieldType>() {
314 .
tableName = RecordTableName<MoreRecords>,
315 .columnName = FieldNameAt<FieldIndex, MoreRecords>,
328 template <
typename...>
329 inline constexpr bool dependent_false =
false;
391 template <
typename Self>
392 auto All(
this Self
const& self) -> ComposedQuery
395 static_assert(detail::dependent_false<Self>,
396 "SELECT requires a projection. Call .Field(...) or .Fields(...) "
397 "before .All() — an empty projection produces malformed "
398 "`SELECT FROM \"T\"` SQL.");
403 template <
typename Self>
404 auto First(
this Self
const& self,
size_t count = 1) -> ComposedQuery
408 static_assert(detail::dependent_false<Self>,
409 "SELECT requires a projection. Call .Field(...) or .Fields(...) "
415 template <
typename Self>
416 auto Range(
this Self
const& self, std::size_t offset, std::size_t limit) -> ComposedQuery
421 static_assert(detail::dependent_false<Self>,
422 "SELECT requires a projection. Call .Field(...) or .Fields(...) "
430 template <
typename Self>
431 LIGHTWEIGHT_FORCE_INLINE
auto&&
Distinct(
this Self&& self)
noexcept
433 self.SqlSelectQueryBuilder::Distinct();
434 return std::forward<Self>(self);
438 template <
typename Self>
439 LIGHTWEIGHT_FORCE_INLINE
auto&&
Varying(
this Self&& self)
noexcept
441 self.SqlSelectQueryBuilder::Varying();
442 return std::forward<Self>(self);
Query builder for building SELECT ... queries.
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 const & Field(SqlQualifiedTableColumnName const &fieldName) const
Adds a single column to the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder & Field(SqlQualifiedTableColumnName const &fieldName)
Adds a single column to the SELECT clause.
LIGHTWEIGHT_API ComposedQuery First(size_t count=1) const
Finalizes building the query as SELECT TOP n field names FROM ... query.
LIGHTWEIGHT_API SqlSelectQueryBuilder const & Fields(std::vector< std::string_view > const &fieldNames) const
Adds a sequence of columns to the SELECT clause.
SqlSelectQueryBuilder(SqlQueryFormatter const &formatter, std::string table, std::string tableAlias) noexcept
Constructs a SELECT query builder.
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 SqlSelectQueryBuilder const & Fields(std::initializer_list< std::string_view > const &fieldNames, std::string_view tableName) const
Adds a sequence of columns from the given table to the SELECT clause.
SqlSelectQueryBuilder & Build(Callable const &callable)
Builds the query using a callable.
LIGHTWEIGHT_API SqlSelectQueryBuilder & Fields(std::initializer_list< SqlQualifiedTableColumnName const > fieldNames)
Adds a sequence of qualified table column names 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() const
Finalizes building the query as SELECT field names FROM ... query.
LIGHTWEIGHT_API SqlSelectQueryBuilder const & Field(std::string_view const &fieldName) const
Adds a single column to the SELECT clause.
LIGHTWEIGHT_API ComposedQuery All()
Finalizes building the query as SELECT field names FROM ... query.
LIGHTWEIGHT_API SqlSelectQueryBuilder const & As(std::string_view alias) const
Aliases the last added field (a column or an aggregate call) in the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder & Field(SqlFieldExpression const &fieldExpression)
Adds an aggregate function call to the SELECT clause.
LIGHTWEIGHT_API ComposedQuery Range(std::size_t offset, std::size_t limit) const
Finalizes building the query as SELECT field names FROM ... query with a range.
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 SqlSelectQueryBuilder const & Fields(std::vector< std::string_view > const &fieldNames, std::string_view tableName) const
Adds a sequence of columns from the given table to the SELECT clause.
LIGHTWEIGHT_FORCE_INLINE SqlQueryFormatter const & Formatter() const noexcept
Returns the SQL query formatter.
LIGHTWEIGHT_API SqlSelectQueryBuilder const & Field(SqlFieldExpression const &fieldExpression) const
Adds an aggregate function call to the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder const & Fields(std::initializer_list< SqlQualifiedTableColumnName const > fieldNames) const
Adds a sequence of qualified table column names to the SELECT clause.
LIGHTWEIGHT_API ComposedQuery Count()
Finalizes building the query as SELECT COUNT(*) ... query.
LIGHTWEIGHT_FORCE_INLINE SqlSearchCondition & SearchCondition() noexcept
Returns the search condition for the query.
LIGHTWEIGHT_API SqlSelectQueryBuilder & Fields(std::span< SqlQualifiedTableColumnName const > fieldNames)
Adds a sequence of qualified table column names to the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder & As(std::string_view alias)
Aliases the last added field (a column or an aggregate call) in the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder const & Fields(std::span< SqlQualifiedTableColumnName const > fieldNames) const
Adds a sequence of qualified table column names to the SELECT clause.
constexpr LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder & Varying() noexcept
Sets the builder mode to Varying, allowing varying final query types.
LIGHTWEIGHT_API ComposedQuery Count() const
Finalizes building the query as SELECT COUNT(*) ... query.
LIGHTWEIGHT_API SqlSelectQueryBuilder & Field(std::string_view const &fieldName)
Adds a single column to the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder & Fields(std::initializer_list< std::string_view > const &fieldNames, std::string_view tableName)
Adds a sequence of columns from the given table 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.
Compile-time gate for SELECT query construction.
SqlSelectQueryStarter(SqlQueryFormatter const &formatter, std::string table, std::string tableAlias) noexcept
Constructs a SELECT query starter.
auto All(this Self const &self) -> ComposedQuery
Empty-projection guard for the All finalizer.
LIGHTWEIGHT_FORCE_INLINE auto && Distinct(this Self &&self) noexcept
State-preserving override: returns Self&&, so the starter identity survives the call and the All / Fi...
LIGHTWEIGHT_FORCE_INLINE auto && Varying(this Self &&self) noexcept
State-preserving override; see Distinct.
auto Range(this Self const &self, std::size_t offset, std::size_t limit) -> ComposedQuery
Empty-projection guard for the Range finalizer — see All.
auto First(this Self const &self, size_t count=1) -> ComposedQuery
Empty-projection guard for the First finalizer — see All.
Represents a single column in a table.
SqlQualifiedTableColumnName represents a column name qualified with a table name.
std::string_view tableName
The table name.