![]() |
|
The database server recognizes the following SQL statements for the registration of user-defined routines in the database:
A user must have the following privileges to issue a CREATE FUNCTION or CREATE PROCEDURE statement that registers a user-defined routine in the database:
After you register the UDR, you can assign routine-level privileges.
Database-level privileges control the ability to extend the database by registering or dropping a user-defined routine. The following users qualify to register a new routine in the database:
Tip: For an explanation of the DBA keyword, see Executing a UDR as DBA.
A DBA must grant the Resource privilege required for any non-DBA user to create a routine. The DBA can revoke the Resource privilege, which prevents that user from creating additional routines.
A DBA or the routine owner can cancel the registration with the DROP ROUTINE, DROP FUNCTION or DROP PROCEDURE statement. A DBA or routine owner can register a modification to the routine with the ALTER ROUTINE, ALTER FUNCTION, or ALTER PROCEDURE statement.
The language-level Usage privilege controls the ability to write a user-defined routine in a particular UDR language. This privilege needs to be granted by user informix or by another user who been granted DBA privilege with WITH GRANT OPTION.
UDR languages have the following GRANT and REVOKE requirements for the Usage privilege:
The following GRANT statement grants Usage privilege on UDRs written in Java to the user named dorian:
By default, the database server:
For more information, see the description of privileges in the Informix Guide to Database Design and Implementation and the description of the GRANT statement in the Informix Guide to SQL: Syntax
When you register a user-defined routine, you automatically receive Execute privilege on that routine. Execute privilege allows you to invoke the user-defined routine. For information about allowing other users to execute your routine, see Assigning Execute Privilege to a Routine.
You write an SPL routine in SPL, the internal language of the database server. For an SPL routine, the CREATE FUNCTION or CREATE PROCEDURE statement performs the following tasks:
For information on how to optimize an SPL routine, see Chapter 13, Improving UDR Performance.
For a summary of the UDR information in the system catalog tables, refer to Reviewing Information about User-Defined Routines
Figure 4-1 shows the parts of a CREATE FUNCTION statement that registers a user-defined function called abs_eq().
When you create an SPL function, you can specify optional routine modifiers that affect how the database server executes the function. SPL procedures do not allow routine modifiers. Use the WITH clause of the CREATE FUNCTION statement to list function modifiers. SPL functions allow the following routine modifiers:
In Figure 4-1, the NOT VARIANT modifier indicates that the abs_eq() SPL function is written so that it always returns the same value when passed the same arguments.
For more information about the CREATE FUNCTION and CREATE PROCEDURE statements and about the syntax of SPL, refer to the Informix Guide to SQL: Syntax. For information about creating using SPL routines, refer to the Informix Guide to SQL: Tutorial.
You can write an external routine in an external language that the database server supports. Once you have a shared-object file for the external routine, you register the routine and its shared-object file in the database.
The CREATE FUNCTION and CREATE PROCEDURE statements provide the location of the external routine, as follows:
For example, Figure 4-2 shows a CREATE FUNCTION statement that registers a user-defined function called abs_eq(), whose corresponding C function is in a shared-object file called abs.so.
The format of the external name often depends on the language in which the UDR is written. You specify the path of the shared-object file in the CREATE FUNCTION (or CREATE PROCEDURE) statement when you register the external routine.
Figure 4-2 shows a CREATE FUNCTION that specifies a shared-object file for a C user-defined function.
Important: Before you can register an external routine, you must have Usage permission for the language in which you wrote the routine. For more information on the Usage privilege, see Language-Level Privilege.
Registering an External Function
To create an external function, write the body of the function in a language other than SPL and then use the CREATE FUNCTION statement to register the function. The RETURNING clause of CREATE FUNCTION specifies the return data type of the external function.
Important: To register an external UDR that is a function, you must use the CREATE FUNCTION statement. You cannot use the CREATE PROCEDURE statement to create an external function.
For example, the following CREATE FUNCTION statement registers an external function called equal() that takes two arguments, arg1 and arg2, of data type udtype1 and returns a single value of the data type BOOLEAN:
In the preceding example, the END FUNCTION keywords are optional. For more information, see the CREATE FUNCTION statement in the Informix Guide to SQL: Syntax.
When you do not want your routine to return a value, you must create the routine as a procedure. To create an external procedure, write the body of the procedure in a language other than SPL and then use the CREATE PROCEDURE statement to register the procedure.
The following example shows how to register an external procedure that is written in C:
In the preceding example, the actual external procedure is located in a C routine called compare_n_insert(), which is located in the C-language shared library /usr/lib/udtype1/lib/libbtype2.so. If the EXTERNAL NAME clause does not specify an entry point within the library, the database server invokes the module at the default entry point, log_compare().
The following example also includes the SPECIFIC keyword to create a function alias, basetype2_lessthan. Once you use the SPECIFIC keyword to create a routine alias, you can use that alias in DROP statements.
For information about the CREATE PROCEDURE statement, see the CREATE PROCEDURE statement in the Informix Guide to SQL: Syntax.
When you create an external routine, you can specify optional modifiers that tell the database server about attributes of the UDR. Use the WITH clause of the CREATE FUNCTION and CREATE PROCEDURE statements to list routine modifiers. Following the WITH keyword, the modifiers that you want to specify are enclosed within parentheses and separated by commas.
The following table shows the routine modifiers that are valid for external routines written in C.
The following example shows how to use the WITH clause to specify a set of modifiers when you create an external function:
The HANDLESNULLS modifier indicates that the basetype2_lessthan() function (in the shared library /usr/lib/basetype2/lib/libbtype2.so) is written so that it can recognize when an SQL null argument is passed.
The following table shows the routine modifiers that are valid for external routines written in Java.
Routine Modifier | Type of UDR |
---|---|
CLASS | Access to JVP |
ITERATOR | Iterator function |
HANDLESNULLS | UDR that handles SQL null values as arguments |
NEGATOR | Negator function |
PARALLELIZABLE | Parallelizable UDR |
The CREATE FUNCTION and CREATE PROCEDURE statements specify any parameters and return value for a C UDR. These statements use SQL data types for parameters and the return value. For example, suppose a C UDR has the following C declaration:
The following CREATE FUNCTION statement registers the func1() user-defined function:
Use the opaque SQL data type, POINTER, to specify a data type for an external routine whose parameter or return type has no equivalent SQL data type. The CREATE FUNCTION or CREATE PROCEDURE statement uses the POINTER data type when the data structure that an external routine receives or returns is a private data type, not one that is available to users.