Search in sources :

Example 1 with SQLUpdateRecognizer

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();
    });
}
Also used : SQLUpdateSetItem(com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem) PostgresqlUpdateRecognizer(io.seata.sqlparser.druid.postgresql.PostgresqlUpdateRecognizer) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLUpdateRecognizer(io.seata.sqlparser.SQLUpdateRecognizer) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Test(org.junit.jupiter.api.Test)

Example 2 with SQLUpdateRecognizer

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();
}
Also used : SQLUpdateRecognizer(io.seata.sqlparser.SQLUpdateRecognizer) StringJoiner(java.util.StringJoiner)

Example 3 with SQLUpdateRecognizer

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();
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) SQLUpdateRecognizer(io.seata.sqlparser.SQLUpdateRecognizer) StringJoiner(java.util.StringJoiner)

Example 4 with SQLUpdateRecognizer

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");
}
Also used : SQLUpdateRecognizer(io.seata.sqlparser.SQLUpdateRecognizer) Test(org.junit.jupiter.api.Test)

Example 5 with SQLUpdateRecognizer

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());
}
Also used : SQLUpdateRecognizer(io.seata.sqlparser.SQLUpdateRecognizer) Test(org.junit.jupiter.api.Test)

Aggregations

SQLUpdateRecognizer (io.seata.sqlparser.SQLUpdateRecognizer)11 Test (org.junit.jupiter.api.Test)7 StringJoiner (java.util.StringJoiner)4 ParametersHolder (io.seata.sqlparser.ParametersHolder)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLBetweenExpr (com.alibaba.druid.sql.ast.expr.SQLBetweenExpr)2 SQLUpdateSetItem (com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 SQLRecognizer (io.seata.sqlparser.SQLRecognizer)2 PostgresqlUpdateRecognizer (io.seata.sqlparser.druid.postgresql.PostgresqlUpdateRecognizer)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 NotSupportYetException (io.seata.common.exception.NotSupportYetException)1 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)1 List (java.util.List)1 Map (java.util.Map)1