6659fb37c836d5821557b784a8558f046123ad95
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / HTML.pm
1 package SQL::Translator::Producer::HTML;
2
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
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
21 use strict;
22 use Data::Dumper;
23 use vars qw($VERSION $NOWRAP $NOLINKTABLE $NAME);
24
25 $VERSION = '1.59';
26
27 $NAME = __PACKAGE__;
28 $NOWRAP = 0 unless defined $NOWRAP;
29 $NOLINKTABLE = 0 unless defined $NOLINKTABLE;
30
31 # Emit XHTML by default
32 $CGI::XHTML = $CGI::XHTML = 42;
33
34 use SQL::Translator::Schema::Constants;
35
36 # -------------------------------------------------------------------
37 # Main entry point.  Returns a string containing HTML.
38 # -------------------------------------------------------------------
39 sub produce {
40     my $t           = shift;
41     my $args        = $t->producer_args;
42     my $schema      = $t->schema;
43     my $schema_name = $schema->name || 'Schema';
44     my $title       = $args->{'title'} || "Description of $schema_name";
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;
55     my $q           = defined $args->{'pretty'}
56                     ? do { require CGI::Pretty;
57                             import CGI::Pretty;
58                                    CGI::Pretty->new }
59                     : do { require CGI;
60                             import CGI; 
61                                    CGI->new };
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 },
70             }),
71             $q->h1({ -class => 'SchemaDescription' }, $title),
72             $q->hr;
73     }
74
75     @table_names = grep { length $_->name } $schema->get_tables; 
76
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';
81
82         # Leading table of links
83         push @html, 
84             $q->comment("Table listing ($count)"),
85             $q->a({ -name => 'top' }),
86             $q->start_table({ -width => '100%', -class => 'LinkTable'}),
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             );
96
97         for my $table (@table_names) {
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'");
107         }
108         push @html, $q->end_table;
109     }
110
111     for my $table ($schema->get_tables) {
112         my $table_name = $table->name       or next;
113         my @fields     = $table->get_fields or next;
114         push @html,
115             $q->comment("Starting table '$table_name'"),
116             $q->a({ -name => $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({ -border => 1 }),
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         my $i = 0;
153         for my $field ( @fields ) {
154             my $name      = $field->name      || '';
155                $name      = qq[<a name="$table_name-$name">$name</a>];
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        = '';
162
163             if ($field->is_foreign_key) {
164                 my $c         = $field->foreign_key_reference;
165                 my $ref_table = $c->reference_table       || '';
166                 my $ref_field = ($c->reference_fields)[0] || '';
167                 $fk           = 
168                 qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>];
169             }
170
171             my @other = ();
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;
175             push @other, $comment      if $comment;
176             my $class = $i++ % 2 ? 'even' : 'odd';
177             push @html,
178                 $q->Tr(
179                     { -class => "tr-$class" },
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                 );
187         }
188         push @html, $q->end_table;
189
190         #
191         # Indices
192         #
193         if ( my @indices = $table->get_indices ) {
194             push @html, 
195                 $q->h3('Indices'),
196                 $q->start_table({ -border => 1 }),
197                     $q->Tr({ -class => 'IndexRow' },
198                         $q->th([ 'Name', 'Fields' ]) 
199                     );
200
201             for my $index ( @indices ) {
202                 my $name   = $index->name || '';
203                 my $fields = join( ', ', $index->fields ) || '';
204
205                 push @html,
206                     $q->Tr({ -class => 'IndexCell' },
207                         $q->td( [ $name, $fields ] )
208                     );
209             }
210
211             push @html, $q->end_table;
212         }
213
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
240         push @html, $q->hr;
241     }
242
243     my $sqlt_version = $t->version;
244     if ($wrap) {
245         push @html,
246             qq[Created by <a href="http://sqlfairy.sourceforge.net">],
247             qq[SQL::Translator $sqlt_version</a>],
248             $q->end_html;
249     }
250
251
252     return join "\n", @html;
253 }
254
255 1;
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
265 SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
266
267 =head1 SYNOPSIS
268
269   use SQL::Translator::Producer::HTML;
270
271 =head1 DESCRIPTION
272
273 Creates an HTML document describing the tables.
274
275 The HTML produced is composed of a number of tables:
276
277 =over 4
278
279 =item Links
280
281 A link table sits at the top of the output, and contains anchored
282 links to elements in the rest of the document.
283
284 If the I<nolinktable> producer arg is present, then this table is not
285 produced.
286
287 =item Tables
288
289 Each table in the schema has its own HTML table.  The top row is a row
290 of E<lt>thE<gt> elements, with a class of B<FieldHeader>; these
291 elements are I<Field Name>, I<Data Type>, I<Size>, I<Default Value>,
292 I<Other> and I<Foreign Key>.  Each successive row describes one field
293 in the table, and has a class of B<FieldCell$item>, where $item id
294 corresponds 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
325 Unless the I<nowrap> producer arg is present, the HTML will be
326 enclosed in a basic HTML header and footer.
327
328 If the I<pretty> producer arg is present, the generated HTML will be
329 nicely spaced and human-readable.  Otherwise, it will have very little
330 insignificant whitespace and be generally smaller.
331
332
333 =head1 AUTHORS
334
335 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
336 Darren Chamberlain E<lt>darren@cpan.orgE<gt>.
337
338 =cut