Fixed copyrights.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / HTML.pm
CommitLineData
aca7d777 1package SQL::Translator::Producer::HTML;
2
3# -------------------------------------------------------------------
64e36ec8 4# $Id: HTML.pm,v 1.10 2003-10-17 19:51:57 dlc Exp $
aca7d777 5# -------------------------------------------------------------------
64e36ec8 6# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7# darren chamberlain <darren@cpan.org>
aca7d777 8#
9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License as
11# published by the Free Software Foundation; version 2.
12#
13# This program is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21# 02111-1307 USA
22# -------------------------------------------------------------------
23
24use strict;
a11fbaea 25use Data::Dumper;
64e36ec8 26use vars qw($VERSION $NOWRAP $NOLINKTABLE $NAME);
27
28$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/;
29$NAME = join ', ', __PACKAGE__, $VERSION;
30$NOWRAP = 0 unless defined $NOWRAP;
31$NOLINKTABLE = 0 unless defined $NOLINKTABLE;
32
33# Emit XHTML by default
34$CGI::XHTML = $CGI::XHTML = 42;
aca7d777 35
36use SQL::Translator::Schema::Constants;
aca7d777 37
38# -------------------------------------------------------------------
64e36ec8 39# Main entry point. Returns a string containing HTML.
40# -------------------------------------------------------------------
aca7d777 41sub produce {
42 my $t = shift;
84474a81 43 my $args = $t->producer_args;
aca7d777 44 my $schema = $t->schema;
45 my $schema_name = $schema->name || 'Schema';
84474a81 46 my $title = $args->{'title'} || "Description of $schema_name";
64e36ec8 47 my $wrap = ! (defined $args->{'nowrap'}
48 ? $args->{'nowrap'}
49 : $NOWRAP);
50 my $linktable = ! (defined $args->{'nolinktable'}
51 ? $args->{'nolinktable'}
52 : $NOLINKTABLE);
53 my %stylesheet = defined $args->{'stylesheet'}
54 ? ( -style => { src => $args->{'stylesheet'} } )
55 : ( );
56 my @html;
1ea530d4 57 my $q = defined $args->{'pretty'}
29ecbf4e 58 ? do { require CGI::Pretty;
59 import CGI::Pretty;
60 CGI::Pretty->new }
61 : do { require CGI;
62 import CGI;
63 CGI->new };
64e36ec8 64 my ($table, @table_names);
65
66 if ($wrap) {
67 push @html,
68 $q->start_html({
69 -title => $title,
70 %stylesheet,
71 -meta => { generator => $NAME },
72 });
73 $q->h1({ -class => 'SchemaDescription' }, $title),
74 $q->a({ -name => 'top' }),
75 $q->hr;
76 }
77
78 @table_names = grep { length $_->name } $schema->get_tables;
aca7d777 79
64e36ec8 80 if ($linktable) {
81 # Generate top menu, with links to full table information
82 my $count = scalar(@table_names);
83 $count = sprintf "%d table%s", $count, $count == 1 ? '' : 's';
aca7d777 84
64e36ec8 85 # Leading table of links
86 push @html,
87 $q->comment("Table listing ($count)"),
88 $q->start_table({ -width => '100%', -class => 'LinkTable' }),
89
90 # XXX This needs to be colspan="$#{$table->fields}" class="LinkTableHeader"
91 $q->Tr(
92 $q->td({ -class => 'LinkTableCell' },
93 $q->h2({ -class => 'LinkTableTitle' },
94 'Tables'
95 ),
96 ),
97 );
ad02944a 98
64e36ec8 99 for my $table (sort @table_names) {
100 my $table_name = $table->name;
101 push @html,
102 $q->comment("Start link to table '$table_name'"),
103 $q->Tr({ -class => 'LinkTableRow' },
104 $q->td({ -class => 'LinkTableCell' },
105 qq[<a id="${table_name}-link" href="#$table_name">$table_name</a>]
106 )
107 ),
108 $q->comment("End link to table '$table_name'");
ad02944a 109 }
64e36ec8 110 push @html, $q->end_table;
ad02944a 111 }
112
64e36ec8 113 for my $table ($schema->get_tables) {
84474a81 114 my $table_name = $table->name or next;
aca7d777 115 my @fields = $table->get_fields or next;
64e36ec8 116 push @html,
117 $q->comment("Starting table '$table_name'");
118 $q->table({ -class => 'TableHeader', -width => '100%' },
119 $q->Tr({ -class => 'TableHeaderRow' },
120 $q->td({ -class => 'TableHeaderCell' }, $q->h3($table_name)),
121 qq[<a name="$table_name">],
122 $q->td({ -class => 'TableHeaderCell', -align => 'right' },
123 qq[<a href="#top">Top</a>]
124 )
125 )
126 );
aca7d777 127
84474a81 128 if ( my @comments = map { $_ ? $_ : () } $table->comments ) {
64e36ec8 129 push @html,
130 $q->b("Comments:"),
131 $q->br,
132 $q->em(map { $q->br, $_ } @comments);
a11fbaea 133 }
134
aca7d777 135 #
136 # Fields
137 #
64e36ec8 138 push @html,
139 $q->start_table,
140 $q->Tr(
141 $q->th({ -class => 'FieldHeader' },
142 [
143 'Field Name',
144 'Data Type',
145 'Size',
146 'Default Value',
147 'Other',
148 'Foreign Key'
149 ]
150 )
151 );
aca7d777 152
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;
64e36ec8 176 push @html,
177 $q->Tr(
178 $q->td({ -class => "FieldCellName" }, $name),
179 $q->td({ -class => "FieldCellType" }, $data_type),
180 $q->td({ -class => "FieldCellSize" }, $size),
181 $q->td({ -class => "FieldCellDefault" }, $default),
182 $q->td({ -class => "FieldCellOther" }, join(', ', @other)),
183 $q->td({ -class => "FieldCellFK" }, $fk),
184 );
aca7d777 185 }
64e36ec8 186 push @html, $q->end_table;
aca7d777 187
188 #
189 # Indices
190 #
191 if ( my @indices = $table->get_indices ) {
64e36ec8 192 push @html,
193 $q->h3('Indices'),
194 $q->start_table({ -border => 1 }),
195 $q->Tr({ -class => 'IndexRow' },
196 $q->th([ 'Name', 'Fields' ])
197 );
aca7d777 198
199 for my $index ( @indices ) {
84474a81 200 my $name = $index->name || '';
201 my $fields = join( ', ', $index->fields ) || '';
202
64e36ec8 203 push @html,
204 $q->Tr({ -class => 'IndexCell' },
205 $q->td( [ $name, $fields ] )
206 );
aca7d777 207 }
208
64e36ec8 209 push @html, $q->end_table;
aca7d777 210 }
211
64e36ec8 212 push @html, $q->hr;
aca7d777 213 }
214
64e36ec8 215 if ($wrap) {
216 push @html,
217 qq[Created by <a href="http://sqlfairy.sourceforge.net">],
218 qq[SQL::Translator</a>],
219 $q->end_html;
220 }
aca7d777 221
64e36ec8 222
223 return join "\n", @html;
aca7d777 224}
225
2261;
227
228# -------------------------------------------------------------------
229# Always be ready to speak your mind,
230# and a base man will avoid you.
231# William Blake
232# -------------------------------------------------------------------
233
234=head1 NAME
235
236SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
237
238=head1 SYNOPSIS
239
240 use SQL::Translator::Producer::HTML;
241
242=head1 DESCRIPTION
243
244Creates an HTML document describing the tables.
245
64e36ec8 246The HTML produced is composed of a number of tables:
247
248=over 4
249
250=item Links
251
252A link table sits at the top of the output, and contains anchored
253links to elements in the rest of the document.
254
255If the I<nolinktable> producer arg is present, then this table is not
256produced.
257
258=item Tables
259
260Each table in the schema has its own HTML table. The top row is a row
261of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these
262elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>,
263I<Other> and I<Foreign Key>. Each successive row describes one field
264in the table, and has a class of B<FieldCell$item>, where $item id
265corresponds to the label of the column. For example:
266
267 <tr>
268 <td class="FieldCellName"><a name="random-id">id</a></td>
269 <td class="FieldCellType">int</td>
270 <td class="FieldCellSize">11</td>
271 <td class="FieldCellDefault"></td>
272 <td class="FieldCellOther">PRIMARY KEY, NOT NULL</td>
273 <td class="FieldCellFK"></td>
274 </tr>
275
276 <tr>
277 <td class="FieldCellName"><a name="random-foo">foo</a></td>
278 <td class="FieldCellType">varchar</td>
279 <td class="FieldCellSize">255</td>
280 <td class="FieldCellDefault"></td>
281 <td class="FieldCellOther">NOT NULL</td>
282 <td class="FieldCellFK"></td>
283 </tr>
284
285 <tr>
286 <td class="FieldCellName"><a name="random-updated">updated</a></td>
287 <td class="FieldCellType">timestamp</td>
288 <td class="FieldCellSize">0</td>
289 <td class="FieldCellDefault"></td>
290 <td class="FieldCellOther"></td>
291 <td class="FieldCellFK"></td>
292 </tr>
293
294=back
295
296Unless the I<nowrap> producer arg is present, the HTML will be
297enclosed in a basic HTML header and footer.
298
299If the I<pretty> producer arg is present, the generated HTML will be
300nicely spaced and human-readable. Otherwise, it will have very little
301insignificant whitespace and be generally smaller.
302
303
aca7d777 304=head1 AUTHOR
305
64e36ec8 306Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
307Darren Chamberlain E<lt>darren@cpan.orgE<gt>
aca7d777 308
309=cut