JSQLParser / JSqlParser

JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes. The generated hierarchy can be navigated using the Visitor Pattern
https://github.com/JSQLParser/JSqlParser/wiki
Apache License 2.0
5.43k stars 1.35k forks source link

[BUG] JSQLParser Version : RDBMS : 在整合mybatis的多租户时,join解析出现多个on,导致出现异常 #2094

Closed youglu closed 1 month ago

youglu commented 1 month ago

mybatis多租户版本 mybatis-plus-extension-3.4.0

问题描述 1:示例SQL如下 select a.name,a.age from emp a left join job b on id =a. job_id and job_name is not null 2:在多租户整合后(租户字段名为 t_id)并需要加入租户条件时变成 select a.name,a.age from emp a left join job b ON id =a. job_id and job_name is not null and t_id=1 ON id =a. job_id

原因分析 经分析定位两个地方 1:net.sf.jsqlparser.statement.select.Join.setOnExpression是或addOnExpression新增 @Deprecated public void setOnExpression(Expression expression) { onExpressions.add(0, expression); }

public Join addOnExpression(Expression expression) {
    onExpressions.add(expression);
    return this;
}

导致在转换sql时出现两个on 2: 在其toString方法处理on表达式时是在循环体中加 ON,导致有多个时就出现此问题 for (Expression onExpression: onExpressions) { builder.append(" ON ").append(onExpression); }

3:建议,以上两处随便调整一下即可解决问题。

manticore-projects commented 1 month ago

Greetings.

Everything works as expected because this feature covers the following:

SELECT *
FROM table1 tb1
    INNER JOIN table2 tb2
    INNER JOIN table3 tb3
    INNER JOIN table4 tb4
        ON ( tb3.aaa = tb4.aaa )
        ON ( tb2.aaa = tb3.aaa )
        ON ( tb1.aaa = tb2.aaa )
;
    @Test
    public void testJoinWithTrailingOnExpressionIssue1302() throws JSQLParserException {
        assertSqlCanBeParsedAndDeparsed("SELECT * FROM TABLE1 tb1\n" + "INNER JOIN TABLE2 tb2\n"
                + "INNER JOIN TABLE3 tb3\n" + "INNER JOIN TABLE4 tb4\n" + "ON (tb3.aaa = tb4.aaa)\n"
                + "ON (tb2.aaa = tb3.aaa)\n" + "ON (tb1.aaa = tb2.aaa)", true);

        assertSqlCanBeParsedAndDeparsed("SELECT *\n" + "FROM\n" + "TABLE1 tbl1\n"
                + "    INNER JOIN TABLE2 tbl2\n" + "        INNER JOIN TABLE3 tbl3\n"
                + "        ON (tbl2.column1 = tbl3.column1)\n"
                + "    ON (tbl1.column2 = tbl2.column2)\n" + "WHERE\n" + "tbl1.column1 = 123",
                true);
    }