Browse Source

OracleDdlGenerator支持指定schema模式运行.

https://github.com/baomidou/mybatis-plus/issues/6656
聂秋荣 1 tháng trước cách đây
mục cha
commit
45f286d042

+ 1 - 0
mybatis-plus-extension/build.gradle

@@ -23,5 +23,6 @@ dependencies {
     testImplementation "${lib.postgresql}"
     testImplementation "${lib.oracle}"
     testImplementation "${lib.sqlite}"
+    testImplementation lib.dm as ConfigurableFileTree
     testImplementation "${lib.'logback-classic'}"
 }

+ 43 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/ddl/history/OracleDdlGenerator.java

@@ -15,6 +15,13 @@
  */
 package com.baomidou.mybatisplus.extension.ddl.history;
 
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.function.Function;
 
 /**
@@ -25,18 +32,53 @@ import java.util.function.Function;
  */
 public class OracleDdlGenerator implements IDdlGenerator {
 
+    /**
+     * 默认使用当前用户模式
+     * @since 3.5.13
+     */
+    private String schema;
+
+    public OracleDdlGenerator() {
+    }
+
+    public OracleDdlGenerator(String schema) {
+        this.schema = schema;
+    }
+
+    /**
+     * 基于当前用户模式实例
+     * @return OracleDdlGenerator
+     */
     public static IDdlGenerator newInstance() {
         return new OracleDdlGenerator();
     }
 
+    @Override
+    public boolean existTable(Connection connection) throws SQLException {
+        DatabaseMetaData metaData = connection.getMetaData();
+        String schema = StringUtils.isNotBlank(this.schema) ? this.schema : connection.getSchema();
+        String tableName = getDdlHistory();
+        int index = tableName.lastIndexOf(StringPool.DOT);
+        if (index > 0) {
+            tableName = tableName.substring(index + 1);
+        }
+        tableName = tableName.replace(StringPool.QUOTE, StringPool.EMPTY);
+        try (ResultSet resultSet = metaData.getTables(connection.getCatalog(), schema, tableName, new String[]{"TABLE"})) {
+            return resultSet.next();
+        }
+    }
+
     @Override
     public boolean existTable(String databaseName, Function<String, Boolean> executeFunction) {
         return executeFunction.apply("SELECT COUNT(1) AS NUM FROM user_tables WHERE table_name='"
-                + getDdlHistory() + "'");
+            + getDdlHistory() + "'");
     }
 
     @Override
     public String getDdlHistory() {
+        if (StringUtils.isNotBlank(schema)) {
+            return schema + ".DDL_HISTORY";
+        }
         return "DDL_HISTORY";
     }
 

+ 20 - 4
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/test/DdlHelperTest.java

@@ -37,7 +37,7 @@ public class DdlHelperTest {
         var dataSource = new UnpooledDataSource(org.postgresql.Driver.class.getName(),
             "jdbc:postgresql://localhost:5432/postgres",
             "postgres", "123456");
-        IDdlGenerator ddlGenerator = new PostgreDdlGenerator();
+        IDdlGenerator ddlGenerator = PostgreDdlGenerator.newInstance();
         DdlHelper.runScript(ddlGenerator, dataSource, List.of("ddl/test.sql"),
             true, DdlScriptErrorHandler.ThrowsErrorHandler.INSTANCE);
         // 指定scheme运行 旧版本的模式是指定 public,为了兼容当使用默认的示例是无法根据指定的模式走的
@@ -90,7 +90,7 @@ public class DdlHelperTest {
 
     @Test
     void testForH2() throws SQLException {
-        var dataSource = new UnpooledDataSource(org.sqlite.JDBC.class.getName(),
+        var dataSource = new UnpooledDataSource(org.h2.Driver.class.getName(),
             "jdbc:h2:mem:test;DATABASE_TO_LOWER=TRUE",
             "sa", "");
         DdlHelper.runScript(null, dataSource, List.of("ddl/test.sql"),
@@ -99,7 +99,7 @@ public class DdlHelperTest {
 
     @Test
     void testForH2Mysql() throws SQLException {
-        var dataSource = new UnpooledDataSource(org.sqlite.JDBC.class.getName(),
+        var dataSource = new UnpooledDataSource(org.h2.Driver.class.getName(),
             "jdbc:h2:mem:test;MODE=MySQL",
             "sa", "");
         var ddlGenerator = new MysqlDdlGenerator();
@@ -109,7 +109,7 @@ public class DdlHelperTest {
 
     @Test
     void testForH2Postgresql() throws SQLException {
-        var dataSource = new UnpooledDataSource(org.sqlite.JDBC.class.getName(),
+        var dataSource = new UnpooledDataSource(org.h2.Driver.class.getName(),
             "jdbc:h2:mem:test;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE",
             "sa", "");
         var ddlGenerator = new PostgreDdlGenerator();
@@ -117,4 +117,20 @@ public class DdlHelperTest {
             true, DdlScriptErrorHandler.ThrowsErrorHandler.INSTANCE);
     }
 
+    @Test
+    @Disabled
+    void testForDm() throws SQLException {
+        var dataSource = new UnpooledDataSource(dm.jdbc.driver.DmDriver.class.getName(),
+            "jdbc:dm://127.0.0.1:5236/DMSERVER",
+            "SYSDBA", "Dm123456");
+        var ddlGenerator = new OracleDdlGenerator();
+        DdlHelper.runScript(ddlGenerator, dataSource, List.of("ddl/test.sql"),
+            true, DdlScriptErrorHandler.ThrowsErrorHandler.INSTANCE);
+        // 指定模式运行
+        ddlGenerator = new OracleDdlGenerator("TEST1");
+        DdlHelper.runScript(ddlGenerator, dataSource, List.of("ddl/test.sql"),
+            true, DdlScriptErrorHandler.ThrowsErrorHandler.INSTANCE);
+    }
+
+
 }