blib/lib/Apache/Status/DBI.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 18 | 28 | 64.2 |
branch | 2 | 4 | 50.0 |
condition | n/a | ||
subroutine | 6 | 6 | 100.0 |
pod | n/a | ||
total | 26 | 38 | 68.4 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Apache::Status::DBI; | ||||||
2 | |||||||
3 | 1 | 1 | 27034 | use warnings; | |||
1 | 2 | ||||||
1 | 38 | ||||||
4 | 1 | 1 | 6 | use strict; | |||
1 | 2 | ||||||
1 | 33 | ||||||
5 | 1 | 1 | 6 | use Carp; | |||
1 | 5 | ||||||
1 | 112 | ||||||
6 | |||||||
7 | our $VERSION = '1.012'; # $Id: DBI.pm 9845 2007-08-16 14:13:30Z timbo $ | ||||||
8 | |||||||
9 | 1 | 1 | 2568 | use DBI (); | |||
1 | 19210 | ||||||
1 | 70 | ||||||
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 | use constant MP2 => (exists $ENV{MOD_PERL_API_VERSION}) | ||||||
14 | ? ($ENV{MOD_PERL_API_VERSION} >= 2) | ||||||
15 | 1 | 50 | 1 | 12 | : eval { require mod_perl2 }; | ||
1 | 2 | ||||||
1 | 6 | ||||||
1 | 585 | ||||||
16 | |||||||
17 | BEGIN { | ||||||
18 | 1 | 50 | 1 | 5 | if (MP2) { | ||
19 | 0 | 0 | require mod_perl2; | ||||
20 | 0 | 0 | require Apache2::Module; | ||||
21 | *escape_html = sub { | ||||||
22 | 0 | 0 | my $s = shift; | ||||
23 | 0 | 0 | $s =~ s/&/&/g; | ||||
24 | 0 | 0 | $s =~ s/</g; | ||||
25 | 0 | 0 | $s =~ s/>/>/g; | ||||
26 | 0 | 0 | return $s; | ||||
27 | } | ||||||
28 | 0 | 0 | } | ||||
29 | else { | ||||||
30 | 1 | 364 | 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 |
||||||
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 = ("", |
||||||
70 | "DBI $DBI::VERSION - Drivers, Connections and Statements \n", |
||||||
71 | ); | ||||||
72 | |||||||
73 | my %drivers = DBI->installed_drivers(); | ||||||
74 | push @s, sprintf("%d drivers loaded: %s ", 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 " DBD::$driver version $version, %d dbh (%d cached, %d active) $h\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, " "; |
||||||
88 | push @s, "".__PACKAGE__." $VERSION"; | ||||||
89 | push @s, "\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 \"%s\" %s\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 %s\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: %s\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__ |