=head1 NAME

       A2E::Sqltab

=head1 Version

0.0.1

=cut

our $VERSION = '0.0.1';

=head2 DESCRIPTION

Provide the sqltab function.

Could become a mix-in library, but at the moment is not because it has
overload functions.  These should probably be transferred to the application sqltab(1).

Other mix-in libraries include

    A2E::Lokvars
    A2E::Osver

The only commandline application is

    sqltab(1)

=head1 IMPLEMENTATION

=head2 Header

=cut
 
package A2E::Sqltab;
use base 'A2E::Daba';
use strict;

=head2 Overload Functions

=cut

sub defvars {
    my $m = shift;
    $m->dpuc('A2E::Sqltab::defvars', @_);
    $m->SUPER::defvars;
    $m->{konfig}->define('html_p|html|H!');
    $m->konfayl('sqltab.konf');
    $m->dpop('A2E::Sqltab::defvars', 1);return 1;
};

=head2 First Level Functions

=head3 function sqltab

generate a sql table in HTML --- usefule in an txt2html context but solved more elegantly by deplate.

=cut

sub sqltab_html {
    my $m = shift;
    $m->dpuc('sqltab_html', @_);
    my $sql = shift;
    my @records = $m->get_records($sql) ;
    my ($row, $fld);
    my $ret = "<table>\n" . join("\n", map { "<tr>" . join('', map { "<td>$_</td>" } @$_) . "</tr>"; } @records) . "\n</table>";
    $m->dpop('sqltab_html', $ret);return $ret;
};

=head3 function sqltab_text

same for plain text, e.g. wiki source

=cut

sub sqltab_text {
    my $m = shift;
    $m->dpuc('sqltab_text', @_);
    my $sql = shift;
    my @records = $m->get_records($sql) ;
    my $ret = join "\n", map { join " | ", @$_ } @records;
    $m->dpop('sqltab_text', $ret);return $ret;    
};

=head2 function sqltab

=cut

sub sqltab {
    my $m = shift;
    $m->dpuc('sqltab', @_);
    my $ret = $m->{konfig}->html_p ? $m->sqltab_html(@_) : $m->sqltab_text(@_);
    $m->dpop('sqltab', $ret);return $ret;    
};

=head1 The End

=cut

return 1;

# Local Variables:
# coding: utf-8
# srcfile: /adv/perl/A2E/Sqltab.pm
# mode: perl
# End:
