| 1 | package SQL::Translator::Producer::POD; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | SQL::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 | |||
| 16 | Creates a POD description of each table, field, index, and constraint. | ||
| 17 | A good starting point for text documentation of a schema. You can | ||
| 18 | easily convert the output to HTML or text using "perldoc" or other | ||
| 19 | interesting formats using Pod::POM or Template::Toolkit's POD plugin. | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use warnings; | ||
| 25 | our $VERSION = '1.66'; | ||
| 26 | |||
| 27 | use SQL::Translator::Schema::Constants; | ||
| 28 | use SQL::Translator::Utils qw(header_comment); | ||
| 29 | |||
| 30 | sub produce { | ||
| 31 | my $t = shift; | ||
| 32 | my $schema = $t->schema; | ||
| 33 | my $schema_name = $schema->name || 'Schema'; | ||
| 34 | my $args = $t->producer_args; | ||
| 35 | my $title = $args->{'title'} || $schema_name; | ||
| 36 | |||
| 37 | my $pod = "=pod\n\n=head1 DESCRIPTION\n\n$title\n\n=head1 TABLES\n\n"; | ||
| 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 | # | ||
| 47 | for my $field (@fields) { | ||
| 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", $field->is_nullable ? 'Yes' : 'No'); | ||
| 61 | |||
| 62 | $pod .= "=back\n\n"; | ||
| 63 | } | ||
| 64 | |||
| 65 | # | ||
| 66 | # Indices | ||
| 67 | # | ||
| 68 | if (my @indices = $table->get_indices) { | ||
| 69 | $pod .= "=head3 INDICES\n\n"; | ||
| 70 | for my $index (@indices) { | ||
| 71 | $pod .= "=head4 " . $index->type . "\n\n=over 4\n\n"; | ||
| 72 | $pod .= "=item * Fields = " . join(', ', $index->fields) . "\n\n"; | ||
| 73 | $pod .= "=back\n\n"; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | # | ||
| 78 | # Constraints | ||
| 79 | # | ||
| 80 | if (my @constraints = $table->get_constraints) { | ||
| 81 | $pod .= "=head3 CONSTRAINTS\n\n"; | ||
| 82 | for my $c (@constraints) { | ||
| 83 | $pod .= "=head4 " . $c->type . "\n\n=over 4\n\n"; | ||
| 84 | if ($c->type eq CHECK_C) { | ||
| 85 | $pod .= "=item * Expression = " . $c->expression . "\n\n"; | ||
| 86 | } else { | ||
| 87 | $pod .= "=item * Fields = " . join(', ', $c->fields) . "\n\n"; | ||
| 88 | |||
| 89 | if ($c->type eq FOREIGN_KEY) { | ||
| 90 | $pod .= "=item * Reference Table = L</" . $c->reference_table . ">\n\n"; | ||
| 91 | $pod .= "=item * Reference Fields = " . join(', ', map {"L</$_>"} $c->reference_fields) . "\n\n"; | ||
| 92 | } | ||
| 93 | |||
| 94 | if (my $update = $c->on_update) { | ||
| 95 | $pod .= "=item * On update = $update\n\n"; | ||
| 96 | } | ||
| 97 | |||
| 98 | if (my $delete = $c->on_delete) { | ||
| 99 | $pod .= "=item * On delete = $delete\n\n"; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | $pod .= "=back\n\n"; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | my $header = (map { $_ || () } split(/\n/, header_comment('', '')))[0]; | ||
| 109 | $header =~ s/^Created by //; | ||
| 110 | $pod .= "=head1 PRODUCED BY\n\n$header\n\n=cut"; | ||
| 111 | |||
| 112 | return $pod; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | # ------------------------------------------------------------------- | ||
| 118 | # Expect poison from the standing water. | ||
| 119 | # William Blake | ||
| 120 | # ------------------------------------------------------------------- | ||
| 121 | |||
| 122 | =pod | ||
| 123 | |||
| 124 | =head1 AUTHOR | ||
| 125 | |||
| 126 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>. | ||
| 127 | |||
| 128 | =head2 CONTRIBUTORS | ||
| 129 | |||
| 130 | Jonathan Yu E<lt>frequency@cpan.orgE<gt> | ||
| 131 | |||
| 132 | =head1 SEE ALSO | ||
| 133 | |||
| 134 | perldoc, perlpod, Pod::POM, Template::Manual::Plugins. | ||
| 135 | |||
| 136 | =cut |