Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL trim函数,使用ExportParameterVisitor提取变量参数错误 #5748

Open
wangweicugw opened this issue Feb 26, 2024 · 1 comment
Open

Comments

@wangweicugw
Copy link

wangweicugw commented Feb 26, 2024

Druid版本

1.2.20

测试用例

    public static void main(String[] args) {
        String sql = "select trim(' ABC ') from t";
        SQLStatement statement = SQLUtils.parseSingleMysqlStatement(sql);
        StringBuilder stringBuilder = new StringBuilder();
        MySqlExportParameterVisitor visitor = new MySqlExportParameterVisitor(new ArrayList<>(), stringBuilder, true);
        visitor.setShardingSupport(false);
        visitor.setParameterizedMergeInList(false);
        visitor.setParameterizedQuesUnMergeInList(true);
        visitor.setPrettyFormat(false);
        visitor.setUppCase(false);
        visitor.config(VisitorFeature.OutputParameterizedQuesUnMergeOr, true);
        statement.accept(visitor);
        System.out.println("visitor.getParameters() = " + visitor.getParameters());
        System.out.println(stringBuilder);
    }

实际输出

visitor.getParameters() = [ABC]
select ? from t

期望输出

visitor.getParameters() = [ABC]
select trim(?) from t
@wangweicugw
Copy link
Author

这样处理是想要在内存中处理trim函数么?

com.alibaba.druid.sql.visitor.SQLASTOutputVisitor#visit(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)

    public boolean visit(SQLMethodInvokeExpr x) {
        SQLExpr owner = x.getOwner();
        if (owner != null) {
            printMethodOwner(owner);
        }

        if (parameterized) {
            List<SQLExpr> arguments = x.getArguments();
            if (x.methodNameHashCode64() == FnvHash.Constants.TRIM
                    && arguments.size() == 1
                    && arguments.get(0) instanceof SQLCharExpr && x.getTrimOption() == null && x.getFrom() == null) {
                print('?');

                if (parameters != null) {
                    SQLCharExpr charExpr = (SQLCharExpr) arguments.get(0);
                    parameters.add(charExpr.getText().trim());
                }

                replaceCount++;
                return false;
            }
        }

        String function = x.getMethodName();
        if (function != null) {
            printFunctionName(function);
        }
        printMethodParameters(x);
        return false;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant