blib/lib/DBIx/HTML.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package DBIx::HTML;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 our $VERSION = '1.03';
5 our $AUTOLOAD;
6  
7 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Spreadsheet::HTML;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 sub connect {
12 my $class = shift;
13 my $self = {
14 head => [],
15 rows => [],
16 dbh => undef,
17 sth => undef,
18 keep_alive => undef,
19 generator => Spreadsheet::HTML->new(
20 headings => sub { join(' ', map { ucfirst(lc($_)) } split ('_', shift || '')) }
21 ),
22 };
23  
24 if (UNIVERSAL::isa( $_[0], 'DBI::db' )) {
25 # use supplied db handle
26 $self->{dbh} = $_[0];
27 $self->{keep_alive} = 1;
28 } else {
29 # create my own db handle
30 eval { $self->{dbh} = DBI->connect( @_ ) };
31 croak $@ and return undef if $@;
32 }
33  
34 return bless $self, $class;
35 }
36  
37 sub do {
38 my $self = shift;
39 my ($sql, $args) = @_;
40  
41 croak "can't call do(): no database handle" unless $self->{dbh};
42  
43 eval {
44 $self->{sth} = $self->{dbh}->prepare( $sql );
45 $self->{sth}->execute( @$args );
46 };
47 croak $@ and return undef if $@;
48  
49 $self->{head} = $self->{sth}{NAME};
50 $self->{rows} = $self->{sth}->fetchall_arrayref;
51 $self->{generator}{data} = [ $self->{head}, @{ $self->{rows} } ];
52 return $self;
53 }
54  
55 sub AUTOLOAD {
56 my $self = shift;
57 croak "must connect() first" unless $self->isa( __PACKAGE__ );
58 (my $method = $AUTOLOAD) =~ s/.*:://;
59 croak "no such method $method for " . ref($self->{generator}) unless $self->{generator}->can( $method );
60 return $self->{generator}->$method( @_ );
61 }
62  
63 # disconnect database handle if i created it
64 sub DESTROY {
65 my $self = shift;
66 if (!$self->{keep_alive} and $self->{dbh}->isa( 'DBI::db' )) {
67 $self->{dbh}->disconnect();
68 }
69 }
70  
71 1;
72 __END__
↑ Summary