MySQL Debugging

Bei der Programmierung von gespeicherte Routinen für den MySQL vermisst man ein Äquivalent zum Oracle Package “dbms_output”. Wir haben uns mit folgender Lösung beholfen.

In einer zusätzlichen Datenbank “dbmc” wird eine Tabelle “debug_output” angelegt.

CREATE DATABASE dbmc;

DROP TABLE IF EXISTS `dbmc`.`debug_output`;
CREATE TABLE `dbmc`.`debug_output` (
  `proc_id` varchar(100) DEFAULT NULL,
  `debug_output` text,
  `line_id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY  (`line_id`)
);

Die drei Prozeduren “debug_on”, “debug_insert” und “debug_show” ermöglichen uns den Zugriff auf die Debugging-Informationen.

DELIMITER $$

DROP PROCEDURE IF EXISTS `dbmc`.`debug_on` $$
CREATE PROCEDURE `dbmc`.`debug_on`(IN $proc_id varchar(100))
begin
  declare $committer int DEFAULT -1;
  SELECT count(proc_id) INTO $committer FROM dbmc.debug_output
  WHERE proc_id = $proc_id;
  IF $committer > 0 then
    DELETE FROM dbmc.debug_output WHERE proc_id = $proc_id;
  end IF;
  INSERT INTO dbmc.debug_output (proc_id,debug_output)
  VALUES ($proc_id, concat(‘debug started: ‘, now()));
end $$

DROP PROCEDURE IF EXISTS `dbmc`.`debug_insert` $$
CREATE PROCEDURE `dbmc`.`debug_insert`(IN $proc_id varchar(100),IN $debug text)
begin
  declare $committer int DEFAULT -1;
  SELECT count(proc_id) INTO $committer FROM dbmc.debug_output
  WHERE proc_id = $proc_id;
  IF $committer > 0 then
    INSERT INTO dbmc.debug_output (proc_id,debug_output)
    VALUES ($proc_id, $debug);
  end IF;
end $$

DROP PROCEDURE IF EXISTS `dbmc`.`debug_show` $$
CREATE PROCEDURE `dbmc`.`debug_show`(IN $proc_id varchar(100))
begin
  SELECT debug_output FROM dbmc.debug_output WHERE proc_id = $proc_id ORDER BY line_id;
end $$

DELIMITER ;

Bei der Anwendung schaltet man mit “dbmc.debug_on(‘ABC’)” die Protokollierung ein. Während der Ausführung erfasst man die Informtionen mit “dbmc.debug_insert(‘ABC’, ‘hallo welt!’)”. Bis zu einem Neustart der Anwendung kann man die erfassten Zeilen mit “dbmc.debug_show(‘ABC’)” abrufen.

This entry was posted in Sonstiges. Bookmark the permalink.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

*

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>