Postgres module

each

procedure · 1 overloadsince 0.27.0Source

A select whose columns come from the loop that reads it: only the fields of T the body reads, plus the primary key, are fetched; the other fields of it stay zero. A json document read only for some top-level fields comes back with just those keys, and it.author.email (a to-one relation) is fetched in the same statement, making it an Orm_Item that usings a T. When it is used whole (passed to a procedure, stored, *it), every column is fetched. The query runs when the loop starts, and a failure lands in error (which is cleared first). Strings in it read from the result, which is freed when the loop ends: copy what you keep.

Example
error: Pg.Pg_Error;
for Pg.each(db, User, *error, where = #code it.score > min) print("%\n", it.email);   // select "id", "email" ...

Not run: the example is marked ignore.

Not run: it needs a PostgreSQL server.

each :: (
    db: $D,
    $T: Type,
    error: *Pg_Error,
    $where: Code = EVERY_ROW,
    $order_by: Code = NO_ORDER,
    $desc := false,
    $limit := -1,
    $offset := 0,
    $from_sql := "",
    $strategy := Orm_Strategy.PRELOAD,
    $call := #caller_code
) -> Orm_Each(D, T, where, order_by, desc, limit, offset, from_sql, strategy, call)

A select whose columns come from the loop that reads it: only the fields of T the body reads, plus the primary key, are fetched; the other fields of it stay zero. A json document read only for some top-level fields comes back with just those keys, and it.author.email (a to-one relation) is fetched in the same statement, making it an Orm_Item that usings a T. When it is used whole (passed to a procedure, stored, *it), every column is fetched. The query runs when the loop starts, and a failure lands in error (which is cleared first). Strings in it read from the result, which is freed when the loop ends: copy what you keep.

Not run: it needs a PostgreSQL server.

Parameters

db: $D
a connection or pool.
$T: Type
the table struct.
error: *Pg_Error
set to the failure, if any, once the loop starts.
$where: Code
the filter. Every row by default.
$order_by: Code
the sort key. No order by default.
$desc := false
sort descending.
$limit := -1
the most rows to read. -1 for all.
$offset := 0
how many rows to skip first.
$from_sql := ""
a query to select from instead of the table.
$strategy := Orm_Strategy.PRELOAD
how to load has-many relations the body reads.
$call := #caller_code
the code of the call, which lets the query text be checked while compiling. Leave the default.

Returns

Orm_Each(D, T, where, order_by, desc, limit, offset, from_sql, strategy, call)
Example
error: Pg.Pg_Error;
for Pg.each(db, User, *error, where = #code it.score > min) print("%\n", it.email);   // select "id", "email" ...

Not run: the example is marked ignore.

See also