1 package SQL::Translator::Producer::HTML;
3 # -------------------------------------------------------------------
4 # $Id: HTML.pm,v 1.8 2003-08-20 14:32:49 dlc Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
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.
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.
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
21 # -------------------------------------------------------------------
25 use vars qw[ $VERSION ];
26 $VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
28 use SQL::Translator::Schema::Constants;
29 use SQL::Translator::Utils qw(header_comment);
31 # -------------------------------------------------------------------
34 my $schema = $t->schema;
35 my $schema_name = $schema->name || 'Schema';
36 my $args = $t->producer_args;
37 my $q = defined $args->{'pretty'}
38 ? do { require CGI::Pretty;
44 my $title = $args->{'title'} || "Description of $schema_name";
46 my $html = $q->start_html(
47 { -title => $title, -bgcolor => 'lightgoldenrodyellow' }
48 ) . $q->h1( $title ). '<a name="top">', $q->hr;
50 if ( my @table_names = map { $_->name } $schema->get_tables ) {
51 $html .= $q->start_table( { -width => '100%' } ).
52 $q->Tr( { -bgcolor => 'khaki' }, $q->td( $q->h2('Tables') ) );
54 for my $table ( @table_names ) {
55 $html .= $q->Tr( $q->td( qq[<a href="#$table">$table</a>] ) );
57 $html .= $q->end_table;
60 for my $table ( $schema->get_tables ) {
61 my $table_name = $table->name or next;
62 my @fields = $table->get_fields or next;
66 { -bgcolor => 'khaki' },
67 $q->td( $q->h3( $table_name ) ) . qq[<a name="$table_name">],
68 $q->td( { -align => 'right' }, qq[<a href="#top">Top</a>] )
72 if ( my @comments = $table->comments ) {
73 $html .= '<b>Comments:</b><br><em>'.join('<br>', @comments).'</em>';
79 $html .= $q->start_table( { -border => 1 } ) . $q->Tr(
80 { -bgcolor => 'lightgrey' },
91 for my $field ( @fields ) {
92 my $name = $field->name;
93 $name = qq[<a name="$table_name-$name">$name</a>];
94 my $data_type = $field->data_type;
95 my $size = $field->size;
96 my $default = $field->default_value;
97 my $comment = $field->comments || '';
100 if ( $field->is_foreign_key ) {
101 my $c = $field->foreign_key_reference;
102 my $ref_table = $c->reference_table || '';
103 my $ref_field = ($c->reference_fields)[0];
105 qq[<a href="#$ref_table-$ref_field">$ref_table.$ref_field</a>];
109 push @other, 'PRIMARY KEY' if $field->is_primary_key;
110 push @other, 'UNIQUE' if $field->is_unique;
111 push @other, 'NOT NULL' unless $field->is_nullable;
112 push @other, $comment if $comment;
113 $html .= $q->Tr( $q->td(
114 { -bgcolor => 'white' },
115 [ $name, $data_type, $size, $default, join(', ', @other), $fk ]
118 $html .= $q->end_table;
123 if ( my @indices = $table->get_indices ) {
124 $html .= $q->h3('Indices');
125 $html .= $q->start_table( { -border => 1 } ) . $q->Tr(
126 { -bgcolor => 'lightgrey' },
127 $q->th( [ 'Name', 'Fields' ] )
130 for my $index ( @indices ) {
132 { -bgcolor => 'white' },
133 $q->td( [ $index->name, join( ', ', $index->fields ) ] )
137 $html .= $q->end_table;
143 $html .= qq[Created by <a href="http://sqlfairy.sourceforge.net">].
144 qq[SQL::Translator</a>];
151 # -------------------------------------------------------------------
152 # Always be ready to speak your mind,
153 # and a base man will avoid you.
155 # -------------------------------------------------------------------
159 SQL::Translator::Producer::HTML - HTML producer for SQL::Translator
163 use SQL::Translator::Producer::HTML;
167 Creates an HTML document describing the tables.
171 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>