/** * Apply the op function to the PreparedStatement and then call executeUpdate() * This allows you to write `prepareStatement(sql).exec { setParametersHere() }` to * set parameters easily and execute the sql effortlessly. * * This was not optimized further to `exec(sql) {}` because your IDE will * allow auto completion for the `prepareStatement` sql parameter but not for a custom function * as it can't recognize the sql anymore. */ fun PreparedStatement.exec(op: PreparedStatement.() -> Unit) = let { op(it) it.executeUpdate() } /** * Fetch a connection from the datasource and apply the op function to it before * closing the connection. */ fun DataSource.withConnection(op: Connection.() -> T) = connection.use { op(it) } /** * Call executeQuery() on the PreparedStatement and call the fn parameter on each * row in the ResultSet to create an observable list of items extracted from the rows. */ fun PreparedStatement.toModel(fn: (ResultSet) -> T) = executeQuery().toModel(fn) /** * Call the fn parameter on each row in the ResultSet to create an observable list of items extracted from the rows. */ fun ResultSet.toModel(fn: (ResultSet) -> T): ObservableList { val list = FXCollections.observableArrayList() while (next()) list.add(fn(this)) return list }