This section describes the XML signature file and where to store it.
software requires the C function description to be in XML. The XML is stored in a file with the extension .udf, and a copy of that file is put in the plugins directory of both the server and Studio. See Compiling a UDF and Putting It in the Correct Directory for details about where to put this XML signature file.
This file's contents are similar to the following:
<!-- Header information --> <UserDefinedFunctions Name="TestingUserDefinedFunctions" Type="ns1:UserDefinedFunctionType" xmlns="http://www.sybase.com/udf/2005/04/" xmlns:udf="http://www.sybase.com/udf/2005/04/" xmlns:ns1="http://www.sybase.com/cpx/2005/04/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <!-- The "Functions" section contains descriptions of 1 or more functions. --> <Functions> <!-- There is a "Functions" section for each function. --> <Function> ... info about the specific function goes here ... </Function> </Functions> </UserDefinedFunctions>
This header information may change from version to version. We recommend that you copy one of the .udf files that we provide, remove all of the Function sections, and then add descriptions for one or more functions.
For each function, the translation of the C function to Sybase CEP UDF signatures is straightforward.
For the example below:
1. void weightedAverage3(float var1, /* first user variable */ 2. float weight1, /* 2nd user var: weight for var1 */ 3. float var2, /* 3rd user var */ 4. float weight2, /* 4th user var */ 5. float var3, /* ... */ 6. float weight3);
The user-defined xml is similar to the following:
2. <Functions> 3. <Function Name="weightedAvg3" InitFunctionName="weightedAvg3Init" ShutdownFunctionName="weightedAvg3Shutdown" Library="user_function_lib" CclName="WAvg3" IsAggregator="false"> 4. <Input> 5. <Parameter Name="var1" Type="C8Float"/> 6. <Parameter Name="weight1" Type="C8Float"/> 7. <Parameter Name="var2" Type="C8Float"/> 8. <Parameter Name="weight2" Type="C8Float"/> 9. <Parameter Name="var3" Type="C8Float"/> 10. <Parameter Name="weight3" Type="C8Float"/> 11. </Input> 12. <Output> 13. <Parameter Name="result" Type="C8Float"/> 14. </Output> 15. </Function> 16. </Functions>
For each function described in the .udf file, the initial function element is always named Function and has the following attributes:
Name: Provides the name of the function that will be called from the user-designated Library. This name is case-sensitive. If you set session state in an initialization function, use C8GetSessionState() to retrieve it.
InitFunctionName (optional): Identifies a function that will be called on initialization. Use the initialization function to perform resource-intensive tasks, like reading from disk, and to set session state for calls to the function identified with the Name attribute.
ShutdownFunctionName (optional): Identifies a function that will be called on shutdown. If you have set session state in an initialization function, you must destroy the session state in the shutdown function.
Library: The .dll containing the user function. For UNIX-like operating systems, this will be a .so libraryand the library name should not be prefaced with "lib". Omit the extensions .dll or .so. You may use the same XML file for both UNIX-like operating systems and Microsoft Windows environments without changing the library name.
CclName: The name that will be used in CCL queries (for example, "select FOO() ... from ..."). The CclName is case insensitive.
IsAggregator: Set to true for aggregate functions and false for other functions.
The Functions element of the XML file can describe any number of functions. This convenience allows you to group related functions and/or library modules. There can be any number of function modules in a library. In the XML file, each function should be described inside its own "<Function> ... </Function>" element. For example:
<Functions> <Function Name="foo" ...> ... </Function> <Function Name="bar" ...> ... </Function> </Functions>
The Input element inside each Function element must contain a list of all input parameters. Only one Input element is permitted per function.
Each Parameter element inside the Input element includes two attributes: the parameter Name and Type. The Name is arbitrary but should reflect the parameter usage because it is used for error reporting. If there is a type mismatch for any parameters at runtime, these names are passed to you.
The software uses strong type checking to help ensure correctness of data at runtime. The Type of each parameter supports this requirement. The types available map directly into C base types via these typedefs:
typedef int C8Bool; typedef char C8Char; typedef char* C8CharPtr; typedef void* C8BlobPtr; typedef void C8Blob; // For Microsoft Windows #if defined(_MSC_VER) typedef __int32 C8Int; typedef unsigned __int32 C8UInt; typedef __int64 C8Long; typedef unsigned __int64 C8ULong; typedef double C8Float; typedef __int64 C8Timestamp; typedef __int64 C8Interval; typedef size_t C8SizeType; #else /* defined(_MSC_VER) */ typedef int32_t C8Int; typedef u_int32_t C8UInt; typedef int64_t C8Long; typedef u_int64_t C8ULong; typedef double C8Float; typedef int64_t C8Timestamp; typedef int64_t C8Interval; typedef size_t C8SizeType; #endif /* defined(_MSC_VER) */
Only the above typedef names are supported in the Type attribute. Throughout the C code for your UDF, you should use the typedef names rather than the underlying C types.
The output element at line 12 of the sample XML file contains only a single Parameter field and describes the function output in the same manner as input parameters. The Name attribute is optional. It is not currently possible to return more than a single value from a UDF.