1 package SQL::Translator::Producer::HTML;
3 # -------------------------------------------------------------------
4 # $Id: HTML.pm 1445 2009-02-07 17:50:03Z ashberlin $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 use vars qw($VERSION $NOWRAP $NOLINKTABLE $NAME);
30 $NOWRAP = 0 unless defined $NOWRAP;
31 $NOLINKTABLE = 0 unless defined $NOLINKTABLE;
33 # Emit XHTML by default
34 $CGI::XHTML = $CGI::XHTML = 42;
36 use SQL::Translator::Schema::Constants;
38 # -------------------------------------------------------------------
39 # Main entry point. Returns a string containing HTML.
40 # -------------------------------------------------------------------
43 my $args = $t->producer_args;
44 my $schema = $t->schema;
45 my $schema_name = $schema->name || 'Schema';
46 my $title = $args->{'title'} || "Description of $schema_name";
47 my $wrap = ! (defined $args->{'nowrap'}
50 my $linktable = ! (defined $args->{'nolinktable'}
51 ? $args->{'nolinktable'}
53 my %stylesheet = defined $args->{'stylesheet'}
54 ? ( -style => { src => $args->{'stylesheet'} } )
57 my $q = defined $args->{'pretty'}
58 ? do { require CGI::Pretty;
64 my ($table, @table_names);
71 -meta => { generator => $NAME },
73 $q->h1({ -class => 'SchemaDescription' }, $title),
77 @table_names = grep { length $_->name } $schema->get_tables;
80 # Generate top menu, with links to full table information
81 my $count = scalar(@table_names);
82 $count = sprintf "%d table%s", $count, $count == 1 ? '' : 's';
84 # Leading table of links
86 $q->comment("Table listing ($count)"),
87 $q->a({ -name => 'top' }),
88 $q->start_table({ -width => '100%', -class => 'LinkTable'}),
90 # XXX This needs to be colspan="$#{$table->fields}" class="LinkTableHeader"
92 $q->td({ -class => 'LinkTableCell' },
93 $q->h2({ -class => 'LinkTableTitle' },
99 for my $table (@table_names) {
100 my $table_name = $table->name;
102 $q->comment("Start link to table '$table_name'"),
103 $q->Tr({ -class => 'LinkTableRow' },
104 $q->td({ -class => 'LinkTableCell' },
105 qq[<a id="${table_name}-link" href="#$table_name">$table_name</a>]
108 $q->comment("End link to table '$table_name'");
110 push @html, $q->end_table;
113 for my $table ($schema->get_tables) {
114 my $table_name = $table->name or next;
115 my @fields = $table->get_fields or next;
117 $q->comment("Starting table '$table_name'"),
118 $q->a({ -name => $table_name }),
119 $q->table({ -class => 'TableHeader', -width => '100%' },
120 $q->Tr({ -class => 'TableHeaderRow' },
121 $q->td({ -class => 'TableHeaderCell' }, $q->h3($table_name)),
122 qq[<a name="$table_name">],
123 $q->td({ -class => 'TableHeaderCell', -align => 'right' },
124 qq[<a href="#top">Top</a>]
129 if ( my @comments = map { $_ ? $_ : () } $table->comments ) {
133 $q->em(map { $q->br, $_ } @comments);
140 $q->start_table({ -border => 1 }),
142 $q->th({ -class => 'FieldHeader' },
155 for my $field ( @fields ) {
156 my $name = $field->name || '';
157 $name = qq[<a name="$table_name-$name">$name</a>];
158 my $data_type = $field->data_type || '';
159 my $size = defined $field->size ? $field->size : '';
160 my $default = defined $field->default_value
161 ? $field->default_value : '';
162 my $comment = $field->comments || '';
165 if ($field->is_foreign_key) {
166 my $c = $field->foreign_key_reference;
167 my $ref_table = $c->reference_table || '';
168 my $ref_field = ($c->reference_fields)[0] || '';
170 qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>];
174 push @other, 'PRIMARY KEY' if $field->is_primary_key;
175 push @other, 'UNIQUE' if $field->is_unique;
176 push @other, 'NOT NULL' unless $field->is_nullable;
177 push @other, $comment if $comment;
178 my $class = $i++ % 2 ? 'even' : 'odd';
181 { -class => "tr-$class" },
182 $q->td({ -class => "FieldCellName" }, $name),
183 $q->td({ -class => "FieldCellType" }, $data_type),
184 $q->td({ -class => "FieldCellSize" }, $size),
185 $q->td({ -class => "FieldCellDefault" }, $default),
186 $q->td({ -class => "FieldCellOther" }, join(', ', @other)),
187 $q->td({ -class => "FieldCellFK" }, $fk),
190 push @html, $q->end_table;
195 if ( my @indices = $table->get_indices ) {
198 $q->start_table({ -border => 1 }),
199 $q->Tr({ -class => 'IndexRow' },
200 $q->th([ 'Name', 'Fields' ])
203 for my $index ( @indices ) {
204 my $name = $index->name || '';
205 my $fields = join( ', ', $index->fields ) || '';
208 $q->Tr({ -class => 'IndexCell' },
209 $q->td( [ $name, $fields ] )
213 push @html, $q->end_table;
220 grep { $_->type ne PRIMARY_KEY } $table->get_constraints;
221 if ( @constraints ) {
223 $q->h3('Constraints'),
224 $q->start_table({ -border => 1 }),
225 $q->Tr({ -class => 'IndexRow' },
226 $q->th([ 'Type', 'Fields' ])
229 for my $c ( @constraints ) {
230 my $type = $c->type || '';
231 my $fields = join( ', ', $c->fields ) || '';
234 $q->Tr({ -class => 'IndexCell' },
235 $q->td( [ $type, $fields ] )
239 push @html, $q->end_table;
245 my $sqlt_version = $t->version;
248 qq[Created by <a href="http://sqlfairy.sourceforge.net">],
249 qq[SQL::Translator $sqlt_version</a>],
254 return join "\n", @html;
259 # -------------------------------------------------------------------
260 # Always be ready to speak your mind,
261 # and a base man will avoid you.
263 # -------------------------------------------------------------------
267 SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
271 use SQL::Translator::Producer::HTML;
275 Creates an HTML document describing the tables.
277 The HTML produced is composed of a number of tables:
283 A link table sits at the top of the output, and contains anchored
284 links to elements in the rest of the document.
286 If the I<nolinktable> producer arg is present, then this table is not
291 Each table in the schema has its own HTML table. The top row is a row
292 of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these
293 elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>,
294 I<Other> and I<Foreign Key>. Each successive row describes one field
295 in the table, and has a class of B<FieldCell$item>, where $item id
296 corresponds to the label of the column. For example:
299 <td class="FieldCellName"><a name="random-id">id</a></td>
300 <td class="FieldCellType">int</td>
301 <td class="FieldCellSize">11</td>
302 <td class="FieldCellDefault"></td>
303 <td class="FieldCellOther">PRIMARY KEY, NOT NULL</td>
304 <td class="FieldCellFK"></td>
308 <td class="FieldCellName"><a name="random-foo">foo</a></td>
309 <td class="FieldCellType">varchar</td>
310 <td class="FieldCellSize">255</td>
311 <td class="FieldCellDefault"></td>
312 <td class="FieldCellOther">NOT NULL</td>
313 <td class="FieldCellFK"></td>
317 <td class="FieldCellName"><a name="random-updated">updated</a></td>
318 <td class="FieldCellType">timestamp</td>
319 <td class="FieldCellSize">0</td>
320 <td class="FieldCellDefault"></td>
321 <td class="FieldCellOther"></td>
322 <td class="FieldCellFK"></td>
327 Unless the I<nowrap> producer arg is present, the HTML will be
328 enclosed in a basic HTML header and footer.
330 If the I<pretty> producer arg is present, the generated HTML will be
331 nicely spaced and human-readable. Otherwise, it will have very little
332 insignificant whitespace and be generally smaller.
337 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
338 Darren Chamberlain E<lt>darren@cpan.orgE<gt>.