Search in sources :

Example 21 with TableMeta

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

the class TestUndoExecutor method testParsePK.

@Test
public void testParsePK() {
    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();
    Field field01 = addField(row0, "id", 1, "12345");
    Field field02 = addField(row0, "age", 1, "2");
    beforeRows.add(row0);
    Row row1 = new Row();
    Field field11 = addField(row1, "id", 1, "12346");
    Field field12 = addField(row1, "age", 1, "2");
    beforeRows.add(row1);
    beforeImage.setRows(beforeRows);
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.UPDATE);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("table_name");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(null);
    TestUndoExecutor executor = new TestUndoExecutor(sqlUndoLog, true);
    Map<String, List<Field>> pkValues = executor.parsePkValues(beforeImage);
    Assertions.assertEquals(2, pkValues.get("id").size());
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(io.seata.rm.datasource.sql.struct.Field) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row) Test(org.junit.jupiter.api.Test)

Example 22 with TableMeta

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

the class BaseH2Test method mockTableMeta.

protected static TableMeta mockTableMeta() {
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "ID" }));
    Mockito.when(tableMeta.getEscapePkNameList("h2")).thenReturn(Arrays.asList(new String[] { "ID" }));
    Mockito.when(tableMeta.getTableName()).thenReturn("table_name");
    ColumnMeta meta0 = Mockito.mock(ColumnMeta.class);
    Mockito.when(meta0.getDataType()).thenReturn(Types.INTEGER);
    Mockito.when(meta0.getColumnName()).thenReturn("ID");
    Mockito.when(tableMeta.getColumnMeta("ID")).thenReturn(meta0);
    ColumnMeta meta1 = Mockito.mock(ColumnMeta.class);
    Mockito.when(meta1.getDataType()).thenReturn(Types.VARCHAR);
    Mockito.when(meta1.getColumnName()).thenReturn("NAME");
    Mockito.when(tableMeta.getColumnMeta("NAME")).thenReturn(meta1);
    return tableMeta;
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta)

Example 23 with TableMeta

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

the class MySQLUndoUpdateExecutorTest 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 MySQLUndoUpdateExecutor(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 24 with TableMeta

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

the class OracleUndoDeleteExecutorTest method upperCase.

private OracleUndoDeleteExecutor 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, "2");
    afterRows.add(row2);
    Row row3 = new Row();
    addField(row3, "ID", 1, "1");
    addField(row3, "AGE", 1, "2");
    afterRows.add(row3);
    afterImage.setRows(afterRows);
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.DELETE);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("TABLE_NAME");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    return new OracleUndoDeleteExecutor(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 25 with TableMeta

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

the class PostgresqlTableMetaCacheTest method getTableMetaTest.

@Test
public void getTableMetaTest() throws SQLException {
    MockDriver mockDriver = new MockDriver(columnMetas, indexMetas, pkMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy proxy = new DataSourceProxy(dataSource);
    TableMetaCache tableMetaCache = TableMetaCacheFactory.getTableMetaCache(JdbcConstants.POSTGRESQL);
    TableMeta tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "pt1", proxy.getResourceId());
    Assertions.assertNotNull(tableMeta);
    tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "t.pt1", proxy.getResourceId());
    Assertions.assertNotNull(tableMeta);
    tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "t.\"pt1\"", proxy.getResourceId());
    Assertions.assertNotNull(tableMeta);
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) TableMetaCache(io.seata.rm.datasource.sql.struct.TableMetaCache) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) Test(org.junit.jupiter.api.Test)

Aggregations

TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)38 ArrayList (java.util.ArrayList)15 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)14 Test (org.junit.jupiter.api.Test)12 Row (io.seata.rm.datasource.sql.struct.Row)11 Field (io.seata.rm.datasource.sql.struct.Field)8 ResultSet (java.sql.ResultSet)7 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)6 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)6 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)5 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)5 PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)5 MockDriver (io.seata.rm.datasource.mock.MockDriver)5 SQLInsertRecognizer (io.seata.sqlparser.SQLInsertRecognizer)5 PreparedStatement (java.sql.PreparedStatement)5 List (java.util.List)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)4 ColumnMeta (io.seata.rm.datasource.sql.struct.ColumnMeta)4 MySQLInsertExecutor (io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor)3