You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow NamedParameterJdbcTemplate to optionally include parameters in comments of SQL #22255
NamedParameterJdbcTemplate skips parameters in comments by the logic in NamedParameterUtils.parseSqlStatement(String sql)
...
intskipToPosition = i;
while (i < statement.length) {
skipToPosition = skipCommentsAndQuotes(statement, i);
if (i == skipToPosition) {
break;
}
else {
i = skipToPosition;
}
}
...
I use NamedParameterJdbcTemplate to run SQLs on our own DBMS engine in the company.
I have special cases to use parameters in comment, for example, "/*@ queryCkey(:cafeId) */ SELECT ... ",
If it is allowed in NamedParameterJdbcTemplate, it will be very helpful for our system.
( For your information, MyBatis allows parameters in comments of SQL)
I'd like you to consider adding an optional flag to support this.
Setting this kind of parameters is not supported by PreparedStatements, because they change the execution plan of the query. Cause major databases support which support prepared-statements just parse the query once, so they don't accept placeholders for what changes the execution plan.
@pashazadeh
I understand your concerns. It is reasonable default to disable parameters in comments.
However, if it is customizable, it will be very helpful for developers who use a special DBMS like us.
If the JDBC driver allows it, I think it's unnatural to prevent it in the Spring framework. So, MyBatis also seems to allow this.
Since Spring framework is traditionally highly customizable, I expect that.
Or.. I think it can be solved by making skipCommentsAndQuotes protected and non-static so that it can be extended in the sub-class of NamedParemterJdbcTemplate .
changed the title [-] Allow NamedParemterJdbcTemplate to optionally include parameters in comments of SQL[/-][+] Allow NamedParameterJdbcTemplate to optionally include parameters in comments of SQL[/+]on Jul 19, 2020
The problem is that we delegate everything to NamedParameterUtils that is static at the moment. I think we'd probably need to make it non static and offer some way to customize it.
With the interesting feedback of @pashazadeh, I am wondering though if it would be a good idea to support it.
@benelog can't you use simple ? for those advanced use cases, rather than named parameters?
@snicoll
Unfortunately, all queries in our system require parameters within SQL hints.
We use our company's own DBMS system, and this DBMS requires that the shard key be included in the hint.
The cost of giving up the NamedParameterJdbcTemplate is high in this case.
We are currently working around this issue by creating a class that inherited the NamedParameterJdbcTemplate.
It is also possible to maintain this workaround.
I raised this issue because there is an internal opinion that it would be beneficial if NamedParameterJdbcTemplate could directly support what is already allowed in MyBatis.
If we are the only ones who want this feature, it would be okay for you to consider implementing it when other users request it too.
Activity
pashazadeh commentedon Jan 17, 2019
Setting this kind of parameters is not supported by PreparedStatements, because they change the execution plan of the query. Cause major databases support which support prepared-statements just parse the query once, so they don't accept placeholders for what changes the execution plan.
benelog commentedon Jan 21, 2019
@pashazadeh
I understand your concerns. It is reasonable default to disable parameters in comments.
However, if it is customizable, it will be very helpful for developers who use a special DBMS like us.
If the JDBC driver allows it, I think it's unnatural to prevent it in the Spring framework. So, MyBatis also seems to allow this.
Since Spring framework is traditionally highly customizable, I expect that.
junoyoon commentedon Jan 21, 2019
Or.. I think it can be solved by making
skipCommentsAndQuotesprotected and non-static so that it can be extended in the sub-class of NamedParemterJdbcTemplate .[-] Allow NamedParemterJdbcTemplate to optionally include parameters in comments of SQL[/-][+] Allow NamedParameterJdbcTemplate to optionally include parameters in comments of SQL[/+]snicoll commentedon Jan 8, 2024
The problem is that we delegate everything to
NamedParameterUtilsthat is static at the moment. I think we'd probably need to make it non static and offer some way to customize it.With the interesting feedback of @pashazadeh, I am wondering though if it would be a good idea to support it.
@benelog can't you use simple
?for those advanced use cases, rather than named parameters?benelog commentedon Jan 9, 2024
@snicoll
Unfortunately, all queries in our system require parameters within SQL hints.
We use our company's own DBMS system, and this DBMS requires that the shard key be included in the hint.
The cost of giving up the NamedParameterJdbcTemplate is high in this case.
We are currently working around this issue by creating a class that inherited the NamedParameterJdbcTemplate.
It is also possible to maintain this workaround.
I raised this issue because there is an internal opinion that it would be beneficial if NamedParameterJdbcTemplate could directly support what is already allowed in MyBatis.
If we are the only ones who want this feature, it would be okay for you to consider implementing it when other users request it too.
snicoll commentedon Jan 11, 2024
Can you share your workaround?
benelog commentedon Jan 14, 2024
@snicoll
I'm sharing it with you for reference to the code below.
I think it's just workaround, not a natural way.
Add option to parse named parameters in SQL comments
bclozel commentedon Oct 23, 2026
Superseded by ##37088