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.
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.
This package contains the following files.
concat
for string concatenation. concat
function
declaration. 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
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.
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> myspwizardThe 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 customersThe result should be:
RESULT OK:The program has succesfully created a file containing the sql statement to generate the stored procedure.
mycompany_select_customers succesfully created in file mycompany_select_customers.sql
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 ;
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
.
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.
char * concat (const char *str, ...)Concatenates strings using dynamic memory allocation
void save_spfile(char *sp_sql, char *sp_fname)Creates the file containing the SQL statement to generate the stored procedure
void show_usage(void)Composes and print the usage message
char * get_sp_start(void)Composes and returns the initial SQL stataments for the stored procedure
char * get_sp_end(void)Composes and returns the final SQL stataments for the stored procedure
char * get_sp_comments(void)Composes and returns the comments to print in the header of the stored procedure
char * get_sp_parameters()Composes and returns the parameters for the stored procedure
char * get_sql_select(void)Composes and returns the SQL statement for a stored procedure of type SELECT
char * get_sql_insert(void)Composes and returns the SQL statement for a stored procedure of type INSERT
char * get_sql_update(void)Composes and returns the SQL statement for a stored procedure of type UPDATE
char * get_sql_delete(void)Composes and returns the SQL statement for a stored procedure of type DELETE
char * get_fdate(void)Composes, formats and returns the current datetime as string
char * get_curr_statement(char *stmnt)Determines the statement to use to build the stored procedure
-s
option.I wrote and built this simple program using the following environment:
Debian GNU/Linux 3.1
2.6.8-2
3.3.5
2.3.2
5.0.22-standard
GNU Emacs 21.4.1
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.