JetBrains exposed Adding a custom field/column with a value in a slice
Asked Answered
H

1

6

I want to add a custom field when doing a complex join with a value Basically something like this:

SELECT p.first_name, "Smith" AS "last_name" 
FROM person p
WHERE first_name = "John"

In kotlin that would be something like this:

PersonTable
  .slice(PersonTable.first_name, "" HERE IS WHERE I’D LIKE TO PUT SOMETHING "")
  .select(PersonTable.first_name eq "John")

The field "last_name" doesn't exist in any table in this example. I want to add it to the ResultRow.

My real query is much more complex but I can't share it publically. Basically what I want is to add the last_name column to every ResultRow I would get out of the query with the value "Smith". And then a way to access that column later.

Can this be done in JetBrains Exposed?

This is what I have tried unsuccessfully:

PersonTable
  .slice(PersonTable.first_name, stringLiteral("Smith").alias("last_name"))
  .select(PersonTable.first_name eq "John")

The above doesn't work the way I want it to. It does add a column that has the value "Smith" but the column name/alias is 'Smith'last_name instead of just last_name. The reason why this doesn't work is that I will be doing a union with another select that will have a different value for last_name and therefore I need the column name to be the same in both those selects.

Hoax answered 28/5, 2021 at 15:20 Comment(0)
L
0

A couple options:

  • Create a view in your database that contains the computed value and has the name you want.
  • Do 2 separate queries and "union" them in the kotlin code while adding your computed value to the one line
Lacerta answered 8/11, 2022 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.