]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 440188: buglist.cgi should display EXPLAIN output when &debug=1
authormkanat%bugzilla.org <>
Fri, 1 Aug 2008 05:37:15 +0000 (05:37 +0000)
committermkanat%bugzilla.org <>
Fri, 1 Aug 2008 05:37:15 +0000 (05:37 +0000)
Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=LpSolit, a=mkanat

Bugzilla/DB.pm
Bugzilla/DB/Mysql.pm
Bugzilla/DB/Oracle.pm
Bugzilla/DB/Pg.pm
buglist.cgi
skins/standard/buglist.css
template/en/default/list/list.html.tmpl

index b23c865c14c9ae3fd4884f70b34c43ecbbaf5532..399f3c64301c3d4b5c49a97096d31fd39849ed9a 100644 (file)
@@ -273,7 +273,7 @@ EOT
 # List of abstract methods we are checking the derived class implements
 our @_abstract_methods = qw(REQUIRED_VERSION PROGRAM_NAME DBD_VERSION
                             new sql_regexp sql_not_regexp sql_limit sql_to_days
-                            sql_date_format sql_interval);
+                            sql_date_format sql_interval bz_explain);
 
 # This overridden import method will check implementation of inherited classes
 # for missing implementation of abstract methods
index 8a64d36465f6e2991bfa1ef6a2f7b957653c78ae..fdb47507870634a6279ee9d3ac7ea0d5cc003523 100644 (file)
@@ -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) = @_;
index 56d9d3fbf40db057294e54a8acea68db76767d69..341818a5c5e3da2f3753627e28c0567f38a802d2 100644 (file)
@@ -104,6 +104,15 @@ sub bz_check_regexp {
         { value => $pattern, dberror => $self->errstr });
 }
 
+sub bz_explain { 
+     my ($self, $sql) = @_; 
+     my $sth = $self->prepare("EXPLAIN PLAN FOR $sql"); 
+     $sth->execute();
+     my $explain = $self->selectcol_arrayref(
+         "SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY)");
+     return join("\n", @$explain); 
+} 
+
 sub sql_regexp {
     my ($self, $expr, $pattern, $nocheck) = @_;
 
index a6a2e3281cc19f03defd7668f1e232ddd45775de..d06decaa34de51c05a7825c36e336e8fdb5cc1e2 100644 (file)
@@ -171,6 +171,12 @@ sub bz_sequence_exists {
     return $exists || 0;
 }
 
+sub bz_explain {
+    my ($self, $sql) = @_;
+    my $explain = $self->selectcol_arrayref("EXPLAIN ANALYZE $sql");
+    return join("\n", @$explain);
+}
+
 #####################################################################
 # Custom Database Setup
 #####################################################################
index 86147869bb00e920131a8e6ddcbc47f50b7d0944..114523286f8d7df55e0f203171a8f9c9ae27fa10 100755 (executable)
@@ -994,6 +994,13 @@ elsif ($fulltext) {
 if ($cgi->param('debug')) {
     $vars->{'debug'} = 1;
     $vars->{'query'} = $query;
+    # Explains are limited to admins because you could use them to figure
+    # out how many hidden bugs are in a particular product (by doing
+    # searches and looking at the number of rows the explain says it's
+    # examining).
+    if (Bugzilla->user->in_group('admin')) {
+        $vars->{'query_explain'} = $dbh->bz_explain($query);
+    }
     $vars->{'debugdata'} = $search->getDebugData();
 }
 
index 71206fcbd915da610490d778d69942c419ab1e78..ca37dc763a6d295cf2ecb194953b217d4c6a9352 100644 (file)
@@ -64,3 +64,7 @@ tr.bz_secure_mode_manual td.first-child {
 #commit, #action {
   margin-top: .25em;
 }
+
+.bz_query_explain {
+    text-align: left;
+}
index 0a8eb402cd59860a037e283078aae41a35e8afd0..512201c27cbd4a809dbe67e296bca5e1f017d461 100644 (file)
@@ -68,6 +68,9 @@
       [% END %]
     </p>
     <p class="bz_query">[% query FILTER html %]</p>
+    [% IF query_explain.defined %]
+      <pre class="bz_query_explain">[% query_explain FILTER html %]</pre>
+    [% END %]
   [% END %]
 
   [% IF user.settings.display_quips.value == 'on' %]