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