Search in sources :

Example 6 with SQLReplaceable

use of com.alibaba.druid.sql.ast.SQLReplaceable in project dble by actiontech.

the class PrepareChangeVisitor method visit.

@Override
public boolean visit(SQLVariantRefExpr x) {
    if (x.getParent() != null && x.getParent() instanceof SQLSelectItem) {
        ((SQLSelectItem) x.getParent()).replace(x, SQLUtils.toSQLExpr("true"));
        return false;
    }
    SQLObject parent;
    SQLObject current = x.getParent();
    // nest lookup. find closest SQLReplaceable and replace if possible.
    while (current != null && (parent = current.getParent()) != null) {
        if (parent instanceof SQLReplaceable) {
            if (current instanceof SQLExpr) {
                ((SQLReplaceable) parent).replace((SQLExpr) current, SQLUtils.toSQLExpr("true"));
                break;
            } else {
                current = parent;
            }
        } else {
            current = parent;
        }
    }
    return false;
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLReplaceable(com.alibaba.druid.sql.ast.SQLReplaceable) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 7 with SQLReplaceable

use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.

the class VertxMycatConnectionPool method deparameterize.

public static String deparameterize(String sql, List<Object> params) {
    if (sql.startsWith("be")) {
        return sql;
    }
    SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
    sqlStatement.accept(new MySqlASTVisitorAdapter() {

        int index;

        @Override
        public void endVisit(SQLVariantRefExpr x) {
            if ("?".equalsIgnoreCase(x.getName())) {
                if (index < params.size()) {
                    Object value = params.get(index++);
                    SQLReplaceable parent = (SQLReplaceable) x.getParent();
                    parent.replace(x, PreparedStatement.fromJavaObject(value));
                }
            }
            super.endVisit(x);
        }
    });
    sql = sqlStatement.toString();
    return sql;
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLReplaceable(com.alibaba.druid.sql.ast.SQLReplaceable)

Aggregations

SQLReplaceable (com.alibaba.druid.sql.ast.SQLReplaceable)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)6 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 XaSqlConnection (cn.mycat.vertx.xa.XaSqlConnection)1 DbType (com.alibaba.druid.DbType)1 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 com.alibaba.druid.sql.ast.expr (com.alibaba.druid.sql.ast.expr)1 com.alibaba.druid.sql.ast.statement (com.alibaba.druid.sql.ast.statement)1 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)1 MycatSQLEvalVisitorUtils (com.alibaba.druid.sql.visitor.MycatSQLEvalVisitorUtils)1 ImmutableList (com.google.common.collect.ImmutableList)1 io.mycat (io.mycat)1 MySQLColumnDef (io.mycat.api.collector.MySQLColumnDef)1 MysqlObjectArrayRow (io.mycat.api.collector.MysqlObjectArrayRow)1