Join the FSF as an Associate Member!

MySPwizard

A stored procedure generator for MySQL databases

Copyright © 2006 Manuele Vaggelli

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.

Table of Contents


What is MySPwizard?

Description

MySPWizard is a simple command-line program to generate stored procedures for MySQL databases. The program creates a file containing the sql instructions to produce stored procedures, than you can execute the file using MySQL in batch mode and the stored procedure will be generated inside mysql.

The program has been developed using the C API distributed with MySQL and compiled using the mysql_config script (please see the makefile).
For further information please refer to "MySQL 5.0 Reference Manual" chapter "22.2. MySQL C API" for developing or modifying this program and chapter "22.2.14. Building Client Programs" for compiling it.

The program use the GNU C library so please refer to the manual "The GNU C Library" for further information.
In particular see chapter "5.4 Copying and Concatenation" for string manipulation and chapter "25.2 Parsing program options using getopt" for parsing program options.

Files Included in this Package

This package contains the following files.


Obtaining MySPwizard

You may obtain the MySPwizard program by clicking on the link located below, all the source code is included along with documentation and the makefile to compile the program.

http://download.savannah.nongnu.org/releases/myspwizard/

To extract files:
tar -xzf myspwizard.tar.gz


Compiling

To compile the program you can use the makefile included in the distribution.
Please modify it accordingly and run make.
Note that the makefile uses the mysql_config script as described in chapter "22.2.14.Building Client Programs" of "MySQL 5.0 Reference Manual" so refer to the official MySQL documentation for further information or trubles in compiling the program.


Using MySPWizard

Once you have compiled the program you can use it as described in the following example, but first of all let's look at the usage of MySPwizard, just type:

shell> myspwizard
The result is:
MySPwizard - Version: 1.0

Copyright (C) 2006 Manuele Vaggelli [manuele.vaggelli@member.fsf.org]
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Usage: myspwizard [OPTIONS] DATABASE TABLE

-h   Connect to host
     (if it is not given 'localhost' is assumed)
-p   Password to use when connecting to the server
-P   Port number to use for connection
     (if it is not given '3306' is assumed)
-u   User to use when connecting to the server
     (if it is not given the current user is assumed)
-s   The SQL Statement Type
     one of: INSERT || UPDATE || DELETE || SELECT
     (if it is not given 'SELECT' is assumed)

And now an example: suppose we have a local instance of MySQL Server, a database named `mycompany` and a table named `customers`. To generate a stored procedure for a select statement just type:
shell> myspwizard mycompany customers
The result should be:
RESULT OK:
mycompany_select_customers succesfully created in file mycompany_select_customers.sql
The program has succesfully created a file containing the sql statement to generate the stored procedure.
The file mycompany_select_customers.sql should look like the following:
/*
Stored Procedure generated by: MySPwizard - Version:1.0
root@localhost
Wed Aug 16 22:56:42 2006
*

USE `mycompany`;

DELIMITER $$

DROP PROCEDURE IF EXISTS `mycompany`.`mycompany_select_customers` $$

CREATE PROCEDURE `mycompany`.`mycompany_select_customers`(
_CustomerID int(10), /* allow_null:NO - key_type:PRI - default_value:NULL - extra:auto_increment */
_CompanyName varchar(40), /* allow_null:NO - key_type: - default_value: - extra: */
_Address varchar(60), /* allow_null:YES - key_type: - default_value:NULL - extra: */
_City varchar(15), /* allow_null:YES - key_type: - default_value:NULL - extra: */
_Region varchar(15), /* allow_null:YES - key_type: - default_value:NULL - extra: */
_PostalCode varchar(10), /* allow_null:YES - key_type: - default_value:NULL - extra: */
_Country varchar(15), /* allow_null:YES - key_type: - default_value:NULL - extra: */
_Phone varchar(24), /* allow_null:YES - key_type: - default_value:NULL - extra: */
_Fax varchar(24) /* allow_null:YES - key_type: - default_value:NULL - extra: */
)
BEGIN

SELECT CustomerID,
CompanyName,
Address,
City,
Region,
PostalCode,
Country,
Phone,
Fax
FROM customers
WHERE CustomerID = _CustomerID
AND CompanyName = _CompanyName
AND Address = _Address
AND City = _City
AND Region = _Region
AND PostalCode = _PostalCode
AND Country = _Country
AND Phone = _Phone
AND Fax = _Fax;

END $$

DELIMITER ;

Now you can do what you prefer with the file, but the normal step is to execute it using mysql in batch mode:
shell> mysql < mycompany_select_customers.sql

For further information about executing mysql in batch mode please see "3.5. Using mysql in Batch Mode" of the "MySQL 5.0 Reference Manual"

Now the stored procedure has been created inside `mycompany` database, you can verify running the following command when you are in mysql interactive mode:
mysql > show procedure status where Db = 'mycompany';
The result should look like the following:
+-----------+----------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment |
+-----------+----------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+
| mycompany | mycompany_select_customers | PROCEDURE | root@localhost | 2006-08-16 23:36:17 | 2006-08-16 23:36:17 | DEFINER | |
+-----------+----------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+

If you use the MySQL Query Browser GUI you can select File > Load Script, then look for mycompany_select_customers.sql file, select it and click on Execute.


Functions

The concat.h file has the declaration of the function concat that is defined in concat.c. This function is used to help to concatenate strings using dynamic memory allocation. The myspwizard.c includes the entry point and all the other functions.

concat.c

myspwizard.c


Miscellaneous

Release Notes 1.0

This is the initial release

I wrote and built this simple program using the following environment:

Future Releases

License

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License is available here. You can also get a copy by writing to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Credits

Support

Please report bugs and suggestions to Please, first of sending mail remember that you need the mysqlclient library and please read carefully the following documentation:
Join the FSF as an Associate Member!