Browse Source

恢复 AutoSqlInjector 调整至 PostgreSqlInjector

hubin 8 năm trước cách đây
mục cha
commit
6fc2d5b1b9

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

@@ -613,11 +613,10 @@ 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(sqlSelectAsColumnConvert(table.getKeyProperty()));
+                    columns.append(table.getKeyColumn()).append(" AS ").append(sqlWordConvert(table.getKeyProperty()));
                 } else {
-                    columns.append(wordConvert);
+                    columns.append(sqlWordConvert(table.getKeyProperty()));
                 }
                 if (_size >= 1) {
                     // 判断其余字段是否存在
@@ -638,7 +637,7 @@ public class AutoSqlInjector implements ISqlInjector {
                     } else {
                         // 字段属性不一致
                         columns.append(fieldInfo.getColumn());
-                        columns.append(" AS ").append(sqlSelectAsColumnConvert(wordConvert));
+                        columns.append(" AS ").append(wordConvert);
                     }
                     if (i + 1 < _size) {
                         columns.append(",");
@@ -673,11 +672,10 @@ 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(sqlSelectAsColumnConvert(wordConvert));
+                columns.append(table.getKeyColumn()).append(" AS ").append(sqlWordConvert(table.getKeyProperty()));
             } else {
-                columns.append(wordConvert);
+                columns.append(sqlWordConvert(table.getKeyProperty()));
             }
         } else {
             // 表字段处理
@@ -691,7 +689,7 @@ public class AutoSqlInjector implements ISqlInjector {
                 } else {
                     // 字段属性不一致
                     columns.append(fieldInfo.getColumn());
-                    columns.append(" AS ").append(sqlSelectAsColumnConvert(wordConvert));
+                    columns.append(" AS ").append(wordConvert);
                 }
             }
         }
@@ -699,19 +697,6 @@ 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 查询条件

+ 95 - 1
src/main/java/com/baomidou/mybatisplus/mapper/PostgreSqlInjector.java

@@ -15,8 +15,14 @@
  */
 package com.baomidou.mybatisplus.mapper;
 
+import java.util.Iterator;
+import java.util.List;
+
 import com.baomidou.mybatisplus.entity.GlobalConfiguration;
+import com.baomidou.mybatisplus.entity.TableFieldInfo;
+import com.baomidou.mybatisplus.entity.TableInfo;
 import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
+import com.baomidou.mybatisplus.toolkit.StringUtils;
 
 /**
  * <p>
@@ -28,9 +34,97 @@ import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
  */
 public class PostgreSqlInjector extends AutoSqlInjector {
 
-    @Override
+    /**
+     * <p>
+     * select sql as 字段转换,默认原样返回,预留子类处理<br>
+     * </p>
+     *
+     * @param columnStr 字段内容
+     * @return
+     */
     protected String sqlSelectAsColumnConvert(String columnStr) {
         GlobalConfiguration globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
         return String.format(globalConfig.getIdentifierQuote(), columnStr);
     }
+
+
+    /**
+     * <p>
+     * SQL 查询所有表字段
+     * </p>
+     *
+     * @param table
+     * @param entityWrapper 是否为包装类型查询
+     * @return
+     */
+    protected String sqlSelectColumns(TableInfo table, boolean entityWrapper) {
+        StringBuilder columns = new StringBuilder();
+        if (null != table.getResultMap()) {
+            /*
+             * 存在 resultMap 映射返回
+			 */
+            if (entityWrapper) {
+                columns.append("<choose><when test=\"ew != null and ew.sqlSelect != null\">${ew.sqlSelect}</when><otherwise>");
+            }
+            columns.append("*");
+            if (entityWrapper) {
+                columns.append("</otherwise></choose>");
+            }
+        } else {
+            /*
+             * 普通查询
+			 */
+            if (entityWrapper) {
+                columns.append("<choose><when test=\"ew != null and ew.sqlSelect != null\">${ew.sqlSelect}</when><otherwise>");
+            }
+            List<TableFieldInfo> fieldList = table.getFieldList();
+            int _size = 0;
+            if (null != fieldList) {
+                _size = fieldList.size();
+            }
+
+            // 主键处理
+            if (StringUtils.isNotEmpty(table.getKeyProperty())) {
+                if (table.isKeyRelated()) {
+                    columns.append(table.getKeyColumn()).append(" AS ").append(sqlSelectAsColumnConvert(table.getKeyProperty()));
+                } else {
+                    columns.append(sqlWordConvert(table.getKeyProperty()));
+                }
+                if (_size >= 1) {
+                    // 判断其余字段是否存在
+                    columns.append(",");
+                }
+            }
+
+            if (_size >= 1) {
+                // 字段处理
+                int i = 0;
+                Iterator<TableFieldInfo> iterator = fieldList.iterator();
+                while (iterator.hasNext()) {
+                    TableFieldInfo fieldInfo = iterator.next();
+                    // 匹配转换内容
+                    String wordConvert = sqlWordConvert(fieldInfo.getProperty());
+                    if (fieldInfo.getColumn().equals(wordConvert)) {
+                        columns.append(wordConvert);
+                    } else {
+                        // 字段属性不一致
+                        columns.append(fieldInfo.getColumn());
+                        columns.append(" AS ").append(sqlSelectAsColumnConvert(wordConvert));
+                    }
+                    if (i + 1 < _size) {
+                        columns.append(",");
+                    }
+                    i++;
+                }
+            }
+            if (entityWrapper) {
+                columns.append("</otherwise></choose>");
+            }
+        }
+
+		/*
+         * 返回所有查询字段内容
+		 */
+        return columns.toString();
+    }
 }