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
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, duration.Duration)
Interval(interval.Interval)
Array(List(Value))
Uuid(BitArray)
Hstore(dict.Dict(String, option.Option(String)))
Enum(String)
Json(json.Json)
}
Constructors
-
Null -
Bool(Bool) -
Int(Int) -
Float(Float) -
Text(String) -
Bytea(BitArray) -
Time(calendar.TimeOfDay) -
Date(calendar.Date) -
Timestamp(timestamp.Timestamp) -
Timestamptz(timestamp.Timestamp, duration.Duration) -
Interval(interval.Interval) -
Array(List(Value)) -
Uuid(BitArray) -
Hstore(dict.Dict(String, option.Option(String))) -
Enum(String) -
Json(json.Json)
Values
pub fn array(elements: List(a), of kind: fn(a) -> Value) -> Value
Returns an Array value. Each element is converted using the provided function.
pub fn date_decoder() -> decode.Decoder(calendar.Date)
Returns a decoder for Date values.
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 hstore(
hstore: dict.Dict(String, option.Option(String)),
) -> Value
Returns an Hstore value. Keys map to optional string values.
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 time_decoder() -> decode.Decoder(calendar.TimeOfDay)
Returns a decoder for TimeOfDay values.
pub fn timestamp_decoder() -> decode.Decoder(timestamp.Timestamp)
Returns a decoder for Timestamp values.
pub fn timestamptz(
timestamp: timestamp.Timestamp,
offset: duration.Duration,
) -> Value
Returns a Timestamp value with a timezone offset.
pub fn to_string(value: Value) -> String
Converts a Value to a string formatted properly for PostgreSQL