1 package SQL::Translator::Producer::HTML;
8 our $NAME = __PACKAGE__;
9 our $NOWRAP = 0 unless defined $NOWRAP;
10 our $NOLINKTABLE = 0 unless defined $NOLINKTABLE;
12 # Emit XHTML by default
13 $CGI::XHTML = $CGI::XHTML = 42;
15 use SQL::Translator::Schema::Constants;
17 # -------------------------------------------------------------------
18 # Main entry point. Returns a string containing HTML.
19 # -------------------------------------------------------------------
22 my $args = $t->producer_args;
23 my $schema = $t->schema;
24 my $schema_name = $schema->name || 'Schema';
25 my $title = $args->{'title'} || "Description of $schema_name";
26 my $wrap = ! (defined $args->{'nowrap'}
29 my $linktable = ! (defined $args->{'nolinktable'}
30 ? $args->{'nolinktable'}
32 my %stylesheet = defined $args->{'stylesheet'}
33 ? ( -style => { src => $args->{'stylesheet'} } )
36 my $q = defined $args->{'pretty'}
37 ? do { require CGI::Pretty;
43 my ($table, @table_names);
50 -meta => { generator => $NAME },
52 $q->h1({ -class => 'SchemaDescription' }, $title),
56 @table_names = grep { length $_->name } $schema->get_tables;
59 # Generate top menu, with links to full table information
60 my $count = scalar(@table_names);
61 $count = sprintf "%d table%s", $count, $count == 1 ? '' : 's';
63 # Leading table of links
65 $q->comment("Table listing ($count)"),
66 $q->a({ -name => 'top' }),
67 $q->start_table({ -width => '100%', -class => 'LinkTable'}),
69 # XXX This needs to be colspan="$#{$table->fields}" class="LinkTableHeader"
71 $q->td({ -class => 'LinkTableCell' },
72 $q->h2({ -class => 'LinkTableTitle' },
78 for my $table (@table_names) {
79 my $table_name = $table->name;
81 $q->comment("Start link to table '$table_name'"),
82 $q->Tr({ -class => 'LinkTableRow' },
83 $q->td({ -class => 'LinkTableCell' },
84 qq[<a id="${table_name}-link" href="#$table_name">$table_name</a>]
87 $q->comment("End link to table '$table_name'");
89 push @html, $q->end_table;
92 for my $table ($schema->get_tables) {
93 my $table_name = $table->name or next;
94 my @fields = $table->get_fields or next;
96 $q->comment("Starting table '$table_name'"),
97 $q->a({ -name => $table_name }),
98 $q->table({ -class => 'TableHeader', -width => '100%' },
99 $q->Tr({ -class => 'TableHeaderRow' },
100 $q->td({ -class => 'TableHeaderCell' }, $q->h3($table_name)),
101 qq[<a name="$table_name">],
102 $q->td({ -class => 'TableHeaderCell', -align => 'right' },
103 qq[<a href="#top">Top</a>]
108 if ( my @comments = map { $_ ? $_ : () } $table->comments ) {
112 $q->em(map { $q->br, $_ } @comments);
119 $q->start_table({ -border => 1 }),
121 $q->th({ -class => 'FieldHeader' },
134 for my $field ( @fields ) {
135 my $name = $field->name || '';
136 $name = qq[<a name="$table_name-$name">$name</a>];
137 my $data_type = $field->data_type || '';
138 my $size = defined $field->size ? $field->size : '';
139 my $default = defined $field->default_value
140 ? $field->default_value : '';
141 my $comment = $field->comments || '';
144 if ($field->is_foreign_key) {
145 my $c = $field->foreign_key_reference;
146 my $ref_table = $c->reference_table || '';
147 my $ref_field = ($c->reference_fields)[0] || '';
149 qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>];
153 push @other, 'PRIMARY KEY' if $field->is_primary_key;
154 push @other, 'UNIQUE' if $field->is_unique;
155 push @other, 'NOT NULL' unless $field->is_nullable;
156 push @other, $comment if $comment;
157 my $class = $i++ % 2 ? 'even' : 'odd';
160 { -class => "tr-$class" },
161 $q->td({ -class => "FieldCellName" }, $name),
162 $q->td({ -class => "FieldCellType" }, $data_type),
163 $q->td({ -class => "FieldCellSize" }, $size),
164 $q->td({ -class => "FieldCellDefault" }, $default),
165 $q->td({ -class => "FieldCellOther" }, join(', ', @other)),
166 $q->td({ -class => "FieldCellFK" }, $fk),
169 push @html, $q->end_table;
174 if ( my @indices = $table->get_indices ) {
177 $q->start_table({ -border => 1 }),
178 $q->Tr({ -class => 'IndexRow' },
179 $q->th([ 'Name', 'Fields' ])
182 for my $index ( @indices ) {
183 my $name = $index->name || '';
184 my $fields = join( ', ', $index->fields ) || '';
187 $q->Tr({ -class => 'IndexCell' },
188 $q->td( [ $name, $fields ] )
192 push @html, $q->end_table;
199 grep { $_->type ne PRIMARY_KEY } $table->get_constraints;
200 if ( @constraints ) {
202 $q->h3('Constraints'),
203 $q->start_table({ -border => 1 }),
204 $q->Tr({ -class => 'IndexRow' },
205 $q->th([ 'Type', 'Fields' ])
208 for my $c ( @constraints ) {
209 my $type = $c->type || '';
210 my $fields = join( ', ', $c->fields ) || '';
213 $q->Tr({ -class => 'IndexCell' },
214 $q->td( [ $type, $fields ] )
218 push @html, $q->end_table;
224 my $sqlt_version = $t->version;
227 qq[Created by <a href="http://sqlfairy.sourceforge.net">],
228 qq[SQL::Translator $sqlt_version</a>],
233 return join "\n", @html;
238 # -------------------------------------------------------------------
239 # Always be ready to speak your mind,
240 # and a base man will avoid you.
242 # -------------------------------------------------------------------
246 SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
250 use SQL::Translator::Producer::HTML;
254 Creates an HTML document describing the tables.
256 The HTML produced is composed of a number of tables:
262 A link table sits at the top of the output, and contains anchored
263 links to elements in the rest of the document.
265 If the I<nolinktable> producer arg is present, then this table is not
270 Each table in the schema has its own HTML table. The top row is a row
271 of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these
272 elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>,
273 I<Other> and I<Foreign Key>. Each successive row describes one field
274 in the table, and has a class of B<FieldCell$item>, where $item id
275 corresponds to the label of the column. For example:
278 <td class="FieldCellName"><a name="random-id">id</a></td>
279 <td class="FieldCellType">int</td>
280 <td class="FieldCellSize">11</td>
281 <td class="FieldCellDefault"></td>
282 <td class="FieldCellOther">PRIMARY KEY, NOT NULL</td>
283 <td class="FieldCellFK"></td>
287 <td class="FieldCellName"><a name="random-foo">foo</a></td>
288 <td class="FieldCellType">varchar</td>
289 <td class="FieldCellSize">255</td>
290 <td class="FieldCellDefault"></td>
291 <td class="FieldCellOther">NOT NULL</td>
292 <td class="FieldCellFK"></td>
296 <td class="FieldCellName"><a name="random-updated">updated</a></td>
297 <td class="FieldCellType">timestamp</td>
298 <td class="FieldCellSize">0</td>
299 <td class="FieldCellDefault"></td>
300 <td class="FieldCellOther"></td>
301 <td class="FieldCellFK"></td>
306 Unless the I<nowrap> producer arg is present, the HTML will be
307 enclosed in a basic HTML header and footer.
309 If the I<pretty> producer arg is present, the generated HTML will be
310 nicely spaced and human-readable. Otherwise, it will have very little
311 insignificant whitespace and be generally smaller.
316 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
317 Darren Chamberlain E<lt>darren@cpan.orgE<gt>.