Postgres module

copy_out

procedure · 4 overloadssince 0.27.0Source

Runs a COPY <table or (query)> TO STDOUT statement and calls proc with each piece of its data, as it arrives, so a table of any size streams through. Returns how many rows the server sent. Any format works: text, CSV or binary.

Example
count, error := Pg.copy_out(db, "copy (select email from users) to stdout (format csv)", write_chunk, file);

Not run: the example is marked ignore.

Not run: it needs a PostgreSQL server.

copy_out :: (
    c: *Connection,
    sql: string,
    proc: Copy_Out_Proc,
    user_data: *void = null
) -> (s64, Pg_Error)

Runs a COPY <table or (query)> TO STDOUT statement and calls proc with each piece of its data, as it arrives, so a table of any size streams through. Returns how many rows the server sent. Any format works: text, CSV or binary.

Not run: it needs a PostgreSQL server.

Parameters

c: *Connection
the connection to run on.
sql: string
the copy ... to stdout statement, in any format.
proc: Copy_Out_Proc
called with each piece of data as it arrives.
user_data: *void
handed to proc as it is.

Returns

s64
Example
count, error := Pg.copy_out(db, "copy (select email from users) to stdout (format csv)", write_chunk, file);

Not run: the example is marked ignore.

copy_out :: (
    pool: *Pool,
    sql: string,
    proc: Copy_Out_Proc,
    user_data: *void = null
) -> (s64, Pg_Error)

copy_out to a callback, on a connection from the pool.

Parameters

pool: *Pool
the pool to take a connection from. It is returned afterwards.
sql: string
the copy ... to stdout statement, in any format.
proc: Copy_Out_Proc
called with each piece of data as it arrives.
user_data: *void
handed to proc as it is.

Returns

s64
copy_out :: (
    c: *Connection,
    sql: string,
    $Row: Type,
    columns: [] string = .[]
) -> (Copy_Rows(Row), Pg_Error)

Runs a copy <table or (query)> to stdout (format binary) statement and decodes the rows into structs: the inverse of copy_in. Each row has the members named in columns, in the statement's column order, or every member when columns is empty. A member needs the type copy_in would send for the column (an s32 for int4, a Nullable for a column that can be NULL, @pg_json for json). The whole result is read into memory.

Not run: it needs a PostgreSQL server.

Parameters

c: *Connection
the connection to run on.
sql: string
the copy ... to stdout (format binary) statement.
$Row: Type
the struct each row is decoded into.
columns: [] string
the members to fill, in the statement's column order. Empty fills every member.

Returns

Example
copied, error := Pg.copy_out(db, "copy users (email, score) to stdout (format binary)", User_Row, .["email", "score"]);
for copied.rows print("% %\n", it.email, it.score);
Pg.free_copy_rows(*copied);

Not run: the example is marked ignore.

copy_out :: (
    pool: *Pool,
    sql: string,
    $Row: Type,
    columns: [] string = .[]
) -> (Copy_Rows(Row), Pg_Error)

copy_out into structs, on a connection from the pool.

Parameters

pool: *Pool
the pool to take a connection from. It is returned afterwards.
sql: string
the copy ... to stdout (format binary) statement.
$Row: Type
the struct each row is decoded into.
columns: [] string
the members to fill, in the statement's column order. Empty fills every member.

Returns

See also