use of io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer in project seata by seata.
the class MySQLUpdateRecognizerTest method updateRecognizerTest_0.
/**
* Update recognizer test 0.
*/
@Test
public void updateRecognizerTest_0() {
String sql = "UPDATE t1 SET name = 'name1' WHERE id = 'id1'";
SQLStatement statement = getSQLStatement(sql);
MySQLUpdateRecognizer mySQLUpdateRecognizer = new MySQLUpdateRecognizer(sql, statement);
Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
Assertions.assertEquals(1, mySQLUpdateRecognizer.getUpdateColumns().size());
Assertions.assertEquals("name", mySQLUpdateRecognizer.getUpdateColumns().get(0));
Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateValues().get(0));
Assertions.assertEquals("id = 'id1'", mySQLUpdateRecognizer.getWhereCondition());
}
use of io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer in project seata by seata.
the class MySQLUpdateRecognizerTest method updateRecognizerTest_1.
/**
* Update recognizer test 1.
*/
@Test
public void updateRecognizerTest_1() {
String sql = "UPDATE t1 SET name1 = 'name1', name2 = 'name2' WHERE id = 'id1'";
SQLStatement statement = getSQLStatement(sql);
MySQLUpdateRecognizer mySQLUpdateRecognizer = new MySQLUpdateRecognizer(sql, statement);
Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
Assertions.assertEquals(2, mySQLUpdateRecognizer.getUpdateColumns().size());
Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateColumns().get(0));
Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateValues().get(0));
Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateColumns().get(1));
Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateValues().get(1));
Assertions.assertEquals("id = 'id1'", mySQLUpdateRecognizer.getWhereCondition());
}
use of io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer in project seata by seata.
the class MySQLUpdateRecognizerTest method testGetUpdateDatabaseNameColumns.
@Test
public void testGetUpdateDatabaseNameColumns() {
// test with normal
String sql = "update d.t set d.t.a = ?, d.t.b = ?, d.t.c = ?";
List<SQLStatement> asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
MySQLUpdateRecognizer recognizer = new MySQLUpdateRecognizer(sql, asts.get(0));
List<String> updateColumns = recognizer.getUpdateColumns();
Assertions.assertEquals(updateColumns.size(), 3);
// test with alias
sql = "update t set a.a = ?, a.b = ?, a.c = ?";
asts = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
recognizer = new MySQLUpdateRecognizer(sql, asts.get(0));
updateColumns = recognizer.getUpdateColumns();
Assertions.assertEquals(updateColumns.size(), 3);
// test with error
Assertions.assertThrows(SQLParsingException.class, () -> {
String s = "update t set a = a";
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, JdbcConstants.MYSQL);
SQLUpdateStatement sqlUpdateStatement = (SQLUpdateStatement) sqlStatements.get(0);
List<SQLUpdateSetItem> updateSetItems = sqlUpdateStatement.getItems();
for (SQLUpdateSetItem updateSetItem : updateSetItems) {
updateSetItem.setColumn(new MySqlCharExpr());
}
MySQLUpdateRecognizer oracleUpdateRecognizer = new MySQLUpdateRecognizer(s, sqlUpdateStatement);
oracleUpdateRecognizer.getUpdateColumns();
});
}
use of io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer in project seata by seata.
the class MySQLUpdateRecognizerTest method updateRecognizerTest_4.
/**
* Update recognizer test 4.
*/
@Test
public void updateRecognizerTest_4() {
String sql = "UPDATE t1 SET name1 = 'name1', name2 = 'name2' WHERE id in (?, ?) and name1 = ?";
SQLStatement statement = getSQLStatement(sql);
MySQLUpdateRecognizer mySQLUpdateRecognizer = new MySQLUpdateRecognizer(sql, statement);
Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
Assertions.assertEquals(2, mySQLUpdateRecognizer.getUpdateColumns().size());
Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateColumns().get(0));
Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateValues().get(0));
Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateColumns().get(1));
Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateValues().get(1));
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");
ArrayList<Object> name1Param = new ArrayList<>();
name1Param.add("name");
Map result = new HashMap();
result.put(1, id1Param);
result.put(2, id2Param);
result.put(3, name1Param);
return result;
}
}, paramAppenderList);
Assertions.assertEquals(Collections.singletonList(Arrays.asList("id1", "id2", "name")), paramAppenderList);
Assertions.assertEquals("id IN (?, ?)\nAND name1 = ?", whereCondition);
}
use of io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer in project seata by seata.
the class MySQLUpdateRecognizerTest method updateRecognizerTest_3.
/**
* Update recognizer test 3.
*/
@Test
public void updateRecognizerTest_3() {
String sql = "UPDATE t1 SET name1 = 'name1', name2 = 'name2' WHERE id in (?, ?)";
SQLStatement statement = getSQLStatement(sql);
MySQLUpdateRecognizer mySQLUpdateRecognizer = new MySQLUpdateRecognizer(sql, statement);
Assertions.assertEquals(sql, mySQLUpdateRecognizer.getOriginalSQL());
Assertions.assertEquals("t1", mySQLUpdateRecognizer.getTableName());
Assertions.assertEquals(2, mySQLUpdateRecognizer.getUpdateColumns().size());
Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateColumns().get(0));
Assertions.assertEquals("name1", mySQLUpdateRecognizer.getUpdateValues().get(0));
Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateColumns().get(1));
Assertions.assertEquals("name2", mySQLUpdateRecognizer.getUpdateValues().get(1));
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 IN (?, ?)", whereCondition);
}
Aggregations