Search in sources :

Example 11 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder in project seata by seata.

the class OracleUpdateRecognizerTest method testGetWhereCondition_0.

@Test
public void testGetWhereCondition_0() {
    String sql = "update t set a = 1";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, DB_TYPE);
    OracleUpdateRecognizer recognizer = new OracleUpdateRecognizer(sql, asts.get(0));
    String whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            return null;
        }
    }, new ArrayList<>());
    Assertions.assertEquals("", whereCondition);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) OracleUpdateRecognizer(io.seata.sqlparser.druid.oracle.OracleUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 12 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder in project seata by seata.

the class PostgresqlUpdateRecognizerTest method testGetWhereCondition_0.

@Test
public void testGetWhereCondition_0() {
    String sql = "update t set a = 1";
    SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
    String whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            return null;
        }
    }, new ArrayList<>());
    Assertions.assertEquals("", whereCondition);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) SQLUpdateRecognizer(io.seata.sqlparser.SQLUpdateRecognizer) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 13 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder in project seata by seata.

the class OracleSelectForUpdateRecognizerTest method testGetWhereCondition_0.

@Test
public void testGetWhereCondition_0() {
    String sql = "select * from t for update";
    List<SQLStatement> asts = SQLUtils.parseStatements(sql, DB_TYPE);
    OracleSelectForUpdateRecognizer recognizer = new OracleSelectForUpdateRecognizer(sql, asts.get(0));
    String whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            return null;
        }
    }, new ArrayList<>());
    Assertions.assertEquals("", whereCondition);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) OracleSelectForUpdateRecognizer(io.seata.sqlparser.druid.oracle.OracleSelectForUpdateRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 14 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder in project seata by seata.

the class PostgresqlSelectForUpdateRecognizerTest method testGetWhereCondition_0.

@Test
public void testGetWhereCondition_0() {
    String sql = "select * from t for update";
    SQLSelectRecognizer recognizer = (SQLSelectRecognizer) SQLVisitorFactory.get(sql, DB_TYPE).get(0);
    String whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            return null;
        }
    }, new ArrayList<>());
    Assertions.assertEquals("", whereCondition);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) SQLSelectRecognizer(io.seata.sqlparser.SQLSelectRecognizer) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 15 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder in project seata by seata.

the class MultiUpdateExecutor method beforeImage.

@Override
protected TableRecords beforeImage() throws SQLException {
    if (sqlRecognizers.size() == 1) {
        UpdateExecutor executor = new UpdateExecutor<>(statementProxy, statementCallback, sqlRecognizers.get(0));
        return executor.beforeImage();
    }
    final TableMeta tmeta = getTableMeta(sqlRecognizers.get(0).getTableName());
    final ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    Set<String> updateColumnsSet = new HashSet<>();
    StringBuilder whereCondition = new StringBuilder();
    boolean noWhereCondition = false;
    for (SQLRecognizer recognizer : sqlRecognizers) {
        sqlRecognizer = recognizer;
        SQLUpdateRecognizer sqlUpdateRecognizer = (SQLUpdateRecognizer) recognizer;
        ParametersHolder parametersHolder = statementProxy instanceof ParametersHolder ? (ParametersHolder) statementProxy : null;
        if (StringUtils.isNotBlank(sqlUpdateRecognizer.getLimit(parametersHolder, paramAppenderList))) {
            throw new NotSupportYetException("Multi update SQL with limit condition is not support yet !");
        }
        if (StringUtils.isNotBlank(sqlUpdateRecognizer.getOrderBy())) {
            throw new NotSupportYetException("Multi update SQL with orderBy condition is not support yet !");
        }
        List<String> updateColumns = sqlUpdateRecognizer.getUpdateColumns();
        updateColumnsSet.addAll(updateColumns);
        if (noWhereCondition) {
            continue;
        }
        String whereConditionStr = buildWhereCondition(sqlUpdateRecognizer, paramAppenderList);
        if (StringUtils.isBlank(whereConditionStr)) {
            noWhereCondition = true;
        } else {
            if (whereCondition.length() > 0) {
                whereCondition.append(" OR ");
            }
            whereCondition.append(whereConditionStr);
        }
    }
    StringBuilder prefix = new StringBuilder("SELECT ");
    final StringBuilder suffix = new StringBuilder(" FROM ").append(getFromTableInSQL());
    if (noWhereCondition) {
        // select all rows
        paramAppenderList.clear();
    } else {
        suffix.append(" WHERE ").append(whereCondition);
    }
    suffix.append(" FOR UPDATE");
    final StringJoiner selectSQLAppender = new StringJoiner(", ", prefix, suffix.toString());
    if (ONLY_CARE_UPDATE_COLUMNS) {
        if (!containsPK(new ArrayList<>(updateColumnsSet))) {
            selectSQLAppender.add(getColumnNamesInSQL(tmeta.getEscapePkNameList(getDbType())));
        }
        for (String updateCol : updateColumnsSet) {
            selectSQLAppender.add(updateCol);
        }
    } else {
        for (String columnName : tmeta.getAllColumns().keySet()) {
            selectSQLAppender.add(ColumnUtils.addEscape(columnName, getDbType()));
        }
    }
    return buildTableRecords(tmeta, selectSQLAppender.toString(), paramAppenderList);
}
Also used : ArrayList(java.util.ArrayList) SQLUpdateRecognizer(io.seata.sqlparser.SQLUpdateRecognizer) ParametersHolder(io.seata.sqlparser.ParametersHolder) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) ArrayList(java.util.ArrayList) List(java.util.List) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) NotSupportYetException(io.seata.common.exception.NotSupportYetException) StringJoiner(java.util.StringJoiner) HashSet(java.util.HashSet)

Aggregations

ParametersHolder (io.seata.sqlparser.ParametersHolder)23 Test (org.junit.jupiter.api.Test)19 Map (java.util.Map)18 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)16 ArrayList (java.util.ArrayList)16 List (java.util.List)14 HashMap (java.util.HashMap)13 MySQLDeleteRecognizer (io.seata.sqlparser.druid.mysql.MySQLDeleteRecognizer)5 MySQLSelectForUpdateRecognizer (io.seata.sqlparser.druid.mysql.MySQLSelectForUpdateRecognizer)4 MySQLUpdateRecognizer (io.seata.sqlparser.druid.mysql.MySQLUpdateRecognizer)4 StringJoiner (java.util.StringJoiner)4 SQLRecognizer (io.seata.sqlparser.SQLRecognizer)3 SQLUpdateRecognizer (io.seata.sqlparser.SQLUpdateRecognizer)3 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 NotSupportYetException (io.seata.common.exception.NotSupportYetException)2 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)2 SQLDeleteRecognizer (io.seata.sqlparser.SQLDeleteRecognizer)2 MySqlOrderingExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr)1 OracleArgumentExpr (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleArgumentExpr)1 SQLSelectRecognizer (io.seata.sqlparser.SQLSelectRecognizer)1