GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — [BUG] JSQLParser Version : 4.0 : ON condition not parsed correctly when modifying SQL with natural join · Issue #2016 · JSQLParser/JSqlParser Closed as not planned Closed as not planned Copy link DescriptionDescription Hi, I am trying to dynamically add condition statements to SQL using JSqlparser. However, it seems there is an issue when the SQL contains a natural join: public static void main(String[] args) throws JSQLParserException { String sql = "SELECT * FROM A a, B b where a.id = b.id"; Statement statement = CCJSqlParserUtil.parse(sql, null); PlainSelect plainSelect = (PlainSelect) ((Select) statement).getSelectBody(); Join join = CollUtil.getFirst(plainSelect.getJoins()); // append a condition "a.id = '100'" join.setOnExpression(new EqualsTo(new Column("a.id"), new StringValue("100"))); // = "SELECT * FROM A a, B b WHERE a.id = b.id" System.out.println(statement.toString()); } I am not sure how JSqlparser handles this situation, but Analysis I tried debugging to find the root cause of this issue. I noticed that in the above code, the query of table B is parsed into a After setting the ON condition with @Override public String toString() { if (isSimple() && isOuter()) { return "OUTER " + rightItem; } else if (isSimple()) { return "" + rightItem; } else { // isSimple() returns true, so this branch is ignored String type = ""; if (isRight()) { type += "RIGHT "; } else if (isNatural()) { type += "NATURAL "; } else if (isFull()) { type += "FULL "; } else if (isLeft()) { type += "LEFT "; } else if (isCross()) { type += "CROSS "; } if (isOuter()) { type += "OUTER "; } else if (isInner()) { type += "INNER "; } else if (isSemi()) { type += "SEMI "; } if (isStraight()) { type = "STRAIGHT_JOIN "; } else if (isApply()) { type += "APPLY "; } else { type += "JOIN "; } return type + rightItem + ((joinWindow != null) ? " WITHIN " + joinWindow : "") + ((onExpression != null) ? " ON " + onExpression + "" : "") + PlainSelect.getFormatedList(usingColumns, "USING", true, true); } } Fix Calling public static void main(String[] args) throws JSQLParserException { String sql = "SELECT * FROM A a, B b where a.id = b.id"; Statement statement = CCJSqlParserUtil.parse(sql, null); PlainSelect plainSelect = (PlainSelect) ((Select) statement).getSelectBody(); Join join = CollUtil.getFirst(plainSelect.getJoins()); join.setOnExpression(new EqualsTo(new Column("a.id"), new StringValue("100"))); join.setSimple(false); // set Join.simple to false // SELECT * FROM A a JOIN B b ON a.id = '100' WHERE a.id = b.id System.out.println(statement.toString()); } In fact, if the ON condition is not null,
Activity
MetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |