Apply requested changes

This commit is contained in:
Christian Beikov 2021-02-24 14:24:21 +01:00
parent 003fddccb5
commit 1ea675d58d
7 changed files with 26 additions and 10 deletions

View File

@ -17,7 +17,6 @@ import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescript
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators; import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.query.sqm.produce.function.internal.PatternRenderer; import org.hibernate.query.sqm.produce.function.internal.PatternRenderer;
import org.hibernate.sql.ast.SqlAstNodeRenderingMode;
import org.hibernate.sql.ast.SqlAstTranslator; import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.spi.SqlAppender; import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.SqlAstNode; import org.hibernate.sql.ast.tree.SqlAstNode;
@ -64,7 +63,7 @@ public class CastFunction extends AbstractSqmSelfRenderingFunctionDescriptor {
String cast = dialect.castPattern( sourceType, targetType ); String cast = dialect.castPattern( sourceType, targetType );
new PatternRenderer( cast, SqlAstNodeRenderingMode.DEFAULT ).render( sqlAppender, arguments, walker ); new PatternRenderer( cast ).render( sqlAppender, arguments, walker );
} }
private CastType getCastType(JdbcMapping sourceMapping) { private CastType getCastType(JdbcMapping sourceMapping) {

View File

@ -13,7 +13,6 @@ import org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression;
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators; import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.query.sqm.produce.function.internal.PatternRenderer; import org.hibernate.query.sqm.produce.function.internal.PatternRenderer;
import org.hibernate.sql.ast.SqlAstNodeRenderingMode;
import org.hibernate.sql.ast.SqlAstTranslator; import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.spi.SqlAppender; import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.SqlAstNode; import org.hibernate.sql.ast.tree.SqlAstNode;
@ -56,7 +55,7 @@ public class TimestampaddFunction
TypeConfiguration.getSqlTemporalType( to.getExpressionType() ) TypeConfiguration.getSqlTemporalType( to.getExpressionType() )
); );
new PatternRenderer( pattern, SqlAstNodeRenderingMode.DEFAULT ).render( sqlAppender, arguments, walker ); new PatternRenderer( pattern ).render( sqlAppender, arguments, walker );
} }
// @Override // @Override

View File

@ -13,7 +13,6 @@ import org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression;
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators; import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.query.sqm.produce.function.internal.PatternRenderer; import org.hibernate.query.sqm.produce.function.internal.PatternRenderer;
import org.hibernate.sql.ast.SqlAstNodeRenderingMode;
import org.hibernate.sql.ast.SqlAstTranslator; import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.spi.SqlAppender; import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.SqlAstNode; import org.hibernate.sql.ast.tree.SqlAstNode;
@ -59,7 +58,7 @@ public class TimestampdiffFunction
TypeConfiguration.getSqlTemporalType( to.getExpressionType() ) TypeConfiguration.getSqlTemporalType( to.getExpressionType() )
); );
new PatternRenderer( pattern, SqlAstNodeRenderingMode.DEFAULT ).render( sqlAppender, arguments, walker ); new PatternRenderer( pattern ).render( sqlAppender, arguments, walker );
} }
// @Override // @Override

View File

@ -12,7 +12,6 @@ import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescript
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators; import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.query.sqm.produce.function.internal.PatternRenderer; import org.hibernate.query.sqm.produce.function.internal.PatternRenderer;
import org.hibernate.sql.ast.SqlAstNodeRenderingMode;
import org.hibernate.sql.ast.SqlAstTranslator; import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.spi.SqlAppender; import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.SqlAstNode; import org.hibernate.sql.ast.tree.SqlAstNode;
@ -48,8 +47,7 @@ public class TrimFunction extends AbstractSqmSelfRenderingFunctionDescriptor {
String trim = dialect.trimPattern( specification, trimCharacter ); String trim = dialect.trimPattern( specification, trimCharacter );
new PatternRenderer( trim, SqlAstNodeRenderingMode.DEFAULT ) new PatternRenderer( trim ).render( sqlAppender, Collections.singletonList( sourceExpr ), walker );
.render( sqlAppender, Collections.singletonList( sourceExpr ), walker );
} }
// @Override // @Override

View File

@ -32,6 +32,10 @@ public class PatternRenderer {
private final int maxParamIndex; private final int maxParamIndex;
private final SqlAstNodeRenderingMode argumentRenderingMode; private final SqlAstNodeRenderingMode argumentRenderingMode;
public PatternRenderer(String pattern) {
this( pattern, SqlAstNodeRenderingMode.DEFAULT );
}
/** /**
* Constructs a template renderer * Constructs a template renderer
* *

View File

@ -6,8 +6,20 @@
*/ */
package org.hibernate.sql.ast; package org.hibernate.sql.ast;
import org.hibernate.sql.ast.tree.SqlAstNode;
/** /**
* The rendering mode to use for {@link org.hibernate.sql.ast.tree.SqlAstNode}.
*
* Some functions/contexts require the use of literals/expressions rather than parameters
* like for example the `char` function in Derby which requires the length as literal.
*
* Another example is a function that renders a function argument into a subquery select and group by item.
* It can use {@link #INLINE_PARAMETERS} so that a database can match such a select item to a group by item.
* Without this, such queries would result in a query error.
*
* @author Christian Beikov * @author Christian Beikov
* @see SqlAstTranslator#render(SqlAstNode, SqlAstNodeRenderingMode)
*/ */
public enum SqlAstNodeRenderingMode { public enum SqlAstNodeRenderingMode {
/** /**
@ -17,11 +29,15 @@ public enum SqlAstNodeRenderingMode {
/** /**
* Render parameters as literals. * Render parameters as literals.
* All parameters within the {@link org.hibernate.sql.ast.tree.SqlAstNode} are rendered as literals.
*/ */
INLINE_PARAMETERS, INLINE_PARAMETERS,
/** /**
* Don't render plain parameters. Render it as literal or as expression. * Don't render plain parameters. Render it as literal or as expression.
* If the {@link org.hibernate.sql.ast.tree.SqlAstNode} to render is a parameter,
* it will be rendered either as literal or wrapped into a semantically equivalent expression
* such that it doesn't appear as plain parameter.
*/ */
NO_PLAIN_PARAMETER NO_PLAIN_PARAMETER
} }

View File

@ -124,8 +124,9 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation>
case Types.FLOAT: case Types.FLOAT:
case Types.NUMERIC: case Types.NUMERIC:
case Types.DECIMAL: case Types.DECIMAL:
appendSql( '(' );
sqlAstNode.accept( this ); sqlAstNode.accept( this );
appendSql( "+0" ); appendSql( "+0)" );
break; break;
case Types.CHAR: case Types.CHAR:
case Types.VARCHAR: case Types.VARCHAR: