Lightweight 0.20260921.0
Loading...
Searching...
No Matches
Lightweight::SqlSelectQueryBuilder Class Reference

Query builder for building SELECT ... queries. More...

#include <Select.hpp>

Inheritance diagram for Lightweight::SqlSelectQueryBuilder:
Lightweight::SqlSelectQueryStarter

Public Types

using SelectType = detail::SelectType
 The select query type alias.
 

Public Member Functions

 SqlSelectQueryBuilder (SqlQueryFormatter const &formatter, std::string table, std::string tableAlias, std::vector< SqlVariant > *inputBindings=nullptr) noexcept
 
template<typename... MoreFields>
SqlSelectQueryBuilder & Fields (std::string_view const &firstField, MoreFields &&... moreFields)
 Adds a sequence of columns to the SELECT clause.
 
LIGHTWEIGHT_API SqlSelectQueryBuilder & Field (std::string_view const &fieldName)
 Adds a single column 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 & Field (SqlQualifiedTableColumnName const &fieldName)
 Adds a single column 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 (SqlFieldExpression const &fieldExpression)
 Adds an aggregate function call to the SELECT clause.
 
LIGHTWEIGHT_API SqlSelectQueryBuilder const & Field (SqlFieldExpression const &fieldExpression) const
 Adds an aggregate function call 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 & As (std::string_view alias) const
 Aliases the last added field (a column or an aggregate call) in 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::vector< std::string_view > const &fieldNames) const
 Adds a sequence of columns 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 & 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_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 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.
 
LIGHTWEIGHT_API SqlSelectQueryBuilder & Fields (std::span< SqlQualifiedTableColumnName const > fieldNames)
 Adds a sequence of qualified table column names to 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 & Fields (std::initializer_list< SqlQualifiedTableColumnName const > fieldNames)
 Adds a sequence of qualified table column names 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.
 
template<typename FirstRecord , typename... MoreRecords>
SqlSelectQueryBuilder & Fields ()
 Adds a sequence of columns from the given tables to the SELECT clause.
 
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 & FieldAs (SqlQualifiedTableColumnName const &fieldName, std::string_view const &alias)
 Adds a single column with an alias to the SELECT clause.
 
template<typename Callable >
SqlSelectQueryBuilder & Build (Callable const &callable)
 Builds the query using a callable.
 
LIGHTWEIGHT_API ComposedQuery Count ()
 
LIGHTWEIGHT_API ComposedQuery Count () const
 
LIGHTWEIGHT_API ComposedQuery All ()
 Finalizes building the query as SELECT field names FROM ... query.
 
LIGHTWEIGHT_API ComposedQuery All () const
 Finalizes building the query as SELECT field names FROM ... query.
 
LIGHTWEIGHT_API ComposedQuery First (size_t count=1)
 Finalizes building the query as SELECT TOP n field names FROM ... query.
 
LIGHTWEIGHT_API ComposedQuery First (size_t count=1) const
 Finalizes building the query as SELECT TOP n field names FROM ... query.
 
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.
 
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_FORCE_INLINE SqlSearchCondition & SearchCondition () noexcept
 Returns the search condition for the query.
 
LIGHTWEIGHT_FORCE_INLINE SqlQueryFormatter const & Formatter () const noexcept
 Returns the SQL query formatter.
 
template<typename Callable >
LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder & Build (Callable const &callable)
 Builds the query using a callable.
 
template<typename FirstRecord , typename... MoreRecords>
LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder & Fields ()
 Adds fields from one or more record types to the SELECT clause.
 

Detailed Description

Query builder for building SELECT ... queries.

Not constructed directly by user code; obtained by adding a projection (Field / Fields / FieldAs / Build) to a SqlSelectQueryStarter — the type returned by SqlQueryBuilder::Select(). The starter intentionally hides the finalizers All, First, and Range so that an empty Select().All() (which would emit malformed SELECT FROM "T" SQL) fails at compile time; the gate is in the starter, not here.

See also
SqlQueryBuilder
SqlSelectQueryStarter

Definition at line 93 of file Select.hpp.

Member Typedef Documentation

◆ SelectType

using Lightweight::SqlSelectQueryBuilder::SelectType = detail::SelectType

The select query type alias.

Definition at line 97 of file Select.hpp.

Constructor & Destructor Documentation

◆ SqlSelectQueryBuilder()

Lightweight::SqlSelectQueryBuilder::SqlSelectQueryBuilder ( SqlQueryFormatter const &  formatter,
std::string  table,
std::string  tableAlias,
std::vector< SqlVariant > *  inputBindings = nullptr 
)
inlineexplicitnoexcept

Constructs a SELECT query builder.

Parameters
formatterDialect the query is written in.
tableTable to select from.
tableAliasAlias for that table, empty for none.
inputBindingsReceives the WHERE values as bound parameters instead of having them written into the query text; null keeps them inline. Values are appended, never cleared. Execute the result with SqlStatement::ExecuteWithVariants(), not ExecuteDirect(), which would leave the markers unbound. Both the vector and anything a stored std::string_view / std::u16string_view points at must outlive the execution, because the binder hands the driver that pointer directly.
Note
An explicit SqlWildcard emits its marker without recording a value here, and a WhereIn() set larger than SqlMaxBoundSetSize falls back to inline literals; either makes the marker count differ from the vector size. A finalizer (All(), First(), Count(), Range()) moves the query out of the builder, so keep building only until the first one: a Where() issued afterwards still appends here while its marker is gone.

Definition at line 116 of file Select.hpp.

Member Function Documentation

◆ Fields() [1/7]

template<typename... MoreFields>
SqlSelectQueryBuilder & Lightweight::SqlSelectQueryBuilder::Fields ( std::string_view const &  firstField,
MoreFields &&...  moreFields 
)

Adds a sequence of columns to the SELECT clause.

Definition at line 292 of file Select.hpp.

◆ Field() [1/3]

LIGHTWEIGHT_API SqlSelectQueryBuilder const & Lightweight::SqlSelectQueryBuilder::Field ( std::string_view const &  fieldName) const

Adds a single column to the SELECT clause.

Const overload: delegates via const_cast — see Count() const.

◆ Field() [2/3]

LIGHTWEIGHT_API SqlSelectQueryBuilder const & Lightweight::SqlSelectQueryBuilder::Field ( SqlQualifiedTableColumnName const &  fieldName) const

Adds a single column to the SELECT clause.

Const overload: delegates via const_cast — see Count() const.

◆ Field() [3/3]

LIGHTWEIGHT_API SqlSelectQueryBuilder const & Lightweight::SqlSelectQueryBuilder::Field ( SqlFieldExpression const &  fieldExpression) const

Adds an aggregate function call to the SELECT clause.

Const overload: delegates via const_cast — see Count() const.

◆ As()

LIGHTWEIGHT_API SqlSelectQueryBuilder const & Lightweight::SqlSelectQueryBuilder::As ( std::string_view  alias) const

Aliases the last added field (a column or an aggregate call) in the SELECT clause.

Const overload: delegates via const_cast — see Count() const.

◆ Fields() [2/7]

LIGHTWEIGHT_API SqlSelectQueryBuilder const & Lightweight::SqlSelectQueryBuilder::Fields ( std::vector< std::string_view > const &  fieldNames) const

Adds a sequence of columns to the SELECT clause.

Const overload: delegates via const_cast — see Count() const.

◆ Fields() [3/7]

LIGHTWEIGHT_API SqlSelectQueryBuilder const & Lightweight::SqlSelectQueryBuilder::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.

Const overload: delegates via const_cast — see Count() const.

◆ Fields() [4/7]

LIGHTWEIGHT_API SqlSelectQueryBuilder const & Lightweight::SqlSelectQueryBuilder::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.

Const overload: delegates via const_cast — see Count() const.

◆ Fields() [5/7]

LIGHTWEIGHT_API SqlSelectQueryBuilder const & Lightweight::SqlSelectQueryBuilder::Fields ( std::span< SqlQualifiedTableColumnName const >  fieldNames) const

Adds a sequence of qualified table column names to the SELECT clause.

Const overload: delegates via const_cast — see Count() const.

◆ Fields() [6/7]

LIGHTWEIGHT_API SqlSelectQueryBuilder const & Lightweight::SqlSelectQueryBuilder::Fields ( std::initializer_list< SqlQualifiedTableColumnName const >  fieldNames) const

Adds a sequence of qualified table column names to the SELECT clause.

Const overload: delegates via const_cast — see Count() const.

◆ Count() [1/2]

LIGHTWEIGHT_API ComposedQuery Lightweight::SqlSelectQueryBuilder::Count ( )

Finalizes building the query as SELECT COUNT(*) ... query.

A preceding GroupBy is honored: the query then counts the rows of each group and yields one row per group, rather than a single total over the whole result set.

◆ Count() [2/2]

LIGHTWEIGHT_API ComposedQuery Lightweight::SqlSelectQueryBuilder::Count ( ) const

Finalizes building the query as SELECT COUNT(*) ... query.

A preceding GroupBy is honored: the query then counts the rows of each group and yields one row per group, rather than a single total over the whole result set. Const overload: doesn't mutate *this — returns a snapshot ComposedQuery so the finalizer can be reached through a const SqlSelectQueryBuilder.

◆ All()

LIGHTWEIGHT_API ComposedQuery Lightweight::SqlSelectQueryBuilder::All ( ) const

Finalizes building the query as SELECT field names FROM ... query.

Const overload: doesn't mutate *this — see Count() const.

◆ First()

LIGHTWEIGHT_API ComposedQuery Lightweight::SqlSelectQueryBuilder::First ( size_t  count = 1) const

Finalizes building the query as SELECT TOP n field names FROM ... query.

Const overload: doesn't mutate *this — see Count() const.

◆ Range()

LIGHTWEIGHT_API ComposedQuery Lightweight::SqlSelectQueryBuilder::Range ( std::size_t  offset,
std::size_t  limit 
) const

Finalizes building the query as SELECT field names FROM ... query with a range.

Const overload: doesn't mutate *this — see Count() const.

◆ SearchCondition()

LIGHTWEIGHT_FORCE_INLINE SqlSearchCondition & Lightweight::SqlSelectQueryBuilder::SearchCondition ( )
inlinenoexcept

Returns the search condition for the query.

Definition at line 254 of file Select.hpp.

◆ Formatter()

LIGHTWEIGHT_FORCE_INLINE SqlQueryFormatter const & Lightweight::SqlSelectQueryBuilder::Formatter ( ) const
inlinenoexcept

Returns the SQL query formatter.

Definition at line 262 of file Select.hpp.

◆ Build()

template<typename Callable >
LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder & Lightweight::SqlSelectQueryBuilder::Build ( Callable const &  callable)
inline

Builds the query using a callable.

Definition at line 315 of file Select.hpp.

◆ Fields() [7/7]

template<typename FirstRecord , typename... MoreRecords>
LIGHTWEIGHT_FORCE_INLINE SqlSelectQueryBuilder & Lightweight::SqlSelectQueryBuilder::Fields ( )
inline

Adds fields from one or more record types to the SELECT clause.

Definition at line 323 of file Select.hpp.

References Lightweight::SqlQualifiedTableColumnName::tableName.


The documentation for this class was generated from the following file: