5#include "../DataMapper/Record.hpp"
6#include "../SqlQueryFormatter.hpp"
10#include <reflection-cpp/reflection.hpp>
21 inline SqlFieldExpression
Count(std::string_view field =
"*") noexcept
24 return SqlFieldExpression { .expression =
"COUNT(*)" };
25 return SqlFieldExpression { .expression = std::format(
"COUNT(\"{}\")", field) };
28 inline SqlFieldExpression
Count(SqlQualifiedTableColumnName
const& field)
noexcept
30 if (field.columnName ==
"*")
31 return SqlFieldExpression { .expression = std::format(R
"(COUNT("{}".*))", field.tableName) };
32 return SqlFieldExpression { .expression = std::format(R
"(COUNT("{}"."{}"))", field.tableName, field.columnName) };
35 inline SqlFieldExpression Sum(std::string_view field)
noexcept
37 return SqlFieldExpression { .
expression = std::format(
"SUM(\"{}\")", field) };
40 inline SqlFieldExpression Sum(SqlQualifiedTableColumnName
const& field)
noexcept
42 return SqlFieldExpression { .
expression = std::format(R
"(SUM("{}"."{}"))", field.tableName, field.columnName) };
45 inline SqlFieldExpression Avg(std::string_view field)
noexcept
47 return SqlFieldExpression { .
expression = std::format(
"AVG(\"{}\")", field) };
50 inline SqlFieldExpression Avg(SqlQualifiedTableColumnName
const& field)
noexcept
52 return SqlFieldExpression { .
expression = std::format(R
"(AVG("{}"."{}"))", field.tableName, field.columnName) };
55 inline SqlFieldExpression Min(std::string_view field)
noexcept
57 return SqlFieldExpression { .
expression = std::format(
"MIN(\"{}\")", field) };
60 inline SqlFieldExpression Min(SqlQualifiedTableColumnName
const& field)
noexcept
62 return SqlFieldExpression { .
expression = std::format(R
"(MIN("{}"."{}"))", field.tableName, field.columnName) };
65 inline SqlFieldExpression Max(std::string_view field)
noexcept
67 return SqlFieldExpression { .
expression = std::format(
"MAX(\"{}\")", field) };
70 inline SqlFieldExpression Max(SqlQualifiedTableColumnName
const& field)
noexcept
72 return SqlFieldExpression { .
expression = std::format(R
"(MAX("{}"."{}"))", field.tableName, field.columnName) };
92 using SelectType = detail::SelectType;
113 std::string tableAlias,
114 std::vector<SqlVariant>* inputBindings =
nullptr) noexcept:
116 _formatter { formatter }
118 _query.formatter = &formatter;
119 _query.searchCondition.tableName = std::move(table);
120 _query.searchCondition.tableAlias = std::move(tableAlias);
121 _query.searchCondition.inputBindings = inputBindings;
122 _query.fields.reserve(256);
126 template <
typename... MoreFields>
127 SqlSelectQueryBuilder& Fields(std::string_view
const& firstField, MoreFields
const&... moreFields);
166 std::string_view tableName);
171 std::string_view tableName)
const;
175 std::string_view tableName);
180 std::initializer_list<std::string_view>
const& fieldNames, std::string_view tableName)
const;
188 std::span<SqlQualifiedTableColumnName const> fieldNames)
const;
196 std::initializer_list<SqlQualifiedTableColumnName const> fieldNames)
const;
199 template <
typename FirstRecord,
typename... MoreRecords>
203 [[deprecated(
"Use Field(...).As(\"alias\") instead.")]]
207 [[deprecated(
"Use Field(...).As(\"alias\") instead.")]]
209 std::string_view
const& alias);
212 template <
typename Callable>
232 LIGHTWEIGHT_API ComposedQuery Count();
237 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery Count() const;
240 LIGHTWEIGHT_API ComposedQuery All();
244 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery All() const;
247 LIGHTWEIGHT_API ComposedQuery First(
size_t count = 1);
251 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery First(
size_t count = 1) const;
254 LIGHTWEIGHT_API ComposedQuery Range(std::
size_t offset, std::
size_t limit);
258 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery Range(std::
size_t offset, std::
size_t limit) const;
261 LIGHTWEIGHT_FORCE_INLINE SqlSearchCondition& SearchCondition() noexcept
263 return _query.searchCondition;
278 LIGHTWEIGHT_API
void RecordProjectedFieldName(std::string name)
const;
282 LIGHTWEIGHT_API
void RenameLastProjectedFieldName(std::string_view alias)
const;
285 LIGHTWEIGHT_API
void RecordProjectionWildcard()
const;
292 mutable bool _aliasAllowed =
false;
295template <
typename... MoreFields>
298 using namespace std::string_view_literals;
300 std::ostringstream fragment;
302 if (!_query.fields.empty())
305 fragment <<
'"' << firstField <<
'"';
306 RecordProjectedFieldName(std::string(firstField));
308 if constexpr (
sizeof...(MoreFields) > 0)
309 (((fragment << R
"(, ")"sv << moreFields << '"'),
310 RecordProjectedFieldName(std::string(std::string_view(moreFields)))),
313 _query.fields += fragment.str();
318template <
typename Callable>
326template <
typename FirstRecord,
typename... MoreRecords>
331 if constexpr (
sizeof...(MoreRecords) == 0)
333 EnumerateRecordMembers<FirstRecord>([&]<
size_t FieldIndex,
typename FieldType>() {
335 Field(FieldNameAt<FieldIndex, FirstRecord>);
340 EnumerateRecordMembers<FirstRecord>([&]<
size_t FieldIndex,
typename FieldType>() {
343 .
tableName = RecordTableName<FirstRecord>,
344 .columnName = FieldNameAt<FieldIndex, FirstRecord>,
348 (EnumerateRecordMembers<MoreRecords>([&]<
size_t FieldIndex,
typename FieldType>() {
351 .
tableName = RecordTableName<MoreRecords>,
352 .columnName = FieldNameAt<FieldIndex, MoreRecords>,
365 template <
typename...>
366 inline constexpr bool dependent_false =
false;
421 std::string tableAlias,
422 std::vector<SqlVariant>* inputBindings =
nullptr) noexcept:
433 template <
typename Self>
434 auto All(
this Self
const& self) -> ComposedQuery
437 static_assert(detail::dependent_false<Self>,
438 "SELECT requires a projection. Call .Field(...) or .Fields(...) "
439 "before .All() — an empty projection produces malformed "
440 "`SELECT FROM \"T\"` SQL.");
445 template <
typename Self>
446 auto First(
this Self
const& self,
size_t count = 1) -> ComposedQuery
450 static_assert(detail::dependent_false<Self>,
451 "SELECT requires a projection. Call .Field(...) or .Fields(...) "
457 template <
typename Self>
458 auto Range(
this Self
const& self, std::size_t offset, std::size_t limit) -> ComposedQuery
463 static_assert(detail::dependent_false<Self>,
464 "SELECT requires a projection. Call .Field(...) or .Fields(...) "
472 template <
typename Self>
473 LIGHTWEIGHT_FORCE_INLINE
auto&&
Distinct(
this Self&& self)
noexcept
475 self.SqlSelectQueryBuilder::Distinct();
476 return std::forward<Self>(self);
481 template <
typename Self>
484 self.SqlSelectQueryBuilder::ReadUncommitted();
485 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 SqlSelectQueryBuilder const & Fields(std::vector< std::string_view > const &fieldNames) const
Adds a sequence of columns 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 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 SqlSelectQueryBuilder const & Field(std::string_view const &fieldName) const
Adds a single column to the SELECT clause.
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 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.
SqlSelectQueryBuilder(SqlQueryFormatter const &formatter, std::string table, std::string tableAlias, std::vector< SqlVariant > *inputBindings=nullptr) noexcept
LIGHTWEIGHT_API SqlSelectQueryBuilder & Fields(std::span< SqlQualifiedTableColumnName const > fieldNames)
Adds a sequence of qualified table column names to the SELECT clause.
LIGHTWEIGHT_API SqlSelectQueryBuilder & ReadUncommitted() noexcept
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.
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.
Compile-time gate for SELECT query construction.
LIGHTWEIGHT_FORCE_INLINE auto && ReadUncommitted(this Self &&self) noexcept
State-preserving override of SqlSelectQueryBuilder::ReadUncommitted(); returns Self&& so Select()....
auto All(this Self const &self) -> ComposedQuery
Empty-projection guard for the All finalizer.
SqlSelectQueryStarter(SqlQueryFormatter const &formatter, std::string table, std::string tableAlias, std::vector< SqlVariant > *inputBindings=nullptr) noexcept
Constructs a SELECT query starter.
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...
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.
Requires that T maps onto a column of its record's table.
@ Count
Number of enumerators; not an operation itself.
Represents a single column in a table.
A SQL expression usable as a projection or as the left side of a HAVING term.
std::string expression
The SQL text of the expression.
SqlQualifiedTableColumnName represents a column name qualified with a table name.
std::string_view tableName
The table name.