1 Type of return: automatic / forced
The mode to perform the call to a SQL function, depends of the database agent. The tag execute-function has an optional attribute return-type or return-like which indicates the data type of the result, the XSQL-Script interpret will try to perform the conversion of the value returned to the indicated type. This is important because depending of the used database agent and the way in which the call is made it is not always possible to obtain the type of data that the SQL function returns. For this it is recommended to force its type and make the program independent of the database agent.
|JDBC call to a SQL function. Attribute [type]|
For IDS, DB2 and MYSQL is not necessary to indicate the returned data type because the call to the function is performed through a cursor JDBC which allows to obtain the data type automatically. Otherwise in ORACLE and SQLSERVER the call is perfomed through a CALL which requires to indicate the data type returned to force the conversion. For this it is recommended to always indicate the type and thus the program is independent of the agent. The following example shows a function which returns a decimal value. This function is written in grammar XSQL, has been converted and compiled in the database.
<function name='get_test_decimal'> <args> <arg name='p_fecha' type='date' /> </args> <returns type='decimal' size='10,6' /> <define> <variable name='v_test' type='decimal' size='10,6' /> </define> <body> <set name='v_test'>1.789</set> <return>v_test</return> </body> </function>
The following XSQL-Script performs a call to the function described previously.
<xsql-script name='script_test_decimal'> <body> <set name='m_cambio' type='decimal'> <execute-function name='get_test_decimal' return-type='decimal'> <date.today /> </execute-function> </set> </body> </xsql-script>
This XSQL-Script will work well in IDS, DB2 and MYSQL, however in ORACLE and SQLSERVER will causes an exception:
unsupported conversion from data=1,789 of class=java.lang.String to type=decimal
Being necessary to indicate the data type returned in the call to the function
<execute-function name='get_test_decimal' return-type='decimal'> <date.today /> </execute-function>
2 Cachear una llamada a una función
The tag execute-function allows to cache a call to a function. This implies that the result returned by the function is stored in cache and the following calls with the same input arguments will obtain the same result (the cached result). It has the sense that the function within the execution block is considered invariant. It should be used when you consider that the function is invariant. The following call to the function is performed with cache of result.
<execute-function name='get_test_decimal' return-type='decimal' cache='true'> <date.today /> </execute-function>
Executes an SQL function compiled in the database and obtains the returned value.
<argument /> *
|Aname||string||Name of the function.|
|Acache||boolean||Indicate if the result should be cached. In this case, the following calls to this function with same input arguments will obtain the cached value. It should be used when the function is invariant for the block.|
|Areturn-type||string||Data type returned by the function. In IDS, DB2 and MYSQL the type is determined automatically, and in ORACLE, SQLSERVER and POSTGRES should be indicated or otherwise a string is returned.It is only applied to BD2, Oracle and Postgres.|
|Areturn-like||string||Excluding with the use of the previous attribute 'return-type'. Data type returned by the function, indicated through a reference of table and column. It is only applied to BD2, Oracle and Postgres.|
|Adb2vtable||boolean||Indicate for the calls to the SQL functions of DB2 if it returns a vtable.|
|Eargument||object||Input arguments to the function called. It it possible that the function does not have any input argument, one or several. The data type and the data will depend of the function.|
|object||Returns the value which is returned by the execution of the function. The data type returned will depend of the function call or will be of the type which has been forced through the attributeb return-type or return-like.|
unsupported conversion from ...
The value of the function is assigned to a variables which types are no convertibles.
Excepcions provoked during the execution of the function.
Performs the call to the function SQL get_test_decimal passing as argument the current date.
<xsql-script name='script_test_decimal'> <body> <set name='m_change' type='decimal'> <execute-function name='get_test_decimal' return-type='decimal'> <date.today /> </execute-function> </set> </body> </xsql-script>