Search in sources :

Example 11 with TableRecords

use of io.seata.rm.datasource.sql.struct.TableRecords in project seata by seata.

the class BaseUndoLogParserTest method testTimestampEncodeAndDecode.

/**
 * will check kryo、jackson、fastjson、protostuff timestamp encode and decode
 */
@Test
public void testTimestampEncodeAndDecode() {
    BranchUndoLog branchUndoLog = new BranchUndoLog();
    branchUndoLog.setXid("192.168.0.1:8091:123456");
    branchUndoLog.setBranchId(123457);
    List<SQLUndoLog> sqlUndoLogs = new ArrayList<>();
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setBeforeImage(TableRecords.empty(new TableMeta()));
    Row row = new Row();
    Field field = new Field();
    field.setName("gmt_create");
    field.setKeyType(KeyType.PRIMARY_KEY);
    field.setType(JDBCType.TIMESTAMP.getVendorTypeNumber());
    Timestamp timestampEncode = new Timestamp(Integer.MAX_VALUE + 1L);
    timestampEncode.setNanos(999999);
    field.setValue(timestampEncode);
    row.add(field);
    TableRecords afterRecords = new TableRecords();
    List<Row> rows = new ArrayList<>();
    rows.add(row);
    afterRecords.setRows(rows);
    afterRecords.setTableMeta(new TableMeta());
    afterRecords.setTableName("test");
    sqlUndoLog.setAfterImage(afterRecords);
    sqlUndoLogs.add(sqlUndoLog);
    branchUndoLog.setSqlUndoLogs(sqlUndoLogs);
    byte[] encode = getParser().encode(branchUndoLog);
    BranchUndoLog decodeBranchLog = getParser().decode(encode);
    Timestamp timestampDecode = (Timestamp) (decodeBranchLog.getSqlUndoLogs().get(0).getAfterImage().getRows().get(0).getFields().get(0).getValue());
    Assertions.assertEquals(timestampEncode, timestampDecode);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(io.seata.rm.datasource.sql.struct.Field) ArrayList(java.util.ArrayList) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row) Timestamp(java.sql.Timestamp) Test(org.junit.jupiter.api.Test)

Example 12 with TableRecords

use of io.seata.rm.datasource.sql.struct.TableRecords in project seata by seata.

the class TestUndoExecutor method dataValidationUpdate.

@Test
public void dataValidationUpdate() throws SQLException {
    execSQL("INSERT INTO table_name(id, name) VALUES (12345,'aaa');");
    execSQL("INSERT INTO table_name(id, name) VALUES (12346,'aaa');");
    TableRecords beforeImage = execQuery(tableMeta, "SELECT * FROM table_name WHERE id IN (12345, 12346);");
    execSQL("update table_name set name = 'xxx' where id in (12345, 12346);");
    TableRecords afterImage = execQuery(tableMeta, "SELECT * FROM table_name WHERE id IN (12345, 12346);");
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.UPDATE);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("table_name");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    TestUndoExecutor spy = new TestUndoExecutor(sqlUndoLog, false);
    // case1: normal case  before:aaa -> after:xxx -> current:xxx
    Assertions.assertTrue(spy.dataValidationAndGoOn(connection));
    // case2: dirty data   before:aaa -> after:xxx -> current:yyy
    execSQL("update table_name set name = 'yyy' where id in (12345, 12346);");
    try {
        spy.dataValidationAndGoOn(connection);
        Assertions.fail();
    } catch (Exception e) {
        Assertions.assertTrue(e instanceof SQLException);
    }
    // case 3: before == current before:aaa -> after:xxx -> current:aaa
    execSQL("update table_name set name = 'aaa' where id in (12345, 12346);");
    Assertions.assertFalse(spy.dataValidationAndGoOn(connection));
    // case 4: before == after   before:aaa -> after:aaa
    afterImage = execQuery(tableMeta, "SELECT * FROM table_name WHERE id IN (12345, 12346);");
    sqlUndoLog.setAfterImage(afterImage);
    Assertions.assertFalse(spy.dataValidationAndGoOn(connection));
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException) Test(org.junit.jupiter.api.Test)

Example 13 with TableRecords

use of io.seata.rm.datasource.sql.struct.TableRecords in project seata by seata.

the class TestUndoExecutor method dataValidationDelete.

@Test
public void dataValidationDelete() throws SQLException {
    execSQL("INSERT INTO table_name(id, name) VALUES (12345,'aaa');");
    execSQL("INSERT INTO table_name(id, name) VALUES (12346,'aaa');");
    TableRecords beforeImage = execQuery(tableMeta, "SELECT * FROM table_name WHERE id IN (12345, 12346);");
    execSQL("delete from table_name where id in (12345, 12346);");
    TableRecords afterImage = execQuery(tableMeta, "SELECT * FROM table_name WHERE id IN (12345, 12346);");
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.INSERT);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("table_name");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    TestUndoExecutor spy = new TestUndoExecutor(sqlUndoLog, true);
    // case1: normal case  before:2 -> after:0 -> current:0
    Assertions.assertTrue(spy.dataValidationAndGoOn(connection));
    // case2: dirty data   before:2 -> after:0 -> current:1
    execSQL("INSERT INTO table_name(id, name) VALUES (12345,'aaa');");
    try {
        Assertions.assertTrue(spy.dataValidationAndGoOn(connection));
        Assertions.fail();
    } catch (Exception e) {
        Assertions.assertTrue(e instanceof SQLException);
    }
    // case3: before == current   before:2 -> after:0 -> current:2
    execSQL("INSERT INTO table_name(id, name) VALUES (12346,'aaa');");
    Assertions.assertFalse(spy.dataValidationAndGoOn(connection));
    // case 4: before == after  before:2 -> after:2
    afterImage = execQuery(tableMeta, "SELECT * FROM table_name WHERE id IN (12345, 12346);");
    sqlUndoLog.setAfterImage(afterImage);
    Assertions.assertFalse(spy.dataValidationAndGoOn(connection));
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException) Test(org.junit.jupiter.api.Test)

Example 14 with TableRecords

use of io.seata.rm.datasource.sql.struct.TableRecords in project seata by seata.

the class BranchUndoLogTest method testEncodeUndoLog.

/**
 * Test encode undo log.
 */
@Test
public void testEncodeUndoLog() {
    BranchUndoLog branchUndoLog = new BranchUndoLog();
    branchUndoLog.setBranchId(641789253L);
    branchUndoLog.setXid("xid:xxx");
    ArrayList<SQLUndoLog> items = new ArrayList<>();
    SQLUndoLog item = new SQLUndoLog();
    item.setSqlType(SQLType.UPDATE);
    TableMeta tableMeta = new TableMeta();
    tableMeta.setTableName("product");
    TableRecords beforeImage = new TableRecords(tableMeta);
    Row rowb = new Row();
    rowb.add(new Field("id", Types.INTEGER, 1));
    rowb.add(new Field("name", Types.VARCHAR, "SEATA"));
    rowb.add(new Field("since", Types.VARCHAR, "2014"));
    beforeImage.add(rowb);
    item.setBeforeImage(beforeImage);
    TableRecords afterImage = new TableRecords(tableMeta);
    Row rowa = new Row();
    rowa.add(new Field("id", Types.INTEGER, 1));
    rowa.add(new Field("name", Types.VARCHAR, "SEATA_IO"));
    rowa.add(new Field("since", Types.VARCHAR, "2014"));
    afterImage.add(rowa);
    item.setAfterImage(afterImage);
    items.add(item);
    branchUndoLog.setSqlUndoLogs(items);
    byte[] bs = UndoLogParserFactory.getInstance().encode(branchUndoLog);
    BranchUndoLog decodeObj = UndoLogParserFactory.getInstance().decode(bs);
    Assertions.assertEquals(decodeObj.getBranchId(), branchUndoLog.getBranchId());
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(io.seata.rm.datasource.sql.struct.Field) ArrayList(java.util.ArrayList) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row) Test(org.junit.jupiter.api.Test)

Example 15 with TableRecords

use of io.seata.rm.datasource.sql.struct.TableRecords in project seata by seata.

the class BaseInsertExecutor method afterImage.

@Override
protected TableRecords afterImage(TableRecords beforeImage) throws SQLException {
    Map<String, List<Object>> pkValues = getPkValues();
    TableRecords afterImage = buildTableRecords(pkValues);
    if (afterImage == null) {
        throw new SQLException("Failed to build after-image for insert");
    }
    return afterImage;
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)52 Row (io.seata.rm.datasource.sql.struct.Row)29 Test (org.junit.jupiter.api.Test)25 ArrayList (java.util.ArrayList)23 Field (io.seata.rm.datasource.sql.struct.Field)21 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)19 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)14 List (java.util.List)13 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)12 SQLException (java.sql.SQLException)9 Collectors (java.util.stream.Collectors)7 CollectionUtils (io.seata.common.util.CollectionUtils)6 ColumnUtils (io.seata.rm.datasource.ColumnUtils)6 AbstractUndoExecutor (io.seata.rm.datasource.undo.AbstractUndoExecutor)6 JdbcConstants (io.seata.sqlparser.util.JdbcConstants)6 SQLRecognizer (io.seata.sqlparser.SQLRecognizer)4 BeforeAll (org.junit.jupiter.api.BeforeAll)4 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)3 SqlGenerateUtils (io.seata.rm.datasource.SqlGenerateUtils)3 UndoExecutorTest (io.seata.rm.datasource.undo.UndoExecutorTest)3