use of org.hibernate.query.sqm.tree.expression.SqmToDuration in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitToDuration.
@Override
public Object visitToDuration(SqmToDuration<?> toDuration) {
// TODO: do we need to temporarily set appliedByUnit
// to null before we recurse down the tree?
// and what about scale?
Expression magnitude = toSqlExpression(toDuration.getMagnitude().accept(this));
DurationUnit unit = (DurationUnit) toDuration.getUnit().accept(this);
// let's start by applying the propagated scale
// so we don't forget to do it in what follows
Expression scaledMagnitude = applyScale(magnitude);
if (adjustedTimestamp != null) {
// adjusted date or timestamp
if (appliedByUnit != null) {
throw new IllegalStateException();
}
return timestampadd().expression(// TODO should be adjustedTimestamp.getType()
(ReturnableType<?>) adjustedTimestampType, unit, scaledMagnitude, adjustedTimestamp);
} else {
BasicValuedMapping durationType = (BasicValuedMapping) toDuration.getNodeType();
Duration duration;
if (scaledMagnitude.getExpressionType().getJdbcMappings().get(0).getJdbcType().isInterval()) {
duration = new Duration(extractEpoch(scaledMagnitude), SECOND, durationType);
} else {
duration = new Duration(scaledMagnitude, unit.getUnit(), durationType);
}
if (appliedByUnit != null) {
// we're applying the 'by unit' operator,
// producing a literal scalar value in
// the given unit
TemporalUnit appliedUnit = appliedByUnit.getUnit().getUnit();
BasicValuedMapping scalarType = (BasicValuedMapping) appliedByUnit.getNodeType();
return new Conversion(duration, appliedUnit, scalarType);
} else {
// a "bare" Duration value (gets rendered as nanoseconds)
return duration;
}
}
}
Aggregations