Search in sources :

Example 6 with DataSourceProxy

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

the class TableRecordsTest method testPkRow.

@Test
public void testPkRow() throws SQLException {
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    MockStatementBase mockStatement = new MockStatement(dataSource.getConnection().getConnection());
    DataSourceProxy proxy = new DataSourceProxy(dataSource);
    TableMeta tableMeta = TableMetaCacheFactory.getTableMetaCache(JdbcConstants.MYSQL).getTableMeta(proxy.getPlainConnection(), "table_records_test", proxy.getResourceId());
    ResultSet resultSet = mockDriver.executeQuery(mockStatement, "select * from table_records_test");
    TableRecords tableRecords = TableRecords.buildRecords(tableMeta, resultSet);
    Assertions.assertEquals(returnValue.length, tableRecords.pkRows().size());
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) ResultSet(java.sql.ResultSet) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) MockStatementBase(com.alibaba.druid.mock.MockStatementBase) Test(org.junit.jupiter.api.Test)

Example 7 with DataSourceProxy

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

the class UpdateExecutorTest 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_update_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_update_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 = "update table_update_executor_test set name = 'WILL'";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLUpdateRecognizer recognizer = new MySQLUpdateRecognizer(sql, asts.get(0));
    updateExecutor = new UpdateExecutor(statementProxy, (statement, args) -> {
        return null;
    }, recognizer);
}
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) 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) MySQLUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 8 with DataSourceProxy

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

the class RMHandlerAT method handle.

@Override
public void handle(UndoLogDeleteRequest request) {
    DataSourceManager dataSourceManager = (DataSourceManager) getResourceManager();
    DataSourceProxy dataSourceProxy = dataSourceManager.get(request.getResourceId());
    if (dataSourceProxy == null) {
        LOGGER.warn("Failed to get dataSourceProxy for delete undolog on {}", request.getResourceId());
        return;
    }
    Date logCreatedSave = getLogCreated(request.getSaveDays());
    Connection conn = null;
    try {
        conn = dataSourceProxy.getPlainConnection();
        int deleteRows = 0;
        do {
            try {
                deleteRows = UndoLogManagerFactory.getUndoLogManager(dataSourceProxy.getDbType()).deleteUndoLogByLogCreated(logCreatedSave, LIMIT_ROWS, conn);
                if (deleteRows > 0 && !conn.getAutoCommit()) {
                    conn.commit();
                }
            } catch (SQLException exx) {
                if (deleteRows > 0 && !conn.getAutoCommit()) {
                    conn.rollback();
                }
                throw exx;
            }
        } while (deleteRows == LIMIT_ROWS);
    } catch (Exception e) {
        LOGGER.error("Failed to delete expired undo_log, error:{}", e.getMessage(), e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException closeEx) {
                LOGGER.warn("Failed to close JDBC resource while deleting undo_log ", closeEx);
            }
        }
    }
}
Also used : DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DataSourceManager(io.seata.rm.datasource.DataSourceManager) Date(java.util.Date) SQLException(java.sql.SQLException)

Example 9 with DataSourceProxy

use of io.seata.rm.datasource.DataSourceProxy 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 10 with DataSourceProxy

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

the class MySQLInsertExecutorTest method init.

@BeforeEach
public void init() throws SQLException {
    ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
    when(connectionProxy.getDbType()).thenReturn(JdbcConstants.MYSQL);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(new MockDataSource());
    when(connectionProxy.getDataSourceProxy()).thenReturn(dataSourceProxy);
    statementProxy = mock(PreparedStatementProxy.class);
    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);
    insertExecutor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
    pkIndexMap = new HashMap<String, Integer>() {

        {
            put(ID_COLUMN, pkIndex);
        }
    };
}
Also used : DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) MySQLInsertExecutor(io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor) MockDataSource(io.seata.rm.datasource.mock.MockDataSource) MockResultSet(io.seata.rm.datasource.mock.MockResultSet) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Mockito.anyString(org.mockito.Mockito.anyString) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

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