## org.hibernate.dialect.function
Class DerbyConcatFunction

    java.lang.Object
      org.hibernate.dialect.function.DerbyConcatFunction

All Implemented Interfaces:
  SQLFunction

    public class DerbyConcatFunction

extends Object
implements SQLFunction

A specialized concat() function definition in which:

1. we translate to use the concat operator ('||')
2. wrap dynamic parameters in CASTs to VARCHAR

This last spec is to deal with a limitation on DB2 and variants (e.g. Derby) where dynamic parameters cannot be used in concatenation unless they are being concatenated with at least one non-dynamic operand. And even then, the rules are so convoluted as to what is allowed and when the CAST is needed and when it is not that we just go ahead and do the CASTing.

Author:
  Steve Ebersole

Constructor Summary
`DerbyConcatFunction()`

Method Summary
`Type` `getReturnType(Type argumentType, Mapping mapping)` The return type of the function.
`boolean` `hasArguments()` Does this function have any arguments?
`boolean` `hasParenthesesIfNoArguments()` If there are no arguments, are parentheses required?
`String` `render(Type argumentType, List args, SessionFactoryImplementor factory)` Render the function call as SQL fragment.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### DerbyConcatFunction

    public DerbyConcatFunction()

Method Detail

### hasArguments

    public boolean hasArguments()

  Does this function have any arguments?

  Here we always return `true`

  Specified by:
    `hasArguments` in interface `SQLFunction`
  Returns:
    True if the function expects to have parameters; false otherwise.

### hasParenthesesIfNoArguments

    public boolean hasParenthesesIfNoArguments()

  If there are no arguments, are parentheses required?

  Here we always return `true`

  Specified by:
    `hasParenthesesIfNoArguments` in interface `SQLFunction`
  Returns:
    True if a no-arg call of this function requires parentheses.

### getReturnType

    public Type getReturnType(Type argumentType,
                              Mapping mapping)
                       throws QueryException

  The return type of the function. May be either a concrete type which is preset, or variable depending upon the type of the first function argument.

  Note, the 'firstArgumentType' parameter should match the one passed into `SQLFunction.render(org.hibernate.type.Type, java.util.List, org.hibernate.engine.SessionFactoryImplementor)`

  Here we always return `Hibernate.STRING`.

  Specified by:
    `getReturnType` in interface `SQLFunction`
  Parameters:
    `argumentType` - The type of the first argument
    `mapping` - The mapping source.
  Returns:
    The type to be expected as a return.
  Throws:
    `QueryException` - Indicates an issue resolving the return type.

### render

    public String render(Type argumentType,
                         List args,
                         SessionFactoryImplementor factory)
                  throws QueryException

  Render the function call as SQL fragment.

  Note, the 'firstArgumentType' parameter should match the one passed into `SQLFunction.getReturnType(org.hibernate.type.Type, org.hibernate.engine.Mapping)`

  Here's the meat.. The whole reason we have a separate impl for this for Derby is to re-define this method. The logic here says that if not all the incoming args are dynamic parameters (i.e. `?`) then we simply use the Derby concat operator (`||`) on the unchanged arg elements. However, if all the args are dynamic parameters, then we need to wrap the individual arg elements in `cast` function calls, use the concatenation operator on the `cast` returns, and then wrap that whole thing in a call to the Derby `varchar` function.

  Specified by:
    `render` in interface `SQLFunction`
  Parameters:
    `argumentType` - The type of the first argument
    `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.