Search in sources :

Example 6 with TableMeta

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

the class MysqlTableMetaCacheTest method getTableMetaTest_0.

/**
 * The table meta fetch test.
 */
@Test
public void getTableMetaTest_0() throws SQLException {
    MockDriver mockDriver = new MockDriver(columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy proxy = new DataSourceProxy(dataSource);
    TableMeta tableMeta = getTableMetaCache().getTableMeta(proxy.getPlainConnection(), "mt1", proxy.getResourceId());
    Assertions.assertEquals("mt1", tableMeta.getTableName());
    Assertions.assertEquals("id", tableMeta.getPrimaryKeyOnlyName().get(0));
    Assertions.assertEquals("id", tableMeta.getColumnMeta("id").getColumnName());
    Assertions.assertEquals("id", tableMeta.getAutoIncreaseColumn().getColumnName());
    Assertions.assertEquals(1, tableMeta.getPrimaryKeyMap().size());
    Assertions.assertEquals(Collections.singletonList("id"), tableMeta.getPrimaryKeyOnlyName());
    Assertions.assertEquals(columnMetas.length, tableMeta.getAllColumns().size());
    assertColumnMetaEquals(columnMetas[0], tableMeta.getAllColumns().get("id"));
    assertColumnMetaEquals(columnMetas[1], tableMeta.getAllColumns().get("name1"));
    assertColumnMetaEquals(columnMetas[2], tableMeta.getAllColumns().get("name2"));
    assertColumnMetaEquals(columnMetas[3], tableMeta.getAllColumns().get("name3"));
    Assertions.assertEquals(indexMetas.length, tableMeta.getAllIndexes().size());
    assertIndexMetaEquals(indexMetas[0], tableMeta.getAllIndexes().get("PRIMARY"));
    Assertions.assertEquals(IndexType.PRIMARY, tableMeta.getAllIndexes().get("PRIMARY").getIndextype());
    assertIndexMetaEquals(indexMetas[1], tableMeta.getAllIndexes().get("name1"));
    Assertions.assertEquals(IndexType.UNIQUE, tableMeta.getAllIndexes().get("name1").getIndextype());
    indexMetas = new Object[][] {};
    mockDriver.setMockIndexMetasReturnValue(indexMetas);
    Assertions.assertThrows(ShouldNeverHappenException.class, () -> {
        getTableMetaCache().getTableMeta(proxy.getPlainConnection(), "mt2", proxy.getResourceId());
    });
    mockDriver.setMockColumnsMetasReturnValue(null);
    Assertions.assertThrows(ShouldNeverHappenException.class, () -> {
        getTableMetaCache().getTableMeta(proxy.getPlainConnection(), "mt2", proxy.getResourceId());
    });
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) Test(org.junit.jupiter.api.Test)

Example 7 with TableMeta

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

the class MySQLUndoInsertExecutorTest 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 MySQLUndoInsertExecutor(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 8 with TableMeta

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

the class OracleUndoUpdateExecutorTest method upperCaseSQL.

private OracleUndoUpdateExecutor upperCaseSQL() {
    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.UPDATE);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("TABLE_NAME");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    return new OracleUndoUpdateExecutor(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 9 with TableMeta

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

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

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