Cleanup case when handling and implement parameter handling for case result arms
This commit is contained in:
parent
badc99705a
commit
4f6019d524
|
@ -154,22 +154,7 @@ public class DB2SqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAst
|
|||
|
||||
@Override
|
||||
protected void renderSelectExpression(Expression expression) {
|
||||
// Null literals have to be casted in the select clause
|
||||
if ( expression instanceof Literal ) {
|
||||
final Literal literal = (Literal) expression;
|
||||
if ( literal.getLiteralValue() == null ) {
|
||||
renderCasted( literal );
|
||||
}
|
||||
else {
|
||||
renderLiteral( literal, true );
|
||||
}
|
||||
}
|
||||
else if ( expression instanceof NullnessLiteral || expression instanceof JdbcParameter || expression instanceof SqmParameterInterpretation ) {
|
||||
renderCasted( expression );
|
||||
}
|
||||
else {
|
||||
expression.accept( this );
|
||||
}
|
||||
renderSelectExpressionWithCastedOrInlinedPlainParameters( expression );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -11,15 +11,18 @@ import java.util.List;
|
|||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.query.ComparisonOperator;
|
||||
import org.hibernate.query.sqm.sql.internal.SqmParameterInterpretation;
|
||||
import org.hibernate.sql.ast.SqlAstWalker;
|
||||
import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
|
||||
import org.hibernate.sql.ast.spi.SqlSelection;
|
||||
import org.hibernate.sql.ast.tree.Statement;
|
||||
import org.hibernate.sql.ast.tree.cte.CteContainer;
|
||||
import org.hibernate.sql.ast.tree.cte.CteStatement;
|
||||
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression;
|
||||
import org.hibernate.sql.ast.tree.expression.Expression;
|
||||
import org.hibernate.sql.ast.tree.expression.JdbcParameter;
|
||||
import org.hibernate.sql.ast.tree.expression.Literal;
|
||||
import org.hibernate.sql.ast.tree.expression.NullnessLiteral;
|
||||
import org.hibernate.sql.ast.tree.expression.QueryLiteral;
|
||||
import org.hibernate.sql.ast.tree.expression.SqlTuple;
|
||||
import org.hibernate.sql.ast.tree.expression.Summarization;
|
||||
import org.hibernate.sql.ast.tree.select.QueryPart;
|
||||
|
@ -93,22 +96,7 @@ public class DerbySqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
|
|||
|
||||
@Override
|
||||
protected void renderSelectExpression(Expression expression) {
|
||||
// Null literals have to be casted in the select clause
|
||||
if ( expression instanceof Literal ) {
|
||||
final Literal literal = (Literal) expression;
|
||||
if ( literal.getLiteralValue() == null ) {
|
||||
renderCasted( literal );
|
||||
}
|
||||
else {
|
||||
renderLiteral( literal, true );
|
||||
}
|
||||
}
|
||||
else if ( expression instanceof NullnessLiteral || expression instanceof JdbcParameter || expression instanceof SqmParameterInterpretation ) {
|
||||
renderCasted( expression );
|
||||
}
|
||||
else {
|
||||
expression.accept( this );
|
||||
}
|
||||
renderSelectExpressionWithCastedOrInlinedPlainParameters( expression );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -49,7 +49,6 @@ import org.hibernate.mapping.Constraint;
|
|||
import org.hibernate.mapping.ForeignKey;
|
||||
import org.hibernate.mapping.Index;
|
||||
import org.hibernate.mapping.Table;
|
||||
import org.hibernate.metamodel.mapping.CompositeIdentifierMapping;
|
||||
import org.hibernate.metamodel.mapping.EntityMappingType;
|
||||
import org.hibernate.metamodel.mapping.SqlExpressable;
|
||||
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
|
||||
|
@ -66,15 +65,12 @@ import org.hibernate.query.sqm.mutation.internal.idtable.AfterUseAction;
|
|||
import org.hibernate.query.sqm.mutation.internal.idtable.IdTable;
|
||||
import org.hibernate.query.sqm.mutation.internal.idtable.PersistentTableStrategy;
|
||||
import org.hibernate.query.sqm.mutation.internal.idtable.PhysicalIdTableExporter;
|
||||
import org.hibernate.query.sqm.mutation.internal.inline.InlineStrategy;
|
||||
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
|
||||
import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.hibernate.sql.*;
|
||||
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
|
||||
import org.hibernate.sql.ast.spi.ANSICaseExpressionWalker;
|
||||
import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
|
||||
import org.hibernate.sql.ast.spi.CaseExpressionWalker;
|
||||
import org.hibernate.sql.ast.spi.StandardSqlAstTranslatorFactory;
|
||||
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl;
|
||||
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorNoOpImpl;
|
||||
|
@ -2185,17 +2181,6 @@ public abstract class Dialect implements ConversionContext {
|
|||
return new ANSICaseFragment();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link CaseExpressionWalker} responsible
|
||||
* for handling this dialect's variations in how CASE statements are
|
||||
* handled.
|
||||
*
|
||||
* @return This dialect's {@link CaseFragment} strategy.
|
||||
*/
|
||||
public CaseExpressionWalker getCaseExpressionWalker() {
|
||||
return ANSICaseExpressionWalker.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* The fragment used to insert a row without specifying any column values.
|
||||
* This is not possible on some databases.
|
||||
|
|
|
@ -134,22 +134,7 @@ public class FirebirdSqlAstTranslator<T extends JdbcOperation> extends AbstractS
|
|||
|
||||
@Override
|
||||
protected void renderSelectExpression(Expression expression) {
|
||||
// Null literals have to be casted in the select clause
|
||||
if ( expression instanceof Literal ) {
|
||||
final Literal literal = (Literal) expression;
|
||||
if ( literal.getLiteralValue() == null ) {
|
||||
renderCasted( literal );
|
||||
}
|
||||
else {
|
||||
renderLiteral( literal, true );
|
||||
}
|
||||
}
|
||||
else if ( expression instanceof NullnessLiteral || expression instanceof JdbcParameter || expression instanceof SqmParameterInterpretation ) {
|
||||
renderCasted( expression );
|
||||
}
|
||||
else {
|
||||
expression.accept( this );
|
||||
}
|
||||
renderSelectExpressionWithCastedOrInlinedPlainParameters( expression );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -58,22 +58,7 @@ public class HSQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAs
|
|||
|
||||
@Override
|
||||
protected void renderSelectExpression(Expression expression) {
|
||||
// Null literals have to be casted in the select clause
|
||||
if ( expression instanceof Literal ) {
|
||||
final Literal literal = (Literal) expression;
|
||||
if ( literal.getLiteralValue() == null ) {
|
||||
renderCasted( literal );
|
||||
}
|
||||
else {
|
||||
renderLiteral( literal, true );
|
||||
}
|
||||
}
|
||||
else if ( expression instanceof NullnessLiteral || expression instanceof JdbcParameter || expression instanceof SqmParameterInterpretation ) {
|
||||
renderCasted( expression );
|
||||
}
|
||||
else {
|
||||
expression.accept( this );
|
||||
}
|
||||
renderSelectExpressionWithCastedOrInlinedPlainParameters( expression );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -14,6 +14,7 @@ import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
|
|||
import org.hibernate.sql.ast.spi.SqlSelection;
|
||||
import org.hibernate.sql.ast.tree.Statement;
|
||||
import org.hibernate.sql.ast.tree.cte.CteStatement;
|
||||
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression;
|
||||
import org.hibernate.sql.ast.tree.expression.Expression;
|
||||
import org.hibernate.sql.ast.tree.expression.Literal;
|
||||
import org.hibernate.sql.ast.tree.expression.SqlTuple;
|
||||
|
@ -60,6 +61,17 @@ public class MaxDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
|
|||
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void visitCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, boolean inSelect) {
|
||||
// Couldn't find documentation for older versions, but 7.7 supports ANSI style case expressions
|
||||
if ( getDialect().getVersion() < 770 ) {
|
||||
visitDecodeCaseSearchedExpression( caseSearchedExpression );
|
||||
}
|
||||
else {
|
||||
visitAnsiCaseSearchedExpression( caseSearchedExpression );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void renderPartitionItem(Expression expression) {
|
||||
if ( expression instanceof Literal ) {
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.hibernate.sql.ast.Clause;
|
|||
import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
|
||||
import org.hibernate.sql.ast.spi.SqlSelection;
|
||||
import org.hibernate.sql.ast.tree.Statement;
|
||||
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression;
|
||||
import org.hibernate.sql.ast.tree.expression.Expression;
|
||||
import org.hibernate.sql.ast.tree.expression.Literal;
|
||||
import org.hibernate.sql.ast.tree.expression.SqlTuple;
|
||||
|
@ -123,6 +124,17 @@ public class OracleSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
|
|||
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void visitCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, boolean inSelect) {
|
||||
// Oracle did not add support for CASE until 9i
|
||||
if ( getDialect().getVersion() < 900 ) {
|
||||
visitDecodeCaseSearchedExpression( caseSearchedExpression );
|
||||
}
|
||||
else {
|
||||
visitAnsiCaseSearchedExpression( caseSearchedExpression );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void renderPartitionItem(Expression expression) {
|
||||
if ( expression instanceof Literal ) {
|
||||
|
|
|
@ -16,6 +16,7 @@ import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
|
|||
import org.hibernate.sql.ast.spi.SqlSelection;
|
||||
import org.hibernate.sql.ast.tree.Statement;
|
||||
import org.hibernate.sql.ast.tree.cte.CteStatement;
|
||||
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression;
|
||||
import org.hibernate.sql.ast.tree.expression.Expression;
|
||||
import org.hibernate.sql.ast.tree.expression.Literal;
|
||||
import org.hibernate.sql.ast.tree.expression.SqlTuple;
|
||||
|
@ -86,6 +87,11 @@ public class RDBMSOS2200SqlAstTranslator<T extends JdbcOperation> extends Abstra
|
|||
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void visitCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, boolean inSelect) {
|
||||
visitDecodeCaseSearchedExpression( caseSearchedExpression );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void renderPartitionItem(Expression expression) {
|
||||
if ( expression instanceof Literal ) {
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.sql.ast.spi;
|
||||
|
||||
import org.hibernate.sql.ast.SqlAstWalker;
|
||||
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression;
|
||||
import org.hibernate.sql.ast.tree.expression.Expression;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
public class ANSICaseExpressionWalker implements CaseExpressionWalker {
|
||||
|
||||
public static ANSICaseExpressionWalker INSTANCE = new ANSICaseExpressionWalker();
|
||||
|
||||
public void visitCaseSearchedExpression(
|
||||
CaseSearchedExpression caseSearchedExpression,
|
||||
StringBuilder sqlBuffer,
|
||||
SqlAstWalker sqlAstWalker){
|
||||
sqlBuffer.append( "case" );
|
||||
|
||||
for ( CaseSearchedExpression.WhenFragment whenFragment : caseSearchedExpression.getWhenFragments() ) {
|
||||
sqlBuffer.append( " when " );
|
||||
whenFragment.getPredicate().accept( sqlAstWalker );
|
||||
sqlBuffer.append( " then " );
|
||||
whenFragment.getResult().accept( sqlAstWalker );
|
||||
}
|
||||
|
||||
Expression otherwise = caseSearchedExpression.getOtherwise();
|
||||
if ( otherwise != null ) {
|
||||
sqlBuffer.append( " else " );
|
||||
otherwise.accept( sqlAstWalker );
|
||||
}
|
||||
|
||||
sqlBuffer.append( " end" );
|
||||
}
|
||||
}
|
|
@ -2496,6 +2496,31 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
|
|||
expression.accept( this );
|
||||
}
|
||||
|
||||
protected void renderSelectExpressionWithCastedOrInlinedPlainParameters(Expression expression) {
|
||||
// Null literals have to be casted in the select clause
|
||||
if ( expression instanceof Literal ) {
|
||||
final Literal literal = (Literal) expression;
|
||||
if ( literal.getLiteralValue() == null ) {
|
||||
renderCasted( literal );
|
||||
}
|
||||
else {
|
||||
renderLiteral( literal, true );
|
||||
}
|
||||
}
|
||||
else if ( expression instanceof NullnessLiteral || expression instanceof JdbcParameter || expression instanceof SqmParameterInterpretation ) {
|
||||
renderCasted( expression );
|
||||
}
|
||||
else if ( expression instanceof CaseSimpleExpression ) {
|
||||
visitCaseSimpleExpression( (CaseSimpleExpression) expression, true );
|
||||
}
|
||||
else if ( expression instanceof CaseSearchedExpression ) {
|
||||
visitCaseSearchedExpression( (CaseSearchedExpression) expression, true );
|
||||
}
|
||||
else {
|
||||
expression.accept( this );
|
||||
}
|
||||
}
|
||||
|
||||
protected void renderCasted(Expression expression) {
|
||||
final List<SqlAstNode> arguments = new ArrayList<>( 2 );
|
||||
arguments.add( expression );
|
||||
|
@ -3280,12 +3305,112 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
|
|||
}
|
||||
|
||||
@Override
|
||||
public void visitCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression) {
|
||||
dialect.getCaseExpressionWalker().visitCaseSearchedExpression( caseSearchedExpression, sqlBuffer, this );
|
||||
public final void visitCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression) {
|
||||
visitCaseSearchedExpression( caseSearchedExpression, false );
|
||||
}
|
||||
|
||||
protected void visitCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, boolean inSelect) {
|
||||
if ( inSelect ) {
|
||||
visitAnsiCaseSearchedExpressionInSelect( caseSearchedExpression );
|
||||
}
|
||||
else {
|
||||
visitAnsiCaseSearchedExpression( caseSearchedExpression );
|
||||
}
|
||||
}
|
||||
|
||||
protected void visitAnsiCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression){
|
||||
appendSql( "case" );
|
||||
|
||||
for ( CaseSearchedExpression.WhenFragment whenFragment : caseSearchedExpression.getWhenFragments() ) {
|
||||
appendSql( " when " );
|
||||
whenFragment.getPredicate().accept( this );
|
||||
appendSql( " then " );
|
||||
whenFragment.getResult().accept( this );
|
||||
}
|
||||
|
||||
Expression otherwise = caseSearchedExpression.getOtherwise();
|
||||
if ( otherwise != null ) {
|
||||
appendSql( " else " );
|
||||
otherwise.accept( this );
|
||||
}
|
||||
|
||||
appendSql( " end" );
|
||||
}
|
||||
|
||||
protected void visitAnsiCaseSearchedExpressionInSelect(CaseSearchedExpression caseSearchedExpression) {
|
||||
appendSql( "case" );
|
||||
|
||||
for ( CaseSearchedExpression.WhenFragment whenFragment : caseSearchedExpression.getWhenFragments() ) {
|
||||
appendSql( " when " );
|
||||
whenFragment.getPredicate().accept( this );
|
||||
appendSql( " then " );
|
||||
renderSelectExpression( whenFragment.getResult() );
|
||||
}
|
||||
|
||||
Expression otherwise = caseSearchedExpression.getOtherwise();
|
||||
if ( otherwise != null ) {
|
||||
appendSql( " else " );
|
||||
renderSelectExpression( otherwise );
|
||||
}
|
||||
|
||||
appendSql( " end" );
|
||||
}
|
||||
|
||||
protected void visitDecodeCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression) {
|
||||
appendSql( "decode( " );
|
||||
|
||||
List<CaseSearchedExpression.WhenFragment> whenFragments = caseSearchedExpression.getWhenFragments();
|
||||
int caseNumber = whenFragments.size();
|
||||
CaseSearchedExpression.WhenFragment firstWhenFragment = null;
|
||||
for ( int i = 0; i < caseNumber; i++ ) {
|
||||
final CaseSearchedExpression.WhenFragment whenFragment = whenFragments.get( i );
|
||||
Predicate predicate = whenFragment.getPredicate();
|
||||
if ( i != 0 ) {
|
||||
appendSql( ", " );
|
||||
getLeftHandExpression( predicate ).accept( this );
|
||||
appendSql( ", " );
|
||||
whenFragment.getResult().accept( this );
|
||||
}
|
||||
else {
|
||||
getLeftHandExpression( predicate ).accept( this );
|
||||
firstWhenFragment = whenFragment;
|
||||
}
|
||||
}
|
||||
appendSql( ", " );
|
||||
firstWhenFragment.getResult().accept( this );
|
||||
|
||||
Expression otherwise = caseSearchedExpression.getOtherwise();
|
||||
if ( otherwise != null ) {
|
||||
appendSql( ", " );
|
||||
otherwise.accept( this );
|
||||
}
|
||||
|
||||
appendSql( ')' );
|
||||
}
|
||||
|
||||
protected Expression getLeftHandExpression(Predicate predicate) {
|
||||
if ( predicate instanceof NullnessPredicate ) {
|
||||
return ( (NullnessPredicate) predicate ).getExpression();
|
||||
}
|
||||
assert predicate instanceof ComparisonPredicate;
|
||||
return ( (ComparisonPredicate) predicate ).getLeftHandExpression();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitCaseSimpleExpression(CaseSimpleExpression caseSimpleExpression) {
|
||||
public final void visitCaseSimpleExpression(CaseSimpleExpression caseSimpleExpression) {
|
||||
visitCaseSimpleExpression( caseSimpleExpression, false );
|
||||
}
|
||||
|
||||
protected void visitCaseSimpleExpression(CaseSimpleExpression caseSimpleExpression, boolean inSelect) {
|
||||
if ( inSelect ) {
|
||||
visitAnsiCaseSimpleExpressionInSelect( caseSimpleExpression );
|
||||
}
|
||||
else {
|
||||
visitAnsiCaseSimpleExpression( caseSimpleExpression );
|
||||
}
|
||||
}
|
||||
|
||||
protected void visitAnsiCaseSimpleExpression(CaseSimpleExpression caseSimpleExpression) {
|
||||
appendSql( "case " );
|
||||
caseSimpleExpression.getFixture().accept( this );
|
||||
for ( CaseSimpleExpression.WhenFragment whenFragment : caseSimpleExpression.getWhenFragments() ) {
|
||||
|
@ -3294,8 +3419,28 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
|
|||
appendSql( " then " );
|
||||
whenFragment.getResult().accept( this );
|
||||
}
|
||||
appendSql( " else " );
|
||||
caseSimpleExpression.getOtherwise().accept( this );
|
||||
final Expression otherwise = caseSimpleExpression.getOtherwise();
|
||||
if ( otherwise != null ) {
|
||||
appendSql( " else " );
|
||||
otherwise.accept( this );
|
||||
}
|
||||
appendSql( " end" );
|
||||
}
|
||||
|
||||
protected void visitAnsiCaseSimpleExpressionInSelect(CaseSimpleExpression caseSimpleExpression) {
|
||||
appendSql( "case " );
|
||||
caseSimpleExpression.getFixture().accept( this );
|
||||
for ( CaseSimpleExpression.WhenFragment whenFragment : caseSimpleExpression.getWhenFragments() ) {
|
||||
appendSql( " when " );
|
||||
whenFragment.getCheckValue().accept( this );
|
||||
appendSql( " then " );
|
||||
renderSelectExpression( whenFragment.getResult() );
|
||||
}
|
||||
final Expression otherwise = caseSimpleExpression.getOtherwise();
|
||||
if ( otherwise != null ) {
|
||||
appendSql( " else " );
|
||||
renderSelectExpression( otherwise );
|
||||
}
|
||||
appendSql( " end" );
|
||||
}
|
||||
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.sql.ast.spi;
|
||||
|
||||
import org.hibernate.sql.ast.SqlAstWalker;
|
||||
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
public interface CaseExpressionWalker {
|
||||
void visitCaseSearchedExpression(
|
||||
CaseSearchedExpression caseSearchedExpression,
|
||||
StringBuilder sqlBuffer,
|
||||
SqlAstWalker sqlAstWalker);
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.sql.ast.spi;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.sql.ast.SqlAstWalker;
|
||||
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression;
|
||||
import org.hibernate.sql.ast.tree.expression.Expression;
|
||||
import org.hibernate.sql.ast.tree.predicate.ComparisonPredicate;
|
||||
import org.hibernate.sql.ast.tree.predicate.NullnessPredicate;
|
||||
import org.hibernate.sql.ast.tree.predicate.Predicate;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
public class DecodeCaseExpressionWalker implements CaseExpressionWalker {
|
||||
|
||||
public static final DecodeCaseExpressionWalker INSTANCE = new DecodeCaseExpressionWalker();
|
||||
|
||||
@Override
|
||||
public void visitCaseSearchedExpression(
|
||||
CaseSearchedExpression caseSearchedExpression, StringBuilder sqlBuffer, SqlAstWalker sqlAstWalker) {
|
||||
sqlBuffer.append( "decode( " );
|
||||
|
||||
List<CaseSearchedExpression.WhenFragment> whenFragments = caseSearchedExpression.getWhenFragments();
|
||||
int caseNumber = whenFragments.size();
|
||||
CaseSearchedExpression.WhenFragment firstWhenFragment = null;
|
||||
for ( int i = 0; i < caseNumber; i++ ) {
|
||||
final CaseSearchedExpression.WhenFragment whenFragment = whenFragments.get( i );
|
||||
Predicate predicate = whenFragment.getPredicate();
|
||||
if ( i != 0 ) {
|
||||
sqlBuffer.append( ", " );
|
||||
getLeftHandExpression( predicate ).accept( sqlAstWalker );
|
||||
sqlBuffer.append( ", " );
|
||||
whenFragment.getResult().accept( sqlAstWalker );
|
||||
}
|
||||
else {
|
||||
getLeftHandExpression( predicate ).accept( sqlAstWalker );
|
||||
firstWhenFragment = whenFragment;
|
||||
}
|
||||
}
|
||||
sqlBuffer.append( ", " );
|
||||
firstWhenFragment.getResult().accept( sqlAstWalker );
|
||||
|
||||
Expression otherwise = caseSearchedExpression.getOtherwise();
|
||||
if ( otherwise != null ) {
|
||||
sqlBuffer.append( ", " );
|
||||
otherwise.accept( sqlAstWalker );
|
||||
}
|
||||
|
||||
sqlBuffer.append( ')' );
|
||||
}
|
||||
|
||||
protected Expression getLeftHandExpression(Predicate predicate) {
|
||||
if ( predicate instanceof NullnessPredicate ) {
|
||||
return ( (NullnessPredicate) predicate ).getExpression();
|
||||
}
|
||||
assert predicate instanceof ComparisonPredicate;
|
||||
return ( (ComparisonPredicate) predicate ).getLeftHandExpression();
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.sql.ast.spi;
|
||||
|
||||
import org.hibernate.sql.ast.SqlAstWalker;
|
||||
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression;
|
||||
import org.hibernate.sql.ast.tree.expression.Expression;
|
||||
import org.hibernate.sql.ast.tree.expression.QueryLiteral;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
public class DerbyCaseExpressionWalker implements CaseExpressionWalker {
|
||||
|
||||
public static DerbyCaseExpressionWalker INSTANCE = new DerbyCaseExpressionWalker();
|
||||
|
||||
@Override
|
||||
public void visitCaseSearchedExpression(
|
||||
CaseSearchedExpression caseSearchedExpression,
|
||||
StringBuilder sqlBuffer,
|
||||
SqlAstWalker sqlAstWalker) {
|
||||
|
||||
sqlBuffer.append( "case" );
|
||||
|
||||
for ( CaseSearchedExpression.WhenFragment whenFragment : caseSearchedExpression.getWhenFragments() ) {
|
||||
sqlBuffer.append( " when " );
|
||||
whenFragment.getPredicate().accept( sqlAstWalker );
|
||||
sqlBuffer.append( " then " );
|
||||
whenFragment.getResult().accept( sqlAstWalker );
|
||||
}
|
||||
|
||||
// TODO (6.0) : not sure this is the correct way to managed the otherwise expression
|
||||
Expression otherwise = caseSearchedExpression.getOtherwise();
|
||||
if ( otherwise != null ) {
|
||||
sqlBuffer.append( " else " );
|
||||
if ( otherwise instanceof QueryLiteral ) {
|
||||
Object value = ( (QueryLiteral) otherwise ).getLiteralValue();
|
||||
if ( value == null ) {
|
||||
// null is not considered the same type as Integer.
|
||||
sqlBuffer.append( "-1" );
|
||||
}
|
||||
else {
|
||||
otherwise.accept( sqlAstWalker );
|
||||
}
|
||||
}
|
||||
else {
|
||||
otherwise.accept( sqlAstWalker );
|
||||
}
|
||||
}
|
||||
sqlBuffer.append( " end" );
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.sql.ast.spi;
|
||||
|
||||
import org.hibernate.sql.ast.SqlAstWalker;
|
||||
import org.hibernate.sql.ast.tree.expression.CaseSearchedExpression;
|
||||
import org.hibernate.sql.ast.tree.expression.Expression;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
public class MckoiCaseExpressionWalker implements CaseExpressionWalker {
|
||||
|
||||
public static final MckoiCaseExpressionWalker INSTANCE = new MckoiCaseExpressionWalker();
|
||||
|
||||
@Override
|
||||
public void visitCaseSearchedExpression(
|
||||
CaseSearchedExpression caseSearchedExpression, StringBuilder sqlBuffer, SqlAstWalker sqlAstWalker) {
|
||||
sqlBuffer.append( "case" );
|
||||
StringBuilder buf2= new StringBuilder( );
|
||||
|
||||
for ( CaseSearchedExpression.WhenFragment whenFragment : caseSearchedExpression.getWhenFragments() ) {
|
||||
sqlBuffer.append( " if( " );
|
||||
whenFragment.getPredicate().accept( sqlAstWalker );
|
||||
sqlBuffer.append( ", " );
|
||||
whenFragment.getResult().accept( sqlAstWalker );
|
||||
sqlBuffer.append(", ");
|
||||
buf2.append(")");
|
||||
}
|
||||
Expression otherwise = caseSearchedExpression.getOtherwise();
|
||||
if ( otherwise != null ) {
|
||||
otherwise.accept( sqlAstWalker );
|
||||
}
|
||||
else {
|
||||
sqlBuffer.append( "null" );
|
||||
}
|
||||
sqlBuffer.append(buf2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue