Exposed doesn't have an equivalent of insert or update aka upsert...
This commit is contained in:
@@ -64,9 +64,11 @@ dependencies {
|
||||
compile(project(":dicore3:dicore3-command"))
|
||||
|
||||
c.kotlinStd(kotlin("stdlib-jdk8"))
|
||||
c.kotlinStd(kotlin("reflect"))
|
||||
c.kotlinStd(kotlinx("coroutines-core:0.23.4"))
|
||||
c.kotlinStd("org.jetbrains.exposed:exposed:0.10.3")
|
||||
|
||||
compile("org.jetbrains.exposed:exposed:0.10.3") { isTransitive = false }
|
||||
compile("joda-time:joda-time:2.10")
|
||||
compile("com.zaxxer:HikariCP:3.2.0")
|
||||
|
||||
val jacksonVersion = "2.9.6"
|
||||
|
||||
@@ -46,10 +46,9 @@ object AddedGlobalT : Table("parcels_added_global") {
|
||||
}
|
||||
|
||||
object ParcelOptionsT : Table("parcel_options") {
|
||||
val parcel_id = integer("parcel_id").references(ParcelsT.id, ReferenceOption.CASCADE)
|
||||
val parcel_id = integer("parcel_id").primaryKey().references(ParcelsT.id, ReferenceOption.CASCADE)
|
||||
val interact_inventory = bool("interact_inventory").default(false)
|
||||
val interact_inputs = bool("interact_inputs").default(false)
|
||||
val index_parcel_id = uniqueIndexR("index_parcel_id", parcel_id)
|
||||
}
|
||||
|
||||
private class ExposedDatabaseException(message: String? = null) : Exception(message)
|
||||
@@ -248,7 +247,12 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource) : Backing
|
||||
|
||||
override suspend fun setParcelAllowsInteractInventory(parcel: Parcel, value: Boolean): Unit = transaction {
|
||||
val id = getOrInitParcelId(parcel)
|
||||
ParcelOptionsT.upsert(ParcelOptionsT.parcel_id) {
|
||||
/*ParcelOptionsT.upsert(ParcelOptionsT.parcel_id) {
|
||||
it[ParcelOptionsT.parcel_id] = id
|
||||
it[ParcelOptionsT.interact_inventory] = value
|
||||
}*/
|
||||
|
||||
ParcelOptionsT.replace {
|
||||
it[ParcelOptionsT.parcel_id] = id
|
||||
it[ParcelOptionsT.interact_inventory] = value
|
||||
}
|
||||
@@ -256,7 +260,7 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource) : Backing
|
||||
|
||||
override suspend fun setParcelAllowsInteractInputs(parcel: Parcel, value: Boolean): Unit = transaction {
|
||||
val id = getOrInitParcelId(parcel)
|
||||
ParcelOptionsT.upsert(ParcelOptionsT.parcel_id) {
|
||||
ParcelOptionsT.replace {
|
||||
it[ParcelOptionsT.parcel_id] = id
|
||||
it[ParcelOptionsT.interact_inputs] = value
|
||||
}
|
||||
|
||||
@@ -33,40 +33,43 @@ class UpsertStatement<Key : Any>(table: Table, conflictColumn: Column<*>? = null
|
||||
val indexName: String
|
||||
val indexColumns: List<Column<*>>
|
||||
|
||||
private fun getUpdateStatement(): UpdateStatement {
|
||||
val map: Map<Column<Any?>, Any?> = values.castUnchecked()
|
||||
val statement = updateBody(table, UpdateStatement(table, null, combineAsConjunctions(indexColumns.castUnchecked(), map))) {
|
||||
map.forEach { col, value -> if (col !in indexColumns)
|
||||
it[col] = value
|
||||
}
|
||||
}
|
||||
return statement
|
||||
}
|
||||
|
||||
init {
|
||||
if (conflictIndex != null) {
|
||||
indexName = conflictIndex.indexName
|
||||
indexColumns = conflictIndex.columns
|
||||
} else if (conflictColumn != null) {
|
||||
indexName = conflictColumn.name
|
||||
indexColumns = listOf(conflictColumn)
|
||||
} else {
|
||||
throw IllegalArgumentException()
|
||||
when {
|
||||
conflictIndex != null -> {
|
||||
indexName = conflictIndex.indexName
|
||||
indexColumns = conflictIndex.columns
|
||||
}
|
||||
conflictColumn != null -> {
|
||||
indexName = conflictColumn.name
|
||||
indexColumns = listOf(conflictColumn)
|
||||
}
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
override fun prepareSQL(transaction: Transaction): String {
|
||||
val insertSQL = super.prepareSQL(transaction)
|
||||
val args = arguments!!.first()
|
||||
val map = mutableMapOf<Column<Any?>, Any?>().apply { args.forEach { put(it.first.castUnchecked(), it.second) } }
|
||||
val updateStatement = getUpdateStatement()
|
||||
val updateSQL = updateStatement.prepareSQL(transaction)
|
||||
super.arguments = listOf(super.arguments!!.first(), updateStatement.firstDataSet)
|
||||
|
||||
val updateSQL = updateBody(table, UpdateStatement(table, null, combineAsConjunctions(indexColumns.castUnchecked(), map))) {
|
||||
map.forEach { col, value ->
|
||||
if (col !in columns) {
|
||||
it[col] = value
|
||||
}
|
||||
}
|
||||
}.prepareSQL(transaction)
|
||||
|
||||
val builder = StringBuilder().apply {
|
||||
return buildString {
|
||||
append(insertSQL)
|
||||
append(" ON CONFLICT(")
|
||||
append(indexName)
|
||||
append(") DO UPDATE ")
|
||||
append(updateSQL)
|
||||
}
|
||||
|
||||
return builder.toString().also { println(it) }
|
||||
}.also { println(it) }
|
||||
}
|
||||
|
||||
private companion object {
|
||||
@@ -100,10 +103,10 @@ inline fun <T : Table> T.upsert(conflictColumn: Column<*>? = null, conflictIndex
|
||||
execute(TransactionManager.current())
|
||||
}
|
||||
|
||||
fun Table.indexR(customIndexName:String? = null, isUnique: Boolean = false, vararg columns: Column<*>): Index {
|
||||
fun Table.indexR(customIndexName: String? = null, isUnique: Boolean = false, vararg columns: Column<*>): Index {
|
||||
val index = Index(columns.toList(), isUnique, customIndexName)
|
||||
indices.add(index)
|
||||
return index
|
||||
}
|
||||
|
||||
fun Table.uniqueIndexR(customIndexName:String? = null, vararg columns: Column<*>): Index = indexR(customIndexName, true, *columns)
|
||||
fun Table.uniqueIndexR(customIndexName: String? = null, vararg columns: Column<*>): Index = indexR(customIndexName, true, *columns)
|
||||
|
||||
Reference in New Issue
Block a user