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