Changes + Reverts for 0.11000, see Changes file for info
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / HTML.pm
CommitLineData
aca7d777 1package SQL::Translator::Producer::HTML;
2
3# -------------------------------------------------------------------
478f608d 4# Copyright (C) 2002-2009 SQLFairy Authors
aca7d777 5#
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.
9#
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.
14#
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
18# 02111-1307 USA
19# -------------------------------------------------------------------
20
21use strict;
a11fbaea 22use Data::Dumper;
da06ac74 23use vars qw($VERSION $NOWRAP $NOLINKTABLE $NAME);
24
11ad2df9 25$VERSION = '1.59';
64e36ec8 26
f9c96971 27$NAME = __PACKAGE__;
64e36ec8 28$NOWRAP = 0 unless defined $NOWRAP;
29$NOLINKTABLE = 0 unless defined $NOLINKTABLE;
30
31# Emit XHTML by default
32$CGI::XHTML = $CGI::XHTML = 42;
aca7d777 33
34use SQL::Translator::Schema::Constants;
aca7d777 35
36# -------------------------------------------------------------------
64e36ec8 37# Main entry point. Returns a string containing HTML.
38# -------------------------------------------------------------------
aca7d777 39sub produce {
40 my $t = shift;
84474a81 41 my $args = $t->producer_args;
aca7d777 42 my $schema = $t->schema;
43 my $schema_name = $schema->name || 'Schema';
84474a81 44 my $title = $args->{'title'} || "Description of $schema_name";
64e36ec8 45 my $wrap = ! (defined $args->{'nowrap'}
46 ? $args->{'nowrap'}
47 : $NOWRAP);
48 my $linktable = ! (defined $args->{'nolinktable'}
49 ? $args->{'nolinktable'}
50 : $NOLINKTABLE);
51 my %stylesheet = defined $args->{'stylesheet'}
52 ? ( -style => { src => $args->{'stylesheet'} } )
53 : ( );
54 my @html;
1ea530d4 55 my $q = defined $args->{'pretty'}
29ecbf4e 56 ? do { require CGI::Pretty;
57 import CGI::Pretty;
58 CGI::Pretty->new }
59 : do { require CGI;
60 import CGI;
61 CGI->new };
64e36ec8 62 my ($table, @table_names);
63
64 if ($wrap) {
65 push @html,
66 $q->start_html({
67 -title => $title,
68 %stylesheet,
69 -meta => { generator => $NAME },
c012e81a 70 }),
64e36ec8 71 $q->h1({ -class => 'SchemaDescription' }, $title),
64e36ec8 72 $q->hr;
73 }
74
75 @table_names = grep { length $_->name } $schema->get_tables;
aca7d777 76
64e36ec8 77 if ($linktable) {
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';
aca7d777 81
64e36ec8 82 # Leading table of links
83 push @html,
84 $q->comment("Table listing ($count)"),
a3de76be 85 $q->a({ -name => 'top' }),
86 $q->start_table({ -width => '100%', -class => 'LinkTable'}),
64e36ec8 87
88 # XXX This needs to be colspan="$#{$table->fields}" class="LinkTableHeader"
89 $q->Tr(
90 $q->td({ -class => 'LinkTableCell' },
91 $q->h2({ -class => 'LinkTableTitle' },
92 'Tables'
93 ),
94 ),
95 );
ad02944a 96
d8b098e5 97 for my $table (@table_names) {
64e36ec8 98 my $table_name = $table->name;
99 push @html,
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>]
104 )
105 ),
106 $q->comment("End link to table '$table_name'");
ad02944a 107 }
64e36ec8 108 push @html, $q->end_table;
ad02944a 109 }
110
64e36ec8 111 for my $table ($schema->get_tables) {
84474a81 112 my $table_name = $table->name or next;
aca7d777 113 my @fields = $table->get_fields or next;
64e36ec8 114 push @html,
a3de76be 115 $q->comment("Starting table '$table_name'"),
116 $q->a({ -name => $table_name }),
64e36ec8 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>]
123 )
124 )
125 );
aca7d777 126
84474a81 127 if ( my @comments = map { $_ ? $_ : () } $table->comments ) {
64e36ec8 128 push @html,
129 $q->b("Comments:"),
130 $q->br,
131 $q->em(map { $q->br, $_ } @comments);
a11fbaea 132 }
133
aca7d777 134 #
135 # Fields
136 #
64e36ec8 137 push @html,
a3de76be 138 $q->start_table({ -border => 1 }),
64e36ec8 139 $q->Tr(
140 $q->th({ -class => 'FieldHeader' },
141 [
142 'Field Name',
143 'Data Type',
144 'Size',
145 'Default Value',
146 'Other',
147 'Foreign Key'
148 ]
149 )
150 );
aca7d777 151
3d4edd30 152 my $i = 0;
aca7d777 153 for my $field ( @fields ) {
84474a81 154 my $name = $field->name || '';
aca7d777 155 $name = qq[<a name="$table_name-$name">$name</a>];
84474a81 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 || '';
161 my $fk = '';
aca7d777 162
64e36ec8 163 if ($field->is_foreign_key) {
84474a81 164 my $c = $field->foreign_key_reference;
165 my $ref_table = $c->reference_table || '';
166 my $ref_field = ($c->reference_fields)[0] || '';
167 $fk =
aca7d777 168 qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>];
169 }
170
84474a81 171 my @other = ();
aca7d777 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;
ad02944a 175 push @other, $comment if $comment;
3d4edd30 176 my $class = $i++ % 2 ? 'even' : 'odd';
64e36ec8 177 push @html,
178 $q->Tr(
3d4edd30 179 { -class => "tr-$class" },
64e36ec8 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),
186 );
aca7d777 187 }
64e36ec8 188 push @html, $q->end_table;
aca7d777 189
190 #
191 # Indices
192 #
193 if ( my @indices = $table->get_indices ) {
64e36ec8 194 push @html,
195 $q->h3('Indices'),
196 $q->start_table({ -border => 1 }),
197 $q->Tr({ -class => 'IndexRow' },
198 $q->th([ 'Name', 'Fields' ])
199 );
aca7d777 200
201 for my $index ( @indices ) {
84474a81 202 my $name = $index->name || '';
203 my $fields = join( ', ', $index->fields ) || '';
204
64e36ec8 205 push @html,
206 $q->Tr({ -class => 'IndexCell' },
207 $q->td( [ $name, $fields ] )
208 );
aca7d777 209 }
210
64e36ec8 211 push @html, $q->end_table;
aca7d777 212 }
213
a3de76be 214 #
215 # Constraints
216 #
217 my @constraints =
218 grep { $_->type ne PRIMARY_KEY } $table->get_constraints;
219 if ( @constraints ) {
220 push @html,
221 $q->h3('Constraints'),
222 $q->start_table({ -border => 1 }),
223 $q->Tr({ -class => 'IndexRow' },
224 $q->th([ 'Type', 'Fields' ])
225 );
226
227 for my $c ( @constraints ) {
228 my $type = $c->type || '';
229 my $fields = join( ', ', $c->fields ) || '';
230
231 push @html,
232 $q->Tr({ -class => 'IndexCell' },
233 $q->td( [ $type, $fields ] )
234 );
235 }
236
237 push @html, $q->end_table;
238 }
239
64e36ec8 240 push @html, $q->hr;
aca7d777 241 }
242
a3de76be 243 my $sqlt_version = $t->version;
64e36ec8 244 if ($wrap) {
245 push @html,
246 qq[Created by <a href="http://sqlfairy.sourceforge.net">],
a3de76be 247 qq[SQL::Translator $sqlt_version</a>],
64e36ec8 248 $q->end_html;
249 }
aca7d777 250
64e36ec8 251
252 return join "\n", @html;
aca7d777 253}
254
2551;
256
257# -------------------------------------------------------------------
258# Always be ready to speak your mind,
259# and a base man will avoid you.
260# William Blake
261# -------------------------------------------------------------------
262
263=head1 NAME
264
265SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
266
267=head1 SYNOPSIS
268
269 use SQL::Translator::Producer::HTML;
270
271=head1 DESCRIPTION
272
273Creates an HTML document describing the tables.
274
64e36ec8 275The HTML produced is composed of a number of tables:
276
277=over 4
278
279=item Links
280
281A link table sits at the top of the output, and contains anchored
282links to elements in the rest of the document.
283
284If the I<nolinktable> producer arg is present, then this table is not
285produced.
286
287=item Tables
288
289Each table in the schema has its own HTML table. The top row is a row
290of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these
291elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>,
292I<Other> and I<Foreign Key>. Each successive row describes one field
293in the table, and has a class of B<FieldCell$item>, where $item id
294corresponds to the label of the column. For example:
295
296 <tr>
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>
303 </tr>
304
305 <tr>
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>
312 </tr>
313
314 <tr>
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>
321 </tr>
322
323=back
324
325Unless the I<nowrap> producer arg is present, the HTML will be
326enclosed in a basic HTML header and footer.
327
328If the I<pretty> producer arg is present, the generated HTML will be
329nicely spaced and human-readable. Otherwise, it will have very little
330insignificant whitespace and be generally smaller.
331
332
977651a5 333=head1 AUTHORS
aca7d777 334
f997b9ab 335Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
977651a5 336Darren Chamberlain E<lt>darren@cpan.orgE<gt>.
aca7d777 337
338=cut