diff --git a/birdie_snapshots/asasdasdas.new b/birdie_snapshots/asasdasdas.new new file mode 100644 index 0000000..bd7e045 --- /dev/null +++ b/birdie_snapshots/asasdasdas.new @@ -0,0 +1,63 @@ +--- +version: 1.2.5 +title: asasdasdas +--- +import gleam/dynamic/decode +import pog +import youid/uuid.{type Uuid} + +/// A row you get from running the `query` query +/// defined in `query.sql`. +/// +/// > 🐿️ This type definition was generated automatically using v-test of the +/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). +/// +pub type QueryRow { + QueryRow(id: Uuid) +} + +/// Runs the `query` query +/// defined in `query.sql`. +/// +/// > 🐿️ This function was generated automatically using v-test of +/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). +/// +pub fn query(db, arg_1) { + let decoder = { + use id <- decode.field(0, uuid_decoder()) + decode.success(QueryRow(id:)) + } + + " +with recursive subcategories as ( + select id + from categories_issue75 + where id = $1 + + union all + + select c.id + from categories_issue75 c + join subcategories sc on c.parent_id = sc.id +) +select i.id +from items_issue75 i +left join items_categories_issue75 ic on ic.item_id = i.id +where ic.category_id in (select id from subcategories);" + |> pog.query + |> pog.parameter(pog.text(uuid.to_string(arg_1))) + |> pog.returning(decoder) + |> pog.execute(db) +} + +// --- Encoding/decoding utils ------------------------------------------------- + +/// A decoder to decode `Uuid`s coming from a Postgres query. +/// +fn uuid_decoder() { + use bit_array <- decode.then(decode.bit_array) + case uuid.from_bit_array(bit_array) { + Ok(uuid) -> decode.success(uuid) + Error(_) -> decode.failure(uuid.v7(), "uuid") + } +} diff --git a/test/squirrel_test.gleam b/test/squirrel_test.gleam index 7dfdd2a..6f63dce 100644 --- a/test/squirrel_test.gleam +++ b/test/squirrel_test.gleam @@ -144,6 +144,38 @@ create table if not exists item_issue43 ( |> pog.query |> pog.execute(db) + // https://github.com/giacomocavalieri/squirrel/issues/75 + let assert Ok(_) = + " +create table if not exists categories_issue75 ( + id uuid primary key, + name varchar(70) not null, + parent_id uuid not null +); + " + |> pog.query + |> pog.execute(db) + + let assert Ok(_) = + " +create table if not exists items_issue75 ( + id uuid primary key, + name varchar(70) not null +); +" + |> pog.query + |> pog.execute(db) + + let assert Ok(_) = + " +create table if not exists items_categories_issue75 ( + item_id uuid not null, + category_id uuid not null, + primary key (item_id, category_id) +);" + |> pog.query + |> pog.execute(db) + pog.disconnect(db) } @@ -738,3 +770,25 @@ where |> should_codegen |> birdie.snap(title: "nullability with foreign key") } + +// https://github.com/giacomocavalieri/squirrel/issues/75 +pub fn recursive_common_table_query_test() { + " +with recursive subcategories as ( + select id + from categories_issue75 + where id = $1 + + union all + + select c.id + from categories_issue75 c + join subcategories sc on c.parent_id = sc.id +) +select i.id, i.name +from items_issue75 i +left join items_categories_issue75 ic on ic.item_id = i.id +where ic.category_id in (select id from subcategories);" + |> should_codegen + |> birdie.snap(title: "asasdasdas") +}