File: POD.pm

package info (click to toggle)
libsql-translator-perl 0.11011-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,380 kB
  • sloc: perl: 251,748; sql: 3,805; xml: 233; makefile: 7
file content (138 lines) | stat: -rw-r--r-- 3,938 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package SQL::Translator::Producer::POD;

=head1 NAME

SQL::Translator::Producer::POD - POD producer for SQL::Translator

=head1 SYNOPSIS

  use SQL::Translator;

  my $t = SQL::Translator->new( parser => '...', producer => 'POD', '...' );
  print $t->translate;

=head1 DESCRIPTION

Creates a POD description of each table, field, index, and constraint.
A good starting point for text documentation of a schema.  You can
easily convert the output to HTML or text using "perldoc" or other
interesting formats using Pod::POM or Template::Toolkit's POD plugin.

=cut

use strict;
use warnings;
our $VERSION = '1.59';

use SQL::Translator::Schema::Constants;
use SQL::Translator::Utils qw(header_comment);

sub produce {
    my $t           = shift;
    my $schema      = $t->schema;
    my $schema_name = $schema->name || 'Schema';
    my $args        = $t->producer_args;
    my $title       = $args->{'title'} || $schema_name;

    my $pod = "=pod\n\n=head1 DESCRIPTION\n\n$title\n\n=head1 TABLES\n\n";

    for my $table ( $schema->get_tables ) {
        my $table_name = $table->name or next;
        my @fields     = $table->get_fields or next;
        $pod .= "=head2 $table_name\n\n=head3 FIELDS\n\n";

        #
        # Fields
        #
        for my $field ( @fields ) {
            $pod .= "=head4 " . $field->name . "\n\n=over 4\n\n";

            my $data_type = $field->data_type;
            my $size      = $field->size;
            $data_type   .= "($size)" if $size;

            $pod .= "=item * $data_type\n\n";
            $pod .= "=item * PRIMARY KEY\n\n" if $field->is_primary_key;

            my $default = $field->default_value;
            $pod .= "=item * Default '$default' \n\n" if defined $default;

            $pod .= sprintf( "=item * Nullable '%s' \n\n",
                $field->is_nullable ? 'Yes' : 'No' );

            $pod .= "=back\n\n";
        }

        #
        # Indices
        #
        if ( my @indices = $table->get_indices ) {
            $pod .= "=head3 INDICES\n\n";
            for my $index ( @indices ) {
                $pod .= "=head4 " . $index->type . "\n\n=over 4\n\n";
                $pod .= "=item * Fields = " .
                    join(', ', $index->fields ) . "\n\n";
                $pod .= "=back\n\n";
            }
        }

        #
        # Constraints
        #
        if ( my @constraints = $table->get_constraints ) {
            $pod .= "=head3 CONSTRAINTS\n\n";
            for my $c ( @constraints ) {
                $pod .= "=head4 " . $c->type . "\n\n=over 4\n\n";
                $pod .= "=item * Fields = " .
                    join(', ', $c->fields ) . "\n\n";

                if ( $c->type eq FOREIGN_KEY ) {
                    $pod .= "=item * Reference Table = L</" .
                        $c->reference_table . ">\n\n";
                    $pod .= "=item * Reference Fields = " .
                        join(', ', map {"L</$_>"} $c->reference_fields ) .
                        "\n\n";
                }

                if ( my $update = $c->on_update ) {
                    $pod .= "=item * On update = $update\n\n";
                }

                if ( my $delete = $c->on_delete ) {
                    $pod .= "=item * On delete = $delete\n\n";
                }

                $pod .= "=back\n\n";
            }
        }
    }

    my $header = ( map { $_ || () } split( /\n/, header_comment('', '') ) )[0];
       $header =~ s/^Created by //;
    $pod .= "=head1 PRODUCED BY\n\n$header\n\n=cut";

    return $pod;
}

1;

# -------------------------------------------------------------------
# Expect poison from the standing water.
# William Blake
# -------------------------------------------------------------------

=pod

=head1 AUTHOR

Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.

=head2 CONTRIBUTORS

Jonathan Yu E<lt>frequency@cpan.orgE<gt>

=head1 SEE ALSO

perldoc, perlpod, Pod::POM, Template::Manual::Plugins.

=cut