Lightweight 0.20260921.0
Loading...
Searching...
No Matches
Lightweight::SqlRowIterator< T > Class Template Reference

SQL query result row iterator. More...

#include <SqlStatement.hpp>

Public Types

using QueryCustomizer = std::function< void(SqlSelectQueryBuilder &)>
 

Public Member Functions

 SqlRowIterator (SqlConnection &conn)
 Constructs a row iterator over all rows of the record's table, using the given SQL connection.
 
 SqlRowIterator (SqlConnection &conn, QueryCustomizer queryCustomizer)
 
iterator begin ()
 Returns an iterator to the first row of the result set.
 
iterator end () noexcept
 Returns a sentinel iterator representing the end of the result set.
 

Detailed Description

template<typename T>
class Lightweight::SqlRowIterator< T >

SQL query result row iterator.

Can be used to iterate over rows of the database and fetch them into a record type.

Template Parameters
TThe record type to fetch the rows into.
struct MyRecord
{
Field<int> field2;
Field<double> field3;
};
for (auto const& row : SqlRowIterator<MyRecord>(conn))
{
// row is of type MyRecord
// row.field1, row.field2, row.field3 are accessible
}
SQL query result row iterator.
Represents a single column in a table.
Definition Field.hpp:84

Pass a second argument to iterate over a subset of the table only. The callable receives the underlying SqlSelectQueryBuilder with the projection for T already applied, so the full WHERE / ORDER BY / LIMIT surface of the query builder is available:

for (auto const& row : SqlRowIterator<MyRecord>(conn, [](auto& query) {
return query.Where("field2", 10).OrWhere([](auto& query) {
return query.Where("field2", 20).Where("field3", 3.14);
});
}))
{
// only the rows matching the condition above are fetched
}

Definition at line 1086 of file SqlStatement.hpp.

Member Typedef Documentation

◆ QueryCustomizer

template<typename T >
using Lightweight::SqlRowIterator< T >::QueryCustomizer = std::function<void(SqlSelectQueryBuilder&)>

Callable refining the SELECT query before it is executed.

It is invoked with the query builder that already carries the projection for T. Any value the callable returns is ignored, so the builder's chaining methods can be returned directly.

Definition at line 1093 of file SqlStatement.hpp.

Constructor & Destructor Documentation

◆ SqlRowIterator() [1/2]

template<typename T >
Lightweight::SqlRowIterator< T >::SqlRowIterator ( SqlConnection &  conn)
inlineexplicit

Constructs a row iterator over all rows of the record's table, using the given SQL connection.

Definition at line 1096 of file SqlStatement.hpp.

◆ SqlRowIterator() [2/2]

template<typename T >
Lightweight::SqlRowIterator< T >::SqlRowIterator ( SqlConnection &  conn,
QueryCustomizer  queryCustomizer 
)
inline

Constructs a row iterator over the subset of rows selected by queryCustomizer.

Parameters
connThe SQL connection to run the query on.
queryCustomizerCallable refining the SELECT query, e.g. by adding WHERE conditions.

Definition at line 1105 of file SqlStatement.hpp.

Member Function Documentation

◆ begin()

template<typename T >
iterator Lightweight::SqlRowIterator< T >::begin ( )
inline

Returns an iterator to the first row of the result set.

Definition at line 1185 of file SqlStatement.hpp.

◆ end()

template<typename T >
iterator Lightweight::SqlRowIterator< T >::end ( )
inlinenoexcept

Returns a sentinel iterator representing the end of the result set.

Definition at line 1196 of file SqlStatement.hpp.


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