Using DBQC with Doobie

Experimental: this generator is retained for evaluation only and is not a supported DBQC product surface.

Doobie is a purely functional JDBC layer for Scala built on Cats Effect. The experimental DBQC generator produces Doobie-compatible Scala code; Doobie retains connection, transaction, and execution ownership.

Workflow

  1. Define your query using the DBQC Scala DSL or JSON API
  2. Run the Doobie code generator (dbqc_doobie.scala)
  3. Copy the generated code into your project
  4. Execute with a Doobie Transactor

The generated code depends only on Doobie — no DBQC runtime dependency.

Code Generator

The dbqc_doobie.scala tool in server/core/dbqc/experiments/client/ generates Doobie code from compiled queries.

From the endpoint:

val (sql, params, outputs) = DbqcClient.compile(
   "http://localhost:8080/api/dbqc/compile",
   queryJson
)
println(DoobieCodeGen.generate("myQuery", sql, params, outputs))

Generated Code Example

Input: a query that filters users by ID and returns name + email.

Output:

import doobie.*
import doobie.implicits.*
import cats.effect.IO

def getUserById(user_id: java.util.UUID): Query0[(String, String)] =
   sql"""
   WITH
     "users" AS (
       SELECT * FROM "public"."users"
     ),
     "params" AS (
       SELECT ? 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[(String, String)]

// Output columns: name, email
//
// Usage:
//   getUserById(myUserId)
//     .to[List]
//     .transact(xa)

Note: The generator converts PostgreSQL-style $1::uuid parameters to JDBC-style ? placeholders and strips type casts, since Doobie handles parameter binding through its Write type class.

Executing the Generated Code

import cats.effect.*
import doobie.*
import doobie.implicits.*

val xa: Transactor[IO] = Transactor.fromDriverManager[IO](
   driver   = "org.postgresql.Driver",
   url      = "jdbc:postgresql://localhost:5432/mydb",
   user     = "postgres",
   password = "password",
)

val result: IO[List[(String, String)]] =
   getUserById(java.util.UUID.fromString("abc-123"))
      .to[List]
      .transact(xa)

result.unsafeRunSync().foreach(println)

Type Mapping

DBQC SqlType Scala Type Doobie Meta
bool Boolean Meta[Boolean]
text String Meta[String]
int2 Short Meta[Short]
int4 Int Meta[Int]
int8 Long Meta[Long]
float4 Float Meta[Float]
float8 Double Meta[Double]
uuid java.util.UUID Meta[java.util.UUID]
date java.time.LocalDate Meta[java.time.LocalDate]
timestamp java.time.OffsetDateTime Meta[java.time.OffsetDateTime]
interval String Meta[String]
jsonb String Meta[String]

Nullable columns (isOpt: true) map to Option[T].

Updates (DML)

For DML without RETURNING, the generated code uses Update0:

def deleteUser(user_id: java.util.UUID): Update0 =
   sql"""...""".update

// Execute:
deleteUser(userId).run.transact(xa)

DML with RETURNING generates a function returning Query0 since rows come back.

Differences from Skunk

Skunk Doobie
Parameter style $1, $2 (native Postgres) ? (JDBC)
Type casts $1::uuid (explicit) Handled by Meta type class
Connection Native Postgres protocol JDBC driver
Streaming fs2 built-in fs2 via .stream
Effect type Cats Effect IO Cats Effect IO

DBQC generates the right style for each. The query algebra and compiled SQL are identical — only the parameter syntax and execution API differ.