Browse Source

fix:修改信息点置空

zhangweixin 3 years ago
parent
commit
d47091aaee

+ 1 - 1
gradle.properties

@@ -25,7 +25,7 @@
 #
 
 PERSAGY_URL = http://www.persagy.com/
-PERSAGY_KOTLIN_VERSION = 1.4.120
+PERSAGY_KOTLIN_VERSION = 1.4.121
 # android
 # SYBOTAN_KOTLIN_VERSION = 1.4.63
 

+ 2 - 2
persagy-kotlin-mybatis/src/main/kotlin/com/persagy/mybatis/IBaseMapper.kt

@@ -83,14 +83,14 @@ interface IBaseMapper<ENTITY: Any> {
      * 根据条件更新
      *
      * @param  entity          更新值
-     * @param  projection      字段映射
+     * @param  nullItems       置空的属性列表
      * @param  filterArgs      过滤条件
      * @param  whereArgs       whereArgs参数值,由 SQlProvider 填充
      */
     @UpdateProvider(type = SSqlProvider::class, method = "update")
     fun update(@Param("entityClass")entityClass: Class<*>,
                @Param("entity")entity: Map<String, Any?>,
-               @Param("projection")projection: ArrayList<String>?,
+               @Param("nullItems")nullItems: ArrayList<String>?,
                @Param("filterArgs")filterArgs: List<SFilter>?,
                @Param("whereArgs")whereArgs: Map<String, Any>): Boolean
 

+ 33 - 12
persagy-kotlin-mybatis/src/main/kotlin/com/persagy/mybatis/SAbstractDriver.kt

@@ -93,36 +93,39 @@ abstract class SAbstractDriver {
         val entity = argsMap["entity"] as HashMap<String, Any?>
         val filterArgs = argsMap["filterArgs"] as List<SFilter>
         val whereArgs = argsMap["whereArgs"] as HashMap<String, Any>
-        val fieldList = ArrayList<String>()
+        val nullItemList = ArrayList<String>()
 
-        if (argsMap.containsKey("projection")) {
-            val fields = argsMap["projection"] as ArrayList<String>?
+        if (argsMap.containsKey("nullItems")) {
+            val fields = argsMap["nullItems"] as ArrayList<String>?
             /** 转换属性名 */
             fields?.forEach {
                 try {
                     val colName = SAbstractDao.columnName(entityClass, it.trim())
-                    fieldList.add(colName)
+                    nullItemList.add(colName)
                 } catch (e: Exception) {
                     e.printStackTrace()
                 }
             }
         }
 
-        val updateNull = fieldList.size > 0
+//        val updateNull = fieldList.size > 0
+        //
         val clause = buildWhereArgList(entityClass, filterArgs, whereArgs)
 
         val sql = object: SQL() {
             init {
                 UPDATE(SSqlProvider.driver.tableName(entityClass))
 
+                // 遍历传入的对象的属性 键值
                 for ((key, value) in entity) {
-                    if (fieldList.size > 0 && !fieldList.contains(key)) {
-                        continue
-                    }
-                    if (value == null && updateNull) {
-                        // PostgreSQL使用参数更新,为空更新不成功。必须使用sql直接设置
-                        SET("${SSqlProvider.driver.escName(key)} = null")
-                    }
+//                    if (nullItemList.size > 0 && !nullItemList.contains(key)) {
+//                        continue
+//                    }
+//                    if (value == null && updateNull) {
+//                        // PostgreSQL使用参数更新,为空更新不成功。必须使用sql直接设置
+//                        SET("${SSqlProvider.driver.escName(key)} = null")
+//                    }
+                    // 值不为空的更新
                     if (value != null) {
                         if (value is Map<*,*>) {
                             SET("${SSqlProvider.driver.escName(key)} = ${SSqlProvider.driver.updateJson(key, value)}")
@@ -136,6 +139,17 @@ abstract class SAbstractDriver {
 //                        }
                     }
                 }
+                // 遍历置空属性列表
+                for (nullItem in nullItemList){
+                    // PostgreSQL使用参数更新,为空更新不成功。必须使用sql直接设置
+                    val pos = nullItem.indexOf(".")
+                    if (pos > 0){
+                        SET(SSqlProvider.driver.updateNullItem(nullItem))
+                    }else{
+                        SET("${SSqlProvider.driver.escName(nullItem)} = null")
+                    }
+                }
+                // 更新条件为空不拼接条件
                 if (clause.isNotEmpty()) {
                     WHERE(clause)
                 }
@@ -403,6 +417,13 @@ abstract class SAbstractDriver {
     abstract fun processFunction(func: SAbstractFunction, colName: String): String;
 
     /**
+     * jsonb 中属性置空
+     *
+     * @param nullItem  表列名
+     */
+    abstract fun updateNullItem(nullItem: String): String
+
+    /**
      * 构造 where 子句参数
      *
      * @param filterList      过滤条件

+ 3 - 3
persagy-kotlin-mybatis/src/main/kotlin/com/persagy/mybatis/SAbstractMybatisDao.kt

@@ -126,13 +126,13 @@ abstract class SAbstractMybatisDao<ENTITY: Any>(entityClazz: Class<ENTITY>) : SA
      * 根据条件更新
      *
      * @param entity      实体
-     * @param projection  将被更新的字段列表(以逗号分隔),为空表示更新所有非空字段
+     * @param nullItems   置空属性列表
      * @param args        删除条件
      * @return 更新是否成功
      */
-    override fun update(entity: ENTITY, projection: ArrayList<String>?, args: List<SFilter>?): Boolean {
+    override fun update(entity: ENTITY, nullItems: ArrayList<String>?, args: List<SFilter>?): Boolean {
         return try {
-            mapper.update(entityClass, entity.toUpdateRow(), projection, args, HashMap())
+            mapper.update(entityClass, entity.toUpdateRow(), nullItems, args, HashMap())
         } catch (e: Exception) {
             e.printStackTrace()
             false

+ 4 - 0
persagy-kotlin-mysql/src/main/kotlin/com/persagy/mysql/SMySqlDriver.kt

@@ -208,6 +208,10 @@ class SMySqlDriver : SAbstractDriver() {
         func.colName = colName
         return func.toSql(SDatabaseType.MySQL)
     }
+
+    override fun updateNullItem(nullItem: String): String {
+        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
+    }
 //----------------------------------------------------------------------------------------------------------------------
     /**
      * 转换where字句参数

+ 12 - 1
persagy-kotlin-postgresql/src/main/kotlin/com/persagy/postgresql/SPostgreSqlDriver.kt

@@ -348,12 +348,23 @@ class SPostgreSqlDriver : SAbstractDriver() {
         return if (path == null) {
             "\"$name\""
         } else {
-            val p = path.replace(".", ",")
+            val p = path.`replace`(".", ",")
             "\"$name\"#>'{$p}'"
         }
     }
 
     /**
+     * 更新字段为空
+     */
+    override fun updateNullItem(nullItem: String): String {
+        val pos = nullItem.indexOf(".")
+        val   path = nullItem.substring(pos + 1).replace(".",",")
+        val  propertyName = nullItem.substring(0, pos)
+        return "${SSqlProvider.driver.escName(propertyName)} = (jsonb_set(${SSqlProvider.driver.escName(propertyName)}::jsonb,'{$path}','null'::jsonb))"
+    }
+
+
+    /**
      * 转换参数值 SQL 语句
      *
      * @param argName     参数名