As mentioned in my earlier post Emulating Oracle Output Functionality, I’ll be speaking at the MySQL Users Conference on the topic of MySQL for Oracle Developers. Here is the second in a series of points regarding current MySQL Stored Procedures and Functions functionality.
- 3. Named Parameters
- 4. Procedure Overloading
3. Named Parameters
Parameters passed to Procedures under normal operations can be considered positional parameters. For each parameter, a calling statement is required to pass the same number of parameters and in the same order. Named parameters allows the user to pass parmeters in the order of their choosing, and also not pass all parameters.
For Example, consider the following Oracle Stored Procedure definition.
EXAMPLE_PROCEDURE(num IN NUMBER DEFAULT 0, dte IN DATE DEFAULT SYSDATE, str IN VARCHAR2 DEFAULT NULL, str2 IN VARCHAR2 DEFAULT NULL);
This can be called in several ways, here are few examples.
EXAMPLE_PROCEDURE(); EXAMPLE_PROCEDURE(9, '01-MAR-2006', 'Hello World', NULL); EXAMPLE_PROCEDURE(dte => '01-MAR-2006'); EXAMPLE_PROCEDURE(dte => '01-MAR-2006', str2 => 'Hello World');
The key to being able to use named parameters, is the addition of the DEFAULT syntax in the variable definition.
Giuseppe Maxia has many MySQL stored procedures, and I applaud his initiate with The MySQL General Purpose Stored Routines Library. Included here is an implementation of handling named parameters. I can’t say I’ve used it personally, however I did browse the code (another Open Source benefit).
4. Procedure Overloading
Oracle provides the capability like Java, which allows for multiple procedures to have the same name, but have a different set of parameter arguments.
Oracle provides the ability to overload a Procedure call with the same number of arguments and varying datatypes, for example, going with the earlier DBMS_OUTPUT.PUT_LINE analogy, Oracle has the following specification.
DBMS_OUTPUT.PUT_LINE (item IN NUMBER); DBMS_OUTPUT.PUT_LINE (item IN VARCHAR2); DBMS_OUTPUT.PUT_LINE (item IN DATE);
Likewise, it possible to have procedures with different numbers and types of arguments.
EXAMPLE_PROCEDURE(num IN NUMBER); EXAMPLE_PROCEDURE(num IN NUMBER, str IN VARCHAR2); EXAMPLE_PROCEDURE(num IN NUMBER, dte IN DATE); EXAMPLE_PROCEDURE(num IN NUMBER, dte IN DATE, str IN VARCHAR2, str2 IN VARCHAR2); etc.
As MySQL growth continues and developers take up placing greater business logic in the database with Stored Procedures, there may be merit to consider these points. On the flip side, 10 years ago, business logic was placed in the database for many reasons including the extent of capable client programming languages, a Client/Server architecture and of course tight coupling with Oracles GUI products. One could argue a justification as to how much business logic is stored in the database, and how much is managed within an application. Given the advent of the Web, and multiple applications and clients assessing coporate data, it’s logical to place essential logic as close to the source of the data as possible, and the introduction of Stored Procedures in MySQL 5 released last year provided the capacity to consider this.
At this time, I would err on the conservative side with the use of Stored Procedures. The type of application, requirements and signficantly the age of the product (being a new product or existing legacy product) all affect the outcome. As mentioned, Giuseppe feels strongly regarding named parameters, and has provided a workaround. The benefit with Open Source is this FREEDOM clearly exists when the community contributes. This can only benefit MySQL in areas such as Stored Procedures.
Myself, only a few days ago over the weekend, I took an Java Open Source product, that lacked the capacity to support the calling of Stored Procedures via JDBC, developed it myself and then submitted my work back to the Apache community. I was even more happy when it was accepted unaltered, committed and in the nightly build for the next day. Read More. I’m now working on the next contribution to the same project, providing JDBC Transaction Support. While this may seem a sidetrack, I’m actually specifically using this product in testing and usability of MySQL, so ultimately everybody wins.