Skip to content

Custom Functions

Edmo Vamerlatti Costa edited this page Apr 20, 2018 · 1 revision

Custom value functions are similar to variables, they are defined on query syntax and resolved during the AST building time. You can register your own invoker class which is responsible for resolve the value invoking the function itself or use the default implementation available on DefaultFunctionInvoker class.

Own Invoker

To create your own function invoker you need implementing the interface CustomFunctionInvoker and pass it as parameter on the withFunctionInvoker function of the ParserConfig builder.

public class HelloFunctionInvoker implements FunctionInvoker {
    @Override
    public Object invoke(String function, Object... args) {
        return "Hello function";
    }

    @Override
    public boolean canHandle(String function, Object... args) {
        return function.equalsIgnoreCase("sayHello");
    }
}

Default Invoker Implementation

The default invoker implementation provides a few builtin functions for Temporal and String objects. You can also register your own functions using the 'with' method of the DefaultFunctionInvoker class defining or not the priority of the resolver class over the others functions. The lower the value, the higher the priority

//Create the Default Invoker and add the implementation of the function @sayHello()
DefaultFunctionInvoker invoker = DefaultFunctionInvoker.with(new HelloFunctionInvoker());
    
//Config object to be passed on parse the query
ParserConfig config = ParserConfig.builder()
                                  .withFunctionInvoker(invoker)
                                  .build();
//Create the Default Invoker and add the implementation of the function @sayHello() with a high priority
CustomFunction prioritizedInvoker = CustomFunction.of(new HelloFunctionInvoker(), -1000);
DefaultCustomFunctionInvoker invoker = DefaultCustomFunctionInvoker.with(prioritizedInvoker);

Query Examples

select name, age where createDate = @plusDays(@today(), -1) and age > 10

select id where greetings = @sayHello()

Content

Clone this wiki locally