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());
}
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);
}
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);
}
}
}
}
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);
}
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);
}
};
}
Aggregations