5#include "../DataMapper/Record.hpp"
6#include "../SqlQueryFormatter.hpp"
10#include <reflection-cpp/reflection.hpp>
18struct [[nodiscard]] SqlFieldExpression final
20 std::string expression;
26 inline SqlFieldExpression Count(std::string_view field =
"*") noexcept
29 return SqlFieldExpression { .expression =
"COUNT(*)" };
30 return SqlFieldExpression { .expression = std::format(
"COUNT(\"{}\")", field) };
33 inline SqlFieldExpression Count(SqlQualifiedTableColumnName
const& field)
noexcept
35 if (field.columnName ==
"*")
36 return SqlFieldExpression { .expression = std::format(R
"(COUNT("{}".*))", field.tableName) };
37 return SqlFieldExpression { .expression = std::format(R
"(COUNT("{}"."{}"))", field.tableName, field.columnName) };
40 inline SqlFieldExpression Sum(std::string_view field)
noexcept
42 return SqlFieldExpression { .expression = std::format(
"SUM(\"{}\")", field) };
45 inline SqlFieldExpression Sum(SqlQualifiedTableColumnName
const& field)
noexcept
47 return SqlFieldExpression { .expression = std::format(R
"(SUM("{}"."{}"))", field.tableName, field.columnName) };
50 inline SqlFieldExpression Avg(std::string_view field)
noexcept
52 return SqlFieldExpression { .expression = std::format(
"AVG(\"{}\")", field) };
55 inline SqlFieldExpression Avg(SqlQualifiedTableColumnName
const& field)
noexcept
57 return SqlFieldExpression { .expression = std::format(R
"(AVG("{}"."{}"))", field.tableName, field.columnName) };
60 inline SqlFieldExpression Min(std::string_view field)
noexcept
62 return SqlFieldExpression { .expression = std::format(
"MIN(\"{}\")", field) };
65 inline SqlFieldExpression Min(SqlQualifiedTableColumnName
const& field)
noexcept
67 return SqlFieldExpression { .expression = std::format(R
"(MIN("{}"."{}"))", field.tableName, field.columnName) };
70 inline SqlFieldExpression Max(std::string_view field)
noexcept
72 return SqlFieldExpression { .expression = std::format(
"MAX(\"{}\")", field) };
75 inline SqlFieldExpression Max(SqlQualifiedTableColumnName
const& field)
noexcept
77 return SqlFieldExpression { .expression = std::format(R
"(MAX("{}"."{}"))", field.tableName, field.columnName) };
97 using SelectType = detail::SelectType;
101 SqlBasicSelectQueryBuilder<SqlSelectQueryBuilder> {},
102 _formatter { formatter }
104 _query.formatter = &formatter;
105 _query.searchCondition.tableName = std::move(table);
106 _query.searchCondition.tableAlias = std::move(tableAlias);
107 _query.fields.reserve(256);
111 template <
typename... MoreFields>
112 SqlSelectQueryBuilder& Fields(std::string_view
const& firstField, MoreFields&&... moreFields);
151 std::string_view tableName);
156 std::string_view tableName)
const;
160 std::string_view tableName);
165 std::initializer_list<std::string_view>
const& fieldNames, std::string_view tableName)
const;
173 std::span<SqlQualifiedTableColumnName const> fieldNames)
const;
181 std::initializer_list<SqlQualifiedTableColumnName const> fieldNames)
const;
184 template <
typename FirstRecord,
typename... MoreRecords>
188 [[deprecated(
"Use Field(...).As(\"alias\") instead.")]]
192 [[deprecated(
"Use Field(...).As(\"alias\") instead.")]]
194 std::string_view
const& alias);
197 template <
typename Callable>
201 LIGHTWEIGHT_API ComposedQuery
Count();
206 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery
Count()
const;
209 LIGHTWEIGHT_API ComposedQuery
All();
213 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery
All()
const;
216 LIGHTWEIGHT_API ComposedQuery
First(
size_t count = 1);
220 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery
First(
size_t count = 1)
const;
223 LIGHTWEIGHT_API ComposedQuery
Range(std::size_t offset, std::size_t limit);
227 [[nodiscard]] LIGHTWEIGHT_API ComposedQuery
Range(std::size_t offset, std::size_t limit)
const;
234 return _query.searchCondition;
251 mutable bool _aliasAllowed =
false;
254template <
typename... MoreFields>
257 using namespace std::string_view_literals;
259 std::ostringstream fragment;
261 if (!_query.fields.empty())
264 fragment <<
'"' << firstField <<
'"';
266 if constexpr (
sizeof...(MoreFields) > 0)
267 ((fragment << R
"(, ")"sv << std::forward<MoreFields>(moreFields) << '"') << ...);
269 _query.fields += fragment.str();
274template <
typename Callable>
282template <
typename FirstRecord,
typename... MoreRecords>
287 if constexpr (
sizeof...(MoreRecords) == 0)
289 EnumerateRecordMembers<FirstRecord>([&]<
size_t FieldIndex,
typename FieldType>() {
291 Field(FieldNameAt<FieldIndex, FirstRecord>);
296 EnumerateRecordMembers<FirstRecord>([&]<
size_t FieldIndex,
typename FieldType>() {
299 .
tableName = RecordTableName<FirstRecord>,
300 .columnName = FieldNameAt<FieldIndex, FirstRecord>,
304 (EnumerateRecordMembers<MoreRecords>([&]<
size_t FieldIndex,
typename FieldType>() {
307 .
tableName = RecordTableName<MoreRecords>,
308 .columnName = FieldNameAt<FieldIndex, MoreRecords>,
321 template <
typename...>
322 inline constexpr bool dependent_false =
false;
384 template <
typename Self>
385 auto All(
this Self
const& self) -> ComposedQuery
388 static_assert(detail::dependent_false<Self>,
389 "SELECT requires a projection. Call .Field(...) or .Fields(...) "
390 "before .All() — an empty projection produces malformed "
391 "`SELECT FROM \"T\"` SQL.");
396 template <
typename Self>
397 auto First(
this Self
const& self,
size_t count = 1) -> ComposedQuery
401 static_assert(detail::dependent_false<Self>,
402 "SELECT requires a projection. Call .Field(...) or .Fields(...) "
408 template <
typename Self>
409 auto Range(
this Self
const& self, std::size_t offset, std::size_t limit) -> ComposedQuery
414 static_assert(detail::dependent_false<Self>,
415 "SELECT requires a projection. Call .Field(...) or .Fields(...) "
423 template <
typename Self>
424 LIGHTWEIGHT_FORCE_INLINE
auto&&
Distinct(
this Self&& self)
noexcept
426 self.SqlSelectQueryBuilder::Distinct();
427 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.
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...
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.
Represents a single column in a table.
SqlQualifiedTableColumnName represents a column name qualified with a table name.
std::string_view tableName
The table name.