org.hibernate.dialect.function
Class AvgWithArgumentCastFunction

java.lang.Object
  extended by org.hibernate.dialect.function.AvgFunction
      extended by org.hibernate.dialect.function.AvgWithArgumentCastFunction
All Implemented Interfaces:
SQLFunction

public class AvgWithArgumentCastFunction
extends AvgFunction

Some databases strictly return the type of the of the aggregation value for AVG which is problematic in the case of averaging integers because the decimals will be dropped. The usual workaround is to cast the integer argument as some form of double/decimal.

A downside to this approach is that we always wrap the avg() argument in a cast even though we may not need or want to. A more full-featured solution would be defining SQLFunction such that we render based on the first argument; essentially have SQLFunction describe the basic metadata about the function and merge the SQLFunction.getReturnType(org.hibernate.type.Type, org.hibernate.engine.Mapping) and SQLFunction.render(java.util.List, org.hibernate.engine.SessionFactoryImplementor) methods into a

Author:
Steve Ebersole

Constructor Summary
AvgWithArgumentCastFunction(String castType)
           
 
Method Summary
 String render(List args, SessionFactoryImplementor factory)
          Render the function call as SQL fragment.
 
Methods inherited from class org.hibernate.dialect.function.AvgFunction
getReturnType, hasArguments, hasParenthesesIfNoArguments, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AvgWithArgumentCastFunction

public AvgWithArgumentCastFunction(String castType)
Method Detail

render

public String render(List args,
                     SessionFactoryImplementor factory)
              throws QueryException
Description copied from interface: SQLFunction
Render the function call as SQL fragment.

Specified by:
render in interface SQLFunction
Overrides:
render in class AvgFunction
Parameters:
args - The function arguments
factory - The SessionFactory
Returns:
The rendered function call
Throws:
QueryException - Indicates a problem rendering the function call.


Copyright © 2001-2010 Red Hat, Inc. All Rights Reserved.