So far in this chapter, we have talked about how you can handle errors raised by MySQL as it executes SQL statements within the stored program. In addition to these system-raised exceptions, however, you will surely have to deal with errors that are specific to an application’s domain of requirements and rules. If that rule is violated in your code, you may want to raise your own error and communicate this problem back to the user. The SQL:2003 specification provides the SIGNAL statement for this purpose.
TheSIGNALstatement allows you to raise your own error conditions. Unfortunately, at the time of writing, theSIGNALstatement is not implemented within the MySQL stored program language (it is currently scheduled for MySQL 5.2).
You can’t use theSIGNALstatement in MySQL 5.0, but we are going to describe it here, in case you are using a later version of MySQL in which the statement has been implemented. Visit this book’s web site (see the Preface for details) to check on the status of this and other enhancements to the MySQL stored program language.
So let’s say that we are creating a stored procedure to process employee date-of-birth changes, as shown in Example 6-16. Our company never employs people under the age of 16, so we put a check in the stored procedure to ensure that the updated date of birth is more than 16 years ago (thecurdate()function returns the current timestamp).
Missing SQL:2003 Features
Example 6-16. Example stored procedure with date-of-birth validation
CREATE PROCEDURE sp_update_employee_dob
This implementation will work, but it has a few disadvantages. The most significant problem is that if the procedure is called from another program, the procedure will return success (at least, it will not raise an error) even if the update was actually rejected. Of course, the calling program could detect this by examining thep_statusvariable, but there is a good chance that the program will assume that the procedure succeeded since the procedure call itself does not raise an exception.
We have designed the procedure so that it depends on the diligence of the programmer calling the procedure to check the value of the returning status argument. It is all too tempting and easy to assume that everything went fine, since there was no error.
To illustrate, if we try to set an employee’s date of birth to the current date from the MySQL command line, everything seems OK:
mysql> CALL sp_update_employee_dob(1,now(),@status);
It is only if we examine the status variable that we realize that the update did not complete:
mysql> SELECT @status;
This stored procedure would be more robust, and less likely to allow errors to slip by, if it actually raised an error condition when the date of birth was invalid. The ANSI SQL:2003SIGNALstatement allows you to do this:
SIGNALtakes the following form:
SIGNAL SQLSTATE sqlstate_code|condition_name [SET MESSAGE_TEXT=string_or_variable];
You can create your ownSQLSTATEcodes (there are some rules for the numbers you are allowed to use) or use an existingSQLSTATEcode or named condition. When MySQL implementsSIGNAL, you will probably be allowed to use a MySQL error code (within designated ranges) as well.
When theSIGNALstatement is executed, a database error condition is raised that acts in exactly the same way as an error that might be raised by an invalid SQL statement or a constraint violation. This error could be returned to the calling program or could be trapped by a handler in this or another stored program. IfSIGNALwere available to us, we might write the employee date-of-birth birth procedure, as shown in Example 6-17.
Example 6-17. Using the SIGNAL statement (expected to be implemented in MySQL 5.2)
CREATE PROCEDURE sp_update_employee_dob
IF DATE_SUB(curdate(), INTERVAL 16 YEAR) <P_DOB THEN
If we ran this new procedure from the MySQL command line (when MySQL implementsSIGNAL), we would expect the following output:
mysql> CALL sp_update_employee(1,now());
UsingSIGNAL, we could make it completely obvious to the user or calling program that the stored program execution failed.
blog comments powered by Disqus