andrewoma/kwery

allow using UUID intead of AUTO_INCREMENT ID

Closed this issue · 1 comments

Hi,
I am using UUID to generate IDs instead of mysql AUTO_INCREMENT ID, but kwery does not allow that.

    override fun <K> insert(sql: String, parameters: Map<String, Any?>, options: StatementOptions, f: (Row) -> K): Pair<Int, K> {
        return withPreparedStatement(sql, listOf(parameters), options.copy(useGeneratedKeys = true)) { statement, ps ->
            bindParameters(parameters, statement)
            val rowsAffected = ps.executeUpdate()
            interceptor.executed(statement)
            val rs = ps.generatedKeys
            try {
                require(rs.next()) { "No generated key received" }
                val keys = f(Row(rs))
                statement.copy(rowsCounts = listOf(rowsAffected)) to (rowsAffected to keys)
            } finally {
                rs.close()
            }
        }
    }

require(rs.next()) { "No generated key received" } keeps throwing the exception "No generated key received" after inserting new data with UUID.

Hi, the insert function only works for tables with generated keys.

Use the update function to insert into tables without generated keys. i.e. update is a general purpose function that supports inserts, updates, deletes, etc. insert is a specialised version to handle generated keys.

    override fun update(sql: String, parameters: Map<String, Any?>, options: StatementOptions): Int {
        return withPreparedStatement(sql, listOf(parameters), options) { statement, ps ->
            bindParameters(parameters, statement)
            val rowsAffected = ps.executeUpdate()
            interceptor.executed(statement)
            statement.copy(rowsCounts = listOf(rowsAffected)) to rowsAffected
        }
    }

Cheers,
Andrew