Ver código fonte

新增 PostgreSql 自动注入器,处理字段大小写敏感,自动双引号转义。

hubin 8 anos atrás
pai
commit
06451d9a5d

+ 1 - 1
src/main/java/com/baomidou/mybatisplus/entity/TableFieldInfo.java

@@ -77,7 +77,7 @@ public class TableFieldInfo {
     private String logicNotDeleteValue;
 
     /**
-     * 字段忽略策略
+     * 字段填充策略
      */
     private FieldFill fieldFill = FieldFill.DEFAULT;
 

+ 21 - 6
src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -613,10 +613,11 @@ public class AutoSqlInjector implements ISqlInjector {
 
             // 主键处理
             if (StringUtils.isNotEmpty(table.getKeyProperty())) {
+                String wordConvert = sqlWordConvert(table.getKeyProperty());
                 if (table.isKeyRelated()) {
-                    columns.append(table.getKeyColumn()).append(" AS ").append(sqlWordConvert(table.getKeyProperty()));
+                    columns.append(table.getKeyColumn()).append(" AS ").append(sqlSelectAsColumnConvert(table.getKeyProperty()));
                 } else {
-                    columns.append(sqlWordConvert(table.getKeyProperty()));
+                    columns.append(wordConvert);
                 }
                 if (_size >= 1) {
                     // 判断其余字段是否存在
@@ -637,7 +638,7 @@ public class AutoSqlInjector implements ISqlInjector {
                     } else {
                         // 字段属性不一致
                         columns.append(fieldInfo.getColumn());
-                        columns.append(" AS ").append(wordConvert);
+                        columns.append(" AS ").append(sqlSelectAsColumnConvert(wordConvert));
                     }
                     if (i + 1 < _size) {
                         columns.append(",");
@@ -672,10 +673,11 @@ public class AutoSqlInjector implements ISqlInjector {
         columns.append("<choose><when test=\"ew != null and ew.sqlSelect != null\">${ew.sqlSelect}</when><otherwise>");
         // 主键处理
         if (StringUtils.isNotEmpty(table.getKeyProperty())) {
+            String wordConvert = sqlWordConvert(table.getKeyProperty());
             if (table.isKeyRelated()) {
-                columns.append(table.getKeyColumn()).append(" AS ").append(sqlWordConvert(table.getKeyProperty()));
+                columns.append(table.getKeyColumn()).append(" AS ").append(sqlSelectAsColumnConvert(wordConvert));
             } else {
-                columns.append(sqlWordConvert(table.getKeyProperty()));
+                columns.append(wordConvert);
             }
         } else {
             // 表字段处理
@@ -689,7 +691,7 @@ public class AutoSqlInjector implements ISqlInjector {
                 } else {
                     // 字段属性不一致
                     columns.append(fieldInfo.getColumn());
-                    columns.append(" AS ").append(wordConvert);
+                    columns.append(" AS ").append(sqlSelectAsColumnConvert(wordConvert));
                 }
             }
         }
@@ -697,6 +699,19 @@ public class AutoSqlInjector implements ISqlInjector {
         return columns.toString();
     }
 
+    /**
+     * <p>
+     * select sql as 字段转换,默认原样返回,预留子类处理<br>
+     * 例如:com.baomidou.mybatisplus.mapper.PostgreSqlInjector<br>
+     * </p>
+     *
+     * @param columnStr 字段内容
+     * @return
+     */
+    protected String sqlSelectAsColumnConvert(String columnStr) {
+        return columnStr;
+    }
+
     /**
      * <p>
      * SQL 查询条件

+ 36 - 0
src/main/java/com/baomidou/mybatisplus/mapper/PostgreSqlInjector.java

@@ -0,0 +1,36 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.mapper;
+
+import com.baomidou.mybatisplus.entity.GlobalConfiguration;
+import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
+
+/**
+ * <p>
+ * PostgreSql 自动注入器,处理字段大小写敏感,自动双引号转义。
+ * </p>
+ *
+ * @author hubin
+ * @Date 2017-07-11
+ */
+public class PostgreSqlInjector extends AutoSqlInjector {
+
+    @Override
+    protected String sqlSelectAsColumnConvert(String columnStr) {
+        GlobalConfiguration globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
+        return String.format(globalConfig.getIdentifierQuote(), columnStr);
+    }
+}