All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nbcp.myoql.db.db.kt Maven / Gradle / Ivy

The newest version!
package nbcp.myoql.db

import nbcp.base.comm.StringMap
import nbcp.base.extend.scopes
import nbcp.myoql.annotation.*
import nbcp.myoql.db.enums.MyOqlDbScopeEnum
import nbcp.myoql.db.mongo.table.MongoBaseGroup
import nbcp.myoql.db.redis.RedisBaseGroup
import nbcp.myoql.db.sql.table.SqlBaseGroup
import java.time.Duration
import java.util.function.Supplier
import kotlin.reflect.KClass


/**
 * 数据库上下文,及操作类。
 */
object db {
//    private val logger by lazy {
//        return@lazy LoggerFactory.getLogger(this::class.java)
//    }

    @JvmStatic
    val sql = dbSql;

    @JvmStatic
    val mongo = dbMongo;

    @JvmStatic
    val es = dbEs;

    @JvmStatic
    val redis = nbcp.myoql.db.DbRedis;

    private val _affectRowCount: ThreadLocal = ThreadLocal.withInitial { return@withInitial -1 }


    /**
     * 读写分离用!当前请求对数据库表执行 insert,update,delete 操作的表. 全小写
     */
    private val _currentRequestChangeDbTable: ThreadLocal> = ThreadLocal.withInitial { return@withInitial LinkedHashSet() }

    val currentRequestChangeDbTable: LinkedHashSet
        get() {
            return _currentRequestChangeDbTable.get();
        }

    /**
     * 最后执行的影响行数,mongo,sql
     */
    @JvmStatic
    var affectRowCount: Int
        get() {
            return _affectRowCount.get()
        }
        set(value) {
            if (scopes.getLatest(MyOqlDbScopeEnum.IGNORE_AFFECT_ROW) != null) {
                return;
            }
            _affectRowCount.set(value);
        }


    private val _lastAutoId: ThreadLocal = ThreadLocal.withInitial { return@withInitial -1; }

    /**
     * 对sql数据来说,记录最后一条插入数据的自增Id
     */
    @JvmStatic
    var lastAutoId: Int
        get() {
            return _lastAutoId.get()
        }
        set(value) {
            _lastAutoId.set(value);
        }

    private val _executeTime: ThreadLocal = ThreadLocal.withInitial { return@withInitial Duration.ZERO; }

    /**
     * 记录最后一次操作的执行时间,单位毫秒
     */
    @JvmStatic
    var executeTime: Duration
        get() {
            return _executeTime.get()
        }
        set(value) {
            if (scopes.getLatest(MyOqlDbScopeEnum.IGNORE_EXECUTE_TIME) != null) {
                return;
            }
            _executeTime.set(value);
        }

//    fun change_id2Id(value: DBObject, remove_id: Boolean = true) {
//        var keys = value.keySet().toTypedArray();
//        var needReplace = keys.contains("_id") && !keys.contains("id")
//        for (k in keys) {
//            var v = value.get(k);
//            if (needReplace && (k == "_id")) {
//                value.put("id", v?.toString() ?: "");
//                if (remove_id) {
//                    value.removeField("_id")
//                }
//                needReplace = false;
//                continue;
//            }
//            if (v == null) {
//                continue;
//            }
//            if (v is MutableMap<*, *>) {
//                change_id2Id(v, remove_id);
//            } else if (v is DBObject) {
//                change_id2Id(v, remove_id);
//            }
//        }
//    }

    //    /**
//     * 填充 city.name
//     */
//    fun fillCityName(value: Any) {
//        RecursionUtil.recursionAny(value, { map, pkey ->
//            true
//        }, { list, pkey ->
//            true
//        }, { value, pkey ->
//            if (value is CityCodeName) {
//                if (value.code > 0 && value.name.isEmpty()) {
//                    value.name = db.rer_base.getCityNameByCode(value.code)
//                }
//            }
//            true
//        })
//    }
    @JvmStatic
    val morBase
        get() = MongoBaseGroup()

    @JvmStatic
    val rerBase
        get() = RedisBaseGroup()

    @JvmStatic
    val sqlBase
        get() = SqlBaseGroup()


//-------------------

//    @JvmStatic
//    fun usingRedisCache(cacheForSelect: FromRedisCache, sql: String, variableMap: JsonMap): FromRedisCache {
//        val spelExecutor = CacheKeySpelExecutor(variableMap);
//        return FromRedisCache(
//            cacheForSelect.tableClass,
//            cacheForSelect.joinTableClasses,
//            spelExecutor.getVariableValue(cacheForSelect.groupKey),
//            spelExecutor.getVariableValue(cacheForSelect.groupValue),
//            spelExecutor.getVariableValue(cacheForSelect.sql.AsString(sql)),
//            cacheForSelect.cacheSeconds,
//            spelExecutor.getVariableValue(cacheForSelect.getTableName()),
//            cacheForSelect.getJoinTableNames()
//        )
//    }

    @JvmStatic
    fun  getRedisCacheJson(
            tableClass: Class,
            /**
             * 缓存表的隔离键, 如:"cityCode"
             */
            groupKey: String,
            /**
             * 缓存表的隔离值,如: "010"
             */
            groupValue: String,

            /**
             * 唯一值
             */
            sql: String,
            consumer: Supplier
    ): T? {
        return getRedisCacheJson(tableClass.simpleName, tableClass, groupKey, groupValue, sql, consumer)
    }

    /**
     * 简单模式
     */
    @JvmStatic
    fun  getRedisCacheJson(
            table: String,
            cacheType: Class,
            /**
             * 缓存表的隔离键, 如:"cityCode"
             */
            groupKey: String,
            /**
             * 缓存表的隔离值,如: "010"
             */
            groupValue: String,

            /**
             * 唯一值
             */
            sql: String,
            consumer: Supplier
    ): T? {
        return FromRedisCache(table = table, groupKey = groupKey, groupValue = groupValue, sql = sql)
                .getJson(
                        cacheType,
                        consumer
                )
    }

    @JvmStatic
    fun  getRedisCacheList(
            tableClass: Class,
            /**
             * 缓存表的隔离键, 如:"cityCode"
             */
            groupKey: String,
            /**
             * 缓存表的隔离值,如: "010"
             */
            groupValue: String,

            /**
             * 唯一值
             */
            sql: String,
            consumer: Supplier?>
    ): List? {
        return getRedisCacheList(tableClass.simpleName, tableClass, groupKey, groupValue, sql, consumer)
    }

    @JvmStatic
    fun  getRedisCacheList(
            table: String,
            cacheType: Class,
            /**
             * 缓存表的隔离键, 如:"cityCode"
             */
            groupKey: String,
            /**
             * 缓存表的隔离值,如: "010"
             */
            groupValue: String,

            /**
             * 唯一值
             */
            sql: String,
            consumer: Supplier?>
    ): List? {
        return FromRedisCache(table = table, groupKey = groupKey, groupValue = groupValue, sql = sql)
                .getList(
                        cacheType,
                        consumer
                )
    }


    @JvmStatic
    fun brokeRedisCache(tableClass: KClass<*>, groupKey: String, groupValue: String) {
        return BrokeRedisCache(
                table = tableClass.java.simpleName,
                groupKey = groupKey,
                groupValue = groupValue
        ).brokeCache()
    }

    @JvmStatic
    fun brokeRedisCache(table: String, groupKey: String, groupValue: String) {
        return BrokeRedisCache(table = table, groupKey = groupKey, groupValue = groupValue).brokeCache()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy