1 package SQL::Translator::Producer::HTML;
6 use vars qw($VERSION $NOWRAP $NOLINKTABLE $NAME);
11 $NOWRAP = 0 unless defined $NOWRAP;
12 $NOLINKTABLE = 0 unless defined $NOLINKTABLE;
14 # Emit XHTML by default
15 $CGI::XHTML = $CGI::XHTML = 42;
17 use SQL::Translator::Schema::Constants;
19 # -------------------------------------------------------------------
20 # Main entry point. Returns a string containing HTML.
21 # -------------------------------------------------------------------
24 my $args = $t->producer_args;
25 my $schema = $t->schema;
26 my $schema_name = $schema->name || 'Schema';
27 my $title = $args->{'title'} || "Description of $schema_name";
28 my $wrap = ! (defined $args->{'nowrap'}
31 my $linktable = ! (defined $args->{'nolinktable'}
32 ? $args->{'nolinktable'}
34 my %stylesheet = defined $args->{'stylesheet'}
35 ? ( -style => { src => $args->{'stylesheet'} } )
38 my $q = defined $args->{'pretty'}
39 ? do { require CGI::Pretty;
45 my ($table, @table_names);
52 -meta => { generator => $NAME },
54 $q->h1({ -class => 'SchemaDescription' }, $title),
58 @table_names = grep { length $_->name } $schema->get_tables;
61 # Generate top menu, with links to full table information
62 my $count = scalar(@table_names);
63 $count = sprintf "%d table%s", $count, $count == 1 ? '' : 's';
65 # Leading table of links
67 $q->comment("Table listing ($count)"),
68 $q->a({ -name => 'top' }),
69 $q->start_table({ -width => '100%', -class => 'LinkTable'}),
71 # XXX This needs to be colspan="$#{$table->fields}" class="LinkTableHeader"
73 $q->td({ -class => 'LinkTableCell' },
74 $q->h2({ -class => 'LinkTableTitle' },
80 for my $table (@table_names) {
81 my $table_name = $table->name;
83 $q->comment("Start link to table '$table_name'"),
84 $q->Tr({ -class => 'LinkTableRow' },
85 $q->td({ -class => 'LinkTableCell' },
86 qq[<a id="${table_name}-link" href="#$table_name">$table_name</a>]
89 $q->comment("End link to table '$table_name'");
91 push @html, $q->end_table;
94 for my $table ($schema->get_tables) {
95 my $table_name = $table->name or next;
96 my @fields = $table->get_fields or next;
98 $q->comment("Starting table '$table_name'"),
99 $q->a({ -name => $table_name }),
100 $q->table({ -class => 'TableHeader', -width => '100%' },
101 $q->Tr({ -class => 'TableHeaderRow' },
102 $q->td({ -class => 'TableHeaderCell' }, $q->h3($table_name)),
103 qq[<a name="$table_name">],
104 $q->td({ -class => 'TableHeaderCell', -align => 'right' },
105 qq[<a href="#top">Top</a>]
110 if ( my @comments = map { $_ ? $_ : () } $table->comments ) {
114 $q->em(map { $q->br, $_ } @comments);
121 $q->start_table({ -border => 1 }),
123 $q->th({ -class => 'FieldHeader' },
136 for my $field ( @fields ) {
137 my $name = $field->name || '';
138 $name = qq[<a name="$table_name-$name">$name</a>];
139 my $data_type = $field->data_type || '';
140 my $size = defined $field->size ? $field->size : '';
141 my $default = defined $field->default_value
142 ? $field->default_value : '';
143 my $comment = $field->comments || '';
146 if ($field->is_foreign_key) {
147 my $c = $field->foreign_key_reference;
148 my $ref_table = $c->reference_table || '';
149 my $ref_field = ($c->reference_fields)[0] || '';
151 qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>];
155 push @other, 'PRIMARY KEY' if $field->is_primary_key;
156 push @other, 'UNIQUE' if $field->is_unique;
157 push @other, 'NOT NULL' unless $field->is_nullable;
158 push @other, $comment if $comment;
159 my $class = $i++ % 2 ? 'even' : 'odd';
162 { -class => "tr-$class" },
163 $q->td({ -class => "FieldCellName" }, $name),
164 $q->td({ -class => "FieldCellType" }, $data_type),
165 $q->td({ -class => "FieldCellSize" }, $size),
166 $q->td({ -class => "FieldCellDefault" }, $default),
167 $q->td({ -class => "FieldCellOther" }, join(', ', @other)),
168 $q->td({ -class => "FieldCellFK" }, $fk),
171 push @html, $q->end_table;
176 if ( my @indices = $table->get_indices ) {
179 $q->start_table({ -border => 1 }),
180 $q->Tr({ -class => 'IndexRow' },
181 $q->th([ 'Name', 'Fields' ])
184 for my $index ( @indices ) {
185 my $name = $index->name || '';
186 my $fields = join( ', ', $index->fields ) || '';
189 $q->Tr({ -class => 'IndexCell' },
190 $q->td( [ $name, $fields ] )
194 push @html, $q->end_table;
201 grep { $_->type ne PRIMARY_KEY } $table->get_constraints;
202 if ( @constraints ) {
204 $q->h3('Constraints'),
205 $q->start_table({ -border => 1 }),
206 $q->Tr({ -class => 'IndexRow' },
207 $q->th([ 'Type', 'Fields' ])
210 for my $c ( @constraints ) {
211 my $type = $c->type || '';
212 my $fields = join( ', ', $c->fields ) || '';
215 $q->Tr({ -class => 'IndexCell' },
216 $q->td( [ $type, $fields ] )
220 push @html, $q->end_table;
226 my $sqlt_version = $t->version;
229 qq[Created by <a href="http://sqlfairy.sourceforge.net">],
230 qq[SQL::Translator $sqlt_version</a>],
235 return join "\n", @html;
240 # -------------------------------------------------------------------
241 # Always be ready to speak your mind,
242 # and a base man will avoid you.
244 # -------------------------------------------------------------------
248 SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
252 use SQL::Translator::Producer::HTML;
256 Creates an HTML document describing the tables.
258 The HTML produced is composed of a number of tables:
264 A link table sits at the top of the output, and contains anchored
265 links to elements in the rest of the document.
267 If the I<nolinktable> producer arg is present, then this table is not
272 Each table in the schema has its own HTML table. The top row is a row
273 of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these
274 elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>,
275 I<Other> and I<Foreign Key>. Each successive row describes one field
276 in the table, and has a class of B<FieldCell$item>, where $item id
277 corresponds to the label of the column. For example:
280 <td class="FieldCellName"><a name="random-id">id</a></td>
281 <td class="FieldCellType">int</td>
282 <td class="FieldCellSize">11</td>
283 <td class="FieldCellDefault"></td>
284 <td class="FieldCellOther">PRIMARY KEY, NOT NULL</td>
285 <td class="FieldCellFK"></td>
289 <td class="FieldCellName"><a name="random-foo">foo</a></td>
290 <td class="FieldCellType">varchar</td>
291 <td class="FieldCellSize">255</td>
292 <td class="FieldCellDefault"></td>
293 <td class="FieldCellOther">NOT NULL</td>
294 <td class="FieldCellFK"></td>
298 <td class="FieldCellName"><a name="random-updated">updated</a></td>
299 <td class="FieldCellType">timestamp</td>
300 <td class="FieldCellSize">0</td>
301 <td class="FieldCellDefault"></td>
302 <td class="FieldCellOther"></td>
303 <td class="FieldCellFK"></td>
308 Unless the I<nowrap> producer arg is present, the HTML will be
309 enclosed in a basic HTML header and footer.
311 If the I<pretty> producer arg is present, the generated HTML will be
312 nicely spaced and human-readable. Otherwise, it will have very little
313 insignificant whitespace and be generally smaller.
318 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
319 Darren Chamberlain E<lt>darren@cpan.orgE<gt>.