Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| docs:3500:0500:0530 [2023/04/05 07:50] – removed - external edit (Unknown date) 127.0.0.1 | docs:3500:0500:0530 [2023/04/06 04:45] (current) – sean | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{tag> | ||
| + | [< | ||
| + | ==== 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. | ||
| + | |||
| + | <code java> | ||
| + | { | ||
| + | supported date functions | ||
| + | public static final StringDAY_OF_WEEK_FUNCTION=" | ||
| + | public static final StringCURRENT_YEAR_FUNCTION=" | ||
| + | public DateFunctionLibrary() | ||
| + | { | ||
| + | } | ||
| + | /** | ||
| + | * @see IFunctionLibrary# | ||
| + | **/ | ||
| + | @Override | ||
| + | public StringgetName() | ||
| + | { | ||
| + | return “DateFunctionLibrary”; | ||
| + | } | ||
| + | /** | ||
| + | *@see IFunctionLibrary# | ||
| + | */ | ||
| + | @Override | ||
| + | public boolean hasFunction(StringfunctionName, | ||
| + | { | ||
| + | if (paramNmb==0) | ||
| + | { | ||
| + | return functionName.equalsIgnoreCase (CURRENT_YEAR_FUNCTION); | ||
| + | } | ||
| + | else if (paramNmb==1) | ||
| + | { | ||
| + | return functionName.equalsIgnoreCase (DAY_OF_WEEK_FUNCTION); | ||
| + | return false ; | ||
| + | } | ||
| + | /** | ||
| + | * @see IFunctionLibrary# | ||
| + | **/ | ||
| + | @Override | ||
| + | public FunctionDescription[]getAvailableFunctions() | ||
| + | { | ||
| + | FunctionDescription[]results= new FunctionDescription[2]; | ||
| + | results[0]= new FunctionDescription(CURRENT_YEAR_FUNCTION, | ||
| + | " | ||
| + | results[1]= new FunctionDescription(DAY_OF_WEEK_FUNCTION, | ||
| + | return results; | ||
| + | } | ||
| + | /** | ||
| + | ** @see IFunctionLibrary# | ||
| + | **/ | ||
| + | public Objectcalculate(StringfunctionName, | ||
| + | { | ||
| + | if (params== null ||params.length==0) | ||
| + | { | ||
| + | if (functionName.equalsIgnoreCase(CURRENT_YEAR_FUNCTION)) | ||
| + | { | ||
| + | Calendarcal=Calendar.getInstance(); | ||
| + | cal.setTime(new Date ()); | ||
| + | return new Integer(cal.get(Calendar.YEAR)); | ||
| + | } | ||
| + | } | ||
| + | else if (params!= null && | ||
| + | { | ||
| + | // a single parameter must be of DateHolder type | ||
| + | if (functionName.equalsIgnoreCase(DAY_OF_WEEK_FUNCTION)) | ||
| + | { | ||
| + | if (!(params[0] instanceof DateHolder)) | ||
| + | return null ; | ||
| + | Datedate=TypeUtils.toDate((DateHolder)params[0]); | ||
| + | Calendarcal=Calendar.getInstance(); | ||
| + | cal.setTime(date); | ||
| + | SimpleDateFormatsdf= new SimpleDateFormat(" | ||
| + | return sdf.format(date); | ||
| + | } | ||
| + | } | ||
| + | return null ; | ||
| + | } | ||
| + | /** | ||
| + | ** @see IFunctionLibrary# | ||
| + | **/ | ||
| + | @Override | ||
| + | public StringtoSQL(StringfunctionName, | ||
| + | throw Exception | ||
| + | { | ||
| + | IDatabaseClientInterfacedbi =sqlHelper.getDatabaseInterface(); | ||
| + | if (parameters== null ||parameters.size()==0) | ||
| + | { | ||
| + | if (dbi instanceof MySQLClientInterface) | ||
| + | { | ||
| + | if (functionName.equalsIgnoreCase (CURRENT_YEAR_FUNCTION)) | ||
| + | { | ||
| + | return " | ||
| + | } | ||
| + | } | ||
| + | else if (dbi instanceof MSSQLServerClientInterface) | ||
| + | { | ||
| + | if (functionName.equalsIgnoreCase (CURRENT_YEAR_FUNCTION)) | ||
| + | { | ||
| + | return " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | else if (parameters!= null && | ||
| + | { | ||
| + | Iteratoriter=parameters.iterator(); | ||
| + | IArithmeticNodearNode=(IArithmeticNode)iter.next(); | ||
| + | // representation of the argument | ||
| + | StringdateArg=arNode.toSQL(sqlHelper); | ||
| + | if (dateArg== null ||dateArg.length()==0) | ||
| + | return ""; | ||
| + | if (functionName.equalsIgnoreCase(DAY_OF_WEEK_FUNCTION)) | ||
| + | { | ||
| + | if (sqlFlavour==CommonConstants.SQL_FLAVOUR_TSQL) | ||
| + | return " | ||
| + | else if (sqlFlavour==CommonConstants.SQL_FLAVOUR_MYSQL) | ||
| + | return " | ||
| + | } | ||
| + | } | ||
| + | throw new UnsupportedSQLElementException(functionName); | ||
| + | } | ||
| + | /** @see IFunctionLibrary# | ||
| + | @Override | ||
| + | public ClassgetTypeClass(StringfunctionName, | ||
| + | { | ||
| + | return Integer. class ; | ||
| + | } | ||
| + | /** @see IFunctionLibrary# | ||
| + | @Override | ||
| + | public HashSetgetRequiredFactPatterns(StringfunctionName, | ||
| + | { | ||
| + | return null; | ||
| + | } | ||
| + | }</ | ||
| + | |||