pg_value
PostgreSQL values, along with their encoders and decoders. Can be used by PostgreSQL client libraries written in gleam. Currently used by pgl.
Types
Offset represents a UTC offset. Timestamptz is composed
of a gleam/time/timestamp.Timestamp and Offset. The offset will be
applied to the timestamp when being encoded.
A timestamp with a positive offset represents some time in the future, relative to UTC. A timestamp with a negative offset represents some time in the past, relative to UTC.
Offsets will be subtracted from the gleam/time/timestamp.Timestamp
so the encoded value is a UTC timestamp.
pub type Offset {
Offset(hours: Int, minutes: Int)
}
Constructors
-
Offset(hours: Int, minutes: Int)
The Value type represents PostgreSQL data types. Values can be encoded
to PostgreSQL’s binary format. Values can be used when interacting with
PostgreSQL databases through client libraries like pgl.
pub type Value {
Null
Bool(Bool)
Int(Int)
Float(Float)
Text(String)
Bytea(BitArray)
Time(calendar.TimeOfDay)
Date(calendar.Date)
Timestamp(timestamp.Timestamp)
Timestamptz(timestamp.Timestamp, Offset)
Interval(interval.Interval)
Array(List(Value))
}
Constructors
-
Null -
Bool(Bool) -
Int(Int) -
Float(Float) -
Text(String) -
Bytea(BitArray) -
Time(calendar.TimeOfDay) -
Date(calendar.Date) -
Timestamp(timestamp.Timestamp) -
Timestamptz(timestamp.Timestamp, Offset) -
Interval(interval.Interval) -
Array(List(Value))
Values
pub fn date(date: calendar.Date) -> Value
pub fn decode(
bits: BitArray,
info: type_info.TypeInfo,
) -> Result(dynamic.Dynamic, String)
Decodes binary PostgreSQL data into a Dynamic value. Dynamic values can then be decoded using gleam/dynamic/decode.
pub fn encode(
value: Value,
info: type_info.TypeInfo,
) -> Result(BitArray, String)
Encodes a Value as a PostgreSQL data type
pub fn interval(interval: interval.Interval) -> Value
pub fn nullable(
inner_type: fn(a) -> Value,
optional: option.Option(a),
) -> Value
Checks if the provided value is option.Some or option.None. If
None then the value returned is value.Null. If Some value is
provided then it is passed to the inner_type function.
Example:
let int = pg_value.nullable(pg_value.int, Some(10))
let null = pg_value.nullable(pg_value.int, None)
pub fn offset(hours: Int) -> Offset
Returns an Offset with the provided hours and 0 minutes.
pub fn time(time_of_day: calendar.TimeOfDay) -> Value
pub fn timestamp(timestamp: timestamp.Timestamp) -> Value
pub fn timestamptz(
timestamp: timestamp.Timestamp,
offset: Offset,
) -> Value
pub fn to_string(value: Value) -> String
Converts a Value to a string formatted properly for PostgreSQL