accounts accounts=accounts::create(conn); accounts->search("balance", ">", 0); accounts->limit(10); for (const auto &row: *all_accounts) { // ... }
limit
() limits the number of rows that the
resultsets iterate over, the upper limit on the resultset's size.
The default value of 0 specifies no limit on the resultset's size.
The above example will iterate over at most ten rows. If the
executed SELECT
query would return more rows, the
additional rows get ignored.
This is done using the underlying database driver's most efficient implementation (which, sadly, is buggy sometimes).
int64_t account_id; // ... accounts accounts=accounts::create(conn); accounts->search("account_id", "=", account_id); accounts::base::rowptr maybe_row=accounts->maybe(); accounts::base::row must_row=accounts->only();
It is common to expect a SELECT
to return at most
one row, or maybe no rows at all. This is the
expected result when using
constraints on the table's primary key.
maybe
() and
only
() methods are shortcuts that implement
the following steps:
They call begin
(2) and
end
() to iterate over the resultset's
input sequence. An overloaded begin
()
takes an optional parameter that sets the maximum number of rows,
as if by limit
() for that input sequence
only. begin
() without a parameter defaults
to using the limit set by limit
().
If the input sequence contains no rows,
maybe
() returns a null
,
and class
::base::rowptronly
() throws an exception.
If the input sequence contains one row,
maybe
() returns a
non-null
(which is an class
::base::rowptrx::ptr
)
and only
() returns a
(which is an class
::base::rowx::ref
).
Both only
() and
maybe
() throw an exception
when the input sequence has two rows (using
begin
(2) to set the maximum number of
returned rows to two, it doesn't matter how many actual rows the
query would return, all that matters is that there's more than
one).