take out duplicate docs
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / POD.pm
1 package SQL::Translator::Producer::POD;
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 =head1 NAME
22
23 SQL::Translator::Producer::POD - POD producer for SQL::Translator
24
25 =head1 SYNOPSIS
26
27   use SQL::Translator;
28
29   my $t = SQL::Translator->new( parser => '...', producer => 'POD', '...' );
30   print $t->translate;
31
32 =head1 DESCRIPTION
33
34 Creates a POD description of each table, field, index, and constraint.
35 A good starting point for text documentation of a schema.  You can
36 easily convert the output to HTML or text using "perldoc" or other
37 interesting formats using Pod::POM or Template::Toolkit's POD plugin.
38
39 =cut
40
41 use strict;
42 use vars qw[ $VERSION ];
43 $VERSION = '1.59';
44
45 use SQL::Translator::Schema::Constants;
46 use SQL::Translator::Utils qw(header_comment);
47
48 sub produce {
49     my $t           = shift;
50     my $schema      = $t->schema;
51     my $schema_name = $schema->name || 'Schema';
52     my $args        = $t->producer_args;
53     my $title       = $args->{'title'} || $schema_name;
54
55     my $pod = "=pod\n\n=head1 DESCRIPTION\n\n$title\n\n=head1 TABLES\n\n";
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         $pod .= "=head2 $table_name\n\n=head3 FIELDS\n\n";
61
62         #
63         # Fields
64         #
65         for my $field ( @fields ) {
66             $pod .= "=head4 " . $field->name . "\n\n=over 4\n\n";
67
68             my $data_type = $field->data_type;
69             my $size      = $field->size;
70             $data_type   .= "($size)" if $size;
71
72             $pod .= "=item * $data_type\n\n";
73             $pod .= "=item * PRIMARY KEY\n\n" if $field->is_primary_key;
74
75             my $default = $field->default_value;
76             $pod .= "=item * Default '$default' \n\n" if defined $default;
77
78             $pod .= sprintf( "=item * Nullable '%s' \n\n",
79                 $field->is_nullable ? 'Yes' : 'No' );
80
81             $pod .= "=back\n\n";
82         }
83
84         #
85         # Indices
86         #
87         if ( my @indices = $table->get_indices ) {
88             $pod .= "=head3 INDICES\n\n";
89             for my $index ( @indices ) {
90                 $pod .= "=head4 " . $index->type . "\n\n=over 4\n\n";
91                 $pod .= "=item * Fields = " .
92                     join(', ', $index->fields ) . "\n\n";
93                 $pod .= "=back\n\n";
94             }
95         }
96
97         #
98         # Constraints
99         #
100         if ( my @constraints = $table->get_constraints ) {
101             $pod .= "=head3 CONSTRAINTS\n\n";
102             for my $c ( @constraints ) {
103                 $pod .= "=head4 " . $c->type . "\n\n=over 4\n\n";
104                 $pod .= "=item * Fields = " .
105                     join(', ', $c->fields ) . "\n\n";
106
107                 if ( $c->type eq FOREIGN_KEY ) {
108                     $pod .= "=item * Reference Table = L</" .
109                         $c->reference_table . ">\n\n";
110                     $pod .= "=item * Reference Fields = " .
111                         join(', ', map {"L</$_>"} $c->reference_fields ) .
112                         "\n\n";
113                 }
114
115                 if ( my $update = $c->on_update ) {
116                     $pod .= "=item * On update = $update\n\n";
117                 }
118
119                 if ( my $delete = $c->on_delete ) {
120                     $pod .= "=item * On delete = $delete\n\n";
121                 }
122
123                 $pod .= "=back\n\n";
124             }
125         }
126     }
127
128     my $header = ( map { $_ || () } split( /\n/, header_comment('', '') ) )[0];
129        $header =~ s/^Created by //;
130     $pod .= "=head1 PRODUCED BY\n\n$header\n\n=cut";
131
132     return $pod;
133 }
134
135 1;
136
137 # -------------------------------------------------------------------
138 # Expect poison from the standing water.
139 # William Blake
140 # -------------------------------------------------------------------
141
142 =pod
143
144 =head1 AUTHOR
145
146 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
147
148 =head2 CONTRIBUTORS
149
150 Jonathan Yu E<lt>frequency@cpan.orgE<gt>
151
152 =head1 SEE ALSO
153
154 perldoc, perlpod, Pod::POM, Template::Manual::Plugins.
155
156 =cut