Support mariadb
This commit is contained in:
@@ -9,6 +9,7 @@ import kotlinx.coroutines.experimental.channels.ProducerScope
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SchemaUtils.create
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.jetbrains.exposed.sql.vendors.DatabaseDialect
|
||||
import org.joda.time.DateTime
|
||||
import java.util.*
|
||||
import javax.sql.DataSource
|
||||
@@ -62,6 +63,14 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource) : Backing
|
||||
|
||||
override val isConnected get() = database != null
|
||||
|
||||
companion object {
|
||||
init {
|
||||
Database.registerDialect("mariadb") {
|
||||
Class.forName("org.jetbrains.exposed.sql.vendors.MysqlDialect").newInstance() as DatabaseDialect
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun init() {
|
||||
if (isShutdown) throw IllegalStateException()
|
||||
dataSource = dataSourceFactory()
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package io.dico.parcels2.storage
|
||||
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.Column
|
||||
import org.jetbrains.exposed.sql.Index
|
||||
import org.jetbrains.exposed.sql.Table
|
||||
import org.jetbrains.exposed.sql.Transaction
|
||||
import org.jetbrains.exposed.sql.statements.InsertStatement
|
||||
import org.jetbrains.exposed.sql.statements.UpdateStatement
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
|
||||
class UpsertStatement<Key : Any>(table: Table, conflictColumn: Column<*>? = null, conflictIndex: Index? = null)
|
||||
@@ -26,12 +28,23 @@ class UpsertStatement<Key : Any>(table: Table, conflictColumn: Column<*>? = null
|
||||
|
||||
override fun prepareSQL(transaction: Transaction) = buildString {
|
||||
append(super.prepareSQL(transaction))
|
||||
append(" ON CONFLICT(")
|
||||
append(indexName)
|
||||
append(") DO UPDATE SET ")
|
||||
|
||||
values.keys.filter { it !in indexColumns}.joinTo(this) { "${transaction.identity(it)}=EXCLUDED.${transaction.identity(it)}" }
|
||||
}.also { println(it) }
|
||||
val dialect = transaction.db.vendor
|
||||
if (dialect == "postgresql") {
|
||||
|
||||
append(" ON CONFLICT(")
|
||||
append(indexName)
|
||||
append(") DO UPDATE SET ")
|
||||
|
||||
values.keys.filter { it !in indexColumns }.joinTo(this) { "${transaction.identity(it)}=EXCLUDED.${transaction.identity(it)}" }
|
||||
|
||||
} else {
|
||||
|
||||
append (" ON DUPLICATE KEY UPDATE ")
|
||||
values.keys.filter { it !in indexColumns }.joinTo(this) { "${transaction.identity(it)}=VALUES(${transaction.identity(it)})" }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,15 @@ fun getHikariConfig(dialectName: String,
|
||||
dataSourceProperties["portNumber"] = port.toString()
|
||||
dataSourceProperties["databaseName"] = dco.database
|
||||
}
|
||||
|
||||
"mariadb" -> run {
|
||||
dataSourceClassName = "org.mariadb.jdbc.MariaDbDataSource"
|
||||
dataSourceProperties["serverName"] = address
|
||||
dataSourceProperties["port"] = port.toString()
|
||||
dataSourceProperties["databaseName"] = dco.database
|
||||
dataSourceProperties["properties"] = "useUnicode=true;characterEncoding=utf8"
|
||||
}
|
||||
|
||||
else -> throw IllegalArgumentException("Unsupported dialect: $dialectName")
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ interface StorageFactory {
|
||||
|
||||
class ConnectionStorageFactory : StorageFactory {
|
||||
override val optionsClass = DataConnectionOptions::class
|
||||
private val types: List<String> = listOf("postgresql")
|
||||
private val types: List<String> = listOf("postgresql", "mariadb")
|
||||
|
||||
fun register(companion: StorageFactory.StorageFactories) {
|
||||
types.forEach { companion.registerFactory(it, this) }
|
||||
|
||||
Reference in New Issue
Block a user