diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java index e8e3b424d91..f816bc0caa4 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java @@ -25,6 +25,9 @@ import org.apache.seata.rm.datasource.util.JdbcUtils; import org.apache.seata.sqlparser.util.JdbcConstants; +/** + * datasource proxy metadata factory + */ public class SeataDataSourceProxyMetadataFactory { public static SeataDataSourceProxyMetadata create(DataSource dataSource) throws SQLException { @@ -32,7 +35,9 @@ public static SeataDataSourceProxyMetadata create(DataSource dataSource) throws try (Connection connection = dataSource.getConnection()) { String jdbcUrl = connection.getMetaData().getURL(); String dbType = JdbcUtils.getDbType(jdbcUrl); - if (JdbcConstants.MYSQL.equals(dbType)) { + if (JdbcConstants.MYSQL.equals(dbType) + || JdbcConstants.MARIADB.equals(dbType) + || JdbcConstants.POLARDBX.equals(dbType)) { dataSourceProxyMetadata = new MySQLDataSourceProxyMetadata(); } else { dataSourceProxyMetadata = new DefaultDataSourceProxyMetadata(); diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MariadbInsertExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MariadbInsertExecutorTest.java index 51b5336af15..3d9522c4a05 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MariadbInsertExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MariadbInsertExecutorTest.java @@ -19,7 +19,6 @@ import java.lang.reflect.Field; import java.sql.SQLException; import java.sql.Types; -import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -32,19 +31,16 @@ import org.apache.seata.rm.datasource.DataSourceProxyTest; import org.apache.seata.rm.datasource.PreparedStatementProxy; import org.apache.seata.rm.datasource.StatementProxy; -import org.apache.seata.rm.datasource.exec.StatementCallback; import org.apache.seata.rm.datasource.exec.mysql.MySQLInsertExecutor; import org.apache.seata.rm.datasource.mock.MockDriver; import org.apache.seata.rm.datasource.mock.MockMariadbDataSource; -import org.apache.seata.rm.datasource.mock.MockResultSet; -import org.apache.seata.sqlparser.struct.TableMeta; import org.apache.seata.sqlparser.SQLInsertRecognizer; +import org.apache.seata.sqlparser.struct.TableMeta; import org.apache.seata.sqlparser.util.JdbcConstants; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.mockito.Mockito; - import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -63,10 +59,6 @@ public void init() throws SQLException { when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy); when(statementProxy.getTargetStatement()).thenReturn(statementProxy); - MockResultSet resultSet = new MockResultSet(statementProxy); - resultSet.mockResultSet(Arrays.asList("Variable_name", "Value"), new Object[][]{{"auto_increment_increment", "1"}}); - when(statementProxy.getTargetStatement().executeQuery("SHOW VARIABLES LIKE 'auto_increment_increment'")).thenReturn(resultSet); - StatementCallback statementCallback = mock(StatementCallback.class); sqlInsertRecognizer = mock(SQLInsertRecognizer.class); tableMeta = mock(TableMeta.class); diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java index 1bb83a30379..0e0d6a57446 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java @@ -19,7 +19,6 @@ import java.lang.reflect.Field; import java.sql.SQLException; import java.sql.Types; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Properties; @@ -33,10 +32,9 @@ import org.apache.seata.rm.datasource.DataSourceProxyTest; import org.apache.seata.rm.datasource.PreparedStatementProxy; import org.apache.seata.rm.datasource.StatementProxy; -import org.apache.seata.rm.datasource.exec.StatementCallback; import org.apache.seata.rm.datasource.exec.polardbx.PolarDBXInsertExecutor; +import org.apache.seata.rm.datasource.metadata.MySQLDataSourceProxyMetadata; import org.apache.seata.rm.datasource.mock.MockDriver; -import org.apache.seata.rm.datasource.mock.MockResultSet; import org.apache.seata.sqlparser.SQLInsertRecognizer; import org.apache.seata.sqlparser.struct.TableMeta; import org.apache.seata.sqlparser.util.JdbcConstants; @@ -48,7 +46,6 @@ /** * Insert executor test for PolarDB-X - * */ public class PolarDBXInsertExecutorTest extends MySQLInsertExecutorTest { @BeforeEach @@ -60,6 +57,9 @@ public void init() throws SQLException { DataSourceProxy dataSourceProxy = mock(DataSourceProxy.class); when(dataSourceProxy.getResourceId()).thenReturn("jdbc:mysql://127.0.0.1:3306/seata"); when(dataSourceProxy.getDbType()).thenReturn(JdbcConstants.POLARDBX); + MySQLDataSourceProxyMetadata dataSourceProxyMetadata = Mockito.mock(MySQLDataSourceProxyMetadata.class); + when(dataSourceProxyMetadata.getVariableValue("auto_increment_increment")).thenReturn("1"); + when(dataSourceProxy.getDataSourceProxyMetadata()).thenReturn(dataSourceProxyMetadata); when(connectionProxy.getDataSourceProxy()).thenReturn(dataSourceProxy); @@ -67,10 +67,6 @@ public void init() throws SQLException { when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy); when(statementProxy.getTargetStatement()).thenReturn(statementProxy); - MockResultSet resultSet = new MockResultSet(statementProxy); - resultSet.mockResultSet(Arrays.asList("Variable_name", "Value"), new Object[][]{{"auto_increment_increment", "1"}}); - when(statementProxy.getTargetStatement().executeQuery("SHOW VARIABLES LIKE 'auto_increment_increment'")).thenReturn(resultSet); - StatementCallback statementCallback = mock(StatementCallback.class); sqlInsertRecognizer = mock(SQLInsertRecognizer.class); tableMeta = mock(TableMeta.class); @@ -84,21 +80,21 @@ public void init() throws SQLException { // new test init property List returnValueColumnLabels = Lists.newArrayList("id", "user_id", "name", "sex", "update_time"); - Object[][] returnValue = new Object[][] { - new Object[] {1, 1, "will", 1, 0}, + Object[][] returnValue = new Object[][]{ + new Object[]{1, 1, "will", 1, 0}, }; - Object[][] columnMetas = new Object[][] { - new Object[] {"", "", "table_insert_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "user_id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "sex", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "update_time", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO"}, + Object[][] columnMetas = new Object[][]{ + new Object[]{"", "", "table_insert_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "user_id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "sex", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "update_time", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO"}, }; - Object[][] indexMetas = new Object[][] { - new Object[] {"PRIMARY", "id", false, "", 3, 1, "A", 34}, - new Object[] {"PRIMARY", "user_id", false, "", 3, 1, "A", 34}, + Object[][] indexMetas = new Object[][]{ + new Object[]{"PRIMARY", "id", false, "", 3, 1, "A", 34}, + new Object[]{"PRIMARY", "user_id", false, "", 3, 1, "A", 34}, }; - Object[][] onUpdateColumnsReturnValue = new Object[][] { + Object[][] onUpdateColumnsReturnValue = new Object[][]{ new Object[]{0, "update_time", Types.INTEGER, "INTEGER", 64, 10, 0, 0} };