use of io.seata.sqlparser.SQLUpdateRecognizer in project seata by seata.
the class PostgresqlUpdateRecognizerTest method testGetUpdateValues.
@Test
public void testGetUpdateValues() {
// test with normal
String sql = "update t set a = ?, b = ?, c = ?";
SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
List<Object> updateValues = recognizer.getUpdateValues();
Assertions.assertEquals(updateValues.size(), 3);
// test with values
sql = "update t set a = 1, b = 2, c = 3";
recognizer = (SQLUpdateRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
updateValues = recognizer.getUpdateValues();
Assertions.assertEquals(updateValues.size(), 3);
// test with error
Assertions.assertThrows(SQLParsingException.class, () -> {
String s = "update t set a = ?";
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, DB_TYPE);
SQLUpdateStatement sqlUpdateStatement = (SQLUpdateStatement) sqlStatements.get(0);
List<SQLUpdateSetItem> updateSetItems = sqlUpdateStatement.getItems();
for (SQLUpdateSetItem updateSetItem : updateSetItems) {
updateSetItem.setValue(new SQLBetweenExpr());
}
PostgresqlUpdateRecognizer postgresqlUpdateRecognizer = new PostgresqlUpdateRecognizer(s, sqlUpdateStatement);
postgresqlUpdateRecognizer.getUpdateValues();
});
}
use of io.seata.sqlparser.SQLUpdateRecognizer in project seata by seata.
the class UpdateExecutor method buildAfterImageSQL.
private String buildAfterImageSQL(TableMeta tableMeta, TableRecords beforeImage) throws SQLException {
StringBuilder prefix = new StringBuilder("SELECT ");
String whereSql = SqlGenerateUtils.buildWhereConditionByPKs(tableMeta.getPrimaryKeyOnlyName(), beforeImage.pkRows().size(), getDbType());
String suffix = " FROM " + getFromTableInSQL() + " WHERE " + whereSql;
StringJoiner selectSQLJoiner = new StringJoiner(", ", prefix.toString(), suffix);
if (ONLY_CARE_UPDATE_COLUMNS) {
SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) sqlRecognizer;
List<String> updateColumns = recognizer.getUpdateColumns();
if (!containsPK(updateColumns)) {
selectSQLJoiner.add(getColumnNamesInSQL(tableMeta.getEscapePkNameList(getDbType())));
}
for (String columnName : updateColumns) {
selectSQLJoiner.add(columnName);
}
} else {
for (String columnName : tableMeta.getAllColumns().keySet()) {
selectSQLJoiner.add(ColumnUtils.addEscape(columnName, getDbType()));
}
}
return selectSQLJoiner.toString();
}
use of io.seata.sqlparser.SQLUpdateRecognizer in project seata by seata.
the class UpdateExecutor method buildBeforeImageSQL.
private String buildBeforeImageSQL(TableMeta tableMeta, ArrayList<List<Object>> paramAppenderList) {
SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) sqlRecognizer;
List<String> updateColumns = recognizer.getUpdateColumns();
StringBuilder prefix = new StringBuilder("SELECT ");
StringBuilder suffix = new StringBuilder(" FROM ").append(getFromTableInSQL());
String whereCondition = buildWhereCondition(recognizer, paramAppenderList);
if (StringUtils.isNotBlank(whereCondition)) {
suffix.append(WHERE).append(whereCondition);
}
String orderBy = recognizer.getOrderBy();
if (StringUtils.isNotBlank(orderBy)) {
suffix.append(orderBy);
}
ParametersHolder parametersHolder = statementProxy instanceof ParametersHolder ? (ParametersHolder) statementProxy : null;
String limit = recognizer.getLimit(parametersHolder, paramAppenderList);
if (StringUtils.isNotBlank(limit)) {
suffix.append(limit);
}
suffix.append(" FOR UPDATE");
StringJoiner selectSQLJoin = new StringJoiner(", ", prefix.toString(), suffix.toString());
if (ONLY_CARE_UPDATE_COLUMNS) {
if (!containsPK(updateColumns)) {
selectSQLJoin.add(getColumnNamesInSQL(tableMeta.getEscapePkNameList(getDbType())));
}
for (String columnName : updateColumns) {
selectSQLJoin.add(columnName);
}
} else {
for (String columnName : tableMeta.getAllColumns().keySet()) {
selectSQLJoin.add(ColumnUtils.addEscape(columnName, getDbType()));
}
}
return selectSQLJoin.toString();
}
use of io.seata.sqlparser.SQLUpdateRecognizer in project seata by seata.
the class PostgresqlUpdateRecognizerTest method testGetTableName.
@Test
public void testGetTableName() {
String sql = "update t set a = ?, b = ?, c = ?";
SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
Assertions.assertEquals(recognizer.getTableName(), "t");
}
use of io.seata.sqlparser.SQLUpdateRecognizer in project seata by seata.
the class PostgresqlUpdateRecognizerTest method testGetTableAlias.
@Test
public void testGetTableAlias() {
String sql = "update t set a = ?, b = ?, c = ?";
SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
Assertions.assertNull(recognizer.getTableAlias());
}
Aggregations