Search in sources :

Example 1 with Row

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

the class PostgresqlUndoInsertExecutor method buildUndoSQL.

@Override
protected String buildUndoSQL() {
    TableRecords afterImage = sqlUndoLog.getAfterImage();
    List<Row> afterImageRows = afterImage.getRows();
    if (CollectionUtils.isEmpty(afterImageRows)) {
        throw new ShouldNeverHappenException("Invalid UNDO LOG");
    }
    return generateDeleteSql(afterImageRows, afterImage);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) Row(io.seata.rm.datasource.sql.struct.Row)

Example 2 with Row

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

the class DataCompareUtilsTest method testRowListToMapWithMultipPk.

@Test
public void testRowListToMapWithMultipPk() {
    List<String> primaryKeyList = new ArrayList<>();
    primaryKeyList.add("id1");
    primaryKeyList.add("id2");
    List<Row> rows = new ArrayList<>();
    Field field1 = new Field("id1", 1, "1");
    Field field11 = new Field("id2", 1, "2");
    Row row = new Row();
    row.add(field1);
    row.add(field11);
    rows.add(row);
    Field field2 = new Field("id1", 1, "3");
    Field field22 = new Field("id2", 1, "4");
    Row row2 = new Row();
    row2.add(field2);
    row2.add(field22);
    rows.add(row2);
    Field field3 = new Field("id1", 1, "5");
    Field field33 = new Field("id2", 1, "6");
    Row row3 = new Row();
    row3.add(field3);
    row3.add(field33);
    rows.add(row3);
    Map<String, Map<String, Field>> result = DataCompareUtils.rowListToMap(rows, primaryKeyList);
    Assertions.assertTrue(result.size() == 3);
    Assertions.assertEquals(result.keySet().iterator().next(), "1_2");
}
Also used : Field(io.seata.rm.datasource.sql.struct.Field) ArrayList(java.util.ArrayList) Row(io.seata.rm.datasource.sql.struct.Row) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with Row

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

the class MySQLUndoLogManagerTest method getUndoLogItem.

private SQLUndoLog getUndoLogItem(int size) throws NoSuchFieldException, IllegalAccessException {
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setTableName("table_plain_executor_test");
    sqlUndoLog.setSqlType(SQLType.INSERT);
    sqlUndoLog.setTableMeta(tableMeta);
    Field rowsField = TableRecords.class.getDeclaredField("rows");
    rowsField.setAccessible(true);
    List<Row> rows = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        Row row = new Row();
        row.add(new io.seata.rm.datasource.sql.struct.Field("id", 1, "value_id_" + i));
        row.add(new io.seata.rm.datasource.sql.struct.Field("name", 1, "value_name_" + i));
        rows.add(row);
    }
    sqlUndoLog.setAfterImage(TableRecords.empty(tableMeta));
    TableRecords afterImage = new TableRecords(tableMeta);
    rowsField.set(afterImage, rows);
    sqlUndoLog.setAfterImage(afterImage);
    return sqlUndoLog;
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) Row(io.seata.rm.datasource.sql.struct.Row)

Example 4 with Row

use of io.seata.rm.datasource.sql.struct.Row 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 5 with Row

use of io.seata.rm.datasource.sql.struct.Row 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)

Aggregations

Row (io.seata.rm.datasource.sql.struct.Row)33 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)29 Field (io.seata.rm.datasource.sql.struct.Field)22 ArrayList (java.util.ArrayList)18 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)17 Test (org.junit.jupiter.api.Test)15 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)11 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)10 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 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 SqlGenerateUtils (io.seata.rm.datasource.SqlGenerateUtils)3 UndoExecutorTest (io.seata.rm.datasource.undo.UndoExecutorTest)3 Map (java.util.Map)3 BeforeAll (org.junit.jupiter.api.BeforeAll)3 SQLException (java.sql.SQLException)2 BaseUndoLogParserTest (io.seata.rm.datasource.undo.BaseUndoLogParserTest)1