blib/lib/SQL/Translator/Producer/HTML.pm

filter: Statementstmt 18.1% 18 / 99 Branchbran 4.1% 2 / 48 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 80.0% 4 / 5 Podpod 0.0% 0 / 1 totaltotal 12.9% 24 / 186 SCAR 69.9
CC
39
Cov
12%
produce
1560.0
BEGIN
1.0
BEGIN
1.0
CRAP 1081.4
« blib/lib/SQL/Translator/Producer/GraphViz.pm ↑ Summary blib/lib/SQL/Translator/Producer/ClassDBI.pm »
1 package SQL::Translator::Producer::HTML;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.6_3';
8 1 our $NAME = __PACKAGE__;
9 1 ▶our $NOWRAP = 0 unless defined $NOWRAP;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SQL::Translator::Producer::HTML::NOWRAP 1 0 1
10 1 ▶our $NOLINKTABLE = 0 unless defined $NOLINKTABLE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SQL::Translator::Producer::HTML::NOLINKTABLE 1 0 1
11  
12 # Emit XHTML by default
13 1 $CGI::XHTML = $CGI::XHTML = 42;
14  
15 1 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # -------------------------------------------------------------------
18 # Main entry point. Returns a string containing HTML.
19 # -------------------------------------------------------------------
20 sub produce {
21 0 ▶ my $t = shift;
Subroutine produce: not called CC=39 SCAR=73.5
Pod undocumented
22 0 my $args = $t->producer_args;
23 0 my $schema = $t->schema;
24 0 ▶ my $schema_name = $schema->name || 'Schema';
Condition0%0/2
$schema->name || 'Schema'
l!l
00
MC/DC0%0/1
$schema->name || 'Schema'
$schema->name
Truth table0%0/2
A
A= $schema->name
Aresult
11
00
25 0 ▶ my $title = $args->{'title'} || "Description of $schema_name";
Condition0%0/2
$args->{'title'} || "Description of $schema_name"
l!l
00
MC/DC0%0/1
$args->{'title'} || "Description of $schema_name"
$args->{'title'}
Truth table0%0/2
A
A= $args->{'title'}
Aresult
11
00
26 my $wrap = ! (defined $args->{'nowrap'}
27 0 ▶ ? $args->{'nowrap'}
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'nowrap'} ? : 0 0 0
28 : $NOWRAP);
29 my $linktable = ! (defined $args->{'nolinktable'}
30 0 ▶ ? $args->{'nolinktable'}
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'nolinktable'} ? : 0 0 0
31 : $NOLINKTABLE);
32 my %stylesheet = defined $args->{'stylesheet'}
33 0 ▶ ? ( -style => { src => $args->{'stylesheet'} } )
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'stylesheet'} ? : 0 0 0
34 : ( );
35 0 my @html;
36 my $q = defined $args->{'pretty'}
37 0 ? do { require CGI::Pretty;
38 0 import CGI::Pretty;
39 0 CGI::Pretty->new }
40 0 ▶ : do { require CGI;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'pretty'} ? : 0 0 0
41 0 import CGI;
42 0 CGI->new };
43 0 my ($table, @table_names);
44  
45 0 ▶ if ($wrap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrap) 0 0 0
46 0 push @html,
47 $q->start_html({
48 -title => $title,
49 %stylesheet,
50 -meta => { generator => $NAME },
51 }),
52 $q->h1({ -class => 'SchemaDescription' }, $title),
53 $q->hr;
54 }
55  
56 0 @table_names = grep { length $_->name } $schema->get_tables;
57  
58 0 ▶ if ($linktable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linktable) 0 0 0
59 # Generate top menu, with links to full table information
60 0 my $count = scalar(@table_names);
61 0 ▶ $count = sprintf "%d table%s", $count, $count == 1 ? '' : 's';
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 0 0 0
62  
63 # Leading table of links
64 0 push @html,
65 $q->comment("Table listing ($count)"),
66 $q->a({ -name => 'top' }),
67 $q->start_table({ -width => '100%', -class => 'LinkTable'}),
68  
69 # XXX This needs to be colspan="$#{$table->fields}" class="LinkTableHeader"
70 $q->Tr(
71 $q->td({ -class => 'LinkTableCell' },
72 $q->h2({ -class => 'LinkTableTitle' },
73 'Tables'
74 ),
75 ),
76 );
77  
78 0 for my $table (@table_names) {
79 0 my $table_name = $table->name;
80 0 push @html,
81 $q->comment("Start link to table '$table_name'"),
82 $q->Tr({ -class => 'LinkTableRow' },
83 $q->td({ -class => 'LinkTableCell' },
84 qq[<a id="${table_name}-link" href="#$table_name">$table_name</a>]
85 )
86 ),
87 $q->comment("End link to table '$table_name'");
88 }
89 0 push @html, $q->end_table;
90 }
91  
92 0 for my $table ($schema->get_tables) {
93 0 ▶ my $table_name = $table->name or next;
Branch: 1 branch
BranchTrueFalseTotal
my $table_name = $table->name or next 0 0 0
94 0 ▶ my @fields = $table->get_fields or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@fields) = $table->get_fields or next 0 0 0
95 0 push @html,
96 $q->comment("Starting table '$table_name'"),
97 $q->a({ -name => $table_name }),
98 $q->table({ -class => 'TableHeader', -width => '100%' },
99 $q->Tr({ -class => 'TableHeaderRow' },
100 $q->td({ -class => 'TableHeaderCell' }, $q->h3($table_name)),
101 qq[<a name="$table_name">],
102 $q->td({ -class => 'TableHeaderCell', -align => 'right' },
103 qq[<a href="#top">Top</a>]
104 )
105 )
106 );
107  
108 0 ▶ if ( my @comments = map { $_ ? $_ : () } $table->comments ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my(@comments) = map({$_ ? $_ : ();} $table->comments)) 0 0 0
$_ ? : 0 0 0
109 push @html,
110 $q->b("Comments:"),
111 $q->br,
112 0 $q->em(map { $q->br, $_ } @comments);
113 }
114  
115 #
116 # Fields
117 #
118 0 push @html,
119 $q->start_table({ -border => 1 }),
120 $q->Tr(
121 $q->th({ -class => 'FieldHeader' },
122 [
123 'Field Name',
124 'Data Type',
125 'Size',
126 'Default Value',
127 'Other',
128 'Foreign Key'
129 ]
130 )
131 );
132  
133 0 my $i = 0;
134 0 for my $field ( @fields ) {
135 0 ▶ my $name = $field->name || '';
Condition0%0/2
$field->name || ''
l!l
00
MC/DC0%0/1
$field->name || ''
$field->name
Truth table0%0/2
A
A= $field->name
Aresult
11
00
136 0 $name = qq[<a name="$table_name-$name">$name</a>];
137 0 ▶ my $data_type = $field->data_type || '';
Condition0%0/2
$field->data_type || ''
l!l
00
MC/DC0%0/1
$field->data_type || ''
$field->data_type
Truth table0%0/2
A
A= $field->data_type
Aresult
11
00
138 0 ▶ my $size = defined $field->size ? $field->size : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $field->size ? : 0 0 0
139 0 ▶ my $default = defined $field->default_value
Branch: 1 branch
BranchTrueFalseTotal
defined $field->default_value ? : 0 0 0
140 ? $field->default_value : '';
141 0 ▶ my $comment = $field->comments || '';
Condition0%0/2
$field->comments || ''
l!l
00
MC/DC0%0/1
$field->comments || ''
$field->comments
Truth table0%0/2
A
A= $field->comments
Aresult
11
00
142 0 my $fk = '';
143  
144 0 ▶ if ($field->is_foreign_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_foreign_key) 0 0 0
145 0 my $c = $field->foreign_key_reference;
146 0 ▶ my $ref_table = $c->reference_table || '';
Condition0%0/2
$c->reference_table || ''
l!l
00
MC/DC0%0/1
$c->reference_table || ''
$c->reference_table
Truth table0%0/2
A
A= $c->reference_table
Aresult
11
00
147 0 ▶ my $ref_field = ($c->reference_fields)[0] || '';
Condition0%0/2
($c->reference_fields)[0] || ''
l!l
00
MC/DC0%0/1
($c->reference_fields)[0] || ''
($c->reference_fields)[0]
Truth table0%0/2
A
A= ($c->reference_fields)[0]
Aresult
11
00
148 0 $fk =
149 qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>];
150 }
151  
152 0 my @other = ();
153 0 ▶ push @other, 'PRIMARY KEY' if $field->is_primary_key;
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_primary_key 0 0 0
154 0 ▶ push @other, 'UNIQUE' if $field->is_unique;
Branch: 1 branch
BranchTrueFalseTotal
if $field->is_unique 0 0 0
155 0 ▶ push @other, 'NOT NULL' unless $field->is_nullable;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->is_nullable 0 0 0
156 0 ▶ push @other, $comment if $comment;
Branch: 1 branch
BranchTrueFalseTotal
if $comment 0 0 0
157 0 ▶ my $class = $i++ % 2 ? 'even' : 'odd';
Branch: 1 branch
BranchTrueFalseTotal
$i++ % 2 ? : 0 0 0
158 0 push @html,
159 $q->Tr(
160 { -class => "tr-$class" },
161 $q->td({ -class => "FieldCellName" }, $name),
162 $q->td({ -class => "FieldCellType" }, $data_type),
163 $q->td({ -class => "FieldCellSize" }, $size),
164 $q->td({ -class => "FieldCellDefault" }, $default),
165 $q->td({ -class => "FieldCellOther" }, join(', ', @other)),
166 $q->td({ -class => "FieldCellFK" }, $fk),
167 );
168 }
169 0 push @html, $q->end_table;
170  
171 #
172 # Indices
173 #
174 0 ▶ if ( my @indices = $table->get_indices ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@indices) = $table->get_indices) 0 0 0
175 0 push @html,
176 $q->h3('Indices'),
177 $q->start_table({ -border => 1 }),
178 $q->Tr({ -class => 'IndexRow' },
179 $q->th([ 'Name', 'Fields' ])
180 );
181  
182 0 for my $index ( @indices ) {
183 0 ▶ my $name = $index->name || '';
Condition0%0/2
$index->name || ''
l!l
00
MC/DC0%0/1
$index->name || ''
$index->name
Truth table0%0/2
A
A= $index->name
Aresult
11
00
184 0 ▶ my $fields = join( ', ', $index->fields ) || '';
Condition0%0/2
join(', ', $index->fields) || ''
l!l
00
MC/DC0%0/1
join(', ', $index->fields) || ''
join(', ', $index->fields)
Truth table0%0/2
A
A= join(', ', $index->fields)
Aresult
11
00
185  
186 0 push @html,
187 $q->Tr({ -class => 'IndexCell' },
188 $q->td( [ $name, $fields ] )
189 );
190 }
191  
192 0 push @html, $q->end_table;
193 }
194  
195 #
196 # Constraints
197 #
198 my @constraints =
199 0 grep { $_->type ne PRIMARY_KEY } $table->get_constraints;
200 0 ▶ if ( @constraints ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@constraints) 0 0 0
201 0 push @html,
202 $q->h3('Constraints'),
203 $q->start_table({ -border => 1 }),
204 $q->Tr({ -class => 'IndexRow' },
205 $q->th([ 'Type', 'Fields' ])
206 );
207  
208 0 for my $c ( @constraints ) {
209 0 ▶ my $type = $c->type || '';
Condition0%0/2
$c->type || ''
l!l
00
MC/DC0%0/1
$c->type || ''
$c->type
Truth table0%0/2
A
A= $c->type
Aresult
11
00
210 0 ▶ my $fields = join( ', ', $c->fields ) || '';
Condition0%0/2
join(', ', $c->fields) || ''
l!l
00
MC/DC0%0/1
join(', ', $c->fields) || ''
join(', ', $c->fields)
Truth table0%0/2
A
A= join(', ', $c->fields)
Aresult
11
00
211  
212 0 push @html,
213 $q->Tr({ -class => 'IndexCell' },
214 $q->td( [ $type, $fields ] )
215 );
216 }
217  
218 0 push @html, $q->end_table;
219 }
220  
221 0 push @html, $q->hr;
222 }
223  
224 0 my $sqlt_version = $t->version;
225 0 ▶ if ($wrap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrap) 0 0 0
226 0 push @html,
227 qq[Created by <a href="http://sqlfairy.sourceforge.net">],
228 qq[SQL::Translator $sqlt_version</a>],
229 $q->end_html;
230 }
231  
232  
233 0 return join "\n", @html;
234 }
235  
236 1 1;
237  
238 # -------------------------------------------------------------------
239 # Always be ready to speak your mind,
240 # and a base man will avoid you.
241 # William Blake
242 # -------------------------------------------------------------------
243  
244 =head1 NAME
245  
246 SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
247  
248 =head1 SYNOPSIS
249  
250 use SQL::Translator::Producer::HTML;
251  
252 =head1 DESCRIPTION
253  
254 Creates an HTML document describing the tables.
255  
256 The HTML produced is composed of a number of tables:
257  
258 =over 4
259  
260 =item Links
261  
262 A link table sits at the top of the output, and contains anchored
263 links to elements in the rest of the document.
264  
265 If the I<nolinktable> producer arg is present, then this table is not
266 produced.
267  
268 =item Tables
269  
270 Each table in the schema has its own HTML table. The top row is a row
271 of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these
272 elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>,
273 I<Other> and I<Foreign Key>. Each successive row describes one field
274 in the table, and has a class of B<FieldCell$item>, where $item id
275 corresponds to the label of the column. For example:
276  
277 <tr>
278 <td class="FieldCellName"><a name="random-id">id</a></td>
279 <td class="FieldCellType">int</td>
280 <td class="FieldCellSize">11</td>
281 <td class="FieldCellDefault"></td>
282 <td class="FieldCellOther">PRIMARY KEY, NOT NULL</td>
283 <td class="FieldCellFK"></td>
284 </tr>
285  
286 <tr>
287 <td class="FieldCellName"><a name="random-foo">foo</a></td>
288 <td class="FieldCellType">varchar</td>
289 <td class="FieldCellSize">255</td>
290 <td class="FieldCellDefault"></td>
291 <td class="FieldCellOther">NOT NULL</td>
292 <td class="FieldCellFK"></td>
293 </tr>
294  
295 <tr>
296 <td class="FieldCellName"><a name="random-updated">updated</a></td>
297 <td class="FieldCellType">timestamp</td>
298 <td class="FieldCellSize">0</td>
299 <td class="FieldCellDefault"></td>
300 <td class="FieldCellOther"></td>
301 <td class="FieldCellFK"></td>
302 </tr>
303  
304 =back
305  
306 Unless the I<nowrap> producer arg is present, the HTML will be
307 enclosed in a basic HTML header and footer.
308  
309 If the I<pretty> producer arg is present, the generated HTML will be
310 nicely spaced and human-readable. Otherwise, it will have very little
311 insignificant whitespace and be generally smaller.
312  
313  
314 =head1 AUTHORS
315  
316 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
317 Darren Chamberlain E<lt>darren@cpan.orgE<gt>.
318  
319 =cut
« blib/lib/SQL/Translator/Producer/GraphViz.pm ↑ Summary blib/lib/SQL/Translator/Producer/ClassDBI.pm »