1 package SQL::Translator::Producer::HTML;
5 use vars qw($VERSION $NOWRAP $NOLINKTABLE $NAME);
10 $NOWRAP = 0 unless defined $NOWRAP;
11 $NOLINKTABLE = 0 unless defined $NOLINKTABLE;
13 # Emit XHTML by default
14 $CGI::XHTML = $CGI::XHTML = 42;
16 use SQL::Translator::Schema::Constants;
18 # -------------------------------------------------------------------
19 # Main entry point. Returns a string containing HTML.
20 # -------------------------------------------------------------------
23 my $args = $t->producer_args;
24 my $schema = $t->schema;
25 my $schema_name = $schema->name || 'Schema';
26 my $title = $args->{'title'} || "Description of $schema_name";
27 my $wrap = ! (defined $args->{'nowrap'}
30 my $linktable = ! (defined $args->{'nolinktable'}
31 ? $args->{'nolinktable'}
33 my %stylesheet = defined $args->{'stylesheet'}
34 ? ( -style => { src => $args->{'stylesheet'} } )
37 my $q = defined $args->{'pretty'}
38 ? do { require CGI::Pretty;
44 my ($table, @table_names);
51 -meta => { generator => $NAME },
53 $q->h1({ -class => 'SchemaDescription' }, $title),
57 @table_names = grep { length $_->name } $schema->get_tables;
60 # Generate top menu, with links to full table information
61 my $count = scalar(@table_names);
62 $count = sprintf "%d table%s", $count, $count == 1 ? '' : 's';
64 # Leading table of links
66 $q->comment("Table listing ($count)"),
67 $q->a({ -name => 'top' }),
68 $q->start_table({ -width => '100%', -class => 'LinkTable'}),
70 # XXX This needs to be colspan="$#{$table->fields}" class="LinkTableHeader"
72 $q->td({ -class => 'LinkTableCell' },
73 $q->h2({ -class => 'LinkTableTitle' },
79 for my $table (@table_names) {
80 my $table_name = $table->name;
82 $q->comment("Start link to table '$table_name'"),
83 $q->Tr({ -class => 'LinkTableRow' },
84 $q->td({ -class => 'LinkTableCell' },
85 qq[<a id="${table_name}-link" href="#$table_name">$table_name</a>]
88 $q->comment("End link to table '$table_name'");
90 push @html, $q->end_table;
93 for my $table ($schema->get_tables) {
94 my $table_name = $table->name or next;
95 my @fields = $table->get_fields or next;
97 $q->comment("Starting table '$table_name'"),
98 $q->a({ -name => $table_name }),
99 $q->table({ -class => 'TableHeader', -width => '100%' },
100 $q->Tr({ -class => 'TableHeaderRow' },
101 $q->td({ -class => 'TableHeaderCell' }, $q->h3($table_name)),
102 qq[<a name="$table_name">],
103 $q->td({ -class => 'TableHeaderCell', -align => 'right' },
104 qq[<a href="#top">Top</a>]
109 if ( my @comments = map { $_ ? $_ : () } $table->comments ) {
113 $q->em(map { $q->br, $_ } @comments);
120 $q->start_table({ -border => 1 }),
122 $q->th({ -class => 'FieldHeader' },
135 for my $field ( @fields ) {
136 my $name = $field->name || '';
137 $name = qq[<a name="$table_name-$name">$name</a>];
138 my $data_type = $field->data_type || '';
139 my $size = defined $field->size ? $field->size : '';
140 my $default = defined $field->default_value
141 ? $field->default_value : '';
142 my $comment = $field->comments || '';
145 if ($field->is_foreign_key) {
146 my $c = $field->foreign_key_reference;
147 my $ref_table = $c->reference_table || '';
148 my $ref_field = ($c->reference_fields)[0] || '';
150 qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>];
154 push @other, 'PRIMARY KEY' if $field->is_primary_key;
155 push @other, 'UNIQUE' if $field->is_unique;
156 push @other, 'NOT NULL' unless $field->is_nullable;
157 push @other, $comment if $comment;
158 my $class = $i++ % 2 ? 'even' : 'odd';
161 { -class => "tr-$class" },
162 $q->td({ -class => "FieldCellName" }, $name),
163 $q->td({ -class => "FieldCellType" }, $data_type),
164 $q->td({ -class => "FieldCellSize" }, $size),
165 $q->td({ -class => "FieldCellDefault" }, $default),
166 $q->td({ -class => "FieldCellOther" }, join(', ', @other)),
167 $q->td({ -class => "FieldCellFK" }, $fk),
170 push @html, $q->end_table;
175 if ( my @indices = $table->get_indices ) {
178 $q->start_table({ -border => 1 }),
179 $q->Tr({ -class => 'IndexRow' },
180 $q->th([ 'Name', 'Fields' ])
183 for my $index ( @indices ) {
184 my $name = $index->name || '';
185 my $fields = join( ', ', $index->fields ) || '';
188 $q->Tr({ -class => 'IndexCell' },
189 $q->td( [ $name, $fields ] )
193 push @html, $q->end_table;
200 grep { $_->type ne PRIMARY_KEY } $table->get_constraints;
201 if ( @constraints ) {
203 $q->h3('Constraints'),
204 $q->start_table({ -border => 1 }),
205 $q->Tr({ -class => 'IndexRow' },
206 $q->th([ 'Type', 'Fields' ])
209 for my $c ( @constraints ) {
210 my $type = $c->type || '';
211 my $fields = join( ', ', $c->fields ) || '';
214 $q->Tr({ -class => 'IndexCell' },
215 $q->td( [ $type, $fields ] )
219 push @html, $q->end_table;
225 my $sqlt_version = $t->version;
228 qq[Created by <a href="http://sqlfairy.sourceforge.net">],
229 qq[SQL::Translator $sqlt_version</a>],
234 return join "\n", @html;
239 # -------------------------------------------------------------------
240 # Always be ready to speak your mind,
241 # and a base man will avoid you.
243 # -------------------------------------------------------------------
247 SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
251 use SQL::Translator::Producer::HTML;
255 Creates an HTML document describing the tables.
257 The HTML produced is composed of a number of tables:
263 A link table sits at the top of the output, and contains anchored
264 links to elements in the rest of the document.
266 If the I<nolinktable> producer arg is present, then this table is not
271 Each table in the schema has its own HTML table. The top row is a row
272 of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these
273 elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>,
274 I<Other> and I<Foreign Key>. Each successive row describes one field
275 in the table, and has a class of B<FieldCell$item>, where $item id
276 corresponds to the label of the column. For example:
279 <td class="FieldCellName"><a name="random-id">id</a></td>
280 <td class="FieldCellType">int</td>
281 <td class="FieldCellSize">11</td>
282 <td class="FieldCellDefault"></td>
283 <td class="FieldCellOther">PRIMARY KEY, NOT NULL</td>
284 <td class="FieldCellFK"></td>
288 <td class="FieldCellName"><a name="random-foo">foo</a></td>
289 <td class="FieldCellType">varchar</td>
290 <td class="FieldCellSize">255</td>
291 <td class="FieldCellDefault"></td>
292 <td class="FieldCellOther">NOT NULL</td>
293 <td class="FieldCellFK"></td>
297 <td class="FieldCellName"><a name="random-updated">updated</a></td>
298 <td class="FieldCellType">timestamp</td>
299 <td class="FieldCellSize">0</td>
300 <td class="FieldCellDefault"></td>
301 <td class="FieldCellOther"></td>
302 <td class="FieldCellFK"></td>
307 Unless the I<nowrap> producer arg is present, the HTML will be
308 enclosed in a basic HTML header and footer.
310 If the I<pretty> producer arg is present, the generated HTML will be
311 nicely spaced and human-readable. Otherwise, it will have very little
312 insignificant whitespace and be generally smaller.
317 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
318 Darren Chamberlain E<lt>darren@cpan.orgE<gt>.