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

fix: fix jsqlParser parse sql counter output keyword #6056

Open
wants to merge 1 commit into
base: 3.0
Choose a base branch
from

Conversation

LJiangTao
Copy link

该Pull Request关联的Issue

未关联任何 ISSUE

修改描述

在 JsqlParser 中对 SELECT id, output FROM <table> 时会提示 JSqlParseException. 而查询列中的 output 并非 MySQL 关键词, 但 JSqlParser 却对其进行了校验

测试用例

public class JSqlParserOutputTest {

    private String exceptionSQL = "SELECT output  FROM a";
    private String passSql = "SELECT `output`, out_put  FROM a";


    @Test(expected = JSQLParserException.class)
    public void exception() throws JSQLParserException, ParseException {

        var parse = new CCJSqlParser(exceptionSQL);

        var c = parse.getConfiguration();
        c.setValue(Feature.timeOut, 1000 * 1000 * 60);
        parse.withConfiguration(c);

        CCJSqlParserUtil.parseStatement(parse);
    }

    @Test
    public void passSql() throws JSQLParserException, ParseException {

        var parse = new CCJSqlParser(passSql);

        var c = parse.getConfiguration();
        c.setValue(Feature.timeOut, 1000 * 1000 * 60);
        parse.withConfiguration(c);

        CCJSqlParserUtil.parseStatement(parse);
    }

}

修复效果的截屏

@LJiangTao
Copy link
Author

com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor#beforeQuery 方法会对 SQL 进行校验.
由于 output 并非 MySQL 关键字, 所以在生成时并不会为其包装 (增加单引号). 在 JSqlParser 时就会抛出异常, 验证失败.
而且在 net.sf.jsqlparser.parser.CCJSqlParserConstants#K_OUTPUT 中包含了该关键字参数.

由于校验流程太过复杂, 虽然找到是没有增加单引号所致, 但具体在哪里校验没有找到. 切入口在 net.sf.jsqlparser.parser.CCJSqlParser#SelectItemsList

public class TenantLineInnerInterceptor extends BaseMultiTableInnerInterceptor implements InnerInterceptor {

    private TenantLineHandler tenantLineHandler;

    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        if (InterceptorIgnoreHelper.willIgnoreTenantLine(ms.getId())) {
            return;
        }
        PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql);

        // JSQLParser invoked here.
        mpBs.sql(parserSingle(mpBs.sql(), null));
    }

   // IGNORED 

@nieqiurong
Copy link
Contributor

这个待定 改这里不是一个方案.

@LJiangTao
Copy link
Author

的确, 可以试着增加一个特殊关键字组. 然后在 generator 时增加判断校验.

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

Successfully merging this pull request may close these issues.

None yet

3 participants