|
@@ -29,8 +29,7 @@ package com.persagy.postgresql
|
|
import com.persagy.base.extensions.isBaseType
|
|
import com.persagy.base.extensions.isBaseType
|
|
import com.persagy.base.extensions.toJson
|
|
import com.persagy.base.extensions.toJson
|
|
import com.persagy.base.extensions.toJsonAll
|
|
import com.persagy.base.extensions.toJsonAll
|
|
-import com.persagy.database.SAbstractFunction
|
|
|
|
-import com.persagy.database.SFilter
|
|
|
|
|
|
+import com.persagy.database.*
|
|
import com.persagy.database.enums.SDatabaseType
|
|
import com.persagy.database.enums.SDatabaseType
|
|
import com.persagy.database.enums.SOps
|
|
import com.persagy.database.enums.SOps
|
|
import com.persagy.mybatis.SAbstractDriver
|
|
import com.persagy.mybatis.SAbstractDriver
|
|
@@ -59,11 +58,19 @@ class SPostgreSqlDriver : SAbstractDriver() {
|
|
*/
|
|
*/
|
|
@Suppress("UNCHECKED_CAST")
|
|
@Suppress("UNCHECKED_CAST")
|
|
override fun insert(argsMap: Map<String, Any?>): String {
|
|
override fun insert(argsMap: Map<String, Any?>): String {
|
|
|
|
+ val builder = argsMap["builder"] as SInsetBuilder<*>
|
|
val entityClass = argsMap["entityClass"] as Class<*>
|
|
val entityClass = argsMap["entityClass"] as Class<*>
|
|
val entity = argsMap["entity"] as HashMap<String, Any?>
|
|
val entity = argsMap["entity"] as HashMap<String, Any?>
|
|
val sql = object: SQL() {
|
|
val sql = object: SQL() {
|
|
init {
|
|
init {
|
|
- INSERT_INTO(SSqlProvider.driver.tableName(entityClass))
|
|
|
|
|
|
+
|
|
|
|
+ /** 如果查询构建器朱指定了表名 */
|
|
|
|
+ if (builder!=null&&!builder.tableName.isNullOrEmpty()) {
|
|
|
|
+ INSERT_INTO(builder.tableName!!)
|
|
|
|
+ } else {
|
|
|
|
+ INSERT_INTO(SSqlProvider.driver.tableName(entityClass))
|
|
|
|
+ }
|
|
|
|
+
|
|
for ((key, value) in entity) {
|
|
for ((key, value) in entity) {
|
|
if (value != null) {
|
|
if (value != null) {
|
|
if (value is Map<*,*>) {
|
|
if (value is Map<*,*>) {
|
|
@@ -94,12 +101,13 @@ class SPostgreSqlDriver : SAbstractDriver() {
|
|
* 在数据库中替换实体
|
|
* 在数据库中替换实体
|
|
*
|
|
*
|
|
* @param argsMap 注入参数
|
|
* @param argsMap 注入参数
|
|
- * @param insertSql 插入 SQL 语句
|
|
|
|
|
|
+ *
|
|
* @return 替换 SQL 语句
|
|
* @return 替换 SQL 语句
|
|
*/
|
|
*/
|
|
@Suppress("UNCHECKED_CAST")
|
|
@Suppress("UNCHECKED_CAST")
|
|
- override fun replace(argsMap: Map<String, Any?>, insertSql: String): String {
|
|
|
|
|
|
+ override fun replace(argsMap: Map<String, Any?>): String {
|
|
val sql: String
|
|
val sql: String
|
|
|
|
+ val builder = argsMap["builder"] as SReplaceBuilder<*>
|
|
val entity = argsMap["entity"] as Map<String, Any?>
|
|
val entity = argsMap["entity"] as Map<String, Any?>
|
|
val keyColumnList = argsMap["keyColumnList"] as ArrayList<String>
|
|
val keyColumnList = argsMap["keyColumnList"] as ArrayList<String>
|
|
|
|
|
|
@@ -141,7 +149,7 @@ class SPostgreSqlDriver : SAbstractDriver() {
|
|
confict.append("\"$it\"")
|
|
confict.append("\"$it\"")
|
|
}
|
|
}
|
|
|
|
|
|
- sql = "$insertSql ON conflict ( $confict ) DO UPDATE SET $upSqlBuilder"
|
|
|
|
|
|
+ sql = "${replaceInsert(argsMap)} ON conflict ( $confict ) DO UPDATE SET $upSqlBuilder"
|
|
logger.debug("REPLACE SQL= $sql")
|
|
logger.debug("REPLACE SQL= $sql")
|
|
return sql
|
|
return sql
|
|
}
|
|
}
|
|
@@ -154,6 +162,7 @@ class SPostgreSqlDriver : SAbstractDriver() {
|
|
*/
|
|
*/
|
|
@Suppress("UNCHECKED_CAST")
|
|
@Suppress("UNCHECKED_CAST")
|
|
override fun delete(argsMap: Map<String, Any?>): String {
|
|
override fun delete(argsMap: Map<String, Any?>): String {
|
|
|
|
+ val builder = argsMap["builder"] as SDeleteBuilder<*>
|
|
val entityClass = argsMap["entityClass"] as Class<*>
|
|
val entityClass = argsMap["entityClass"] as Class<*>
|
|
/** 从入参中获得过虑条件 */
|
|
/** 从入参中获得过虑条件 */
|
|
val filterArgs = argsMap["filterArgs"] as List<SFilter>
|
|
val filterArgs = argsMap["filterArgs"] as List<SFilter>
|
|
@@ -163,7 +172,12 @@ class SPostgreSqlDriver : SAbstractDriver() {
|
|
|
|
|
|
val sql = object: SQL() {
|
|
val sql = object: SQL() {
|
|
init {
|
|
init {
|
|
- DELETE_FROM(SSqlProvider.driver.tableName(entityClass))
|
|
|
|
|
|
+ /** 如果查询构建器朱指定了表名 */
|
|
|
|
+ if (builder!=null&&!builder.tableName.isNullOrEmpty()) {
|
|
|
|
+ DELETE_FROM(builder.tableName!!)
|
|
|
|
+ } else {
|
|
|
|
+ DELETE_FROM(SSqlProvider.driver.tableName(entityClass))
|
|
|
|
+ }
|
|
if (clause.isNotEmpty()) {
|
|
if (clause.isNotEmpty()) {
|
|
WHERE(clause)
|
|
WHERE(clause)
|
|
}
|
|
}
|
|
@@ -182,10 +196,15 @@ class SPostgreSqlDriver : SAbstractDriver() {
|
|
*/
|
|
*/
|
|
@Suppress("UNCHECKED_CAST")
|
|
@Suppress("UNCHECKED_CAST")
|
|
override fun deleteAll(argsMap: Map<String, Any?>): String {
|
|
override fun deleteAll(argsMap: Map<String, Any?>): String {
|
|
|
|
+ val builder = argsMap["builder"] as SDeleteBuilder<*>?
|
|
val entityClass = argsMap["entityClass"] as Class<*>
|
|
val entityClass = argsMap["entityClass"] as Class<*>
|
|
val sql = object: SQL() {
|
|
val sql = object: SQL() {
|
|
init {
|
|
init {
|
|
- DELETE_FROM(SSqlProvider.driver.tableName(entityClass))
|
|
|
|
|
|
+ if (builder!=null&&!builder.tableName.isNullOrEmpty()) {
|
|
|
|
+ DELETE_FROM(builder.tableName!!)
|
|
|
|
+ } else {
|
|
|
|
+ DELETE_FROM(SSqlProvider.driver.tableName(entityClass))
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}.toString()
|
|
}.toString()
|
|
|
|
|
|
@@ -438,4 +457,51 @@ class SPostgreSqlDriver : SAbstractDriver() {
|
|
return "COALESCE(\"$name\", '{}') || '${value.toJsonAll().replace("'", "''")}'"
|
|
return "COALESCE(\"$name\", '{}') || '${value.toJsonAll().replace("'", "''")}'"
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 在数据库中替换实体-生成replace语句部分插入sql语句
|
|
|
|
+ *
|
|
|
|
+ * @param argsMap 注入参数
|
|
|
|
+ *
|
|
|
|
+ * @return 替换 SQL 语句
|
|
|
|
+ */
|
|
|
|
+ @Suppress("UNCHECKED_CAST")
|
|
|
|
+ private fun replaceInsert(argsMap: Map<String, Any?>): String {
|
|
|
|
+ val builder = argsMap["builder"] as SReplaceBuilder<*>?
|
|
|
|
+ val entityClass = argsMap["entityClass"] as Class<*>
|
|
|
|
+ val entity = argsMap["entity"] as HashMap<String, Any?>
|
|
|
|
+ val sql = object : SQL() {
|
|
|
|
+ init {
|
|
|
|
+
|
|
|
|
+ /** 如果查询构建器朱指定了表名 */
|
|
|
|
+ if (builder!=null&&!builder.tableName.isNullOrEmpty()) {
|
|
|
|
+ INSERT_INTO(builder.tableName!!)
|
|
|
|
+ } else {
|
|
|
|
+ INSERT_INTO(SSqlProvider.driver.tableName(entityClass))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for ((key, value) in entity) {
|
|
|
|
+ if (value != null) {
|
|
|
|
+ if (value is Map<*, *>) {
|
|
|
|
+ VALUES(SSqlProvider.driver.escName(key), "'${value.toJsonAll().replace("'", "''")}'")
|
|
|
|
+ } else if (value is List<*>) {
|
|
|
|
+ VALUES(SSqlProvider.driver.escName(key), "'${value.toJsonAll().replace("'", "''")}'")
|
|
|
|
+ } else if (value::class.java.isPrimitive) {
|
|
|
|
+ VALUES(SSqlProvider.driver.escName(key), "'${value.toJsonAll().replace("'", "''")}'")
|
|
|
|
+ } else {
|
|
|
|
+ VALUES(SSqlProvider.driver.escName(key), SSqlProvider.driver.entityValue(entity, key))
|
|
|
|
+ }
|
|
|
|
+// if (value::class.java.isPrimitive) {
|
|
|
|
+// VALUES(driver.escName(key), driver.entityValue(entity, key))
|
|
|
|
+// } else {
|
|
|
|
+// VALUES(driver.escName(key), "'${value.toJsonAll()}'")
|
|
|
|
+// }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }.toString()
|
|
|
|
+
|
|
|
|
+ logger.debug("INSERT SQL= $sql")
|
|
|
|
+
|
|
|
|
+ return sql
|
|
|
|
+ }
|
|
}
|
|
}
|