Skip to content

Commit

Permalink
v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Dec 22, 2024
1 parent bbcf951 commit 6c30d42
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 127 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v3.0.0 - 2024-12-22

- Updated for `gleam_stdlib` v0.50.0.
- The `decode_*` functions have been replaced with `*_decoder` functions.
- `gleam/dynamic/decode.Decoder` is now used for decoding.

## v2.0.0 - 2024-12-21

- Add support for `sslmode` in connection strings.
Expand Down
6 changes: 3 additions & 3 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "pog"
version = "2.0.0"
version = "3.0.0"
gleam = ">= 1.4.0"
licences = ["Apache-2.0"]
description = "A PostgreSQL database client for Gleam, based on PGO"
Expand All @@ -16,11 +16,11 @@ pages = [
]

[dependencies]
gleam_stdlib = ">= 0.20.0 and < 2.0.0"
gleam_stdlib = ">= 0.50.0 and < 2.0.0"
pgo = ">= 0.12.0 and < 2.0.0"

[dev-dependencies]
gleeunit = "~> 1.0"
gleeunit = ">= 1.0.0 and < 2.0.0"
exception = ">= 2.0.0 and < 3.0.0"
gleam_erlang = ">= 0.30.0 and < 1.0.0"

Expand Down
13 changes: 6 additions & 7 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
packages = [
{ name = "backoff", version = "1.1.6", build_tools = ["rebar3"], requirements = [], otp_app = "backoff", source = "hex", outer_checksum = "CF0CFFF8995FB20562F822E5CC47D8CCF664C5ECDC26A684CBE85C225F9D7C39" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "gleam_erlang", version = "0.30.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "760618870AE4A497B10C73548E6E44F43B76292A54F0207B3771CBB599C675B4" },
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
{ name = "opentelemetry_api", version = "1.3.0", build_tools = ["rebar3", "mix"], requirements = ["opentelemetry_semantic_conventions"], otp_app = "opentelemetry_api", source = "hex", outer_checksum = "B9E5FF775FD064FA098DBA3C398490B77649A352B40B0B730A6B7DC0BDD68858" },
{ name = "opentelemetry_semantic_conventions", version = "0.2.0", build_tools = ["rebar3", "mix"], requirements = [], otp_app = "opentelemetry_semantic_conventions", source = "hex", outer_checksum = "D61FA1F5639EE8668D74B527E6806E0503EFC55A42DB7B5F39939D84C07D6895" },
{ name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" },
{ name = "gleam_stdlib", version = "0.50.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "489AFCEE829FADE57F20F4C69AD67C59D80610D8A3C7DB5A7E12BE75946AAF55" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "opentelemetry_api", version = "1.4.0", build_tools = ["rebar3", "mix"], requirements = [], otp_app = "opentelemetry_api", source = "hex", outer_checksum = "3DFBBFAA2C2ED3121C5C483162836C4F9027DEF469C41578AF5EF32589FCFC58" },
{ name = "pg_types", version = "0.4.0", build_tools = ["rebar3"], requirements = [], otp_app = "pg_types", source = "hex", outer_checksum = "B02EFA785CAECECF9702C681C80A9CA12A39F9161A846CE17B01FB20AEEED7EB" },
{ name = "pgo", version = "0.14.0", build_tools = ["rebar3"], requirements = ["backoff", "opentelemetry_api", "pg_types"], otp_app = "pgo", source = "hex", outer_checksum = "71016C22599936E042DC0012EE4589D24C71427D266292F775EBF201D97DF9C9" },
]

[requirements]
exception = { version = ">= 2.0.0 and < 3.0.0" }
gleam_erlang = { version = ">= 0.30.0 and < 1.0.0" }
gleam_stdlib = { version = ">= 0.20.0 and < 2.0.0" }
gleeunit = { version = "~> 1.0" }
gleam_stdlib = { version = ">= 0.50.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
pgo = { version = ">= 0.12.0 and < 2.0.0" }
73 changes: 32 additions & 41 deletions src/pog.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

// TODO: add time and timestamp with zone once pgo supports them

import gleam/dynamic.{type DecodeErrors, type Decoder, type Dynamic}
import gleam/dynamic.{type DecodeErrors, type Dynamic}
import gleam/dynamic/decode.{type Decoder}
import gleam/float
import gleam/int
import gleam/list
Expand Down Expand Up @@ -430,7 +431,7 @@ pub fn query(sql: String) -> Query(Nil) {
Query(
sql:,
parameters: [],
row_decoder: fn(_) { Ok(Nil) },
row_decoder: decode.success(Nil),
timeout: option.None,
)
}
Expand Down Expand Up @@ -461,10 +462,6 @@ pub fn timeout(query: Query(t1), timeout: Int) -> Query(t1) {

/// Run a query against a PostgreSQL database.
///
/// The provided dynamic decoder is used to decode the rows returned by
/// PostgreSQL. If you are not interested in any returned rows you may want to
/// use the `dynamic.dynamic` decoder.
///
pub fn execute(
query query: Query(t),
on pool: Connection,
Expand All @@ -477,7 +474,7 @@ pub fn execute(
query.timeout,
))
use rows <- result.then(
list.try_map(over: rows, with: query.row_decoder)
list.try_map(over: rows, with: decode.run(_, query.row_decoder))
|> result.map_error(UnexpectedResultType),
)
Ok(Returned(count, rows))
Expand Down Expand Up @@ -756,47 +753,41 @@ pub fn error_code_name(error_code: String) -> Result(String, Nil) {
}
}

pub fn decode_timestamp(
value: dynamic.Dynamic,
) -> Result(Timestamp, DecodeErrors) {
dynamic.decode2(
Timestamp,
dynamic.element(0, decode_date),
dynamic.element(1, decode_time),
)(value)
pub fn timestamp_decoder() -> decode.Decoder(Timestamp) {
use date <- decode.field(0, date_decoder())
use time <- decode.field(1, time_decoder())
decode.success(Timestamp(date, time))
}

pub fn decode_date(value: dynamic.Dynamic) -> Result(Date, DecodeErrors) {
dynamic.decode3(
Date,
dynamic.element(0, dynamic.int),
dynamic.element(1, dynamic.int),
dynamic.element(2, dynamic.int),
)(value)
pub fn date_decoder() -> decode.Decoder(Date) {
use year <- decode.field(0, decode.int)
use month <- decode.field(1, decode.int)
use day <- decode.field(2, decode.int)
decode.success(Date(year:, month:, day:))
}

pub fn decode_time(value: dynamic.Dynamic) -> Result(Time, DecodeErrors) {
case dynamic.tuple3(dynamic.int, dynamic.int, decode_seconds)(value) {
Error(e) -> Error(e)
Ok(#(hours, minutes, #(seconds, microseconds))) ->
Ok(Time(hours:, minutes:, seconds:, microseconds:))
}
pub fn time_decoder() -> decode.Decoder(Time) {
use hours <- decode.field(0, decode.int)
use minutes <- decode.field(1, decode.int)
use #(seconds, microseconds) <- decode.field(2, seconds_decoder())
decode.success(Time(hours:, minutes:, seconds:, microseconds:))
}

fn decode_seconds(value: dynamic.Dynamic) -> Result(#(Int, Int), DecodeErrors) {
case dynamic.int(value) {
Ok(i) -> Ok(#(i, 0))
Error(_) ->
case dynamic.float(value) {
Error(e) -> Error(e)
Ok(i) -> {
let floored = float.floor(i)
let seconds = float.round(floored)
let microseconds = float.round({ i -. floored } *. 1_000_000.0)
Ok(#(seconds, microseconds))
}
}
fn seconds_decoder() -> decode.Decoder(#(Int, Int)) {
let int = {
decode.int
|> decode.map(fn(i) { #(i, 0) })
}
let float = {
decode.float
|> decode.map(fn(f) {
let floored = float.floor(f)
let seconds = float.round(floored)
let microseconds = float.round({ f -. floored } *. 1_000_000.0)
#(seconds, microseconds)
})
}
decode.one_of(int, [float])
}

pub type Date {
Expand Down
Loading

0 comments on commit 6c30d42

Please sign in to comment.