Search in sources :

Example 16 with TableMeta

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

the class BaseTransactionalExecutorTest method testBuildLockKey.

@Test
public void testBuildLockKey() {
    // build expect data
    String tableName = "test_name";
    String fieldOne = "1";
    String fieldTwo = "2";
    String split1 = ":";
    String split2 = ",";
    String pkColumnName = "id";
    // test_name:1,2
    String buildLockKeyExpect = tableName + split1 + fieldOne + split2 + fieldTwo;
    // mock field
    Field field1 = mock(Field.class);
    when(field1.getValue()).thenReturn(fieldOne);
    Field field2 = mock(Field.class);
    when(field2.getValue()).thenReturn(fieldTwo);
    List<Map<String, Field>> pkRows = new ArrayList<>();
    pkRows.add(Collections.singletonMap(pkColumnName, field1));
    pkRows.add(Collections.singletonMap(pkColumnName, field2));
    // mock tableMeta
    TableMeta tableMeta = mock(TableMeta.class);
    when(tableMeta.getTableName()).thenReturn(tableName);
    when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { pkColumnName }));
    // mock tableRecords
    TableRecords tableRecords = mock(TableRecords.class);
    when(tableRecords.getTableMeta()).thenReturn(tableMeta);
    when(tableRecords.size()).thenReturn(pkRows.size());
    when(tableRecords.pkRows()).thenReturn(pkRows);
    // mock executor
    BaseTransactionalExecutor executor = mock(BaseTransactionalExecutor.class);
    when(executor.buildLockKey(tableRecords)).thenCallRealMethod();
    when(executor.getTableMeta()).thenReturn(tableMeta);
    assertThat(executor.buildLockKey(tableRecords)).isEqualTo(buildLockKeyExpect);
}
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) Test(org.junit.jupiter.api.Test)

Example 17 with TableMeta

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

the class BaseTransactionalExecutorTest method testBuildLockKeyWithMultiPk.

@Test
public void testBuildLockKeyWithMultiPk() {
    // build expect data
    String tableName = "test_name";
    String pkOneValue1 = "1";
    String pkOneValue2 = "2";
    String pkTwoValue1 = "one";
    String pkTwoValue2 = "two";
    String split1 = ":";
    String split2 = ",";
    String split3 = "_";
    String pkOneColumnName = "id";
    String pkTwoColumnName = "userId";
    // test_name:1_one,2_two
    String buildLockKeyExpect = tableName + split1 + pkOneValue1 + split3 + pkTwoValue1 + split2 + pkOneValue2 + split3 + pkTwoValue2;
    // mock field
    Field pkOneField1 = mock(Field.class);
    when(pkOneField1.getValue()).thenReturn(pkOneValue1);
    Field pkOneField2 = mock(Field.class);
    when(pkOneField2.getValue()).thenReturn(pkOneValue2);
    Field pkTwoField1 = mock(Field.class);
    when(pkTwoField1.getValue()).thenReturn(pkTwoValue1);
    Field pkTwoField2 = mock(Field.class);
    when(pkTwoField2.getValue()).thenReturn(pkTwoValue2);
    List<Map<String, Field>> pkRows = new ArrayList<>();
    Map<String, Field> row1 = new HashMap<String, Field>() {

        {
            put(pkOneColumnName, pkOneField1);
            put(pkTwoColumnName, pkTwoField1);
        }
    };
    pkRows.add(row1);
    Map<String, Field> row2 = new HashMap<String, Field>() {

        {
            put(pkOneColumnName, pkOneField2);
            put(pkTwoColumnName, pkTwoField2);
        }
    };
    pkRows.add(row2);
    // mock tableMeta
    TableMeta tableMeta = mock(TableMeta.class);
    when(tableMeta.getTableName()).thenReturn(tableName);
    when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { pkOneColumnName, pkTwoColumnName }));
    // mock tableRecords
    TableRecords tableRecords = mock(TableRecords.class);
    when(tableRecords.getTableMeta()).thenReturn(tableMeta);
    when(tableRecords.size()).thenReturn(pkRows.size());
    when(tableRecords.pkRows()).thenReturn(pkRows);
    // mock executor
    BaseTransactionalExecutor executor = mock(BaseTransactionalExecutor.class);
    when(executor.buildLockKey(tableRecords)).thenCallRealMethod();
    when(executor.getTableMeta()).thenReturn(tableMeta);
    assertThat(executor.buildLockKey(tableRecords)).isEqualTo(buildLockKeyExpect);
}
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) Test(org.junit.jupiter.api.Test)

Example 18 with TableMeta

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

the class PostgresqlInsertExecutorTest method init.

@BeforeEach
public void init() {
    ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
    when(connectionProxy.getDbType()).thenReturn(JdbcConstants.POSTGRESQL);
    statementProxy = mock(PreparedStatementProxy.class);
    when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
    StatementCallback statementCallback = mock(StatementCallback.class);
    sqlInsertRecognizer = mock(SQLInsertRecognizer.class);
    tableMeta = mock(TableMeta.class);
    insertExecutor = Mockito.spy(new PostgresqlInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
    pkIndexMap = new HashMap<String, Integer>() {

        {
            put(ID_COLUMN, pkIndex);
        }
    };
}
Also used : PostgresqlInsertExecutor(io.seata.rm.datasource.exec.postgresql.PostgresqlInsertExecutor) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 19 with TableMeta

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

the class MySQLInsertExecutorTest method init.

@BeforeEach
public void init() throws SQLException {
    ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
    when(connectionProxy.getDbType()).thenReturn(JdbcConstants.MYSQL);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(new MockDataSource());
    when(connectionProxy.getDataSourceProxy()).thenReturn(dataSourceProxy);
    statementProxy = mock(PreparedStatementProxy.class);
    when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
    when(statementProxy.getTargetStatement()).thenReturn(statementProxy);
    MockResultSet resultSet = new MockResultSet(statementProxy);
    resultSet.mockResultSet(Arrays.asList("Variable_name", "Value"), new Object[][] { { "auto_increment_increment", "1" } });
    when(statementProxy.getTargetStatement().executeQuery("SHOW VARIABLES LIKE 'auto_increment_increment'")).thenReturn(resultSet);
    StatementCallback statementCallback = mock(StatementCallback.class);
    sqlInsertRecognizer = mock(SQLInsertRecognizer.class);
    tableMeta = mock(TableMeta.class);
    insertExecutor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
    pkIndexMap = new HashMap<String, Integer>() {

        {
            put(ID_COLUMN, pkIndex);
        }
    };
}
Also used : DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) MySQLInsertExecutor(io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor) MockDataSource(io.seata.rm.datasource.mock.MockDataSource) MockResultSet(io.seata.rm.datasource.mock.MockResultSet) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Mockito.anyString(org.mockito.Mockito.anyString) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 20 with TableMeta

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

the class OracleTableMetaCacheTest 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.ORACLE);
    TableMeta tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "t.ot1", proxy.getResourceId());
    Assertions.assertNotNull(tableMeta);
    tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "t.\"ot1\"", 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