Depurar procedimientos almacenados mysql
Depurar procedimientos de bases de datos suele ser una tarea ingrata a no ser que tengamos alguna herramienta de desarrollo. Pero aún estas herramientas suelen modificar internamente el procedimiento para efectuar el depurado además de crear multitud de tablas intermedias para guardar el estado de las variables en cualquier punto de la ejecución.
Si no te interesa este enfoque y sólo necesitas ir haciendo un log de lo que ocurre propongo el siguiente sistema que he ido mejorando a partir de diversas fuentes y experiencias.
La idea se basa en la creación de una tabla temporal donde vamos realizando el log mediante una llamada al procedimiento doLog(msg).
Primero creamos una tabla de configuración:
CREATE TABLE `internal_config` (
`debug` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into internal_config(debug) values(0);
Que nos servirá para activar/desactivar el debug con:
update internal_config set debug=true;
El procedimiento que crea la tabla temporal es:
CREATE PROCEDURE `setupTmpLog`()
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS tmplog(
lin integer NOT NULL AUTO_INCREMENT primary key,
msg VARCHAR(512)
) ENGINE = MEMORY;
END
y el que escribe el log:
CREATE PROCEDURE `doLog`(IN logMsg VARCHAR(512))
BEGIN
declare do_debug bool default false;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET do_debug=false;
DECLARE CONTINUE HANDLER FOR 1146 -- Table not found
BEGIN
CALL setupTmpLog();
INSERT INTO tmplog(msg) VALUES ('resetup tmp table');
INSERT INTO tmplog(msg) VALUES (logMsg);
END;
select debug from internal_config limit 1 into do_debug;
-- escribimos en el log solo si debug esta activado
if (do_debug) then
INSERT INTO tmplog(msg) VALUES (logMsg);
end if;
END
y finalmente, un procedimiento que nos muestra el log (con el último mensaje primero) y después borra la tabla:
CREATE PROCEDURE `view_log`()
BEGIN
DECLARE CONTINUE HANDLER FOR 1146 -- Table not found
BEGIN
CALL setupTmpLog();
end;
select msg from tmplog order by lin desc;
drop table tmplog;
END
Ya lo tenemos todo, para ir logeando en nuestros procedimientos sólo tenemos que llamar a doLog así:
call doLog(concat("Hola", " que ", " tal " ));
Y cuando queramos ver (y limpiar) el log hacemos:
call view_log();
Espero que os sirva. Good Luck ;-)
Fuentes:
http://www.drdobbs.com/database/debugging-mysql-stored-procedures/218100564?pgno=1
ebooks de ingeniería
Estos sitios tienen libros interesantes de ingeniería. Si alguno de ellos te interesa, puedes adquirirlos legalmente en las librerías especializadas:
http://bookos.org (sitio ruso, tienen de todo)
http://ieee-books.blogspot.com.es (brutal)
http://es.scribd.com (conocido sitio para compartir pdf)
http://freelibros.org
Problemas resueltos:
http://www.loseskakeados.com
Foros teleco:
http://www.foroteleco.com
http://www.lacomunateleco.com/descargas
http://teleco.apuntesulpgc.es
http://www.gradotelecovigo.com/
Vídeos y otros recursos:
Open courseware upv:
http://www.upv.es/contenidos/OCW/menu_urlc.html?http://www.upv.es/pls/oalu/sic_asi.OCW_Ramasi?p_rama=T&p_idioma=c&p_vista=MSE
http://users.alliedmods.net/~faluco/apuntespak/
entender los joins sql gráficamente
Este gráfico puede servirnos para recordar qué partes se incluyen o se excluyen en los diferentes tipos de joins:
INNER JOIN
LEFT JOIN
RIGHT JOIN
OUTER JOIN
LEFT JOIN EXCLUDING INNER JOIN
RIGHT JOIN EXCLUDING INNER JOIN
OUTER JOIN EXCLUDING INNER JOIN
Fuente:
Visual representation of SQL joins
Use zip
The following examples illustrate typical uses of the command zip for packaging a set of files into an "archive" file, also called "zip file". The command uses the standard zip file format. The archive files can therefore be used to tranfer files and directories between commonly used operating systems.
zip archivefile1 doc1 doc2 doc3
This command creates a file "archivefile1.zip" which contains a copy of the files doc1, doc2, and doc3, located in the current directory. zip archivefile1 *
This command creates a file "archivefile1.zip" which contains a copy of all files in the current directory in compressed form. However, files whose name starts with a "." are not included. The extension ".zip" is added by the program. zip archivefile1 .* *
This version includes the files that start with a dot. But subdirectories are still not included. zip -r archivefile1 .
This copies the current directory, including all subdirectories into the archive file. zip -r archivefile2 papers
This copies the directory "papers", located in the current directory, into "archivefile2.zip". zip -r archivefile3 /home/joe/papers
This copies the directory "/home/joe/papers" into "archivefile3.zip". Since in this case the absolute path is given, it doesn't matter what the current directory is, except that the zip file will be created there. unzip archivefile1.zip
This writes the files extracted from "archivefile1.zip" to the current directory.