question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`CASE...WHEN...THEN...END` Support

See original GitHub issue

This is a bigger one, but, any plans on implementing CASE support?

https://modern-sql.com/feature/case

Looks like it is supported by: BigQuery, Db2, MariaDB, MySQL, Oracle DB, PostgreSQL, SQL Server, SQLite, so basically all major engines.

Syntax:

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END;

Probably would fit well as another method on ExpressionBuilder, ie:

db.selectFrom("table")
 .select(eb => {
  return eb.case(cb => {
   return cb.when("expression1", "expression2")
    .when("expression3", "expression4")
    .else("expression5")
  })
 })

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
koskimascommented, Nov 3, 2022

So we’d have orWhen, orWhenExists etc? That’s an option and it would take care of the case statement but it’s not reusable. I’m talking about a builder that could be used to build any expression. For example

select(eb => eb.bin('first_name', '=', 'Sami').as('is_sami'))
2reactions
koskimascommented, Nov 2, 2022

If we go down the road of adding builders for all kinds of expressions, we need to really think it through and create a cohesive expression builder instead of adding different things ad-hoc. I’m also not sure we should do it at all. The code quickly becomes unreadable and it’s better just to write raw SQL and pay the small type-unsafety penalty.

Let’s think about the case builder for example. The syntax could indeed be something like this

eb.case(maybeExpr)
  .when(expr).then(expr)
  .when(expr).then(expr)
  .else(expr)

The expressions can be anything but let’s say they are simple comparison operations. We’d have two options:

  1. Use raw SQL for each subexpression
  2. Use the expression builder

Since we are on the road of full type-safety, we’d of course want to use the expression builder. We’d get something like this:

eb.case()
  .when(eb.bin('first_name', '=', 'foo')).then(eb.val(1))
  .when(eb.bin('first_name', 'in', ['bar', 'baz'])).then(eb.val(2))
  .else(eb.val(3))

In this example I used two non-existent expression builder methods bin and val. That’s not too bad yet, but compared to the raw SQL it’s quite a bit of boilerplate

sql<number>`
  case
    when first_name = 'foo' then 1
    when first_name in ('bar', 'baz') then 2
    else 3
  end
`
Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL CASE Expression - W3Schools
The CASE expression goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So, once a...
Read more >
CASE (Transact-SQL) - SQL Server - Microsoft Learn
The searched CASE expression evaluates a set of Boolean expressions to determine the result. Both formats support an optional ELSE argument.
Read more >
SQL CASE | Intermediate SQL - Mode Analytics
Every CASE statement must end with the END statement. The ELSE statement is optional, and provides a way to capture values not specified...
Read more >
SQL Server: CASE Statement - TechOnTheNet
In SQL Server (Transact-SQL), the CASE statement has the functionality of an IF-THEN-ELSE statement. You can use the CASE statement within a SQL...
Read more >
CASE statement in SQL
Case Statement with Order by clause ; Select EmployeeName,Gender,Salary ; from Employee ; ORDER BY CASE Gender ; WHEN 'F' THEN Salary End...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found