Using DBQC with Skunk
Experimental: this generator is retained for evaluation only and is not a supported DBQC product surface.
Skunk is a purely functional PostgreSQL client for Scala built on Cats Effect and fs2. The experimental DBQC generator produces Skunk-compatible Scala code; Skunk retains connection, transaction, and execution ownership.
Workflow
- Define your query using the DBQC Scala DSL or JSON API
- Run the Skunk code generator (
dbqc_skunk.scala) - Copy the generated code into your project
- Execute with a Skunk
Session
The generated code depends only on Skunk — no DBQC runtime dependency.
Code Generator
The dbqc_skunk.scala tool in server/core/dbqc/experiments/client/ generates Skunk code from compiled queries.
From the endpoint:
val (sql, params, outputs) = DbqcClient.compile(
"http://localhost:8080/api/dbqc/compile",
queryJson
)
println(SkunkCodeGen.generate("myQuery", sql, params, outputs))
From local compilation:
import com.volvwealth.dbqc.api.*
import com.volvwealth.dbqc.impl.Compiler
val compiled = Compiler.compile(mySource).getOrElse(???)
// Feed compiled.sql, compiled.paramOrder, compiled.rowSchema to SkunkCodeGen
Generated Code Example
Input: a query that filters users by ID and returns name + email.
Output:
import skunk.*
import skunk.implicits.*
import skunk.codec.all.*
val getUserByIdQuery: Query[java.util.UUID, String *: String *: EmptyTuple] =
sql"""
WITH
"users" AS (
SELECT * FROM "public"."users"
),
"params" AS (
SELECT $1::uuid AS "user_id"
),
"node_filter" AS (
SELECT * FROM "users"
WHERE ("users"."id" IS NOT DISTINCT FROM "params"."user_id")
),
"node_project" AS (
SELECT "users"."name" AS "name", "users"."email" AS "email"
FROM "node_filter"
)
SELECT * FROM "node_project";
""".query(text *: text)
.contramap[java.util.UUID](identity)
// Parameter order: user_id
// Output columns: name, email
Executing the Generated Code
import cats.effect.*
import skunk.*
import skunk.implicits.*
import natchez.Trace.Implicits.noop
val session: Resource[IO, Session[IO]] =
Session.single[IO](
host = "localhost",
port = 5432,
user = "postgres",
database = "mydb",
password = Some("password"),
)
val program: IO[List[String *: String *: EmptyTuple]] =
session.use: s =>
s.execute(getUserByIdQuery)(java.util.UUID.fromString("abc-123"))
program.unsafeRunSync()
Type Mapping
| DBQC SqlType | Skunk Codec | Scala Type |
|---|---|---|
bool |
bool |
Boolean |
text |
text |
String |
int2 |
int2 |
Short |
int4 |
int4 |
Int |
int8 |
int8 |
Long |
float4 |
float4 |
Float |
float8 |
float8 |
Double |
uuid |
uuid |
java.util.UUID |
date |
date |
java.time.LocalDate |
timestamp |
timestamptz |
java.time.OffsetDateTime |
interval |
interval |
java.time.Duration |
jsonb |
jsonb |
io.circe.Json |
Nullable columns (isOpt: true) use .opt codecs and Option[T] types.
Commands (DML)
For INSERT, UPDATE, and DELETE queries (no result rows), the generated code uses Command instead of Query:
val deleteUserCmd: Command[java.util.UUID] =
sql"""...""".command
.contramap[java.util.UUID](identity)
// Execute:
session.use(s => s.execute(deleteUserCmd)(userId))
If DML uses RETURNING, it generates a Query since rows come back.