# $Id$

use strict;
use warnings;
use English qw(-no_match_vars);

use Sympa::DatabaseDescription;

my %full_db_struct = Sympa::DatabaseDescription::full_db_struct();
my %indexes        = %Sympa::DatabaseDescription::indexes;
my @tables =
    sort { $a->{order} <=> $b->{order} }
    map { {name => $_, %{$full_db_struct{$_}},} } keys %full_db_struct;

my $pod = '';
foreach my $tbl (@tables) {
    my @fields =
        sort { $a->{order} <=> $b->{order} }
        map { {name => $_, %{$tbl->{fields}->{$_}},} } keys %{$tbl->{fields}};

    $pod .= sprintf "=head3 %s\n\n", $tbl->{name};
    $pod .= sprintf "%s\n\n",        $tbl->{doc};

    $pod .= "Fields:\n\n=over\n\n";
    foreach my $fld (@fields) {
        $pod .= sprintf "=item %s %s%s%s\n\n", $fld->{name}, $fld->{struct},
            (($fld->{not_null} and not $fld->{primary}) ? ' not null' : ''),
            ($fld->{autoincrement} ? ' auto_increment' : '');
        $pod .= "(Primary key)\n\n" if $fld->{primary};
        $pod .= sprintf "%s\n\n", $fld->{doc} if $fld->{doc};
    }
    $pod .= "=back\n\n";

    if (exists $indexes{$tbl->{name}}) {
        $pod .= "Indexes:\n\n=over\n\n";
        my $indexes = $indexes{$tbl->{name}};
        foreach my $name (sort keys %$indexes) {
            $pod .= sprintf "=item %s\n\n", $name;
            $pod .= sprintf "%s\n\n", join ', ', @{$indexes->{$name}};
        }
        $pod .= "=back\n\n";
    }
}

printf do { local $RS; <DATA> }, $pod;

__END__

=encoding utf-8

=head1 NAME

sympa_database - Structure of Sympa core database

=head1 DECRIPTION

Core database of Sympa is based on SQL.
In following list of tables and indexes, data types are based on
MySQL/MariaDB.  Corresponding types are used by other platforms
(PostgreSQL, SQLite, ...).

=head2 Tables

%s

=head1 SEE ALSO

I<Sympa Administration Manual>.
L<https://sympa-community.github.io/manual/>.

=cut
