| 1 | package Apache::Status::DBI; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | our $VERSION = '1.012'; # $Id: DBI.pm 9845 2007-08-16 14:13:30Z timbo $ | ||||||||||
| 8 | |||||||||||
| 9 | 2 | ▶ | use DBI (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # if MOD_PERL_API_VERSION env var exists then use it to determine mod_perl v1 or v2 | ||||||||||
| 12 | # if not, then assume mod_perl v2 if we can load mod_perl2 module | ||||||||||
| 13 | 2 | ▶ | use constant MP2 => (exists $ENV{MOD_PERL_API_VERSION}) | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||
| 14 | ? ($ENV{MOD_PERL_API_VERSION} >= 2) | ||||||||||
| 15 | 2 | ▶ | : eval { require mod_perl2 }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | |||||||||||
| 17 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=13.7
| |||||||||||
| 18 | 2 | ▶ | if (MP2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 0 | require mod_perl2; | |||||||||
| 20 | 0 | require Apache2::Module; | |||||||||
| 21 | 0 | ▶ | *escape_html = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 22 | 0 | my $s = shift; | |||||||||
| 23 | 0 | $s =~ s/&/&/g; | |||||||||
| 24 | 0 | $s =~ s/</</g; | |||||||||
| 25 | 0 | $s =~ s/>/>/g; | |||||||||
| 26 | 0 | return $s; | |||||||||
| 27 | } | ||||||||||
| 28 | 0 | } | |||||||||
| 29 | else { | ||||||||||
| 30 | 2 | require Apache; | |||||||||
| 31 | 0 | require Apache::Util; | |||||||||
| 32 | 0 | Apache::Util->import(qw(escape_html)); | |||||||||
| 33 | } | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | my %apache_status_menu_items = ( | ||||||||||
| 37 | DBI_handles => [ 'DBI Handles', \&apache_status_dbi_handles ], | ||||||||||
| 38 | ); | ||||||||||
| 39 | my $apache_status_class; | ||||||||||
| 40 | if (MP2) { | ||||||||||
| 41 | $apache_status_class = "Apache2::Status" if Apache2::Module::loaded('Apache2::Status'); | ||||||||||
| 42 | } | ||||||||||
| 43 | elsif ($INC{'Apache.pm'} # is Apache.pm loaded? | ||||||||||
| 44 | and Apache->can('module') # really? | ||||||||||
| 45 | and Apache->module('Apache::Status')) { # Apache::Status too? | ||||||||||
| 46 | $apache_status_class = "Apache::Status"; | ||||||||||
| 47 | } | ||||||||||
| 48 | if ($apache_status_class) { | ||||||||||
| 49 | while ( my ($url, $menu_item) = each %apache_status_menu_items ) { | ||||||||||
| 50 | $apache_status_class->menu_item($url => @$menu_item); | ||||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | =pod | ||||||||||
| 56 | |||||||||||
| 57 | =over 1 | ||||||||||
| 58 | |||||||||||
| 59 | =item B<apache_status_dbi_handles> | ||||||||||
| 60 | |||||||||||
| 61 | Displays all handles and associated information via the Apache::Status | ||||||||||
| 62 | webpages in a running httpd mod_perl enabled server. | ||||||||||
| 63 | |||||||||||
| 64 | =back | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | sub apache_status_dbi_handles { | ||||||||||
| 68 | my($r, $q) = @_; | ||||||||||
| 69 | my @s = ("<pre>", | ||||||||||
| 70 | "<b>DBI $DBI::VERSION - Drivers, Connections and Statements</b><p>\n", | ||||||||||
| 71 | ); | ||||||||||
| 72 | |||||||||||
| 73 | my %drivers = DBI->installed_drivers(); | ||||||||||
| 74 | push @s, sprintf("%d drivers loaded: %s<p>", scalar keys %drivers, join(", ", keys %drivers)); | ||||||||||
| 75 | |||||||||||
| 76 | while ( my ($driver, $h) = each %drivers) { | ||||||||||
| 77 | my $version = do { no strict; ${"DBD::${driver}::VERSION"} || 'undef' }; ## no critic | ||||||||||
| 78 | my @children = grep { defined } @{$h->{ChildHandles}}; | ||||||||||
| 79 | |||||||||||
| 80 | push @s, sprintf "<hr><b>DBD::$driver</b> <font size=-2 color=grey>version $version, %d dbh (%d cached, %d active) $h</font>\n\n", | ||||||||||
| 81 | scalar @children, scalar keys %{$h->{CachedKids}||{}}, $h->{ActiveKids}; | ||||||||||
| 82 | |||||||||||
| 83 | @children = sort { ($a->{Name}||"$a") cmp ($b->{Name}||"$b") } @children; | ||||||||||
| 84 | push @s, _apache_status_dbi_handle($_, 1) for @children; | ||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | push @s, "<hr>"; | ||||||||||
| 88 | push @s, "<font size=-2 color=grey>".__PACKAGE__." $VERSION</font>"; | ||||||||||
| 89 | push @s, "</pre>\n"; | ||||||||||
| 90 | return \@s; | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | sub _apache_status_dbi_handle { | ||||||||||
| 96 | my ($h, $level) = @_; | ||||||||||
| 97 | my $pad = " " x $level; | ||||||||||
| 98 | my $type = $h->{Type}; | ||||||||||
| 99 | my @children = grep { defined } @{$h->{ChildHandles}}; | ||||||||||
| 100 | my @boolean_attr = qw( | ||||||||||
| 101 | Active Executed RaiseError PrintError ShowErrorStatement PrintWarn | ||||||||||
| 102 | CompatMode InactiveDestroy HandleError HandleSetErr | ||||||||||
| 103 | ChopBlanks LongTruncOk TaintIn TaintOut Profile); | ||||||||||
| 104 | my @scalar_attr = qw( | ||||||||||
| 105 | ErrCount TraceLevel FetchHashKeyName LongReadLen | ||||||||||
| 106 | ); | ||||||||||
| 107 | my @scalar_attr2 = qw(); | ||||||||||
| 108 | |||||||||||
| 109 | my @s; | ||||||||||
| 110 | if ($type eq 'db') { | ||||||||||
| 111 | push @s, sprintf "DSN \"<b>%s</b>\" <font size=-2 color=grey>%s</font>\n", $h->{Name}, $h; | ||||||||||
| 112 | @children = sort { ($a->{Statement}||"$a") cmp ($b->{Statement}||"$b") } @children; | ||||||||||
| 113 | push @boolean_attr, qw(AutoCommit); | ||||||||||
| 114 | push @scalar_attr, qw(Username); | ||||||||||
| 115 | } | ||||||||||
| 116 | else { | ||||||||||
| 117 | push @s, sprintf " sth <font size=-2 color=grey>%s</font>\n", $h; | ||||||||||
| 118 | push @scalar_attr2, qw(NUM_OF_PARAMS NUM_OF_FIELDS CursorName); | ||||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | push @s, sprintf "%sAttributes: %s\n", $pad, | ||||||||||
| 122 | join ", ", grep { $h->{$_} } @boolean_attr; | ||||||||||
| 123 | push @s, sprintf "%sAttributes: %s\n", $pad, | ||||||||||
| 124 | join ", ", map { "$_=".DBI::neat($h->{$_}) } @scalar_attr; | ||||||||||
| 125 | if (my $sql = escape_html($h->{Statement} || '')) { | ||||||||||
| 126 | $sql =~ s/\n/ /g; | ||||||||||
| 127 | push @s, sprintf "%sStatement: <b>%s</b>\n", $pad, $sql; | ||||||||||
| 128 | my $ParamValues = $type eq 'st' && $h->{ParamValues}; | ||||||||||
| 129 | push @s, sprintf "%sParamValues: %s\n", $pad, | ||||||||||
| 130 | join ", ", map { "$_=".DBI::neat($ParamValues->{$_}) } sort keys %$ParamValues | ||||||||||
| 131 | if $ParamValues && %$ParamValues; | ||||||||||
| 132 | } | ||||||||||
| 133 | push @s, sprintf "%sAttributes: %s\n", $pad, | ||||||||||
| 134 | join ", ", map { "$_=".DBI::neat($h->{$_}) } @scalar_attr2 | ||||||||||
| 135 | if @scalar_attr2; | ||||||||||
| 136 | push @s, sprintf "%sRows: %s\n", $pad, $h->rows | ||||||||||
| 137 | if $type eq 'st' || $h->rows != -1; | ||||||||||
| 138 | if (defined( my $err = $h->err )) { | ||||||||||
| 139 | push @s, sprintf "%s%s %s %s\n", $pad, | ||||||||||
| 140 | ($err ? "Error" : length($err) ? "Warning" : "Information"), | ||||||||||
| 141 | $err, escape_html($h->errstr); | ||||||||||
| 142 | } | ||||||||||
| 143 | push @s, sprintf " sth: %d (%d cached, %d active)\n", | ||||||||||
| 144 | scalar @children, scalar keys %{$h->{CachedKids}||{}}, $h->{ActiveKids} | ||||||||||
| 145 | if @children; | ||||||||||
| 146 | push @s, "\n"; | ||||||||||
| 147 | |||||||||||
| 148 | push @s, map { _apache_status_dbi_handle($_, $level + 1) } @children; | ||||||||||
| 149 | |||||||||||
| 150 | return @s; | ||||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | |||||||||||
| 154 | 1; # Magic true value required at end of module | ||||||||||
| 155 | __END__ | ||||||||||