Search in sources :

Example 51 with TableRecords

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

the class MySQLKeywordCheckerTest method testDeleteKeywordCheck.

/**
 * Test keyword check with DELETE case
 */
@Test
public void testDeleteKeywordCheck() {
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setTableName("`lock`");
    sqlUndoLog.setSqlType(SQLType.DELETE);
    TableRecords afterImage = TableRecords.empty(new UndoExecutorTest.MockTableMeta("product", "key"));
    TableRecords beforeImage = 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);
    beforeImage.add(afterRow1);
    beforeImage.add(afterRow);
    sqlUndoLog.setAfterImage(afterImage);
    sqlUndoLog.setBeforeImage(beforeImage);
    MySQLUndoDeleteExecutorExtension mySQLUndoDeleteExecutor = new MySQLUndoDeleteExecutorExtension(sqlUndoLog);
    Assertions.assertEquals("INSERT INTO `lock` (`desc`, since, `key`) VALUES (?, ?, ?)", mySQLUndoDeleteExecutor.getSql());
}
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 52 with TableRecords

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

the class MySQLKeywordCheckerTest method testUpdateKeywordCheck.

/**
 * Test keyword check with UPDATE case
 */
@Test
public void testUpdateKeywordCheck() {
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setTableName("`lock`");
    sqlUndoLog.setSqlType(SQLType.UPDATE);
    TableRecords beforeImage = new TableRecords(new UndoExecutorTest.MockTableMeta("product", "key"));
    Row beforeRow = new Row();
    Field pkField = new Field();
    pkField.setKeyType(KeyType.PRIMARY_KEY);
    pkField.setName("`key`");
    pkField.setType(Types.INTEGER);
    pkField.setValue(213);
    beforeRow.add(pkField);
    Field name = new Field();
    name.setName("`desc`");
    name.setType(Types.VARCHAR);
    name.setValue("SEATA");
    beforeRow.add(name);
    Field since = new Field();
    since.setName("since");
    since.setType(Types.VARCHAR);
    since.setValue("2014");
    beforeRow.add(since);
    beforeImage.add(beforeRow);
    TableRecords afterImage = new TableRecords(new UndoExecutorTest.MockTableMeta("product", "key"));
    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(afterRow);
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    MySQLUndoUpdateExecutorExtension mySQLUndoUpdateExecutor = new MySQLUndoUpdateExecutorExtension(sqlUndoLog);
    Assertions.assertEquals("UPDATE `lock` SET `desc` = ?, since = ? WHERE `key` = ?", mySQLUndoUpdateExecutor.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)

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