Returning
Definition
The returning
methods allows you to add the RETURNING
clause to the query.
Available methods
1. returning(KColumnAllowedToReturning... kColumnsAllowedToReturning)
- kColumnsAllowedToReturning: are all the expresions that will be added to the
RETURNING
clause.
Among the possible values are:KTableColumn
,KColumn
,Columns with alias
,KRaw
,Case conditional expression
.
Method hierarchy
The returning
method can be used right after the following methods or objects:
and the subsequent methods that can be called are:
How to reference a KTableColumn
correctly?
The KTableColumn
referenced in the RETURNING
clause cannot contain aliases, for this the useNoAlias
method must be executed on all KTableColumn
.
Calling useNoAlias
from a KTableColumn
1. useNoAlias()
It does not receive any parameters.
info
The KTableColumn
that invokes the method will be the affected.
Calling useNoAlias
from the KFunction
class
1. useNoAlias(
KTableColumn
kTableColumn)
- kTableColumn: is the column that will be the affected.
To use this way, you need to import the static functions as follows:
import static com.myzlab.k.KFunction.*;
Example
Java code:
final KValues valuesInsert = values()
.append("Name", "Code A")
.append("English", "Code B");
final KCollection<Language> languagesInserted =
k
.insertInto(LANGUAGE)
.columns(LANGUAGE.NAME, LANGUAGE.PLATFORM_CODE)
.values(valuesInsert)
.returning(
LANGUAGE.ID.useNoAlias(),
concat(LANGUAGE.NAME.useNoAlias(), val("-"), LANGUAGE.PLATFORM_CODE.useNoAlias()).as("nameDetail"),
coalesce(LANGUAGE.NAME.useNoAlias(), LANGUAGE.PLATFORM_CODE.useNoAlias()),
raw("i18n_key"),
caseConditional()
.when(LANGUAGE.CREATED_AT.useNoAlias().gt(LocalDateTime.now().minusDays(7))).then(LANGUAGE.FILE.useNoAlias())
.elseResult(val("No file available"))
.end()
.as("file")
)
.execute(Language.class);
SQL generated:
INSERT INTO language (name, platform_code)
VALUES (?1, ?2), (?3, ?4)
RETURNING
id,
CONCAT(name || ?5 || platform_code) AS "nameDetail",
COALESCE(name, platform_code),
i18n_key,
CASE WHEN created_at > ?6 THEN file ELSE ?7 END AS "file"
Parameters:
- ?1: "Name"
- ?2: "Code A"
- ?3: "English"
- ?4: "Code B"
- ?5: "-"
- ?6: 2023-01-20T20:11:04.973543
- ?7: "No file available"