Search in sources :

Example 6 with StatementProxy

use of io.seata.rm.datasource.StatementProxy in project seata by seata.

the class UpdateExecutorTest method testBeforeImage.

@Test
public void testBeforeImage() throws SQLException {
    Assertions.assertNotNull(updateExecutor.beforeImage());
    String sql = "update table_update_executor_test set name = 'WILL' where id = 1";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLUpdateRecognizer recognizer = new MySQLUpdateRecognizer(sql, asts.get(0));
    updateExecutor = new UpdateExecutor(statementProxy, (statement, args) -> null, recognizer);
    Assertions.assertNotNull(updateExecutor.beforeImage());
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) StatementProxy(io.seata.rm.datasource.StatementProxy) Field(java.lang.reflect.Field) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Test(org.junit.jupiter.api.Test) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) SQLException(java.sql.SQLException) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 7 with StatementProxy

use of io.seata.rm.datasource.StatementProxy in project seata by seata.

the class DeleteExecutorTest method init.

@BeforeAll
public static void init() {
    List<String> returnValueColumnLabels = Lists.newArrayList("id", "name");
    Object[][] returnValue = new Object[][] { new Object[] { 1, "Tom" }, new Object[] { 2, "Jack" } };
    Object[][] columnMetas = new Object[][] { new Object[] { "", "", "table_delete_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_delete_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" } };
    Object[][] indexMetas = new Object[][] { new Object[] { "PRIMARY", "id", false, "", 3, 1, "A", 34 } };
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource);
    try {
        Field field = dataSourceProxy.getClass().getDeclaredField("dbType");
        field.setAccessible(true);
        field.set(dataSourceProxy, "mysql");
        ConnectionProxy connectionProxy = new ConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
        MockStatementBase mockStatement = new MockStatement(dataSource.getConnection().getConnection());
        statementProxy = new StatementProxy(connectionProxy, mockStatement);
    } catch (Exception e) {
        throw new RuntimeException("init failed");
    }
    String sql = "delete from t where id = 1";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLDeleteRecognizer recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
    deleteExecutor = new DeleteExecutor(statementProxy, (statement, args) -> {
        return null;
    }, recognizer);
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) StatementProxy(io.seata.rm.datasource.StatementProxy) MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) Field(java.lang.reflect.Field) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Test(org.junit.jupiter.api.Test) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) SQLException(java.sql.SQLException) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLException(java.sql.SQLException) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) Field(java.lang.reflect.Field) StatementProxy(io.seata.rm.datasource.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 8 with StatementProxy

use of io.seata.rm.datasource.StatementProxy in project seata by seata.

the class DeleteExecutorTest method testBeforeImage.

@Test
public void testBeforeImage() throws SQLException {
    Assertions.assertNotNull(deleteExecutor.beforeImage());
    String sql = "delete from t";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLDeleteRecognizer recognizer = new MySQLDeleteRecognizer(sql, asts.get(0));
    deleteExecutor = new DeleteExecutor(statementProxy, (statement, args) -> null, recognizer);
    Assertions.assertNotNull(deleteExecutor.beforeImage());
}
Also used : MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) StatementProxy(io.seata.rm.datasource.StatementProxy) MySQLDeleteRecognizer(io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer) Field(java.lang.reflect.Field) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Test(org.junit.jupiter.api.Test) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) SQLException(java.sql.SQLException) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 9 with StatementProxy

use of io.seata.rm.datasource.StatementProxy in project seata by seata.

the class OracleInsertExecutorTest method testStatement_pkValueByAuto_NotSupportYetException.

@Test
public void testStatement_pkValueByAuto_NotSupportYetException() throws Exception {
    mockInsertColumns();
    mockStatementInsertRows();
    statementProxy = mock(StatementProxy.class);
    when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
    when(connectionProxy.getDbType()).thenReturn(JdbcConstants.ORACLE);
    insertExecutor = Mockito.spy(new OracleInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
    doReturn(tableMeta).when(insertExecutor).getTableMeta();
    Map<String, ColumnMeta> map = new HashMap<>();
    map.put(ID_COLUMN, mock(ColumnMeta.class));
    doReturn(map).when(tableMeta).getPrimaryKeyMap();
    ResultSet rs = mock(ResultSet.class);
    doReturn(rs).when(statementProxy).getGeneratedKeys();
    doReturn(false).when(rs).next();
    Assertions.assertThrows(NotSupportYetException.class, () -> {
        insertExecutor.getGeneratedKeys();
    });
    doReturn(pkIndexMap).when(insertExecutor).getPkIndex();
    Assertions.assertThrows(NotSupportYetException.class, () -> {
        insertExecutor.getPkValuesByColumn();
    });
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) HashMap(java.util.HashMap) OracleInsertExecutor(io.seata.rm.datasource.exec.oracle.OracleInsertExecutor) StatementProxy(io.seata.rm.datasource.StatementProxy) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) ResultSet(java.sql.ResultSet) Test(org.junit.jupiter.api.Test)

Example 10 with StatementProxy

use of io.seata.rm.datasource.StatementProxy in project seata by seata.

the class PlainExecutorTest method init.

@BeforeEach
public void init() throws SQLException {
    List<String> returnValueColumnLabels = Lists.newArrayList("id", "name");
    Object[][] returnValue = new Object[][] { new Object[] { 1, "Tom" }, new Object[] { 2, "Jack" } };
    Object[][] columnMetas = new Object[][] { new Object[] { "", "", "table_plain_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_plain_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" } };
    Object[][] indexMetas = new Object[][] { new Object[] { "PRIMARY", "id", false, "", 3, 1, "A", 34 } };
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource);
    ConnectionProxy connectionProxy = new ConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
    MockStatementBase mockStatement = new MockStatement(dataSource.getConnection().getConnection());
    StatementProxy statementProxy = new StatementProxy(connectionProxy, mockStatement);
    plainExecutor = new PlainExecutor(statementProxy, (statement, args) -> null);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) MockDriver(io.seata.rm.datasource.mock.MockDriver) StatementProxy(io.seata.rm.datasource.StatementProxy) Test(org.junit.jupiter.api.Test) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) SQLException(java.sql.SQLException) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) Types(java.sql.Types) MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) StatementProxy(io.seata.rm.datasource.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

StatementProxy (io.seata.rm.datasource.StatementProxy)12 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)10 Test (org.junit.jupiter.api.Test)10 MockStatement (com.alibaba.druid.mock.MockStatement)9 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)9 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)9 MockDriver (io.seata.rm.datasource.mock.MockDriver)9 Lists (com.google.common.collect.Lists)8 Field (java.lang.reflect.Field)8 SQLException (java.sql.SQLException)8 Types (java.sql.Types)8 List (java.util.List)8 BeforeAll (org.junit.jupiter.api.BeforeAll)8 MockStatementBase (com.alibaba.druid.mock.MockStatementBase)7 SQLUtils (com.alibaba.druid.sql.SQLUtils)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)7 JdbcConstants (com.alibaba.druid.util.JdbcConstants)7 Assertions (org.junit.jupiter.api.Assertions)7 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)5 NotSupportYetException (io.seata.common.exception.NotSupportYetException)2