Function library example
See the code snippet below for an example of a function library. For illustration purposes this code snippet contains only a handful of functions supported by the standard Date Function Library.
public class DateFunctionLibrary implements IFunctionLibrary { // supported date functions public DateFunctionLibrary() { } /** * @see https://docs.awareim.com/ref/p_m/getname_ifunction */ @Override { return "DateFunctionLibrary"; } /** * @see https://docs.awareim.com/ref/p_m/hasfunction */ @Override { if (paramNmb == 0) { return functionName.equalsIgnoreCase(CURRENT_YEAR_FUNCTION); } else if (paramNmb == 1) { return functionName.equalsIgnoreCase(DAY_OF_WEEK_FUNCTION); } return false; } /** * @see https://docs.awareim.com/ref/p_m/getavailablefunctions */ @Override public FunctionDescription[] getAvailableFunctions() { FunctionDescription[] results = new FunctionDescription[2]; results[0] = new FunctionDescription(CURRENT_YEAR_FUNCTION, "Parameters: none\nReturn: current year as 4 digit Number"); results[1] = new FunctionDescription(DAY_OF_WEEK_FUNCTION, "Parameters: attribute of Date type or expression producing Date" + "\nReturn: day of week of the provided date as Plain Text ('Monday', 'Tuesday' etc)"); return results; } /** * @see https://docs.awareim.com/ref/p_m/calculate */ { if (params == null || params.length == 0) { if (functionName.equalsIgnoreCase(CURRENT_YEAR_FUNCTION)) { } } else if (params != null && params.length == 1) { // a single parameter must be of DateHolder type if (functionName.equalsIgnoreCase(DAY_OF_WEEK_FUNCTION)) { if (!(params[0] instanceof DateHolder)) return null; cal.setTime(date); return sdf.format(date); } } return null; } /** * @see https://docs.awareim.com/ref/p_m/tosql */ @Override throws Exception { IDatabaseClientInterface dbi = sqlHelper.getDatabaseInterface(); if (parameters == null || parameters.size() == 0) { if (dbi instanceof MySQLClientInterface) { if (functionName.equalsIgnoreCase(CURRENT_YEAR_FUNCTION)) { return "YEAR(CURDATE())"; } } else if (dbi instanceof MSSQLServerClientInterface) { if (functionName.equalsIgnoreCase(CURRENT_YEAR_FUNCTION)) { return "DATEPART (yy, GETDATE())"; } } } else if (parameters != null && parameters.size() == 1) { IArithmeticNode arNode = (IArithmeticNode) iter.next(); // representation of the argument if (dateArg == null || dateArg.length() == 0) return ""; // the expression won't be calculated if (functionName.equalsIgnoreCase(DAY_OF_WEEK_FUNCTION)) { if (sqlFlavour == CommonConstants.SQL_FLAVOUR_TSQL) return "DATENAME(dw," + dateArg + ")"; else if (sqlFlavour == CommonConstants.SQL_FLAVOUR_MYSQL) return "DAYNAME(" + dateArg + ")"; } } throw new UnsupportedSQLElementException(functionName); } /** * @see https://docs.awareim.com/ref/p_m/gettypeclass */ @Override { } /** * @see https://docs.awareim.com/ref/p_m/getrequiredfactpatterns */ @Override { return null; } }