Writing SPL Routines
An SPL routine consists of a beginning statement, a statement block, and an ending statement. Within the statement block, you can use SQL or SPL statements.
Using the CREATE PROCEDURE or CREATE FUNCTION Statement
You must first decide if the routine you are creating returns values or not. If the routine does not return values, you create an SPL procedure. To create an SPL procedure, you use the CREATE PROCEDURE statement. If the routine returns a value, you create an SPL function. To create an SPL function, you use the CREATE FUNCTION statement.
Beginning and Ending the Routine
When you create an SPL procedure that does not return values, start with the CREATE PROCEDURE statement and end with the END PROCEDURE keyword. Figure 14-1 shows how to begin and end an SPL procedure.
Figure 14-1
The name that you assign to the SPL routine can be up to 18 characters long. For more information about naming conventions, see the Identifier segment in the Informix Guide to SQL: Syntax.
To create an SPL function that returns one or more values, start with the CREATE FUNCTION statement and end with the END FUNCTION keyword. Figure 14-2 shows how to begin and end an SPL function.
Figure 14-2
The entire text of an SPL routine, including spaces and tabs, must not exceed 64 kilobytes.
In SPL routines, the END PROCEDURE or END FUNCTION keywords are required. Furthermore, the keyword PROCEDURE or FUNCTION must match in the beginning and ending statements.
Specifying a Routine Name
You specify a name for the routine immediately following the CREATE PROCEDURE or CREATE FUNCTION statement and before the parameter list, as Figure 14-3 shows.
Figure 14-3
Universal Server allows you to create more than one SPL routine with the same name but with different parameters. This feature is known as routine overloading. For example, you might create each of the following SPL routines in your database:
If you call a routine with the name multiply(), the database server evaluates the name of the routine and its arguments to determine which routine to execute. Routine resolution is the process in which the database server searches for a routine signature that it can use, given the name of the routine and a list of arguments. Every routine has a signature that uniquely identifies the routine based on the following information:
The routine signature is used in a CREATE, DROP, or EXECUTE statement if you enter the full parameter list of the routine. For example, each statement in Figure 14-4 uses a routine signature.
Figure 14-4
Adding a Specific Name
Due to routine overloading, an SPL routine might not be uniquely identified by its name alone. However, a routine can be uniquely identified by a specific name. A specific name is a unique identifier that you define in the CREATE PROCEDURE or CREATE FUNCTION statement, in addition to the routine name. A specific name is defined with the SPECIFIC keyword and is unique in the database. Two routines in the same database cannot have the same specific name, even if they have different owners.
A specific name can be up to 18 characters long. Figure 14-5 shows how to define the specific name calc in a CREATE FUNCTION statement that creates the calculate() function.
Figure 14-5
Because the owner bsmith has given the SPL function the specific name calc1, no other user can define a routine-SPL or external-with the specific name calc1. Now you can refer to the routine as bsmith.calculate, or with the SPECIFIC keyword as calc1, in any statement that requires the SPECIFIC keyword.
Adding a Parameter List
When you create an SPL routine, you can define a parameter list, so that the routine accepts one or more arguments when it is invoked. The parameter list is optional.
A parameter to an SPL routine must have a name and can be defined with a default value. A parameter can specify any of the following categories of data types:
A parameter cannot specify any of the following data types:
Each statement in Figure 14-6 shows a valid parameter list.
Figure 14-6
When you define a parameter, you accomplish two tasks at once:
If you define a parameter with a default value, the user can execute the SPL routine with or without the corresponding argument. If the user executes the SPL routine without the argument, the database server assigns the parameter the default value as an argument.
When you invoke an SPL routine, you can give an argument a null value. SPL routines handle null values by default. However, you cannot give an argument a null value if the argument is a collection element.
Using Simple Large Objects as Parameters
Although you cannot define a parameter with a TEXT or BYTE data type, you can use the REFERENCES keyword to define a parameter that points to a TEXT or BYTE data type as Figure 14-7 shows.
Figure 14-7
The REFERENCES keyword means that the SPL routine is passed a descriptor that contains a pointer to the simple large object, not the object itself.
Undefined Arguments
When you invoke an SPL routine, you can specify all, some, or none of the defined arguments. If you do not specify an argument, and if its corresponding parameter does not have a default value, the argument, which is used as a variable within the SPL routine, is given a status of undefined.
Undefined is a special status used for SPL variables that have no value. The SPL routine executes without error, as long as you do not attempt to use the variable that has the status undefined in the body of the routine.
The undefined status is not the same as a null value. Null means the value is not available or not applicable.
Adding a Return Clause
If you use CREATE FUNCTION to create an SPL routine, you must specify a return clause that returns one or more values.
To specify a return clause, use the RETURNING or RETURNS keyword with a list of data types the routine will return. The data types can be any SQL data types except SERIAL, SERIAL8, TEXT, BYTE, CLOB, or BLOB.
The return clause in Figure 14-8 specifies that the SPL routine will return an INT value and a REAL value.
Figure 14-8
Once you specify a return clause, you must also specify a RETURN statement in the body of the routine that explicitly returns the values to the calling routine. For more information on writing the RETURN statement, see "Returning Values from an SPL Function".
To specify that the function should return a simple large object (a TEXT or BYTE value), you must use the REFERENCES clause, as in Figure 14-9, because an SPL routine returns only a pointer to the object, not the object itself.
Figure 14-9
Specifying a Document Clause
The DOCUMENT and WITH LISTING IN clauses follow END PROCEDURE or END FUNCTION.
The DOCUMENT clause lets you add comments to your SPL routine that another routine can select from the system catalog tables, if needed. The DOCUMENT clause in Figure 14-10 contains a usage statement that shows a user how to run the SPL procedure.
Figure 14-10
Remember to place single or double quotation marks around the literal clause. If the literal clause extends past one line, place quotation marks around each line.
Specifying a Listing File
The WITH LISTING IN option allows you to direct any compile-time warnings that may occur to a file. Figure 14-11, which is similar to Figure 14-10, logs the compile-time warnings in /tmp/warn_file.
Figure 14-11
Remember to place single or double quotation marks around the filename or pathname.
Adding Comments
You can add a comment to any line of an SPL routine, even a blank line.
To add a comment, place a double dash (--) before the comment or enclose the comment in braces ( { } ). The double dash complies with the ANSI standard. The braces are an Informix extension to the ANSI standard.
To add a multiple-line comment, you can either
All the examples in Figure 14-12 are valid comments.
Figure 14-12
Dropping an SPL Routine
Once you create an SPL routine, you cannot change the body of the routine. Instead, you need to drop the routine and re-create it. Before you drop the routine, however, make sure that you have a copy of its text somewhere outside the database.
In general, use DROP PROCEDURE with a procedure name and DROP FUNCTION with a function name, as Figure 14-13 shows.
Figure 14-13
However, if the database has other routines of the same name (overloaded routines), you cannot drop the SPL routine by its routine name alone. To drop a routine that has been overloaded, you must specify either its signature or its specific name. Figure 14-14 shows two ways that you might drop a routine that is overloaded.
Figure 14-14
If you do not know the type of a routine (function or procedure), you can use the DROP ROUTINE statement to drop it. DROP ROUTINE works with either functions or procedures. DROP ROUTINE also has a SPECIFIC keyword, as Figure 14-15 shows.
Figure 14-15
Before you drop an SPL routine stored on a remote database server, be aware of the following restriction. You can drop an SPL routine with a fully qualified routine name in the form database@dbservername:owner.routinename only if the routine name alone, without its arguments, is enough to identify the routine. Because user-defined data types on one database might not exist on another database, you cannot use qualified names with arguments that are user-defined types.
|