Browse Source

指定schema.

https://gitee.com/baomidou/mybatis-plus/issues/I1X45H
nieqiurong 4 years ago
parent
commit
74c829f130

+ 4 - 10
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/DecoratorDbQuery.java

@@ -121,9 +121,7 @@ public class DecoratorDbQuery extends AbstractDbQuery {
      */
     public String tableFieldsSql(String tableName) {
         String tableFieldsSql = this.tableFieldsSql();
-        if (DbType.POSTGRE_SQL == dbType) {
-            tableFieldsSql = String.format(tableFieldsSql, this.schema, tableName);
-        } else if (DbType.KINGBASE_ES == dbType) {
+        if (DbType.KINGBASE_ES == dbType) {
             tableFieldsSql = String.format(tableFieldsSql, this.schema, tableName);
         } else if (DbType.DB2 == dbType) {
             tableFieldsSql = String.format(tableFieldsSql, this.schema, tableName);
@@ -202,13 +200,9 @@ public class DecoratorDbQuery extends AbstractDbQuery {
     }
 
     public void query(String sql, Consumer<ResultSetWrapper> consumer) throws SQLException {
-        //TODO 先插在这里,等后面重构了#tablesSql里面的一堆默认逻辑在处理。
-        if (StringUtils.isBlank(connection.getSchema()) && StringUtils.isNotBlank(this.schema)) {
-            try {
-                connection.setSchema(schema);
-            } catch (SQLException sqlException) {
-                //这取决驱动与数据库实现,暂时忽略掉这错误.
-            }
+        if (StringUtils.isNotBlank(this.schema)) {
+            //切换至指定schema.
+            connection.setSchema(this.schema);
         }
         PreparedStatement preparedStatement = connection.prepareStatement(sql);
         try (ResultSet resultSet = preparedStatement.executeQuery()) {

+ 1 - 1
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/PostgreSqlQuery.java

@@ -33,7 +33,7 @@ public class PostgreSqlQuery extends AbstractDbQuery {
     public String tableFieldsSql() {
         return "SELECT A.attname AS name,format_type (A.atttypid,A.atttypmod) AS type,col_description (A.attrelid,A.attnum) AS comment,\n" +
             "(CASE WHEN (SELECT COUNT (*) FROM pg_constraint AS PC WHERE A.attnum = PC.conkey[1] AND PC.contype = 'p') > 0 THEN 'PRI' ELSE '' END) AS key \n" +
-            "FROM pg_class AS C,pg_attribute AS A WHERE A.attrelid='%s.%s'::regclass AND A.attrelid= C.oid AND A.attnum> 0 AND NOT A.attisdropped ORDER  BY A.attnum";
+            "FROM pg_class AS C,pg_attribute AS A WHERE A.attrelid='%s'::regclass AND A.attrelid= C.oid AND A.attnum> 0 AND NOT A.attisdropped ORDER  BY A.attnum";
     }
 
 

+ 1 - 1
mybatis-plus-generator/src/test/java/com/baomidou/mybatisplus/test/generator/MyPostgreSqlQuery.java

@@ -25,7 +25,7 @@ public class MyPostgreSqlQuery extends PostgreSqlQuery {
         // 固定 abc  def 内容,实际可以查询字段大小等信息
         return "SELECT 1 AS abc, 2 AS def, A.attname AS name,format_type (A.atttypid,A.atttypmod) AS type,col_description (A.attrelid,A.attnum) AS comment,\n" +
             "(CASE WHEN (SELECT COUNT (*) FROM pg_constraint AS PC WHERE A.attnum = PC.conkey[1] AND PC.contype = 'p') > 0 THEN 'PRI' ELSE '' END) AS key \n" +
-            "FROM pg_class AS C,pg_attribute AS A WHERE A.attrelid='%s.%s'::regclass AND A.attrelid= C.oid AND A.attnum> 0 AND NOT A.attisdropped ORDER  BY A.attnum";
+            "FROM pg_class AS C,pg_attribute AS A WHERE A.attrelid='%s'::regclass AND A.attrelid= C.oid AND A.attnum> 0 AND NOT A.attisdropped ORDER  BY A.attnum";
     }