Search in sources :

Example 6 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class MySQLUndoLogManagerTest method testNeedCompress.

@Test
public void testNeedCompress() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    SQLUndoLog smallUndoItem = getUndoLogItem(1);
    BranchUndoLog smallBranchUndoLog = new BranchUndoLog();
    smallBranchUndoLog.setBranchId(1L);
    smallBranchUndoLog.setXid("test_xid");
    smallBranchUndoLog.setSqlUndoLogs(Collections.singletonList(smallUndoItem));
    UndoLogParser parser = UndoLogParserFactory.getInstance();
    byte[] smallUndoLogContent = parser.encode(smallBranchUndoLog);
    Method method = AbstractUndoLogManager.class.getDeclaredMethod("needCompress", byte[].class);
    method.setAccessible(true);
    Assertions.assertFalse((Boolean) method.invoke(undoLogManager, smallUndoLogContent));
    SQLUndoLog hugeUndoItem = getUndoLogItem(10000);
    BranchUndoLog hugeBranchUndoLog = new BranchUndoLog();
    hugeBranchUndoLog.setBranchId(2L);
    hugeBranchUndoLog.setXid("test_xid1");
    hugeBranchUndoLog.setSqlUndoLogs(Collections.singletonList(hugeUndoItem));
    byte[] hugeUndoLogContent = parser.encode(hugeBranchUndoLog);
    Assertions.assertTrue((Boolean) method.invoke(undoLogManager, hugeUndoLogContent));
}
Also used : JacksonUndoLogParser(io.seata.rm.datasource.undo.parser.JacksonUndoLogParser) UndoLogParser(io.seata.rm.datasource.undo.UndoLogParser) BranchUndoLog(io.seata.rm.datasource.undo.BranchUndoLog) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 7 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class MySQLKeywordCheckerTest method testInsertKeywordCheck.

/**
 * Test keyword check with INSERT case
 */
@Test
public void testInsertKeywordCheck() {
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setTableName("`lock`");
    sqlUndoLog.setSqlType(SQLType.INSERT);
    TableRecords beforeImage = TableRecords.empty(new UndoExecutorTest.MockTableMeta("product", "key"));
    TableRecords afterImage = new TableRecords(new UndoExecutorTest.MockTableMeta("product", "key"));
    Row afterRow1 = new Row();
    Field pkField = new Field();
    pkField.setKeyType(KeyType.PRIMARY_KEY);
    pkField.setName("`key`");
    pkField.setType(Types.INTEGER);
    pkField.setValue(213);
    afterRow1.add(pkField);
    Field name = new Field();
    name.setName("`desc`");
    name.setType(Types.VARCHAR);
    name.setValue("SEATA");
    afterRow1.add(name);
    Field since = new Field();
    since.setName("since");
    since.setType(Types.VARCHAR);
    since.setValue("2014");
    afterRow1.add(since);
    Row afterRow = new Row();
    Field pkField1 = new Field();
    pkField1.setKeyType(KeyType.PRIMARY_KEY);
    pkField1.setName("`key`");
    pkField1.setType(Types.INTEGER);
    pkField1.setValue(214);
    afterRow.add(pkField1);
    Field name1 = new Field();
    name1.setName("`desc`");
    name1.setType(Types.VARCHAR);
    name1.setValue("GTS");
    afterRow.add(name1);
    Field since1 = new Field();
    since1.setName("since");
    since1.setType(Types.VARCHAR);
    since1.setValue("2016");
    afterRow.add(since1);
    afterImage.add(afterRow1);
    afterImage.add(afterRow);
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    MySQLUndoInsertExecutorExtension mySQLUndoInsertExecutor = new MySQLUndoInsertExecutorExtension(sqlUndoLog);
    Assertions.assertEquals("DELETE FROM `lock` WHERE `key` = ?", mySQLUndoInsertExecutor.getSql().trim());
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(io.seata.rm.datasource.sql.struct.Field) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) UndoExecutorTest(io.seata.rm.datasource.undo.UndoExecutorTest) Row(io.seata.rm.datasource.sql.struct.Row) UndoExecutorTest(io.seata.rm.datasource.undo.UndoExecutorTest) Test(org.junit.jupiter.api.Test)

Example 8 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class OracleUndoInsertExecutorTest method upperCase.

public OracleUndoInsertExecutor upperCase() {
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "ID" }));
    Mockito.when(tableMeta.getTableName()).thenReturn("TABLE_NAME");
    TableRecords beforeImage = new TableRecords();
    beforeImage.setTableName("TABLE_NAME");
    beforeImage.setTableMeta(tableMeta);
    List<Row> beforeRows = new ArrayList<>();
    Row row0 = new Row();
    addField(row0, "ID", 1, "1");
    addField(row0, "AGE", 1, "1");
    beforeRows.add(row0);
    Row row1 = new Row();
    addField(row1, "ID", 1, "1");
    addField(row1, "AGE", 1, "1");
    beforeRows.add(row1);
    beforeImage.setRows(beforeRows);
    TableRecords afterImage = new TableRecords();
    afterImage.setTableName("TABLE_NAME");
    afterImage.setTableMeta(tableMeta);
    List<Row> afterRows = new ArrayList<>();
    Row row2 = new Row();
    addField(row2, "ID", 1, "1");
    addField(row2, "AGE", 1, "1");
    afterRows.add(row2);
    Row row3 = new Row();
    addField(row3, "ID", 1, "1");
    addField(row3, "AGE", 1, "1");
    afterRows.add(row3);
    afterImage.setRows(afterRows);
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.INSERT);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("TABLE_NAME");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    return new OracleUndoInsertExecutor(sqlUndoLog);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ArrayList(java.util.ArrayList) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row)

Example 9 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class MySQLUndoInsertExecutorTest method init.

@BeforeAll
public static void init() {
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "id" }));
    Mockito.when(tableMeta.getTableName()).thenReturn("table_name");
    TableRecords beforeImage = new TableRecords();
    beforeImage.setTableName("table_name");
    beforeImage.setTableMeta(tableMeta);
    List<Row> beforeRows = new ArrayList<>();
    Row row0 = new Row();
    addField(row0, "id", 1, "12345");
    addField(row0, "age", 1, "1");
    beforeRows.add(row0);
    Row row1 = new Row();
    addField(row1, "id", 1, "12346");
    addField(row1, "age", 1, "1");
    beforeRows.add(row1);
    beforeImage.setRows(beforeRows);
    TableRecords afterImage = new TableRecords();
    afterImage.setTableName("table_name");
    afterImage.setTableMeta(tableMeta);
    List<Row> afterRows = new ArrayList<>();
    Row row2 = new Row();
    addField(row2, "id", 1, "12345");
    addField(row2, "age", 1, "2");
    afterRows.add(row2);
    Row row3 = new Row();
    addField(row3, "id", 1, "12346");
    addField(row3, "age", 1, "2");
    afterRows.add(row3);
    afterImage.setRows(afterRows);
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.UPDATE);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("table_name");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    executor = new MySQLUndoInsertExecutor(sqlUndoLog);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ArrayList(java.util.ArrayList) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 10 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class OracleUndoUpdateExecutorTest method upperCaseSQL.

private OracleUndoUpdateExecutor upperCaseSQL() {
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "ID" }));
    Mockito.when(tableMeta.getTableName()).thenReturn("TABLE_NAME");
    TableRecords beforeImage = new TableRecords();
    beforeImage.setTableName("TABLE_NAME");
    beforeImage.setTableMeta(tableMeta);
    List<Row> beforeRows = new ArrayList<>();
    Row row0 = new Row();
    addField(row0, "ID", 1, "1");
    addField(row0, "AGE", 1, "1");
    beforeRows.add(row0);
    Row row1 = new Row();
    addField(row1, "ID", 1, "1");
    addField(row1, "AGE", 1, "1");
    beforeRows.add(row1);
    beforeImage.setRows(beforeRows);
    TableRecords afterImage = new TableRecords();
    afterImage.setTableName("TABLE_NAME");
    afterImage.setTableMeta(tableMeta);
    List<Row> afterRows = new ArrayList<>();
    Row row2 = new Row();
    addField(row2, "ID", 1, "1");
    addField(row2, "AGE", 1, "1");
    afterRows.add(row2);
    Row row3 = new Row();
    addField(row3, "ID", 1, "1");
    addField(row3, "AGE", 1, "1");
    afterRows.add(row3);
    afterImage.setRows(afterRows);
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.UPDATE);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("TABLE_NAME");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    return new OracleUndoUpdateExecutor(sqlUndoLog);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ArrayList(java.util.ArrayList) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row)

Aggregations

SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)22 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)13 Test (org.junit.jupiter.api.Test)12 Row (io.seata.rm.datasource.sql.struct.Row)11 ArrayList (java.util.ArrayList)9 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)6 Field (io.seata.rm.datasource.sql.struct.Field)4 BeforeAll (org.junit.jupiter.api.BeforeAll)4 UndoExecutorTest (io.seata.rm.datasource.undo.UndoExecutorTest)3 Savepoint (java.sql.Savepoint)3 MockSavepoint (com.alibaba.druid.mock.MockSavepoint)2 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)2 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)2 BranchUndoLog (io.seata.rm.datasource.undo.BranchUndoLog)2 SQLType (io.seata.sqlparser.SQLType)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 Map (java.util.Map)2