From 73a4dd56109c4799fd1d4ac7ed56ff72a47279bb Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Fri, 1 Aug 2008 05:37:15 +0000 Subject: Bug 440188: buglist.cgi should display EXPLAIN output when &debug=1 Patch By Max Kanat-Alexander r=LpSolit, a=mkanat --- Bugzilla/DB/Mysql.pm | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'Bugzilla/DB/Mysql.pm') diff --git a/Bugzilla/DB/Mysql.pm b/Bugzilla/DB/Mysql.pm index 8a64d3646..fdb475078 100644 --- a/Bugzilla/DB/Mysql.pm +++ b/Bugzilla/DB/Mysql.pm @@ -48,6 +48,8 @@ use Bugzilla::Util; use Bugzilla::Error; use Bugzilla::DB::Schema::Mysql; +use List::Util qw(max); + # This module extends the DB interface via inheritance use base qw(Bugzilla::DB); @@ -204,6 +206,30 @@ sub sql_group_by { return "GROUP BY $needed_columns"; } +sub bz_explain { + my ($self, $sql) = @_; + my $sth = $self->prepare("EXPLAIN $sql"); + $sth->execute(); + my $columns = $sth->{'NAME'}; + my $lengths = $sth->{'mysql_max_length'}; + my $format_string = '|'; + my $i = 0; + foreach my $column (@$columns) { + # Sometimes the column name is longer than the contents. + my $length = max($lengths->[$i], length($column)); + $format_string .= ' %-' . $length . 's |'; + $i++; + } + + my $first_row = sprintf($format_string, @$columns); + my @explain_rows = ($first_row, '-' x length($first_row)); + while (my $row = $sth->fetchrow_arrayref) { + my @fixed = map { defined $_ ? $_ : 'NULL' } @$row; + push(@explain_rows, sprintf($format_string, @fixed)); + } + + return join("\n", @explain_rows); +} sub _bz_get_initial_schema { my ($self) = @_; -- cgit v1.2.3-24-g4f1b