typelevel / typelevel/doobie

typechecking compositions of sql queries

Open
#239 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
2.2k
Forks
379
Avg merge
14m
Merged PRs (30d)
8

Description

Given

def insert2(name: String, age: Option[Short]): ConnectionIO[Person] =
  for {
    _  <- sql"insert into person (name, age) values ($name, $age)".update.run
    id <- sql"select lastval()".query[Long].unique
    p  <- sql"select id, name, age from person where id = $id".query[Person].unique
  } yield p

we can typecheck this operation if we factor it as:

def q1(name: String, age: Option[Short]) = 
  sql"insert into person (name, age) values ($name, $age)".update
val q2 = sql"select lastval()".query[Long]
def q3(id: Long) = sql"select id, name, age from person where id = $id".query[Person]

def insert2refactored(name: String, age: Option[Short]): ConnectionIO[Person] =
  for {
    _  <- q1(name, age).run
    id <- q2.unique
    p  <- q3(id).unique
  } yield p

and test with

check(q1("",None))
check(q2)
check(q3(0))

but this is yucky and kills the convenience of the for comprehension.

Given the appropriate Arbitrary instances or whatever, would it be possible to compose the pieces of insert2 in such a way that insert2 can be typechecked directly, without factoring out and assigning names to the component queries?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the inline insert2 composition and compare it with the q1, q2, and q3 refactoring. Run the shown check(q1("", None)), check(q2), and check(q3(0)) examples to understand the current typechecking approach. Done means determining whether the original for-comprehension can be typechecked directly without naming its component queries.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.