import albedo_sqlite import gleam/dynamic/decode import gleam/option.{type Option} import sqlight /// Shared return type for queries in this directory: /// src/albedo_ext_artifacts/versions/sql/history.sql /// src/albedo_ext_artifacts/versions/sql/version.sql pub type VersionRow { VersionRow( version: Int, entry: String, manifest: String, cell: Option(String), created_at: Int, bytes: Int, ) } fn version_row_decoder() -> decode.Decoder(VersionRow) { use version <- decode.field(0, decode.int) use entry <- decode.field(1, decode.string) use manifest <- decode.field(2, decode.string) use cell <- decode.field(3, decode.optional(decode.string)) use created_at <- decode.field(4, decode.int) use bytes <- decode.field(5, decode.int) decode.success(VersionRow( version:, entry:, manifest:, cell:, created_at:, bytes:, )) } /// Generated from src/albedo_ext_artifacts/versions/sql/history.sql pub fn history( db db: sqlight.Connection, id id: String, ) -> Result(List(VersionRow), String) { albedo_sqlite.rows( "SELECT v.version,v.entry,v.manifest,v.cell,v.created_at,v.bytes FROM artifact_versions v WHERE v.artifact=@id ORDER BY v.version DESC", on: db, with: [sqlight.text(id)], expecting: version_row_decoder(), ) } /// Generated from src/albedo_ext_artifacts/versions/sql/version.sql pub fn version( db db: sqlight.Connection, id id: String, version version: Option(Int), ) -> Result(List(VersionRow), String) { albedo_sqlite.rows( "SELECT v.version,v.entry,v.manifest,v.cell,v.created_at,v.bytes FROM artifact_versions v JOIN artifacts a ON a.id=v.artifact WHERE v.artifact=@id AND v.version=COALESCE(@version,a.version)", on: db, with: [sqlight.text(id), sqlight.nullable(sqlight.int, version)], expecting: version_row_decoder(), ) }