async_postgres/pg_types/copy_rows

The field-to-column plan and the binary COPY encoders behind copyInRows.

Binary COPY carries no type, so a wrong payload is read as another value (an int8 as a float8): only the type pairs copyConv allows are written.

Internal module: not part of the public API.

Types

CopyConv = enum
  ccPayload,                ## the value's own encoder, its OID checked on every value
  ccInt2,                   ## an integer, or each element of an integer array, as int2
  ccInt4, ccInt8, ccFloat4, ## a float rounded to float4, as PostgreSQL's float8 cast does
  ccFloat8, ccNumeric,      ## an integer or a decimal string as numeric
  ccText,                   ## a string's bytes, which the text types, json and xml read as is
  ccJsonb,                  ## JSON text behind jsonb's version byte
  ccUuid,                   ## a string parsed as a uuid
  ccTimestamp,              ## a DateTime, or each element of an array of them
  ccTimestampTz, ccDate, ccLabel ## an enum's label
How a field's value reaches its column's binary form, fixed per column before the COPY starts.
CopyRowsPlan = object
  columns*: seq[string]      ## Names as the server described them.
  oids*: seq[int32]
  convs*: seq[CopyConv]
  writes*: seq[bool]         ## Per field in declaration order: whether it is written.
  enumChecks*: seq[tuple[oid: int32, err: string]] ## User types that labels go to, with the error raised unless each is an
                                                   ## enum: another type's receive function may misread a label.
The columns a row type writes, in COPY order.

Procs

proc checkEnumColumns(plan: CopyRowsPlan; enums: openArray[int32]) {.
    ...raises: [PgTypeError], tags: [], forbids: [].}
Raise PgTypeError unless each type of plan.enumChecks is among enums.
proc copyConv[F](_: typedesc[F]; oid: int32): CopyConv
The conversion writing an F to a column of type oid. Raises PgTypeError for a pair none covers.
proc encodeCopyRow[T](buf: var seq[byte]; row: T; plan: CopyRowsPlan)
Append row as one binary COPY tuple.
proc encodeCopyRows[T](buf: var seq[byte]; rows: openArray[T]; next: var int;
                       plan: CopyRowsPlan; limit: int; base = 0)
Append rows from next on until buf holds limit bytes, advancing next. Errors name the row as base + next.
proc encodeCopyValue[F](buf: var seq[byte]; v: F; conv: CopyConv; oid: int32)
Append v as one binary COPY field (length and payload) of a column of type oid, by the conv that copyConv gave for the pair.
proc planCopyRows[T](fields: seq[FieldDescription]; columnsGiven: bool): CopyRowsPlan
The plan writing T rows to the columns fields describes, by to's name rules or, for an anonymous tuple, by position.