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);
}
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);
}
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);
}
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());
}
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);
}
Aggregations