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