Ebean的RawSql不允许在”case when”子句中使用命名参数。

huangapple 未分类评论43阅读模式
英文:

Ebean RawSql does not allow named parameters in "case when" clause

问题

我在使用Ebean RawSql查询时遇到了一个问题。以下是一个简单的示例:

Finder<Long, Test> finder = new Finder<>(Test.class);

String query = "SELECT t.ID AS t_id, t.NAME as t_name, (SELECT CASE WHEN t.NAME = :name THEN 1 ELSE 0 END) AS sort_priority FROM test ORDER BY sort_priority";

RawSql raw = RawSqlBuilder.parse(query)
    .columnMapping("t_id", "id")
    .columnMapping("t_name", "name")
    .columnMappingIgnore("sort_priority")
    .create();

Query<Test> testQuery = finder.query().setRawSql(raw).setParameter("name", "Test1");

但是,当我执行这个查询时,出现了一个错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':name THEN 1 ELSE 0 END) AS sort_priority FROM test  ORDER BY sort_priorit' at line 1

如果我在查询中使用问号(?)代替命名参数,一切都正常。但我是否可以在保持命名参数的同时修复这个问题,并且同时使用 "case when" 子句呢?

英文:

I've encountered a problem while working with Ebean RawSql query. Here's a simple example.

Finder&lt;Long, Test&gt; finder= new Finder&lt;&gt;(Test.class);

String query = &quot;SELECT t.ID AS t_id, t.NAME as t_name, (SELECT CASE WHEN t.NAME = :name THEN 1 ELSE 0 END) AS sort_priority FROM test ORDER BY sort_priority&quot;;

RawSql raw = RawSqlBuilder.parse(query)
.columnMapping(&quot;t_id&quot;, &quot;id&quot;)
.columnMapping(&quot;t_name&quot;, &quot;name&quot;)
.columnMappingIgnore(&quot;sort_priority&quot;)
.create();
        
Query&lt;Test&gt; testQuery = finder.query().setRawSql(raw).setParameter(&quot;name&quot;, &quot;Test1&quot;);

But when I execute this query I get an error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#39;:name THEN 1 ELSE 0 END) AS sort_priority FROM test  ORDER BY sort_priorit&#39; at line 1

If I use ? instead of named parameters everything is okay. But can I somehow fix it with keeping named parameter and "case when" clause at the same time?

huangapple
  • 本文由 发表于 2020年7月24日 00:59:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/63059460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定