class PG::Row
A row of a result: each column's value as the server's text, and typed readers on top. A column is named by its index from 0 or by its name (the first column of that name).
The readers decode PostgreSQL's text format: int for int2/int4/int8
(and any integer text), float for float4/float8/numeric
(NaN, Infinity), bool for t/f, bytes for bytea's hex form,
time for timestamp, timestamptz and date (a timestamp without zone,
and a date at midnight, are taken as UTC), string for anything. Each
raises PG::NullError on NULL; its ? form gives nil instead. A value
a reader cannot decode raises ArgumentError.
Class methods
def self.new(columns: Array[PG::Field], values: Array[Bytes?]) -> PG::Row
A row of values, one per column of columns.
def self.parse_int(text: String) -> Int
Text as an Int (-42).
def self.parse_float(text: String) -> Float
Text as a Float, with PostgreSQL's spellings of the special values.
def self.parse_array(raw: Bytes) -> Array[String?]
An array's text output ({1,NULL,"a b"}) as its elements' text, nil
for NULL: quoted elements unescaped. Elements are separated by ,,
as for every built-in type but box (whose arrays use ;). Raises ArgumentError for an
array of more than one dimension or with explicit bounds
([0:1]={1,2}).
PG::Row.parse_array(b"{1,NULL,\"a,\\\"b\"}") # => ["1", nil, "a,\"b"]
PG::Row.parse_array(b"{}") # => []
def self.parse_bytea(raw: Bytes) -> Bytes
bytea's hex output (\x0aff) as Bytes.
PG::Row.parse_bytea(b"\\x0aff") # => b"\n\xff"
def self.parse_time(text: String) -> Time
A timestamp, timestamptz or date in PostgreSQL's ISO output
(2024-03-05 14:07:09.5+09, 2024-03-05): a value without an offset
is UTC.
PG::Row.parse_time("2024-03-05 14:07:09+09").utc_offset # => 32400
PG::Row.parse_time("2024-03-05") # => 2024-03-05 00:00:00 UTC
Methods
def columns -> Array[PG::Field]
The columns.
def size -> Int
The number of columns.
def [](column: Int | String) -> String?
The column's text, or nil for NULL. Raises IndexError for a column
the row does not have.
def int(column: Int | String) -> Int
The column's value as an Int.
def int?(column: Int | String) -> Int?
As #int, nil for NULL.
def float(column: Int | String) -> Float
The column's value as a Float.
def float?(column: Int | String) -> Float?
As #float, nil for NULL.
def string(column: Int | String) -> String
The column's value as text.
def string?(column: Int | String) -> String?
As #string, nil for NULL.
def bool(column: Int | String) -> Bool
The column's value as a Bool.
def bool?(column: Int | String) -> Bool?
As #bool, nil for NULL.
def bytes(column: Int | String) -> Bytes
The column's value as Bytes: bytea's hex form decoded, or the text's bytes for another type.
def bytes?(column: Int | String) -> Bytes?
As #bytes, nil for NULL.
def time(column: Int | String) -> Time
The column's value as a Time.
def time?(column: Int | String) -> Time?
As #time, nil for NULL.
def ints(column: Int | String) -> Array[Int?]
The column's array as Ints (a NULL element as nil). Raises
ArgumentError for an array of more than one dimension, as the
readers do for text that is not their type.
def ints?(column: Int | String) -> Array[Int?]?
As #ints, nil for a NULL array.
def floats(column: Int | String) -> Array[Float?]
The column's array as Floats.
def floats?(column: Int | String) -> Array[Float?]?
As #floats, nil for a NULL array.
def strings(column: Int | String) -> Array[String?]
The column's array as text.
def strings?(column: Int | String) -> Array[String?]?
As #strings, nil for a NULL array.
def bools(column: Int | String) -> Array[Bool?]
The column's array as Bools.
def bools?(column: Int | String) -> Array[Bool?]?
As #bools, nil for a NULL array.
def times(column: Int | String) -> Array[Time?]
The column's array as Times.
def times?(column: Int | String) -> Array[Time?]?
As #times, nil for a NULL array.
def to_h -> Hash[String, String?]
The row as a hash from column names to text values.
def inspect -> String
A developer-readable representation, as p prints it: strings quoted,
collections with their elements' inspect, an object with its fields
(#<Point @x=1, @y=2>).
from Object
def to_s -> String
The value as a string, for puts and interpolation. For an instance of
a class, the class name in the form #<Point>; for an exception, its
message; for a data value or an enum variant, the default inspect.
It does not call an inspect the class defines.
from Object