Search in sources :

Example 1 with MySQLSelectForUpdateRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer in project seata by seata.

the class SelectForUpdateExecutorTest method init.

@BeforeAll
public static void init() {
    RootContext.unbind();
    List<String> returnValueColumnLabels = Lists.newArrayList("id", "name");
    Object[][] returnValue = new Object[][] { new Object[] { 1, "Tom" }, new Object[] { 2, "Jack" } };
    Object[][] columnMetas = new Object[][] { new Object[] { "", "", "table_select_for_update_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "table_select_for_update_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" } };
    Object[][] indexMetas = new Object[][] { new Object[] { "PRIMARY", "id", false, "", 3, 1, "A", 34 } };
    MockDriver mockDriver = new MockDriver(returnValueColumnLabels, returnValue, columnMetas, indexMetas);
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl("jdbc:mock:xxx");
    dataSource.setDriver(mockDriver);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource);
    try {
        Field field = dataSourceProxy.getClass().getDeclaredField("dbType");
        field.setAccessible(true);
        field.set(dataSourceProxy, "mysql");
        connectionProxy = new MockConnectionProxy(dataSourceProxy, dataSource.getConnection().getConnection());
        connectionProxy.bind("xid");
        MockStatement mockStatement = new MockStatement(dataSource.getConnection().getConnection());
        statementProxy = new StatementProxy(connectionProxy, mockStatement);
    } catch (Exception e) {
        throw new RuntimeException("init failed");
    }
    String sql = "select * from table_select_for_update_executor_test where id = 1";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLSelectForUpdateRecognizer recognizer = new MySQLSelectForUpdateRecognizer(sql, asts.get(0));
    selectForUpdateExecutor = new SelectForUpdateExecutor(statementProxy, (statement, args) -> {
        return null;
    }, recognizer);
}
Also used : MockConnectionProxy(io.seata.rm.datasource.mock.MockConnectionProxy) MockDriver(io.seata.rm.datasource.mock.MockDriver) SQLUtils(com.alibaba.druid.sql.SQLUtils) StatementProxy(io.seata.rm.datasource.StatementProxy) MockConnectionProxy(io.seata.rm.datasource.mock.MockConnectionProxy) Field(java.lang.reflect.Field) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) MockLockConflictConnectionProxy(io.seata.rm.datasource.mock.MockLockConflictConnectionProxy) Test(org.junit.jupiter.api.Test) RootContext(io.seata.core.context.RootContext) List(java.util.List) Lists(com.google.common.collect.Lists) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy) BeforeAll(org.junit.jupiter.api.BeforeAll) MockStatement(com.alibaba.druid.mock.MockStatement) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) JdbcConstants(com.alibaba.druid.util.JdbcConstants) Assertions(org.junit.jupiter.api.Assertions) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Types(java.sql.Types) MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Field(java.lang.reflect.Field) StatementProxy(io.seata.rm.datasource.StatementProxy) MockStatement(com.alibaba.druid.mock.MockStatement) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with MySQLSelectForUpdateRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer in project seata by seata.

the class MySQLSelectForUpdateRecognizerTest method testGetSqlType.

@Test
public void testGetSqlType() {
    String sql = "select * from t where id = ? for update";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLSelectForUpdateRecognizer recognizer = new MySQLSelectForUpdateRecognizer(sql, asts.get(0));
    Assertions.assertEquals(recognizer.getSQLType(), SQLType.SELECT_FOR_UPDATE);
}
Also used : MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 3 with MySQLSelectForUpdateRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer in project seata by seata.

the class MySQLSelectForUpdateRecognizerTest method selectForUpdateRecognizerTest_1.

/**
 * Select for update recognizer test 1.
 */
@Test
public void selectForUpdateRecognizerTest_1() {
    String sql = "SELECT name FROM t1 WHERE id = ? FOR UPDATE";
    SQLStatement statement = getSQLStatement(sql);
    MySQLSelectForUpdateRecognizer mySQLUpdateRecognizer = new MySQLSelectForUpdateRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
    ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    String whereCondition = mySQLUpdateRecognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> idParam = new ArrayList<>();
            idParam.add("id1");
            Map result = new HashMap();
            result.put(1, idParam);
            return result;
        }
    }, paramAppenderList);
    Assertions.assertEquals(Collections.singletonList(Collections.singletonList("id1")), paramAppenderList);
    Assertions.assertEquals("id = ?", whereCondition);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) ArrayList(java.util.ArrayList) List(java.util.List) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 4 with MySQLSelectForUpdateRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer in project seata by seata.

the class MySQLSelectForUpdateRecognizerTest method testGetTableAlias.

@Test
public void testGetTableAlias() {
    String sql = "select * from t where id = ? for update";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
    MySQLSelectForUpdateRecognizer recognizer = new MySQLSelectForUpdateRecognizer(sql, asts.get(0));
    Assertions.assertNull(recognizer.getTableAlias());
}
Also used : MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 5 with MySQLSelectForUpdateRecognizer

use of io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer in project seata by seata.

the class MySQLSelectForUpdateRecognizerTest method selectForUpdateRecognizerTest_5.

/**
 * Select for update recognizer test 5.
 */
@Test
public void selectForUpdateRecognizerTest_5() {
    String sql = "SELECT name1, name2 FROM t1 WHERE id between ? and ? FOR UPDATE";
    SQLStatement statement = getSQLStatement(sql);
    MySQLSelectForUpdateRecognizer mySQLUpdateRecognizer = new MySQLSelectForUpdateRecognizer(sql, statement);
    Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
    Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
    // test overflow parameters
    ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    String whereCondition = mySQLUpdateRecognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> id1Param = new ArrayList<>();
            id1Param.add("id1");
            ArrayList<Object> id2Param = new ArrayList<>();
            id2Param.add("id2");
            Map result = new HashMap();
            result.put(1, id1Param);
            result.put(2, id2Param);
            return result;
        }
    }, paramAppenderList);
    Assertions.assertEquals(Collections.singletonList(Arrays.asList("id1", "id2")), paramAppenderList);
    Assertions.assertEquals("id BETWEEN ? AND ?", whereCondition);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MySQLSelectForUpdateRecognizer(io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer) ArrayList(java.util.ArrayList) List(java.util.List) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)10 MySQLSelectForUpdateRecognizer (io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer)10 Test (org.junit.jupiter.api.Test)10 List (java.util.List)6 ParametersHolder (io.seata.sqlparser.ParametersHolder)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 MockStatement (com.alibaba.druid.mock.MockStatement)2 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)2 SQLUtils (com.alibaba.druid.sql.SQLUtils)2 JdbcConstants (com.alibaba.druid.util.JdbcConstants)2 Lists (com.google.common.collect.Lists)2 RootContext (io.seata.core.context.RootContext)2 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)2 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)2 StatementProxy (io.seata.rm.datasource.StatementProxy)2 MockConnectionProxy (io.seata.rm.datasource.mock.MockConnectionProxy)2 MockDriver (io.seata.rm.datasource.mock.MockDriver)2 MockLockConflictConnectionProxy (io.seata.rm.datasource.mock.MockLockConflictConnectionProxy)2