tjohnn
07/11/2019, 1:35 PMKotlinNullPointerException
whenever I attempt to use it for inserting a new data which doesn't have an id, the id being null is the cause of the error based on what I see so far, the id is auto_increment so I cannot set it explicitly, is there a way for me to fix this or Am I doing something wrong?runjorel
07/11/2019, 3:30 PMIdTable
etctjohnn
07/11/2019, 4:16 PMLongIdTable
preciselyINSERT INTO product_variants (available_quantity, color_id, created_at, discount_price, id, price, product_id, size_id, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE size_id=VALUES(size_id), color_id=VALUES(color_id), price=VALUES(price), discount_price=VALUES(discount_price), available_quantity=VALUES(available_quantity), updated_at=VALUES(updated_at)
I do not seem to understand why this is failing. Any help is appreciated.runjorel
07/12/2019, 7:26 PM.batchInsertOnDuplicateKeyUpdate(data, columns){ .. }
tjohnn
07/13/2019, 9:01 AMProductVariants.batchInsertOnDuplicateKeyUpdate(product.variants!!,
listOf(ProductVariants.sizeId, ProductVariants.colorId,
ProductVariants.price, ProductVariants.discountPrice, ProductVariants.availableQuantity,
ProductVariants.updatedAt)
) { batch, variant ->
if(variant.id ?: 0 > 0) batch[id] = EntityID(variant.id, Products)
batch[productId] = EntityID(variant.productId, Products)
batch[sizeId] = EntityID(variant.sizeId, Sizes)
batch[colorId] = EntityID(variant.colorId, Colors)
batch[price] = variant.price!!
batch[discountPrice] = variant.discountPrice ?: 0.0
batch[availableQuantity] = variant.availableQuantity ?: 0
batch[createdAt] = DateTime.now()
batch[updatedAt] = DateTime.now()
}
runjorel
07/13/2019, 3:46 PMdata
parameter in the function, BUT there is a data
property in BatchInsertStatement
. I am wondering if there is something weird going on with that inner data.forEach
expression. Which data
is it referring to?T.batchInsertOnDuplicateKeyUpdate(data: List<E>...
tjohnn
07/13/2019, 5:32 PMdata
class from which I am supposed to bind values for each column of the tableauto_increment
tapac
07/27/2019, 8:09 PMtjohnn
07/28/2019, 9:56 AMclass BatchInsertUpdateOnDuplicate(table: Table, private val onDupUpdate: List<Column<*>>) : BatchInsertStatement(table, false) {
override fun prepareSQL(transaction: Transaction): String {
val onUpdateSQL = if (onDupUpdate.isNotEmpty()) {
" ON DUPLICATE KEY UPDATE " + onDupUpdate.joinToString { "${transaction.identity(it)}=VALUES(${transaction.identity(it)})" }
} else ""
val statement = super.prepareSQL(transaction) + onUpdateSQL
print(statement)
return statement
}
}
fun <T : Table, E> T.batchInsertOnDuplicateKeyUpdate(data: List<E>, onDupUpdateColumns: List<Column<*>>, body: T.(BatchInsertUpdateOnDuplicate, E) -> Unit) {
data.
takeIf {
it.isNotEmpty()
}?.let {
val insert = BatchInsertUpdateOnDuplicate(this, onDupUpdateColumns)
data.forEach {
insert.addBatch()
body(insert, it)
}
TransactionManager.current().exec(insert)
}
}
I am using it like this:
ProductVariants.batchInsertOnDuplicateKeyUpdate(
variants,
listOf(ProductVariants.sizeId, ProductVariants.colorId, ProductVariants.price )
) { batch, variant ->
batch[id] = EntityID(variant.id, ProductVariants)
batch[productId] = EntityID(variant.productId, Products)
batch[sizeId] = EntityID(variant.sizeId, Sizes)
batch[colorId] = EntityID(variant.colorId, Colors)
batch[price] = variant.price
}
It works fine for existing variants but not for new ones.
Please let me now if you need more info.tapac
07/29/2019, 9:41 AMvariants
collection.
If you expect to make update
when your id
already exists in database then you should provide that id.tjohnn
07/29/2019, 2:47 PMtapac
07/30/2019, 4:15 PMtjohnn
07/30/2019, 9:59 PM