Red Hat Training

A Red Hat training course is available for Red Hat JBoss Data Virtualization

4.6. Using Function Modifiers

In some cases you may need to translate the function differently or even insert additional function calls above or below the function being translated. The JDBC translator provides an abstract class FunctionModifier for this purpose.
During the start method a modifier instance can be registered against a given function name via a call to JDBCExecutionFactory.registerFunctionModifier.
The FunctionModifier has a method called translate. Use the translate method to change the way the function is represented.
An example of overriding the translate method to change the MOD(a, b) function into an infix operator for Sybase (a % b). The translate method returns a list of strings and language objects that will be assembled by the translator into a final string. The strings will be used as is and the language objects will be further processed by the translator.
public class ModFunctionModifier extends FunctionModifier 
{
   public List translate(Function function) 
   {
      List parts = new ArrayList();
      parts.add("(");        
      Expression[] args = function.getParameters().toArray(new Expression[0]);
      parts.add(args[0]);
      parts.add(" % "); 
      parts.add(args[1]);
      parts.add(")");    
      return parts;
   }
}
In addition to building your own FunctionModifiers, there are a number of pre-built generic function modifiers that are provided with the translator.

Table 4.2. Common Modifiers

Modifier
Description
AliasModifier
Handles renaming a function ("ucase" to "upper" for example)
EscapeSyntaxModifier
Wraps a function in the standard JDBC escape syntax for functions: {fn xxxx()}
To register the function modifiers for your supported functions, you must call the ExecutionFactory.registerFunctionModifier(String name, FunctionModifier modifier) method.
public class ExtendedJDBCExecutionFactory extends JDBCExecutionFactory
{              
   @Override
   public void start() 
   {
      super.start();

      // register functions.
      registerFunctionModifier("abs", new MyAbsModifier()); 
      registerFunctionModifier("concat", new AliasModifier("concat2")); 
   }
}
Support for the two functions being registered ("abs" and "concat") must be declared in the capabilities as well. Functions that do not have modifiers registered will be translated as usual.