Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySql BatchInsert returns BigInteger #280

Closed
coolemza opened this issue Mar 30, 2018 · 3 comments
Closed

MySql BatchInsert returns BigInteger #280

coolemza opened this issue Mar 30, 2018 · 3 comments
Labels
waiting for reply Additional information required

Comments

@coolemza
Copy link

coolemza commented Mar 30, 2018

BatchInsert on MySql db returns list of BigInteger as list of insert id's, even when primary key id is integer, h2:mem:test, returns list of integer in this case

@Tapac
Copy link
Contributor

Tapac commented Apr 5, 2018

Could you provide sample to test that case? Because I use Exposed with MySQL and never faced such behavior.

@Tapac Tapac added the waiting for reply Additional information required label Apr 26, 2018
@xJoeWoo
Copy link
Contributor

xJoeWoo commented Nov 23, 2018

I met this issue today as well. Here was my trace to this issue:

  1. In batchInsert() method, result uses generatedKey from BatchInsertStatement

    result += statement.generatedKey.orEmpty()

  2. generatedKey references to resultedValues from InsertStatement

    override val generatedKey: List<Map<Column<*>, Any>>?
    get() = resultedValues

  3. resultedValues is assigned by processResults()

  4. processResults() is reading results from java.sql.ResultSet by getObject(), which will return a BigInteger object ignoring INT or LONG type of column, then return BigInteger object to batchInsert() directly

    autoGeneratedKeys.add(hashMapOf(firstAutoIncColumn to rs.getObject(colIndx ?: 1)))


Not like insertAndGetId() or insertIgnoreAndGetId() method, the generated result of batchInsert() from ResultSet is not processed by a column's valueFromDB()

fun <Key:Comparable<Key>, T: IdTable<Key>> T.insertIgnoreAndGetId(body: T.(UpdateBuilder<*>)->Unit) = InsertStatement<EntityID<Key>>(this, isIgnore = true).run {
body(this)
execute(TransactionManager.current())
get(id)
}

infix operator fun <T> get(column: Column<T>): T? = resultedValues?.get(0)?.get(column)?.let { column.columnType.valueFromDB(it )} as? T
?: if (!isIgnore) error("No key generated") else null


My opinion is adding column type handling to batchInsert() before returning the result, making result's type consistent with XXXAndGetId() methods.

xJoeWoo added a commit to xJoeWoo/Exposed that referenced this issue Nov 23, 2018
xJoeWoo added a commit to xJoeWoo/Exposed that referenced this issue Nov 23, 2018
@Tapac
Copy link
Contributor

Tapac commented Feb 15, 2020

Should be fixed in master since 0.12.1 (related issue #424)

@Tapac Tapac closed this as completed Feb 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for reply Additional information required
Projects
None yet
Development

No branches or pull requests

3 participants