JetBrains / JetBrains/Exposed

Possible feature: closure for results of insert statement

Open
#808 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Kotlin
Stars
9.3k
Forks
798
Avg merge
4d 2h
Merged PRs (30d)
26

Description

What if you allowed a closure to be passed to insert to get the value(s) of the column(s) that make up the primary key? That is, instead of:
```
val pragueId = Cities.insert {
it.update(name, stringLiteral(" Prague ").trim().substring(1, 2))
}[Cities.id]
```
what if there were the option of, say:
```
val pragueId = Cities.insert {
it.update(name, stringLiteral(" Prague ").trim().substring(1, 2))
} getKey { it[id] }
```
`getKey` would be an inline function taking a code block with a receiver type, so that fields could be referenced. Two nice things about this syntax:

* It's more like the select statement, where the getters are in a lambda-with-receiver
* If you have more than one column in the primary key, you can naturally use a destructuring assignment, without any intermediate variables to hold the thing that holds the `ResultSet`

Just thought I'd share this idea. It occurred to me when making a much less ambitious Kotlin/SQL library, https://db9010.jovial.com/, where the equivalent would be:
```
val pragueId = Cities.run {
myDatabase.insertInto(this) {
it[name] = "Prague"
} run {
it[id]
}
}
```
It kind of bothered me that the way you address column values after an insert wasn't harmonious with the rest of Exposed.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.