-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Panache: support generating count queries with more than one column
Using a subselect. Only useful for HR until they support count queries, but it was easy enough to fix
- Loading branch information
Showing
5 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
.../src/main/java/io/quarkus/panache/hibernate/common/runtime/RequiresSubqueryException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.quarkus.panache.hibernate.common.runtime; | ||
|
||
public class RequiresSubqueryException extends RuntimeException { | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...src/main/java/io/quarkus/panache/hibernate/common/runtime/SubQueryAliasParserVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.quarkus.panache.hibernate.common.runtime; | ||
|
||
import org.antlr.v4.runtime.tree.TerminalNode; | ||
import org.hibernate.grammars.hql.HqlParser.JoinContext; | ||
import org.hibernate.grammars.hql.HqlParser.QueryOrderContext; | ||
import org.hibernate.grammars.hql.HqlParser.SelectionContext; | ||
import org.hibernate.grammars.hql.HqlParser.SimpleQueryGroupContext; | ||
import org.hibernate.grammars.hql.HqlParserBaseVisitor; | ||
|
||
public class SubQueryAliasParserVisitor extends HqlParserBaseVisitor<String> { | ||
|
||
private int inSimpleQueryGroup; | ||
private StringBuilder sb = new StringBuilder(); | ||
private int counter; | ||
|
||
@Override | ||
public String visitSimpleQueryGroup(SimpleQueryGroupContext ctx) { | ||
inSimpleQueryGroup++; | ||
try { | ||
return super.visitSimpleQueryGroup(ctx); | ||
} finally { | ||
inSimpleQueryGroup--; | ||
} | ||
} | ||
|
||
@Override | ||
public String visitSelection(SelectionContext ctx) { | ||
super.visitSelection(ctx); | ||
if (inSimpleQueryGroup == 1) { | ||
if (ctx.variable() == null) { | ||
sb.append(" as __v" + counter++); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String visitJoin(JoinContext ctx) { | ||
if (inSimpleQueryGroup == 1 && ctx.FETCH() != null) { | ||
// ignore fetch joins for main query | ||
return null; | ||
} | ||
return super.visitJoin(ctx); | ||
} | ||
|
||
@Override | ||
public String visitQueryOrder(QueryOrderContext ctx) { | ||
if (inSimpleQueryGroup == 1) { | ||
// ignore order/limit/offset for main query | ||
return null; | ||
} | ||
return super.visitQueryOrder(ctx); | ||
} | ||
|
||
@Override | ||
public String visitTerminal(TerminalNode node) { | ||
append(node.getText()); | ||
return null; | ||
} | ||
|
||
@Override | ||
protected String defaultResult() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected String aggregateResult(String aggregate, String nextResult) { | ||
if (nextResult != null) { | ||
append(nextResult); | ||
} | ||
return null; | ||
} | ||
|
||
private void append(String nextResult) { | ||
// don't add space at start, or around dots, commas | ||
if (!sb.isEmpty() && sb.charAt(sb.length() - 1) != '.' && !nextResult.equals(".") && !nextResult.equals(",")) { | ||
sb.append(" "); | ||
} | ||
sb.append(nextResult); | ||
} | ||
|
||
public String result() { | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters