🐿️ Type safe SQL in Gleam
58

Configure Feed

Select the types of activity you want to include in your feed.

Giacomo Cavalieri (Dec 23, 2025, 9:10 PM +0100) aeb7a60c b93d3283

+156 -1
+28
birdie_snapshots/bit_decoding.accepted
··· 1 + --- 2 + version: 1.5.1 3 + title: bit decoding 4 + file: ./test/squirrel_test.gleam 5 + test_name: bit_decoding_test 6 + --- 7 + 8 + import gleam/dynamic/decode 9 + import pog 10 + 11 + pub type QueryRow { 12 + QueryRow(res: BitArray) 13 + } 14 + 15 + pub fn query( 16 + db: pog.Connection, 17 + ) -> Result(pog.Returned(QueryRow), pog.QueryError) { 18 + let decoder = { 19 + use res <- decode.field(0, decode.bit_array) 20 + decode.success(QueryRow(res:)) 21 + } 22 + 23 + "select 11::bit(4) as res" 24 + |> pog.query 25 + |> pog.returning(decoder) 26 + |> pog.execute(db) 27 + } 28 +
+30
birdie_snapshots/bit_encoding.accepted
··· 1 + --- 2 + version: 1.5.1 3 + title: bit encoding 4 + file: ./test/squirrel_test.gleam 5 + test_name: bit_encoding_test 6 + --- 7 + 8 + import gleam/dynamic/decode 9 + import pog 10 + 11 + pub type QueryRow { 12 + QueryRow(res: Bool) 13 + } 14 + 15 + pub fn query( 16 + db: pog.Connection, 17 + arg_1: BitArray, 18 + ) -> Result(pog.Returned(QueryRow), pog.QueryError) { 19 + let decoder = { 20 + use res <- decode.field(0, decode.bool) 21 + decode.success(QueryRow(res:)) 22 + } 23 + 24 + "select true as res where $1 = 11::bit(4)" 25 + |> pog.query 26 + |> pog.parameter(pog.bytea(arg_1)) 27 + |> pog.returning(decoder) 28 + |> pog.execute(db) 29 + } 30 +
+28
birdie_snapshots/bit_varying_decoding.accepted
··· 1 + --- 2 + version: 1.5.1 3 + title: bit varying decoding 4 + file: ./test/squirrel_test.gleam 5 + test_name: bit_varying_decoding_test 6 + --- 7 + 8 + import gleam/dynamic/decode 9 + import pog 10 + 11 + pub type QueryRow { 12 + QueryRow(bit_varying_value: BitArray) 13 + } 14 + 15 + pub fn query( 16 + db: pog.Connection, 17 + ) -> Result(pog.Returned(QueryRow), pog.QueryError) { 18 + let decoder = { 19 + use bit_varying_value <- decode.field(0, decode.bit_array) 20 + decode.success(QueryRow(bit_varying_value:)) 21 + } 22 + 23 + "select bit_varying_value from bits" 24 + |> pog.query 25 + |> pog.returning(decoder) 26 + |> pog.execute(db) 27 + } 28 +
+32
birdie_snapshots/bit_varying_encoding.accepted
··· 1 + --- 2 + version: 1.5.1 3 + title: bit varying encoding 4 + file: ./test/squirrel_test.gleam 5 + test_name: bit_varying_encoding_test 6 + --- 7 + 8 + import gleam/dynamic/decode 9 + import pog 10 + 11 + pub type QueryRow { 12 + QueryRow(res: Bool) 13 + } 14 + 15 + pub fn query( 16 + db: pog.Connection, 17 + arg_1: BitArray, 18 + ) -> Result(pog.Returned(QueryRow), pog.QueryError) { 19 + let decoder = { 20 + use res <- decode.field(0, decode.bool) 21 + decode.success(QueryRow(res:)) 22 + } 23 + 24 + "select true as res 25 + from bits 26 + where $1 = bit_varying_value" 27 + |> pog.query 28 + |> pog.parameter(pog.bytea(arg_1)) 29 + |> pog.returning(decoder) 30 + |> pog.execute(db) 31 + } 32 +
+1 -1
src/squirrel/internal/database/postgres.gleam
··· 267 267 "int2" | "int4" | "int8" -> Ok(gleam.Int) 268 268 "json" | "jsonb" -> Ok(gleam.Json) 269 269 "uuid" -> Ok(gleam.Uuid) 270 - "bytea" -> Ok(gleam.BitArray) 270 + "bit" | "varbit" | "bytea" -> Ok(gleam.BitArray) 271 271 "date" -> Ok(gleam.Date) 272 272 "time" -> Ok(gleam.TimeOfDay) 273 273 "timestamp" -> Ok(gleam.Timestamp)
+2
test/integration_test.gleam
··· 80 80 TestType("uuid", [TestValue("uuid.v7()")]), 81 81 // Bytea 82 82 TestType("bytea", [TestValue("<<1, 2, 3>>")]), 83 + TestType("bit(3)", [TestValue("<<1:1, 0:1, 0:1>>")]), 84 + TestType("varbit(24)", [TestValue("<<250, 255>>")]), 83 85 // Date 84 86 TestType("date", [TestValue("calendar.Date(1998, calendar.October, 11)")]), 85 87 // TimeOfDay
+35
test/squirrel_test.gleam
··· 64 64 65 65 let assert Ok(_) = 66 66 " 67 + create table if not exists bits( 68 + bit_value bit(5) not null, 69 + bit_varying_value bit varying(5) not null 70 + )" 71 + |> pog.query 72 + |> pog.execute(db) 73 + 74 + let assert Ok(_) = 75 + " 67 76 do $$ begin 68 77 if not exists (select * from pg_type where typname = 'squirrel_colour') then 69 78 create type squirrel_colour as enum ('red', 'grey', 'light brown'); ··· 576 585 "select array['red'::squirrel_colour] as res" 577 586 |> should_codegen 578 587 |> birdie.snap(title: "enum array decoding") 588 + } 589 + 590 + pub fn bit_decoding_test() { 591 + "select 11::bit(4) as res" 592 + |> should_codegen 593 + |> birdie.snap(title: "bit decoding") 594 + } 595 + 596 + pub fn bit_encoding_test() { 597 + "select true as res where $1 = 11::bit(4)" 598 + |> should_codegen 599 + |> birdie.snap(title: "bit encoding") 600 + } 601 + 602 + pub fn bit_varying_decoding_test() { 603 + "select bit_varying_value from bits" 604 + |> should_codegen 605 + |> birdie.snap(title: "bit varying decoding") 606 + } 607 + 608 + pub fn bit_varying_encoding_test() { 609 + "select true as res 610 + from bits 611 + where $1 = bit_varying_value" 612 + |> should_codegen 613 + |> birdie.snap(title: "bit varying encoding") 579 614 } 580 615 581 616 // https://github.com/giacomocavalieri/squirrel/issues/119