nbcp.myoql.db.sql.SqlEntityCollector.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.sql
import nbcp.base.db.annotation.*
import nbcp.base.extend.ForEachExt
import nbcp.myoql.db.cache.RedisCacheColumns
import nbcp.myoql.db.cache.RedisCacheDefine
import nbcp.myoql.db.comm.*
import nbcp.myoql.db.db
import nbcp.myoql.db.mysql.ExistsSqlSourceConfigCondition
import nbcp.myoql.db.sql.base.SqlBaseMetaTable
import nbcp.myoql.db.sql.component.SqlBaseQueryClip
import nbcp.myoql.db.sql.component.SqlDeleteClip
import nbcp.myoql.db.sql.component.SqlInsertClip
import nbcp.myoql.db.sql.component.SqlUpdateClip
import nbcp.myoql.db.sql.event.*
import org.springframework.beans.factory.config.BeanPostProcessor
import org.springframework.context.annotation.Conditional
import org.springframework.stereotype.Component
import javax.sql.DataSource
/**
* 事件处理中心
*/
@Component
@Conditional(ExistsSqlSourceConfigCondition::class)
class SqlEntityCollector : BeanPostProcessor {
companion object {
//需要删 除后放入垃圾箱的实体
@JvmStatic
val dustbinEntities = mutableSetOf>() //mongo meta class
// 冗余字段的引用。如 user.corp.name 引用的是 corp.name
@JvmStatic
val refsMap = mutableListOf()
//注册的 select Bean
@JvmStatic
val selectEvents = mutableListOf()
//注册的 Insert Bean
@JvmStatic
val insertEvents = mutableListOf()
//注册的 Update Bean
@JvmStatic
val updateEvents = mutableListOf()
//注册的 Delete Bean
@JvmStatic
val deleteEvents = mutableListOf()
@JvmStatic
val dataSources = mutableListOf()
@JvmStatic
val sysRedisCacheDefines = mutableMapOf>()
}
override fun postProcessAfterInitialization(bean: Any, beanName: String): Any? {
if (bean is IDataGroup) {
var group = bean::class.java.getAnnotation(MetaDataGroup::class.java)
if (group.dbType.isSqlType()) {
db.sql.groups.add(bean)
bean.getEntities().forEach { moer ->
if (moer is SqlBaseMetaTable<*>) {
var entityClass = moer.entityClass
addDustbin(moer)
addRedisCache(moer);
addRef(entityClass)
}
}
}
}
// if (SpringUtil.runningInTest) {
// return super.postProcessAfterInitialization(bean, beanName)
// }
if (bean is ISqlEntitySelect) {
selectEvents.add(bean)
}
if (bean is ISqlEntityInsert) {
insertEvents.add(bean)
}
if (bean is ISqlEntityUpdate) {
updateEvents.add(bean)
}
if (bean is ISqlEntityDelete) {
deleteEvents.add(bean)
}
if (bean is ISqlDataSource) {
dataSources.add(bean)
}
return super.postProcessAfterInitialization(bean, beanName)
}
private val dataSourceMap = mutableMapOf();
/**
* 在拦截器中获取数据源。
*/
fun getDataSource(tableName: String, isRead: Boolean): DataSource? {
var key = "${tableName}-${isRead}"
var ret = dataSourceMap.get(key);
if (ret != null) return ret;
dataSources.ForEachExt { iSqlDataSource, _ ->
var v = iSqlDataSource.run(tableName, isRead)
if (v == null) {
return@ForEachExt false;
}
return@ForEachExt true;
}
if (ret != null) {
dataSourceMap.put(key, ret);
}
return ret;
}
private fun addRef(entityClass: Class<*>) {
var refs = entityClass.getAnnotationsByType(DbEntityFieldRef::class.java)
refs.forEach {
refsMap.add(DbEntityFieldRefData(entityClass, it))
}
if (entityClass.superclass != null) {
addRef(entityClass.superclass);
}
}
private fun addDustbin(moer: SqlBaseMetaTable<*>) {
var moerClass = moer::class.java;
var logicalDelete = moerClass.getAnnotation(LogicalDelete::class.java)
if (logicalDelete != null) {
return;
}
var dustbin = moerClass.getAnnotation(RemoveToSysDustbin::class.java)
if (dustbin != null) {
dustbinEntities.add(moer)
}
}
private fun addRedisCache(moer: SqlBaseMetaTable<*>) {
var list = mutableListOf()
var moerClass = moer::class.java
moerClass.getAnnotationsByType(DbEntityIndex::class.java)
.filter { it.unique }
.forEach {
list.add(RedisCacheColumns(*it.value))
}
moerClass.getAnnotationsByType(RedisCacheDefine::class.java)
.filter { it.value.size > 0 }
.forEach{
list.add(RedisCacheColumns(*it.value))
}
if (list.isEmpty()) return;
sysRedisCacheDefines.put(moer.tableName, list.toTypedArray())
}
fun onSelecting(select: SqlBaseQueryClip): Array> {
if (select.tableName.isEmpty()) return arrayOf();
//先判断是否进行了类拦截.
var list = mutableListOf>()
selectEvents.ForEachExt { it, _ ->
var ret = it.beforeSelect(select);
if (ret.result == false) {
return@ForEachExt false;
}
list.add(it to ret)
return@ForEachExt true
}
return list.toTypedArray()
}
fun onInserting(insert: SqlInsertClip<*, *>): Array> {
if (insert.tableName.isEmpty()) return arrayOf();
//先判断是否进行了类拦截.
var list = mutableListOf>()
insertEvents.ForEachExt { it, _ ->
var ret = it.beforeInsert(insert);
if (ret.result == false) {
return@ForEachExt false;
}
list.add(it to ret)
return@ForEachExt true
}
return list.toTypedArray()
}
fun onUpdating(update: SqlUpdateClip<*>): Array> {
if (update.tableName.isEmpty()) return arrayOf();
//先判断是否进行了类拦截.
var list = mutableListOf>()
updateEvents.ForEachExt { it, _ ->
var ret = it.beforeUpdate(update);
if (ret.result == false) {
return@ForEachExt false;
}
list.add(it to ret)
return@ForEachExt true
}
return list.toTypedArray()
}
fun onDeleting(delete: SqlDeleteClip<*>): Array> {
if (delete.tableName.isEmpty()) return arrayOf();
//先判断是否进行了类拦截.
var list = mutableListOf>()
deleteEvents.ForEachExt { it, _ ->
var ret = it.beforeDelete(delete);
if (ret.result == false) {
return@ForEachExt false;
}
list.add(it to ret)
return@ForEachExt true
}
return list.toTypedArray()
}
}