-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
support custom where sql construction logic #2010
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution. Please consider the recommended changes and then I would merge it.
src/main/java/net/sf/jsqlparser/util/deparser/DeleteDeParser.java
Outdated
Show resolved
Hide resolved
src/main/java/net/sf/jsqlparser/util/deparser/DeleteDeParser.java
Outdated
Show resolved
Hide resolved
src/main/java/net/sf/jsqlparser/util/deparser/MergeDeParser.java
Outdated
Show resolved
Hide resolved
src/main/java/net/sf/jsqlparser/util/deparser/MergeDeParser.java
Outdated
Show resolved
Hide resolved
src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java
Outdated
Show resolved
Hide resolved
@manticore-projects thanks, updated deparseDistinctClause、deparseSelectItemsClause、deparseOrderByElementsClause requires two parameters ,
|
Greetings. In my opinion, your approach is flawed. Instead of tampering with the Those are specific solutions though that won't fit well into the generic de-parser. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I won't agree with the StringBuffer based WHERE
clause exception. Rest is ok though and I would merge it.
@@ -92,7 +92,11 @@ public void deParse(Delete delete) { | |||
|
|||
if (delete.getWhere() != null) { | |||
buffer.append(" WHERE "); | |||
delete.getWhere().accept(expressionVisitor); | |||
int len = buffer.length(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove those specific parts from the PR as it does not fit into the generic Deparser.
@@ -251,7 +225,11 @@ public void visit(PlainSelect plainSelect) { | |||
|
|||
if (plainSelect.getWhere() != null) { | |||
buffer.append(" WHERE "); | |||
int len = buffer.length(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove those specific parts from the PR as it does not fit into the generic Deparser.
@@ -90,7 +90,11 @@ public void deParse(Update update) { | |||
|
|||
if (update.getWhere() != null) { | |||
buffer.append(" WHERE "); | |||
update.getWhere().accept(expressionVisitor); | |||
int len = buffer.length(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove those specific parts from the PR as it does not fit into the generic Deparser.
Please fix QA exceptions and then I would merge. |
@manticore-projects thanks, I add deparseWhereClause like before。 class SqlTemple {
private final Statement sqlStatement;
public SqlTemple() throws ParseException {
String sql = "SELECT * FROM user WHERE name = :param_name AND status = :param_status";
CCJSqlParser sqlParser = new CCJSqlParser(sql).withAllowComplexParsing(true);
this.sqlStatement = sqlParser.SingleStatement();
}
public String createJdbcSql(Map<String, Object> map) {
StringBuilder buffer = new StringBuilder();
MyExprDeParser myParser = new MyExprDeParser (buffer, map);
SelectDeParser parser = new SelectDeParser();
StatementDeParser deParser = new StatementDeParser(myParser, parser, buffer);
sqlStatement.accept(deParser);
//map is empty return "SELECT * FROM user"
//map is {"param_name":"aa"} reutrn "SELECT * FROM user WHERE name = :param_name"
//map is {"param_status":2} reutrn "SELECT * FROM user WHERE status = :param_status"
//map is {"param_name":"aa","param_status":2} reutrn "SELECT * FROM user WHERE name = :param_name AND status = :param_status"
return buffer.toString();
}
} |
My framework source: https://github.com/redkale/redkale-plugins/tree/main/src/main/java/org/redkalex/source/parser public interface ForumInfoMapper extends BaseMapper<ForumInfo> {
@Sql("SELECT f.forum_groupid, s.forum_section_color "
+ "FROM forum_info f, forum_section s "
+ " WHERE f.forumid = s.forumid AND "
+ "s.forum_sectionid = #{bean.forumSectionid} AND "
+ "f.forumid = #{bean.forumid} AND s.forum_section_color = #{bean.forumSectionColor}")
public ForumResult findForumResult(ForumBean bean);
@Sql("SELECT f.forum_groupid, s.forum_section_color "
+ "FROM forum_info f, forum_section s "
+ " WHERE f.forumid = s.forumid AND "
+ "s.forum_sectionid = #{bean.forumSectionid} AND "
+ "f.forumid = #{bean.forumid} AND s.forum_section_color = #{bean.forumSectionColor}")
public CompletableFuture<ForumResult> findForumResultAsync(ForumBean bean);
} |
QA still failing. |
Before you push, please run the QA locally via |
Although I still believe that your approach is not best practise. Rather than tampering with the select *
from t
where ( :param1 is null or column1= :param1) AND ( :param2 IS NULL or column2 = :param2 ) Then you can set the parameters |
I hope that XxxDeParser can overload this part of the logic when building the where condition sql statement. My requirement is to dynamically build the filter conditions in where according to the parameters.