lpil / pog

🐘 A PostgreSQL database client for Gleam, based on PGO
https://hexdocs.pm/pgo
Apache License 2.0
146 stars 13 forks source link

Postgres enum support #40

Closed isaacharrisholt closed 1 month ago

isaacharrisholt commented 1 month ago

Hallo, I'd like to be able to insert Postgres enums with PGO. I've tried inserting them as text to no avail by doing something like:

$1::enum_type
lpil commented 1 month ago

Good idea. I'm not sure what the API might be though 🤔

isaacharrisholt commented 1 month ago

Honestly pgo.enum(String) is probably best, and users can have their own custom type -> string functions if they want them

lpil commented 1 month ago

That may require us to generate atoms at runtime, which doesn't seems great. We'll need to look at what PGO does internally.

giacomocavalieri commented 1 month ago

I can't seem to replicate the issue, enums work totally fine from my experiments:

create type mood as enum ('sad', 'ok', 'happy');
create table moods (
   current_mood mood primary key
);
let assert Ok(pgo.Returned(1, [])) =
  "insert into moods values ($1)"
  |> pgo.execute(db, [pgo.text("happy")], dynamic.dynamic)

let assert Ok(pgo.Returned(1, ["happy"])) =
  "select current_mood from moods"
  |> pgo.execute(db, [], dynamic.element(0, dynamic.string))

let assert Ok(pgo.Returned(1, ["happy"])) =
  "select current_mood from moods where current_mood = $1"
  |> pgo.execute(db, [dynamic.text("happy")], dynamic.element(0, dynamic.string))

They're always treated as strings, both when used as query params or appear in the returned values

lpil commented 1 month ago

Hooray!