typelevel / typelevel/skunk

Consider supporting numbered parameters

Open
#555 0 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.7k
Forks
175
Avg merge
5d 14h
Merged PRs (30d)
9

Description

As well as...

val f: Query[String ~ Int, Country] =
  sql"""
    SELECT name, population
    FROM   country
    WHERE  name LIKE $varchar
    AND    population < $int4
  """.query(country)

it would be nice to have something like...

val f: Query[String ~ Int, Country] =
  sql"""
    SELECT name, population
    FROM   country
    WHERE  name LIKE $1
    AND    population < $2
  """.query(country)
     .params(varchar ~ int4)

or similar

Occasionally I've come across queries that simply can't be supported by the inability to re-use parameters within it. It's always possible to rewrite the query to cope with this but it nearly always becomes more complicated and I feel that the query shouldn't be subject to the limitation of the database api.

Here's a contrived example that doesn't work because the same value has had to be split into 2 params...

PREPARE foo_fail (int, int) AS
  select case when a.someData > $1 then 0 else a.someData end,
         sum(a.someOtherData)
  from (select 1 as someData, 2 as someOtherData) a
  group by case when a.someData > $2 then 0 else a.someData end;
EXECUTE foo_fail(1, 1);

and here it works with a repeated use of a single param...

PREPARE foo_success (int) AS
  select case when a.someData > $1 then 0 else a.someData end,
         sum(a.someOtherData)
  from (select 1 as someData, 2 as someOtherData) a
  group by case when a.someData > $1 then 0 else a.someData end;
EXECUTE foo_success(1);

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 SQL interpolator and the Query.params API illustrated in the issue, then trace how named parameters are translated into PostgreSQL parameters. Compare the existing $varchar and $int4 form with the proposed $1 and $2 form, including repeated references. Done means numbered parameters can be reused while preserving type-safe parameter binding and query construction.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.