Updated MANIFEST.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / HTML.pm
1 package SQL::Translator::Producer::HTML;
2
3 # -------------------------------------------------------------------
4 # $Id: HTML.pm,v 1.7 2003-08-20 13:50:47 dlc Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
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 CGI;
25 use Data::Dumper;
26 use vars qw[ $VERSION ];
27 $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
28
29 use SQL::Translator::Schema::Constants;
30 use SQL::Translator::Utils qw(header_comment);
31
32 # -------------------------------------------------------------------
33 sub produce {
34     my $t           = shift;
35     my $schema      = $t->schema;
36     my $schema_name = $schema->name || 'Schema';
37     my $args        = $t->producer_args;
38     my $q           = defined $args->{'pretty'}
39                     ? do { require CGI::Pretty; CGI::Pretty->new }
40                     : CGI->new;
41     my $title       = $args->{'title'} || "Description of $schema_name";
42
43     my $html  = $q->start_html( 
44         { -title => $title, -bgcolor => 'lightgoldenrodyellow' } 
45     ) .  $q->h1( $title ).  '<a name="top">', $q->hr;
46
47     if ( my @table_names = map { $_->name } $schema->get_tables ) {
48         $html .= $q->start_table( { -width => '100%' } ).
49             $q->Tr( { -bgcolor => 'khaki' }, $q->td( $q->h2('Tables') ) );
50
51         for my $table ( @table_names ) {
52             $html .= $q->Tr( $q->td( qq[<a href="#$table">$table</a>] ) );
53         }
54         $html .= $q->end_table;
55     }
56
57     for my $table ( $schema->get_tables ) {
58         my $table_name = $table->name or next;
59         my @fields     = $table->get_fields or next;
60         $html         .= $q->table( 
61             { -width => '100%' },
62             $q->Tr(
63                 { -bgcolor => 'khaki' },
64                 $q->td( $q->h3( $table_name ) ) . qq[<a name="$table_name">],
65                 $q->td( { -align => 'right' }, qq[<a href="#top">Top</a>] )
66             )
67         );
68
69         if ( my @comments = $table->comments ) {
70             $html .= '<b>Comments:</b><br><em>'.join('<br>', @comments).'</em>';
71         }
72
73         #
74         # Fields
75         #
76         $html .= $q->start_table( { -border => 1 } ) . $q->Tr(
77             { -bgcolor => 'lightgrey' },
78             $q->th( [ 
79                 'Field Name', 
80                 'Data Type', 
81                 'Size', 
82                 'Default', 
83                 'Other', 
84                 'Foreign Key' 
85             ] ) 
86         );
87
88         for my $field ( @fields ) {
89             my $name      = $field->name;
90                $name      = qq[<a name="$table_name-$name">$name</a>];
91             my $data_type = $field->data_type;
92             my $size      = $field->size;
93             my $default   = $field->default_value;
94             my $comment   = $field->comments || '';
95
96             my $fk;
97             if ( $field->is_foreign_key ) {
98                 my $c = $field->foreign_key_reference;
99                 my $ref_table = $c->reference_table || '';
100                 my $ref_field = ($c->reference_fields)[0];
101                 $fk = 
102                 qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>];
103             }
104
105             my @other;
106             push @other, 'PRIMARY KEY' if $field->is_primary_key;
107             push @other, 'UNIQUE'      if $field->is_unique;
108             push @other, 'NOT NULL'    unless $field->is_nullable;
109             push @other, $comment      if $comment;
110             $html .= $q->Tr( $q->td(
111                 { -bgcolor => 'white' },
112                 [ $name, $data_type, $size, $default, join(', ', @other), $fk ]
113             ) );
114         }
115         $html .= $q->end_table;
116
117         #
118         # Indices
119         #
120         if ( my @indices = $table->get_indices ) {
121             $html .= $q->h3('Indices');
122             $html .= $q->start_table( { -border => 1 } ) . $q->Tr(
123                 { -bgcolor => 'lightgrey' }, 
124                 $q->th( [ 'Name', 'Fields' ] ) 
125             );
126
127             for my $index ( @indices ) {
128                 $html .= $q->Tr( 
129                     { -bgcolor => 'white' },
130                     $q->td( [ $index->name, join( ', ', $index->fields ) ] )
131                 );
132             }
133
134             $html .= $q->end_table;
135         }
136
137         $html .= $q->hr;
138     }
139
140     $html .= qq[Created by <a href="http://sqlfairy.sourceforge.net">].
141         qq[SQL::Translator</a>];
142
143     return $html;
144 }
145
146 1;
147
148 # -------------------------------------------------------------------
149 # Always be ready to speak your mind,
150 # and a base man will avoid you.
151 # William Blake
152 # -------------------------------------------------------------------
153
154 =head1 NAME
155
156 SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
157
158 =head1 SYNOPSIS
159
160   use SQL::Translator::Producer::HTML;
161
162 =head1 DESCRIPTION
163
164 Creates an HTML document describing the tables.
165
166 =head1 AUTHOR
167
168 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
169
170 =cut