
nbcp.myoql.db.comm.MyOqlMultipleDataSourceDefine.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktmyoql Show documentation
Show all versions of ktmyoql Show documentation
kotlin orm -- mysql,mongo , just like ktorm
The newest version!
package nbcp.myoql.db.comm
import nbcp.base.comm.JsonMap
import nbcp.base.extend.*
import nbcp.base.extend.HasValue
import nbcp.base.utils.SpringUtil
import org.springframework.beans.factory.InitializingBean
data class DataSourceConnectionProperties(
var uri: String = "",
var username: String = "",
var password: String = "",
var port: Int = 0
)
data class DataSourceReadWriteProperties(
var ds: DataSourceConnectionProperties = DataSourceConnectionProperties(),
var tables: List = listOf(),
var readTables: List = listOf()
)
abstract class MyOqlMultipleDataSourceDefine(val baseConfigPrefix: String) : InitializingBean {
private var map = mutableMapOf()
private var dbReadDataSource: MutableMap = mutableMapOf()
private var dbWriteDataSource: MutableMap = mutableMapOf()
/**
* 返回配置数据源的名称
*/
fun getDataSourceName(name: String, isRead: Boolean? = null): String {
if (isRead != null && isRead) {
dbReadDataSource.getOrDefault(name, "")
.apply {
if (this.HasValue) {
return this;
}
}
}
return dbWriteDataSource.getOrDefault(name, "")
}
override fun afterPropertiesSet() {
this.dbReadDataSource = mutableMapOf()
this.dbWriteDataSource = mutableMapOf()
var map2 = org.springframework.boot.context.properties.bind.Binder.get(SpringUtil.context.environment)
.bindOrCreate(baseConfigPrefix, JsonMap::class.java)
map2.forEach {
if (it.value is Map<*, *> == false) {
return@forEach
}
var v = (it.value as MutableMap)
if (v.containsKey("tables")) {
v.put("tables", getList(v.get("tables")))
}
if (v.containsKey("read-tables")) {
v.put("read-tables", getList(v.get("read-tables")))
}
this.map.put(it.key, it.value!!.ConvertJson(DataSourceReadWriteProperties::class.java))
}
map.forEach {
var key = it.key
var collections = it.value;
this.dbReadDataSource.putAll(collections.readTables.map { it to key })
this.dbWriteDataSource.putAll(collections.tables.map { it to key })
}
}
private fun getList(v: Any?): List {
if (v == null) return listOf();
if (v is Map<*, Any?>) {
return v.values.toList();
}
if (v is List) return v;
throw RuntimeException("不识别的类型:${v::class.java.simpleName}")
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy