1 package SQL::Translator::Producer::HTML;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 use vars qw($VERSION $NOWRAP $NOLINKTABLE $NAME);
28 $NOWRAP = 0 unless defined $NOWRAP;
29 $NOLINKTABLE = 0 unless defined $NOLINKTABLE;
31 # Emit XHTML by default
32 $CGI::XHTML = $CGI::XHTML = 42;
34 use SQL::Translator::Schema::Constants;
36 # -------------------------------------------------------------------
37 # Main entry point. Returns a string containing HTML.
38 # -------------------------------------------------------------------
41 my $args = $t->producer_args;
42 my $schema = $t->schema;
43 my $schema_name = $schema->name || 'Schema';
44 my $title = $args->{'title'} || "Description of $schema_name";
45 my $wrap = ! (defined $args->{'nowrap'}
48 my $linktable = ! (defined $args->{'nolinktable'}
49 ? $args->{'nolinktable'}
51 my %stylesheet = defined $args->{'stylesheet'}
52 ? ( -style => { src => $args->{'stylesheet'} } )
55 my $q = defined $args->{'pretty'}
56 ? do { require CGI::Pretty;
62 my ($table, @table_names);
69 -meta => { generator => $NAME },
71 $q->h1({ -class => 'SchemaDescription' }, $title),
75 @table_names = grep { length $_->name } $schema->get_tables;
78 # Generate top menu, with links to full table information
79 my $count = scalar(@table_names);
80 $count = sprintf "%d table%s", $count, $count == 1 ? '' : 's';
82 # Leading table of links
84 $q->comment("Table listing ($count)"),
85 $q->a({ -name => 'top' }),
86 $q->start_table({ -width => '100%', -class => 'LinkTable'}),
88 # XXX This needs to be colspan="$#{$table->fields}" class="LinkTableHeader"
90 $q->td({ -class => 'LinkTableCell' },
91 $q->h2({ -class => 'LinkTableTitle' },
97 for my $table (@table_names) {
98 my $table_name = $table->name;
100 $q->comment("Start link to table '$table_name'"),
101 $q->Tr({ -class => 'LinkTableRow' },
102 $q->td({ -class => 'LinkTableCell' },
103 qq[<a id="${table_name}-link" href="#$table_name">$table_name</a>]
106 $q->comment("End link to table '$table_name'");
108 push @html, $q->end_table;
111 for my $table ($schema->get_tables) {
112 my $table_name = $table->name or next;
113 my @fields = $table->get_fields or next;
115 $q->comment("Starting table '$table_name'"),
116 $q->a({ -name => $table_name }),
117 $q->table({ -class => 'TableHeader', -width => '100%' },
118 $q->Tr({ -class => 'TableHeaderRow' },
119 $q->td({ -class => 'TableHeaderCell' }, $q->h3($table_name)),
120 qq[<a name="$table_name">],
121 $q->td({ -class => 'TableHeaderCell', -align => 'right' },
122 qq[<a href="#top">Top</a>]
127 if ( my @comments = map { $_ ? $_ : () } $table->comments ) {
131 $q->em(map { $q->br, $_ } @comments);
138 $q->start_table({ -border => 1 }),
140 $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;
176 my $class = $i++ % 2 ? 'even' : 'odd';
179 { -class => "tr-$class" },
180 $q->td({ -class => "FieldCellName" }, $name),
181 $q->td({ -class => "FieldCellType" }, $data_type),
182 $q->td({ -class => "FieldCellSize" }, $size),
183 $q->td({ -class => "FieldCellDefault" }, $default),
184 $q->td({ -class => "FieldCellOther" }, join(', ', @other)),
185 $q->td({ -class => "FieldCellFK" }, $fk),
188 push @html, $q->end_table;
193 if ( my @indices = $table->get_indices ) {
196 $q->start_table({ -border => 1 }),
197 $q->Tr({ -class => 'IndexRow' },
198 $q->th([ 'Name', 'Fields' ])
201 for my $index ( @indices ) {
202 my $name = $index->name || '';
203 my $fields = join( ', ', $index->fields ) || '';
206 $q->Tr({ -class => 'IndexCell' },
207 $q->td( [ $name, $fields ] )
211 push @html, $q->end_table;
218 grep { $_->type ne PRIMARY_KEY } $table->get_constraints;
219 if ( @constraints ) {
221 $q->h3('Constraints'),
222 $q->start_table({ -border => 1 }),
223 $q->Tr({ -class => 'IndexRow' },
224 $q->th([ 'Type', 'Fields' ])
227 for my $c ( @constraints ) {
228 my $type = $c->type || '';
229 my $fields = join( ', ', $c->fields ) || '';
232 $q->Tr({ -class => 'IndexCell' },
233 $q->td( [ $type, $fields ] )
237 push @html, $q->end_table;
243 my $sqlt_version = $t->version;
246 qq[Created by <a href="http://sqlfairy.sourceforge.net">],
247 qq[SQL::Translator $sqlt_version</a>],
252 return join "\n", @html;
257 # -------------------------------------------------------------------
258 # Always be ready to speak your mind,
259 # and a base man will avoid you.
261 # -------------------------------------------------------------------
265 SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
269 use SQL::Translator::Producer::HTML;
273 Creates an HTML document describing the tables.
275 The HTML produced is composed of a number of tables:
281 A link table sits at the top of the output, and contains anchored
282 links to elements in the rest of the document.
284 If the I<nolinktable> producer arg is present, then this table is not
289 Each table in the schema has its own HTML table. The top row is a row
290 of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these
291 elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>,
292 I<Other> and I<Foreign Key>. Each successive row describes one field
293 in the table, and has a class of B<FieldCell$item>, where $item id
294 corresponds to the label of the column. For example:
297 <td class="FieldCellName"><a name="random-id">id</a></td>
298 <td class="FieldCellType">int</td>
299 <td class="FieldCellSize">11</td>
300 <td class="FieldCellDefault"></td>
301 <td class="FieldCellOther">PRIMARY KEY, NOT NULL</td>
302 <td class="FieldCellFK"></td>
306 <td class="FieldCellName"><a name="random-foo">foo</a></td>
307 <td class="FieldCellType">varchar</td>
308 <td class="FieldCellSize">255</td>
309 <td class="FieldCellDefault"></td>
310 <td class="FieldCellOther">NOT NULL</td>
311 <td class="FieldCellFK"></td>
315 <td class="FieldCellName"><a name="random-updated">updated</a></td>
316 <td class="FieldCellType">timestamp</td>
317 <td class="FieldCellSize">0</td>
318 <td class="FieldCellDefault"></td>
319 <td class="FieldCellOther"></td>
320 <td class="FieldCellFK"></td>
325 Unless the I<nowrap> producer arg is present, the HTML will be
326 enclosed in a basic HTML header and footer.
328 If the I<pretty> producer arg is present, the generated HTML will be
329 nicely spaced and human-readable. Otherwise, it will have very little
330 insignificant whitespace and be generally smaller.
335 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
336 Darren Chamberlain E<lt>darren@cpan.orgE<gt>.