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