12.4. Calling Stored Programs from Application CodeMost languages used to build applications that interact with MySQL are able to fully exploit stored programs In general, the techniques for using stored programs differ from those for standard SQL statements in two significant respects:
So, calling a stored program requires a slightly different program flow from standard SQL processing. The overall sequence of events is shown in the UML "retro" diagram (e.g., flowchart) in Figure 12-3. Here's a brief description of each of these steps. Remember that in the next five chapters, we will be showing you how to follow these steps in various languages. 12.4.1. Preparing a Stored Program Call for ExecutionWe'll normally want to call a stored program more than once in our application. Typically, we first create a statement handle for the stored program. We then iteratively execute the program, perhaps providing different values for the program's parameters with each execution. It's usually possible to bypass the preparation stage and execute a stored program directlyat least if the stored program returns no result sets. However, if the stored program takes parameters and you execute the stored program more than once in your program, we recommend that you go to the extra effort of preparing the statement that includes your stored program call. 12.4.2. Registering ParametersWe can pass parameters into stored programs that require them as literals (e.g., concatenate the text of the parameter values into the stored program CALL statement). Figure 12-3. General processing flow when calling a stored program from an external languageHowever, in all of the languages we discuss in subsequent chapters, there are specific parameter-handling methods that allow us to re-execute a stored program with new parameters without having to re-prepare the stored program call. As we said previously, it's best to use these explicit methods if you are going to execute the stored program more than onceboth because it is slightly more efficient and because, in some cases, only the prepared statement methods offer full support for bidirectional parameters and multiple result sets. The methods for passing parameters to stored programs are usually the same as the methods used to pass parameters (or "bind variables") to normal SQL statements. 12.4.3. Setting Output ParametersSome languages allow us to specifically define and process output parameters Both techniquesthe direct API calls provided by .NET and JDBC and the session variable solution required by other languagesare documented in the relevant language-specific chapters that follow. 12.4.4. Executing the Stored ProgramOnce the input parameters are set andin the case of .NET and Javaonce the output parameters are registered, we can execute the stored program. The method for executing If the stored program returns no result sets 12.4.5. Retrieving Result SetsThe process of retrieving However, unlike SELECT and SHOW statements, a stored program may return multiple result sets, and this requires a different flow of control in our application. To correctly process all of the result sets that may be returned from a stored program, the programming language API must include a method to switch to the "next" result set and possibly a separate method for determining if there are any more result sets to return. JDBC and ADO.NET languages have included these methods since their earliest incarnations (for use with SQL Server and other RDBMSs that support multiple result sets), and these interfaces have been fully implemented for use with MySQL stored programs 12.4.6. Retrieving Output ParametersOnce all result sets have been retrieved, we are able to retrieve any stored program output parameters. Not all languages provide methods for directly retrieving the values of output parameterssee the "Setting Output Parameters" section earlier for a description of a language-independent method of retrieving output parameters indirectly through user variables. JDBC and ADO.NET provide specific calls that allow you to directly retrieve the value of an output parameter. 12.4.7. Closing or Re-Executing the Stored ProgramNow that we have retrieved the output parameters, the current stored program execution is complete. If we are sure that we are not going to re-execute the stored program, we should close it using language-specific methods to release all resources associated with the stored program execution. This usually means closing 12.4.8. Calling Stored FunctionsIn some languagesJDBC and .NET, in particularstored functions |
Saturday, October 24, 2009
Section 12.4. Calling Stored Programs from Application Code
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment