From 801974fa4c8abb2865a4aeff525fc29638e10f72 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Fri, 18 Nov 2016 10:25:17 +0100 Subject: [PATCH] Fix for HHH-11258 --- hibernate-core/src/main/antlr/sql-gen.g | 23 ++++++++++++++----- .../hql/internal/ast/SqlGenerator.java | 6 ++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/antlr/sql-gen.g b/hibernate-core/src/main/antlr/sql-gen.g index 45bc5b442a..fd70c3a08e 100644 --- a/hibernate-core/src/main/antlr/sql-gen.g +++ b/hibernate-core/src/main/antlr/sql-gen.g @@ -28,7 +28,7 @@ options { private StringBuilder buf = new StringBuilder(); private boolean captureExpression = false; - private StringBuilder expr = new StringBuilder(); + protected java.util.List exprs = new java.util.ArrayList(java.util.Arrays.asList(new StringBuilder())); protected void out(String s) { getStringBuilder().append( s ); @@ -75,7 +75,7 @@ options { } protected StringBuilder getStringBuilder() { - return captureExpression ? expr : buf; + return captureExpression ? exprs.get(exprs.size() - 1) : buf; } protected void nyi(AST n) { @@ -97,16 +97,27 @@ options { } protected void captureExpressionStart() { - captureExpression = true; + if ( captureExpression ) { + exprs.add( new StringBuilder() ); + } else { + captureExpression = true; + } } protected void captureExpressionFinish() { - captureExpression = false; + // Capturing will only stop when we leave the last capture context + if ( exprs.size() == 1 ) { + captureExpression = false; + } } protected String resetCapture() { - final String expression = expr.toString(); - expr = new StringBuilder(); + StringBuilder sb = exprs.remove( exprs.size() - 1 ); + final String expression = sb.toString(); + if ( exprs.isEmpty() ) { + sb.setLength(0); + exprs.add( sb ); + } return expression; } diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/SqlGenerator.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/SqlGenerator.java index ec1accddad..6f6a0ea208 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/SqlGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/SqlGenerator.java @@ -102,7 +102,11 @@ public class SqlGenerator extends SqlGeneratorBase implements ErrorReporter { @Override protected void out(String s) { - writer.clause( s ); + if ( exprs.size() > 1 ) { + super.out( s ); + } else { + writer.clause( s ); + } } @Override