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

SQLASTOutputVisitor格式化有bug只拼了一半单引号导致生成的SQL语法不对 #3085

Closed
lizongbo opened this issue Feb 20, 2019 · 4 comments
Labels
Milestone

Comments

@lizongbo
Copy link
Collaborator

lizongbo commented Feb 20, 2019

我继承StatFilter将sql做了采样分析,采样的时候将sql进行格式化只会再次参数化处理,结果生产遇到问题
然后发现SQLASTOutputVisitor在拼sql的时候针对Reader等类型只拼了前半截单引号,没有后半截单引号就return了。

https://github.com/alibaba/druid/blob/master/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java#L2310

我的代码如下:

private static List<Object> getSqlParamObjetList(StatementProxy statement) {
        List<Object> parameters = null;
        int parametersSize = statement.getParametersSize();
        if (parametersSize > 0) {
            parameters = new ArrayList<>(parametersSize);
            for (int i = 0; i < parametersSize; ++i) {
                JdbcParameter jdbcParam = statement.getParameter(i);
                Object param = jdbcParam != null ? jdbcParam.getValue() : null;
                if (param instanceof String) {
                    String str = (String) param;
                    if (str.length() > 300) {// 超长文本做截取
                        param = str.subSequence(0, 300) + ".........";
                    }
                }
//以下是为了兼容druid SQLASTOutputVisitor的bug,返回指定字符串
                if (param instanceof InputStream) {
                    param = ("< InputStream >");
                }
                if (param instanceof Reader) {
                    param = ("< Reader >");
                }
                if (param instanceof Blob) {
                    param = ("< Blob >");
                }
                if (param instanceof NClob) {
                    param = ("< NClob >");
                }

                if (param instanceof Clob) {
                    param = ("< Clob >");
                }
                parameters.add(param);
            }
        }
        return parameters;
    }
 private void trySamplingSql(String method, StatementProxy statement, String sql) {
        String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
        List<Object> parameters = getSqlParamObjetList(statement);

  String formattedSql = SQLUtils.format(sql, dbType, parameters);
        String mergedSql = null;
        try {
//parameters里有Reader等类型就会出错了。
            mergedSql = ParameterizedOutputVisitorUtils.parameterize(formattedSql, "mysql");
        } catch (Exception e) {
            if (LOG.isWarnEnabled()) {
                String msg = "doAsyncSqlSampling|buterr|" + (formattedSql);
                LOG.warn(msg, e);
            }
            return;
        }
//以mergedSql为基准每调采样10次。
}
@wenshao wenshao added the Bug label Feb 22, 2019
@wenshao wenshao added this to the 1.1.14 milestone Feb 22, 2019
@wenshao
Copy link
Member

wenshao commented Feb 24, 2019

你这个是什么版本?

wenshao added a commit that referenced this issue Feb 24, 2019
@wenshao
Copy link
Member

wenshao commented Feb 27, 2019

问题已修复,请用新版本 https://github.com/alibaba/druid/releases/tag/1.1.14

@wenshao wenshao closed this as completed Feb 27, 2019
@wenshao
Copy link
Member

wenshao commented Feb 27, 2019

问题已修复,请用新版本 https://github.com/alibaba/druid/releases/tag/1.1.14

@wenshao wenshao reopened this Feb 27, 2019
@wenshao wenshao closed this as completed Feb 27, 2019
@lizongbo
Copy link
Collaborator Author

👍

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

No branches or pull requests

2 participants