Created
February 26, 2018 00:12
-
-
Save barinek/004153cec67dbe69e62f530db239a72e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> create(connection: Connection, sql: String, id: (Long) -> T, vararg params: Any): T { | |
return connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS).use { statement -> | |
for (i in params.indices) { | |
val param = params[i] | |
val parameterIndex = i + 1 | |
when (param) { | |
is String -> statement.setString(parameterIndex, param) | |
is Int -> statement.setInt(parameterIndex, param) | |
is Long -> statement.setLong(parameterIndex, param) | |
is Boolean -> statement.setBoolean(parameterIndex, param) | |
is LocalDate -> statement.setDate(parameterIndex, Date.valueOf(param)) | |
} | |
} | |
statement.executeUpdate() | |
val keys = statement.generatedKeys | |
keys.next() | |
id(keys.getLong(1)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment