1 package SQL::Translator::Producer::HTML;
3 # -------------------------------------------------------------------
4 # $Id: HTML.pm,v 1.14 2004-03-10 22:52:17 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 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);
27 $VERSION = sprintf "%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/;
28 $NAME = join ', ', __PACKAGE__, $VERSION;
29 $NOWRAP = 0 unless defined $NOWRAP;
30 $NOLINKTABLE = 0 unless defined $NOLINKTABLE;
32 # Emit XHTML by default
33 $CGI::XHTML = $CGI::XHTML = 42;
35 use SQL::Translator::Schema::Constants;
37 # -------------------------------------------------------------------
38 # Main entry point. Returns a string containing HTML.
39 # -------------------------------------------------------------------
42 my $args = $t->producer_args;
43 my $schema = $t->schema;
44 my $schema_name = $schema->name || 'Schema';
45 my $title = $args->{'title'} || "Description of $schema_name";
46 my $wrap = ! (defined $args->{'nowrap'}
49 my $linktable = ! (defined $args->{'nolinktable'}
50 ? $args->{'nolinktable'}
52 my %stylesheet = defined $args->{'stylesheet'}
53 ? ( -style => { src => $args->{'stylesheet'} } )
56 my $q = defined $args->{'pretty'}
57 ? do { require CGI::Pretty;
63 my ($table, @table_names);
70 -meta => { generator => $NAME },
72 $q->h1({ -class => 'SchemaDescription' }, $title),
76 @table_names = grep { length $_->name } $schema->get_tables;
79 # Generate top menu, with links to full table information
80 my $count = scalar(@table_names);
81 $count = sprintf "%d table%s", $count, $count == 1 ? '' : 's';
83 # Leading table of links
85 $q->comment("Table listing ($count)"),
86 $q->a({ -name => 'top' }),
87 $q->start_table({ -width => '100%', -class => 'LinkTable'}),
89 # XXX This needs to be colspan="$#{$table->fields}" class="LinkTableHeader"
91 $q->td({ -class => 'LinkTableCell' },
92 $q->h2({ -class => 'LinkTableTitle' },
98 for my $table (@table_names) {
99 my $table_name = $table->name;
101 $q->comment("Start link to table '$table_name'"),
102 $q->Tr({ -class => 'LinkTableRow' },
103 $q->td({ -class => 'LinkTableCell' },
104 qq[<a id="${table_name}-link" href="#$table_name">$table_name</a>]
107 $q->comment("End link to table '$table_name'");
109 push @html, $q->end_table;
112 for my $table ($schema->get_tables) {
113 my $table_name = $table->name or next;
114 my @fields = $table->get_fields or next;
116 $q->comment("Starting table '$table_name'"),
117 $q->a({ -name => $table_name }),
118 $q->table({ -class => 'TableHeader', -width => '100%' },
119 $q->Tr({ -class => 'TableHeaderRow' },
120 $q->td({ -class => 'TableHeaderCell' }, $q->h3($table_name)),
121 qq[<a name="$table_name">],
122 $q->td({ -class => 'TableHeaderCell', -align => 'right' },
123 qq[<a href="#top">Top</a>]
128 if ( my @comments = map { $_ ? $_ : () } $table->comments ) {
132 $q->em(map { $q->br, $_ } @comments);
139 $q->start_table({ -border => 1 }),
141 $q->th({ -class => 'FieldHeader' },
153 for my $field ( @fields ) {
154 my $name = $field->name || '';
155 $name = qq[<a name="$table_name-$name">$name</a>];
156 my $data_type = $field->data_type || '';
157 my $size = defined $field->size ? $field->size : '';
158 my $default = defined $field->default_value
159 ? $field->default_value : '';
160 my $comment = $field->comments || '';
163 if ($field->is_foreign_key) {
164 my $c = $field->foreign_key_reference;
165 my $ref_table = $c->reference_table || '';
166 my $ref_field = ($c->reference_fields)[0] || '';
168 qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>];
172 push @other, 'PRIMARY KEY' if $field->is_primary_key;
173 push @other, 'UNIQUE' if $field->is_unique;
174 push @other, 'NOT NULL' unless $field->is_nullable;
175 push @other, $comment if $comment;
178 $q->td({ -class => "FieldCellName" }, $name),
179 $q->td({ -class => "FieldCellType" }, $data_type),
180 $q->td({ -class => "FieldCellSize" }, $size),
181 $q->td({ -class => "FieldCellDefault" }, $default),
182 $q->td({ -class => "FieldCellOther" }, join(', ', @other)),
183 $q->td({ -class => "FieldCellFK" }, $fk),
186 push @html, $q->end_table;
191 if ( my @indices = $table->get_indices ) {
194 $q->start_table({ -border => 1 }),
195 $q->Tr({ -class => 'IndexRow' },
196 $q->th([ 'Name', 'Fields' ])
199 for my $index ( @indices ) {
200 my $name = $index->name || '';
201 my $fields = join( ', ', $index->fields ) || '';
204 $q->Tr({ -class => 'IndexCell' },
205 $q->td( [ $name, $fields ] )
209 push @html, $q->end_table;
216 grep { $_->type ne PRIMARY_KEY } $table->get_constraints;
217 if ( @constraints ) {
219 $q->h3('Constraints'),
220 $q->start_table({ -border => 1 }),
221 $q->Tr({ -class => 'IndexRow' },
222 $q->th([ 'Type', 'Fields' ])
225 for my $c ( @constraints ) {
226 my $type = $c->type || '';
227 my $fields = join( ', ', $c->fields ) || '';
230 $q->Tr({ -class => 'IndexCell' },
231 $q->td( [ $type, $fields ] )
235 push @html, $q->end_table;
241 my $sqlt_version = $t->version;
244 qq[Created by <a href="http://sqlfairy.sourceforge.net">],
245 qq[SQL::Translator $sqlt_version</a>],
250 return join "\n", @html;
255 # -------------------------------------------------------------------
256 # Always be ready to speak your mind,
257 # and a base man will avoid you.
259 # -------------------------------------------------------------------
263 SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
267 use SQL::Translator::Producer::HTML;
271 Creates an HTML document describing the tables.
273 The HTML produced is composed of a number of tables:
279 A link table sits at the top of the output, and contains anchored
280 links to elements in the rest of the document.
282 If the I<nolinktable> producer arg is present, then this table is not
287 Each table in the schema has its own HTML table. The top row is a row
288 of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these
289 elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>,
290 I<Other> and I<Foreign Key>. Each successive row describes one field
291 in the table, and has a class of B<FieldCell$item>, where $item id
292 corresponds to the label of the column. For example:
295 <td class="FieldCellName"><a name="random-id">id</a></td>
296 <td class="FieldCellType">int</td>
297 <td class="FieldCellSize">11</td>
298 <td class="FieldCellDefault"></td>
299 <td class="FieldCellOther">PRIMARY KEY, NOT NULL</td>
300 <td class="FieldCellFK"></td>
304 <td class="FieldCellName"><a name="random-foo">foo</a></td>
305 <td class="FieldCellType">varchar</td>
306 <td class="FieldCellSize">255</td>
307 <td class="FieldCellDefault"></td>
308 <td class="FieldCellOther">NOT NULL</td>
309 <td class="FieldCellFK"></td>
313 <td class="FieldCellName"><a name="random-updated">updated</a></td>
314 <td class="FieldCellType">timestamp</td>
315 <td class="FieldCellSize">0</td>
316 <td class="FieldCellDefault"></td>
317 <td class="FieldCellOther"></td>
318 <td class="FieldCellFK"></td>
323 Unless the I<nowrap> producer arg is present, the HTML will be
324 enclosed in a basic HTML header and footer.
326 If the I<pretty> producer arg is present, the generated HTML will be
327 nicely spaced and human-readable. Otherwise, it will have very little
328 insignificant whitespace and be generally smaller.
333 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
334 Darren Chamberlain E<lt>darren@cpan.orgE<gt>.