DataTable/DBI.pm

filter: Statementstmt 25.8% 16 / 62 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 41.6% 5 / 12 Podpod 33.3% 2 / 6 totaltotal 13.1% 23 / 175 CC 32 SCAR 41.4
CC
32
Cov
12%
next_row
110.0
list_HTML
90.0
format
72.0
CRAP 62.6
« DataTable.pm ↑ Summary
1 package HTML::DataTable::DBI;
2 1 ▶use base HTML::DataTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 HTML::DataTable::DBI - Print HTML tables from SQL queries
13  
14 =head1 VERSION
15  
16 Version 0.54
17  
18 =cut
19  
20 1 our $VERSION = 0.54;
21  
22 =head1 SYNOPSIS
23  
24 use HTML::DataTable::DBI;
25 my $list = HTML::DataTable::DBI->new(
26 data => $cgi_data,
27 columns => [
28 # hashrefs describing column formats
29 ],
30 sql => 'SELECT * FROM table_name WHERE foreign_key = ?',
31 sql_params => [ $some_value ],
32 delete => { } # Delete spec as shown below
33 );
34 print $list;
35  
36 =head1 METHODS
37  
38 Look in HTML::DataTable for column-definition and table formatting attributes
39  
40 =head2 ADDITIONAL ATTRIBUTES
41  
42 =head3 dsn
43  
44 A list containing a DBI connect string, username, and password.
45  
46 =head3 dbh
47  
48 You can supply a live DBI database handle instead of a DSN.
49  
50 =head3 sql
51  
52 A SQL query with optional "?" placeholders, which will be run and its results formatted and shown in the table.
53  
54 =head3 sql_params
55  
56 An optional arrayref containing the actual parameters for the SQL query.
57  
58 =head3 delete
59  
60 An optional hashref telling the list what record to delete. If this is included, a column will be added to the table to show trash icons. The hashref can take either of two forms. If the SQL query for this table is not parameterized - that is, the record's ID is all we need to know which record to delete - then the hashref can simply map the column index of the record ID to the CGI argument that supplies the one to delete:
61  
62 delete => {
63 sql => 'DELETE FROM table WHERE record_id = ?',
64 id => [ 0 => $args{delete} ],
65 }
66  
67 whereas if the query had a parameter the delete hashref has to give both the local and foreign keys:
68  
69 delete => {
70 sql => 'DELETE FROM table WHERE record_id = ? AND foreign_id = ?',
71 local => [ record_id => $args{speaker_id} ],
72 foreign => [ 0 => $args{delete} ],
73 }
74  
75 An optional "noun" attribute in that hashref can supply a word to replace "record" in the delete confirmation alert.
76  
77 =head3 trash_icon
78  
79 The URL of a trash can icon for use in the "Delete" column - defaults to /art/trash.gif.
80  
81 =head2 ADDITIONAL COLUMN ATTRIBUTES
82  
83 =head3 sql
84  
85 A paramterized SQL query that will be run to get results for this column.
86  
87 =head3 foreign_key_col
88  
89 The index of the column in the results from the main table's SQL query that will be used in the column's query's parameter. Defaults to 0.
90  
91 =head3 separator
92  
93 A character string that will be used to concatenate the results of the columns's query. Defaults to ", ".
94  
95 =cut
96  
97 0 ▶sub set_letter {
Subroutine set_letter: not called CC=1 SCAR=6.9
Pod undocumented
98 0 my ($me, $letter) = @_;
99  
100 0 $me->{sql} .= ' WHERE lower(' . $me->{sort}->[0] . ") LIKE '" . lc $letter . "%'";
101 }
102  
103 0 ▶sub set_search {
Subroutine set_search: not called CC=5 SCAR=34.0
Pod undocumented
104 0 my ($me, $letter) = @_;
105  
106 0 ▶ $me->{_dbh} ||= $me->{dbh} || DBI->connect( @{ $me->{dsn} } ) || die "No DB connection";
Condition0%0/8
$me->{"_dbh"} ||= $me->{"dbh"} || "DBI"->connect(@{$$me{"dsn"};}) || die("No DB connection")
l!l&&r!l&&!r
000
$me->{'dbh'} || 'DBI'->connect(@{$$me{'dsn'};}) || die('No DB connection')
l!l
00
$me->{"dbh"} or "DBI"->connect(@{$$me{"dsn"};})
l!l&&r!l&&!r
000
MC/DC0%0/3
$me->{"_dbh"} ||= $me->{"dbh"} || "DBI"->connect(@{$$me{"dsn"};}) || die("No DB connection")
$me->{"_dbh"} $me->{"dbh"} "DBI"->connect(@{$$me{"dsn"};})
Truth table0%0/4
A ||= B or C
A= $me->{"_dbh"}B= $me->{"dbh"}C= "DBI"->connect(@{$$me{"dsn"};})
ABCresult
1XX1
01X1
0011
0000
107  
108 $me->{sql} .= ' WHERE ' . join ' OR ', map "position( lower(\$1) in lower($_) ) > 0",
109 0 ▶ ref $me->{search} eq 'ARRAY' ? @{$me->{search}} : split ' ', $me->{search};
Branch: 1 branch
BranchTrueFalseTotal
ref $me->{'search'} eq 'ARRAY' ? : 0 0 0
110 }
111  
112 0 ▶sub set_sort_order {
Subroutine set_sort_order: not called CC=2 SCAR=17.9
Pod undocumented
113 0 my $me = shift;
114  
115 0 ▶ return unless $me->{sort}->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $me->{"sort"}[0] 0 0 0
116 0 $me->{sql} .= ' ORDER BY ' . join ', ', map "$_ $me->{sort_dir}", @{$me->{sort}};
117 }
118  
119 0 ▶sub list_HTML {
Subroutine list_HTML: not called CC=9 SCAR=45.0
Pod documented
120 0 my $me = shift;
121  
122 0 ▶ if ( my $d = $me->{delete} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $me->{"delete"}) 0 0 0
123 0 ▶ $me->{trash_icon} ||= '/art/trash.gif';
Condition0%0/2
$me->{"trash_icon"} ||= "/art/trash.gif"
l!l
00
MC/DC0%0/1
$me->{"trash_icon"} ||= "/art/trash.gif"
$me->{"trash_icon"}
Truth table0%0/2
A
A= $me->{"trash_icon"}
Aresult
11
00
124 0 ▶ my $to_delete = $d->{id} || $d->{foreign};
Condition0%0/3
$d->{'id'} || $d->{'foreign'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$d->{'id'} || $d->{'foreign'}
$d->{'id'} $d->{'foreign'}
Truth table0%0/3
A || B
A= $d->{'id'}B= $d->{'foreign'}
ABresult
1X1
011
000
125 0 ▶ if ( $to_delete->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_delete->[1]) 0 0 0
126 0 ▶ $me->{_dbh} ||= $me->{dbh} || DBI->connect( @{ $me->{dsn} } ) || die "No DB connection";
Condition0%0/8
$me->{"_dbh"} ||= $me->{"dbh"} || "DBI"->connect(@{$$me{"dsn"};}) || die("No DB connection")
l!l&&r!l&&!r
000
$me->{'dbh'} || 'DBI'->connect(@{$$me{'dsn'};}) || die('No DB connection')
l!l
00
$me->{"dbh"} or "DBI"->connect(@{$$me{"dsn"};})
l!l&&r!l&&!r
000
MC/DC0%0/3
$me->{"_dbh"} ||= $me->{"dbh"} || "DBI"->connect(@{$$me{"dsn"};}) || die("No DB connection")
$me->{"_dbh"} $me->{"dbh"} "DBI"->connect(@{$$me{"dsn"};})
Truth table0%0/4
A ||= B or C
A= $me->{"_dbh"}B= $me->{"dbh"}C= "DBI"->connect(@{$$me{"dsn"};})
ABCresult
1XX1
01X1
0011
0000
127 0 ▶ $me->{_dbh}->prepare( $d->{sql} )->execute( $d->{local}[1] || (), $to_delete->[1] );
Condition0%0/3
$d->{'local'}[1] || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$d->{'local'}[1] || ()
$d->{'local'}[1] ()
Truth table0%0/3
A || B
A= $d->{'local'}[1]B= ()
ABresult
1X1
011
000
128 }
129 0 push @{$me->{columns}}, {
130 style => 'text-align: center; vertical-align: middle;',
131 0 ▶ format => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
132 sprintf '<a href="?%s=%d&delete=%d" onclick="return confirm( &quot;Are you sure you want to delete this %s?&quot; )"><img src="%s"></a>',
133 0 ▶ ($d->{local} ? @{$d->{local}} : (_noop => 0)),
Condition0%0/2
$d->{'noun'} || 'record'
l!l
00
MC/DC0%0/1
$d->{'noun'} || 'record'
$d->{'noun'}
Truth table0%0/2
A
A= $d->{'noun'}
Aresult
11
00
134 $_[$to_delete->[0]],
135 $d->{noun} || 'record',
136 0 ▶ $me->{trash_icon};
Branch: 1 branch
BranchTrueFalseTotal
$d->{'local'} ? : 0 0 0
137 }
138 }
139 0 }
140  
141 0 return $me->SUPER::list_HTML(@_);
142 }
143  
144 0 ▶sub next_row {
Subroutine next_row: not called CC=10 SCAR=47.0
Pod undocumented
145 0 my $me = shift;
146  
147 0 ▶ return $me->SUPER::next_row(@_) unless $me->{sql} or $me->{_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $me->{"sql"} or $me->{"_sth"} 0 0 0
Condition0%0/3
$me->{"sql"} or $me->{"_sth"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$me->{"sql"} or $me->{"_sth"}
$me->{"sql"} $me->{"_sth"}
Truth table0%0/3
A or B
A= $me->{"sql"}B= $me->{"_sth"}
ABresult
1X1
011
000
148  
149 0 ▶ unless ( $me->{_sth} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($me->{"_sth"}) 0 0 0
150 0 ▶ $me->{_dbh} ||= $me->{dbh} || DBI->connect( @{ $me->{dsn} } ) || die "No DB connection";
Condition0%0/8
$me->{"_dbh"} ||= $me->{"dbh"} || "DBI"->connect(@{$$me{"dsn"};}) || die("No DB connection")
l!l&&r!l&&!r
000
$me->{'dbh'} || 'DBI'->connect(@{$$me{'dsn'};}) || die('No DB connection')
l!l
00
$me->{"dbh"} or "DBI"->connect(@{$$me{"dsn"};})
l!l&&r!l&&!r
000
MC/DC0%0/3
$me->{"_dbh"} ||= $me->{"dbh"} || "DBI"->connect(@{$$me{"dsn"};}) || die("No DB connection")
$me->{"_dbh"} $me->{"dbh"} "DBI"->connect(@{$$me{"dsn"};})
Truth table0%0/4
A ||= B or C
A= $me->{"_dbh"}B= $me->{"dbh"}C= "DBI"->connect(@{$$me{"dsn"};})
ABCresult
1XX1
01X1
0011
0000
151 0 ▶ $me->{_dbh}->trace( $me->{trace} ) if $me->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"trace"} 0 0 0
152 0 ( $me->{_sth} = $me->{_dbh}->prepare( $me->{sql} ) )->execute( @{$me->{sql_params}} );
153 }
154  
155 0 ▶ my @row = $me->{_sth}->fetchrow or do {
Branch: 1 branch
BranchTrueFalseTotal
my(@row) = $me->{"_sth"}->fetchrow or do { $me->{"_sth"}->finish; undef $me->{"_sth"}; $me->{"_dbh"}->disconnect unless $me->{"dbh"}; return } 0 0 0
156 0 $me->{_sth}->finish;
157 0 undef $me->{_sth};
158 0 ▶ $me->{_dbh}->disconnect unless $me->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $me->{"dbh"} 0 0 0
159 0 return;
160 };
161  
162 0 return \@row;
163 }
164  
165 0 ▶sub format {
Subroutine format: not called CC=8 SCAR=42.8
Pod documented
166 0 my ($me, $col, $d) = @_;
167  
168 0 ▶ return $me->SUPER::format( $col, $d ) unless $col->{sql};
Branch: 1 branch
BranchTrueFalseTotal
unless $col->{"sql"} 0 0 0
169  
170 0 my (@related);
171 0 ▶ $col->{_subquery_sth} ||= $me->{_dbh}->prepare( $col->{sql} );
Condition0%0/3
$col->{"_subquery_sth"} ||= $me->{"_dbh"}->prepare($col->{"sql"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$col->{"_subquery_sth"} ||= $me->{"_dbh"}->prepare($col->{"sql"})
$col->{"_subquery_sth"} $me->{"_dbh"}->prepare($col->{"sql"})
Truth table0%0/3
A ||= B
A= $col->{"_subquery_sth"}B= $me->{"_dbh"}->prepare($col->{"sql"})
ABresult
1X1
011
000
172 0 ▶ $col->{_subquery_sth}->execute( $d->[ $col->{foreign_key_col} || 0 ] );
Condition0%0/2
$col->{'foreign_key_col'} || 0
l!l
00
MC/DC0%0/1
$col->{'foreign_key_col'} || 0
$col->{'foreign_key_col'}
Truth table0%0/2
A
A= $col->{'foreign_key_col'}
Aresult
11
00
173 0 ▶ while ( my @d = $col->{_subquery_sth}->fetchrow ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@d) = $col->{"_subquery_sth"}->fetchrow) 0 0 0
174 0 push @related, $me->SUPER::format( $col, \@d );
175 }
176  
177 0 ▶ @related ? join( $col->{separator} || $col->{sep} || ', ', @related ) : $col->{none};
Branch: 1 branch
BranchTrueFalseTotal
@related ? : 0 0 0
Condition0%0/5
$col->{'separator'} || $col->{'sep'} || ', '
l!l
00
$col->{"separator"} or $col->{"sep"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$col->{'separator'} || $col->{'sep'} || ', '
$col->{"separator"} $col->{"sep"}
Truth table0%0/3
A or B
A= $col->{"separator"}B= $col->{"sep"}
ABresult
1X1
011
000
178 }
179  
180 1 1;
181  
182 =head1 SEE ALSO
183  
184 HTML::DataTable
185  
186 =head1 AUTHORS
187  
188 Nic Wolff <nic@angel.net>
189 Jason Barden <jason@angel.net>
190  
191 =cut
« DataTable.pm ↑ Summary