HHH-17242 Partly revert original solution to retain correct expression types

This commit is contained in:
Christian Beikov 2023-09-26 15:45:04 +02:00
parent 8af4882dea
commit 22ed4793b9
5 changed files with 18 additions and 86 deletions

View File

@ -473,15 +473,6 @@ public class OracleLegacyDialect extends Dialect {
@Override
public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType) {
return timestampaddPattern( unit, temporalType, intervalType, false );
}
@Override
public String timestampaddPattern(
TemporalUnit unit,
TemporalType temporalType,
IntervalType intervalType,
boolean hasTimeZone) {
final StringBuilder pattern = new StringBuilder();
switch ( unit ) {
case YEAR:
@ -494,23 +485,22 @@ public class OracleLegacyDialect extends Dialect {
pattern.append( ADD_MONTH_EXPRESSION );
break;
case WEEK:
if ( hasTimeZone ) {
pattern.append( "(?3+numtodsinterval(?2*7,'day'))" );
if ( temporalType != TemporalType.DATE ) {
pattern.append( "(?3+numtodsinterval((?2)*7,'day'))" );
}
else {
pattern.append( "(?3+?2" ).append( unit.conversionFactor( DAY, this ) ).append( ")" );
pattern.append( "(?3+(?2)" ).append( unit.conversionFactor( DAY, this ) ).append( ")" );
}
break;
case DAY:
if ( temporalType == TemporalType.DATE ) {
pattern.append( "(?3+(?2))" );
break;
}
case HOUR:
case MINUTE:
case SECOND:
if ( hasTimeZone ) {
pattern.append( "(?3+numtodsinterval(?2,'?1'))" );
}
else {
pattern.append( "(?3+?2" ).append( unit.conversionFactor( DAY, this ) ).append( ")" );
}
pattern.append( "(?3+numtodsinterval(?2,'?1'))" );
break;
case NANOSECOND:
pattern.append( "(?3+numtodsinterval((?2)/1e9,'second'))" );

View File

@ -1441,22 +1441,6 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
* @param temporalType The type of the temporal
* @param intervalType The type of interval to add or null if it's not a native interval
*/
public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType, boolean withTimeZone) {
return timestampaddPattern( unit, temporalType, intervalType );
}
/**
* Obtain a pattern for the SQL equivalent to a
* {@code timestampadd()} function call. The resulting
* pattern must contain ?1, ?2, and ?3 placeholders
* for the arguments.
*
* @param unit The unit to add to the temporal
* @param temporalType The type of the temporal
* @param intervalType The type of interval to add or null if it's not a native interval
* @deprecated use {@link #timestampaddPattern(TemporalUnit, TemporalType, IntervalType, boolean)} instead
*/
@Deprecated
public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType) {
throw new UnsupportedOperationException( "`" + getClass().getName() + "` does not yet support #timestampaddPattern" );
}

View File

@ -302,15 +302,6 @@ public class DialectDelegateWrapper extends Dialect {
return wrapped.timestampdiffPattern( unit, fromTemporalType, toTemporalType );
}
@Override
public String timestampaddPattern(
TemporalUnit unit,
TemporalType temporalType,
IntervalType intervalType,
boolean withTimeZone) {
return wrapped.timestampaddPattern( unit, temporalType, intervalType, withTimeZone );
}
@Override
public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType) {
return wrapped.timestampaddPattern( unit, temporalType, intervalType );
@ -1649,14 +1640,4 @@ public class DialectDelegateWrapper extends Dialect {
public String getRowIdColumnString(String rowId) {
return wrapped.getRowIdColumnString( rowId );
}
@Override
public boolean useArrayForMultiValuedParameters() {
return wrapped.useArrayForMultiValuedParameters();
}
@Override
public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
return wrapped.getDmlTargetColumnQualifierSupport();
}
}

View File

@ -494,15 +494,6 @@ public class OracleDialect extends Dialect {
@Override
public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType) {
return timestampaddPattern( unit, temporalType, intervalType, false );
}
@Override
public String timestampaddPattern(
TemporalUnit unit,
TemporalType temporalType,
IntervalType intervalType,
boolean hasTimeZone) {
final StringBuilder pattern = new StringBuilder();
switch ( unit ) {
case YEAR:
@ -515,23 +506,22 @@ public class OracleDialect extends Dialect {
pattern.append( ADD_MONTH_EXPRESSION );
break;
case WEEK:
if ( hasTimeZone ) {
pattern.append( "(?3+numtodsinterval(?2*7,'day'))" );
if ( temporalType != TemporalType.DATE ) {
pattern.append( "(?3+numtodsinterval((?2)*7,'day'))" );
}
else {
pattern.append( "(?3+?2" ).append( unit.conversionFactor( DAY, this ) ).append( ")" );
pattern.append( "(?3+(?2)" ).append( unit.conversionFactor( DAY, this ) ).append( ")" );
}
break;
case DAY:
if ( temporalType == TemporalType.DATE ) {
pattern.append( "(?3+(?2))" );
break;
}
case HOUR:
case MINUTE:
case SECOND:
if ( hasTimeZone ) {
pattern.append( "(?3+numtodsinterval(?2,'?1'))" );
}
else {
pattern.append( "(?3+?2" ).append( unit.conversionFactor( DAY, this ) ).append( ")" );
}
pattern.append( "(?3+numtodsinterval(?2,'?1'))" );
break;
case NANOSECOND:
pattern.append( "(?3+numtodsinterval((?2)/1e9,'second'))" );

View File

@ -23,7 +23,6 @@ import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.sql.ast.tree.expression.DurationUnit;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.spi.TypeConfiguration;
import java.util.List;
@ -39,7 +38,7 @@ import static org.hibernate.type.spi.TypeConfiguration.getSqlTemporalType;
* The {@code timestampadd()} or {@code dateadd()} function has a funny
* syntax which accepts a {@link TemporalUnit} as the first argument,
* and the actual set of accepted units varies widely. This class uses
* {@link Dialect#timestampaddPattern(TemporalUnit, TemporalType, IntervalType, boolean)}
* {@link Dialect#timestampaddPattern(TemporalUnit, TemporalType, IntervalType)}
* to abstract these differences.
*
* @author Gavin King
@ -78,19 +77,7 @@ public class TimestampaddFunction
PatternRenderer patternRenderer(TemporalUnit unit, Expression interval, Expression to) {
TemporalType temporalType = getSqlTemporalType( to.getExpressionType() );
IntervalType intervalType = getSqlIntervalType( interval.getExpressionType().getSingleJdbcMapping() );
boolean withTimeZone = hasTimeZone( to.getExpressionType().getSingleJdbcMapping().getJdbcType().getDefaultSqlTypeCode() );
return new PatternRenderer( dialect.timestampaddPattern( unit, temporalType, intervalType, withTimeZone ) );
}
private boolean hasTimeZone(int sqlTypeCode) {
switch ( sqlTypeCode ) {
case SqlTypes.TIME_UTC:
case SqlTypes.TIME_WITH_TIMEZONE:
case SqlTypes.TIMESTAMP_UTC:
case SqlTypes.TIMESTAMP_WITH_TIMEZONE:
return true;
}
return false;
return new PatternRenderer( dialect.timestampaddPattern( unit, temporalType, intervalType ) );
}
// @Override