torres
07/16/2020, 2:18 PMfun <Key:Comparable<Key>, T: IdTable<Key>> T.insertIgnoreAndGetId(body: T.(UpdateBuilder<*>)->Unit) = InsertStatement<EntityID<Key>>(this, isIgnore = true).run {
body(this)
execute(TransactionManager.current())
getOrNull(id)
}
Jakub Pi
07/16/2020, 2:26 PMZach Klippenstein (he/him) [MOD]
07/16/2020, 2:26 PMEvan R.
07/16/2020, 4:40 PM<Key:Comparable<Key>, T: IdTable<Key>>
are the generic type parameters. They assert that Key
implements comparable against itself, and T
implements IdTable<Key>
• T.insertIgnoreAndGetId
defines the method as an extension function on T
, the table
• body: T.(UpdateBuilder<*>)->Unit
means the parameter, body
accepts a function returning Unit
which is either an extension or member of T
that gets passed a parameter of type UpdateBuilder<*>
, where `UpdateBuilder`’s generic type doesn’t matter (it’s a wildcard)
• = InsertStatement<EntityID<Key>>(this, isIgnore = true).run {
means this is a single-expression function, returning the result of InsertStatement.run()
. It constructs an insert statement and runs the “run” block, returning the “run” block’s value
• body(this)
executes the function passed as a parameter, using this
(the insert statement) as the parameter and receiver
• execute(TransactionManager.current())
is calling InsertStatement.execute()
on the current transaction manager to perform the SQL query
• getOrNull(id)
is executing InsertStatement.getOrNull()
to retrieve the value of the id
column from the result. As this is the last statement in the “run” block, either the value of the ID column is returned, or Null on an error
Hope that helped! And I highly recommend checking out the resources that other users posted above.