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.

src/result.qz:66

Class methods

def self.new(columns: Array[PG::Field], values: Array[Bytes?]) -> PG::Row

A row of values, one per column of columns.

src/result.qz:73

def self.parse_int(text: String) -> Int

Text as an Int (-42).

src/result.qz:196

def self.parse_float(text: String) -> Float

Text as a Float, with PostgreSQL's spellings of the special values.

src/result.qz:204

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"{}")                     # => []

src/result.qz:225

def self.parse_bytea(raw: Bytes) -> Bytes

bytea's hex output (\x0aff) as Bytes.

PG::Row.parse_bytea(b"\\x0aff")   # => b"\n\xff"

src/result.qz:277

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

src/result.qz:294

Methods

def columns -> Array[PG::Field]

The columns.

src/result.qz:76

def size -> Int

The number of columns.

src/result.qz:79

def [](column: Int | String) -> String?

The column's text, or nil for NULL. Raises IndexError for a column the row does not have.

src/result.qz:83

def int(column: Int | String) -> Int

The column's value as an Int.

src/result.qz:88

def int?(column: Int | String) -> Int?

As #int, nil for NULL.

src/result.qz:91

def float(column: Int | String) -> Float

The column's value as a Float.

src/result.qz:96

def float?(column: Int | String) -> Float?

As #float, nil for NULL.

src/result.qz:99

def string(column: Int | String) -> String

The column's value as text.

src/result.qz:104

def string?(column: Int | String) -> String?

As #string, nil for NULL.

src/result.qz:107

def bool(column: Int | String) -> Bool

The column's value as a Bool.

src/result.qz:110

def bool?(column: Int | String) -> Bool?

As #bool, nil for NULL.

src/result.qz:116

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.

src/result.qz:127

def bytes?(column: Int | String) -> Bytes?

As #bytes, nil for NULL.

src/result.qz:130

def time(column: Int | String) -> Time

The column's value as a Time.

src/result.qz:137

def time?(column: Int | String) -> Time?

As #time, nil for NULL.

src/result.qz:140

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.

src/result.qz:147

def ints?(column: Int | String) -> Array[Int?]?

As #ints, nil for a NULL array.

src/result.qz:150

def floats(column: Int | String) -> Array[Float?]

The column's array as Floats.

src/result.qz:153

def floats?(column: Int | String) -> Array[Float?]?

As #floats, nil for a NULL array.

src/result.qz:156

def strings(column: Int | String) -> Array[String?]

The column's array as text.

src/result.qz:159

def strings?(column: Int | String) -> Array[String?]?

As #strings, nil for a NULL array.

src/result.qz:162

def bools(column: Int | String) -> Array[Bool?]

The column's array as Bools.

src/result.qz:165

def bools?(column: Int | String) -> Array[Bool?]?

As #bools, nil for a NULL array.

src/result.qz:168

def times(column: Int | String) -> Array[Time?]

The column's array as Times.

src/result.qz:179

def times?(column: Int | String) -> Array[Time?]?

As #times, nil for a NULL array.

src/result.qz:182

def to_h -> Hash[String, String?]

The row as a hash from column names to text values.

src/result.qz:185

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

src/result.qz:191

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

src/result.qz:193