Search in sources :

Example 6 with ParametersHolder

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

the class OracleDeleteRecognizerTest method testGetWhereCondition_0.

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

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            return null;
        }
    }, new ArrayList<>());
    // test for no condition
    Assertions.assertEquals("", whereCondition);
    sql = "delete from t where id = ?";
    asts = SQLUtils.parseStatements(sql, DB_TYPE);
    recognizer = new OracleDeleteRecognizer(sql, asts.get(0));
    whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> idParam = new ArrayList<>();
            idParam.add(1);
            Map result = new HashMap();
            result.put(1, idParam);
            return result;
        }
    }, new ArrayList<>());
    // test for normal sql
    Assertions.assertEquals("id = ?", whereCondition);
    sql = "delete from t where id in (?)";
    asts = SQLUtils.parseStatements(sql, DB_TYPE);
    recognizer = new OracleDeleteRecognizer(sql, asts.get(0));
    whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> idParam = new ArrayList<>();
            idParam.add(1);
            Map result = new HashMap();
            result.put(1, idParam);
            return result;
        }
    }, new ArrayList<>());
    // test for sql with in
    Assertions.assertEquals("id IN (?)", whereCondition);
    sql = "delete from t where id between ? and ?";
    asts = SQLUtils.parseStatements(sql, DB_TYPE);
    recognizer = new OracleDeleteRecognizer(sql, asts.get(0));
    whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

        @Override
        public Map<Integer, ArrayList<Object>> getParameters() {
            ArrayList<Object> idParam = new ArrayList<>();
            idParam.add(1);
            ArrayList<Object> idParam2 = new ArrayList<>();
            idParam.add(2);
            Map result = new HashMap();
            result.put(1, idParam);
            result.put(2, idParam2);
            return result;
        }
    }, new ArrayList<>());
    // test for sql with in
    Assertions.assertEquals("id BETWEEN ? AND ?", whereCondition);
    // test for exception
    Assertions.assertThrows(IllegalArgumentException.class, () -> {
        String s = "delete from t where id in (?)";
        List<SQLStatement> sqlStatements = SQLUtils.parseStatements(s, DB_TYPE);
        SQLDeleteStatement deleteAst = (SQLDeleteStatement) sqlStatements.get(0);
        deleteAst.setWhere(new OracleArgumentExpr());
        new OracleDeleteRecognizer(s, deleteAst).getWhereCondition(new ParametersHolder() {

            @Override
            public Map<Integer, ArrayList<Object>> getParameters() {
                return new HashMap<>();
            }
        }, new ArrayList<>());
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OracleDeleteRecognizer(io.seata.sqlparser.druid.oracle.OracleDeleteRecognizer) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleArgumentExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleArgumentExpr) ParametersHolder(io.seata.sqlparser.ParametersHolder) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 7 with ParametersHolder

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

the class PostgresqlDeleteRecognizerTest method testGetWhereCondition_0.

@Test
public void testGetWhereCondition_0() {
    String sql = "delete from t";
    List<SQLRecognizer> sqlRecognizers = SQLVisitorFactory.get(sql, DB_TYPE);
    SQLDeleteRecognizer recognizer = (SQLDeleteRecognizer) sqlRecognizers.get(0);
    String whereCondition = recognizer.getWhereCondition(new ParametersHolder() {

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

Example 8 with ParametersHolder

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

the class MultiDeleteExecutor method beforeImage.

@Override
protected TableRecords beforeImage() throws SQLException {
    if (sqlRecognizers.size() == 1) {
        DeleteExecutor executor = new DeleteExecutor(statementProxy, statementCallback, sqlRecognizers.get(0));
        return executor.beforeImage();
    }
    final TableMeta tmeta = getTableMeta(sqlRecognizers.get(0).getTableName());
    final ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    StringBuilder whereCondition = new StringBuilder();
    for (SQLRecognizer recognizer : sqlRecognizers) {
        sqlRecognizer = recognizer;
        SQLDeleteRecognizer visitor = (SQLDeleteRecognizer) recognizer;
        ParametersHolder parametersHolder = statementProxy instanceof ParametersHolder ? (ParametersHolder) statementProxy : null;
        if (StringUtils.isNotBlank(visitor.getLimit(parametersHolder, paramAppenderList))) {
            throw new NotSupportYetException("Multi delete SQL with limit condition is not support yet !");
        }
        if (StringUtils.isNotBlank(visitor.getOrderBy())) {
            throw new NotSupportYetException("Multi delete SQL with orderBy condition is not support yet !");
        }
        String whereConditionStr = buildWhereCondition(visitor, paramAppenderList);
        if (StringUtils.isBlank(whereConditionStr)) {
            whereCondition = new StringBuilder();
            paramAppenderList.clear();
            break;
        }
        if (whereCondition.length() > 0) {
            whereCondition.append(" OR ");
        }
        whereCondition.append(whereConditionStr);
    }
    StringBuilder suffix = new StringBuilder(" FROM ").append(getFromTableInSQL());
    if (whereCondition.length() > 0) {
        suffix.append(" WHERE ").append(whereCondition);
    }
    suffix.append(" FOR UPDATE");
    final StringJoiner selectSQLAppender = new StringJoiner(", ", "SELECT ", suffix.toString());
    for (String column : tmeta.getAllColumns().keySet()) {
        selectSQLAppender.add(getColumnNameInSQL(ColumnUtils.addEscape(column, getDbType())));
    }
    return buildTableRecords(tmeta, selectSQLAppender.toString(), paramAppenderList);
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) SQLDeleteRecognizer(io.seata.sqlparser.SQLDeleteRecognizer) NotSupportYetException(io.seata.common.exception.NotSupportYetException) StringJoiner(java.util.StringJoiner)

Example 9 with ParametersHolder

use of io.seata.sqlparser.ParametersHolder 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 10 with ParametersHolder

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

the class DeleteExecutor method buildBeforeImageSQL.

private String buildBeforeImageSQL(SQLDeleteRecognizer visitor, TableMeta tableMeta, ArrayList<List<Object>> paramAppenderList) {
    String whereCondition = buildWhereCondition(visitor, paramAppenderList);
    StringBuilder suffix = new StringBuilder(" FROM ").append(getFromTableInSQL());
    if (StringUtils.isNotBlank(whereCondition)) {
        suffix.append(WHERE).append(whereCondition);
    }
    String orderBy = visitor.getOrderBy();
    if (StringUtils.isNotBlank(orderBy)) {
        suffix.append(orderBy);
    }
    ParametersHolder parametersHolder = statementProxy instanceof ParametersHolder ? (ParametersHolder) statementProxy : null;
    String limit = visitor.getLimit(parametersHolder, paramAppenderList);
    if (StringUtils.isNotBlank(limit)) {
        suffix.append(limit);
    }
    suffix.append(" FOR UPDATE");
    StringJoiner selectSQLAppender = new StringJoiner(", ", "SELECT ", suffix.toString());
    for (String column : tableMeta.getAllColumns().keySet()) {
        selectSQLAppender.add(getColumnNameInSQL(ColumnUtils.addEscape(column, getDbType())));
    }
    return selectSQLAppender.toString();
}
Also used : ParametersHolder(io.seata.sqlparser.ParametersHolder) StringJoiner(java.util.StringJoiner)

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