DBIx-HTML

 view release on metacpan or  search on metacpan

lib/DBIx/HTML.pm  view on Meta::CPAN

sub do {
    my $self = shift;
    my ($sql, $args) = @_;

    croak "can't call do(): no database handle" unless $self->{dbh};

    eval {
        $self->{sth} = $self->{dbh}->prepare( $sql );
        $self->{sth}->execute( @$args );
    };
    croak $@ and return undef if $@;

    $self->{generator}{data} = [ $self->{sth}{NAME}, @{ $self->{sth}->fetchall_arrayref } ];
    return $self;
}

sub AUTOLOAD {
    my $self = shift;
    croak "must connect() first" unless $self->isa( __PACKAGE__ );
    (my $method = $AUTOLOAD) =~ s/.*:://;
    croak "no such method $method for " . ref($self->{generator}) unless $self->{generator}->can( $method );
    return $self->{generator}->$method( @_ );
} 

sub DESTROY {
    my $self = shift;
    if (!$self->{your_db} and $self->{dbh}->isa( 'DBI::db' )) {
        $self->{dbh}->disconnect();
    }
}

1;
__END__
=head1 NAME

DBIx::HTML - Just another HTML table generating DBI extension.

=head1 SYNOPSIS

    use DBIx::HTML;

    my $generator = DBIx::HTML->connect( @db_credentials );
    $generator->do( $query );

    # supports multiple orientations
    print $generator->portrait;
    print $generator->landscape;

    # stackable method calls:
    print DBIx::HTML
        ->connect( @db_credentials )
        ->do( 'select foo,baz from bar' )
        ->landscape
    ;

    # rotating attributes:
    print $generator->portrait( tr => { class => [qw( odd even )] } );

=head1 DESCRIPTION

Generate HTML tables from database queries (HTML4, XHTML and HTML5).
Can generate landscape and other rotated views, Handsontable tables,
checkboard patterns, and can create animations of cell values and
backgrounds via jQuery.

Connect to the database and issue a query. The result will be
an HTML HTML5 or XHTML table containing the query results wrapped
in <td> tags and headings wrapped in <th> tags. Headings values have
the first character in each word upper cased, with underscores replaced
by spaces. All automatic settings can be overridden. For example, if
you do not want the headings to be automatically styled, you can remove
them like so:

  print $generator->portrait( headings => undef );

  # or add your own styling
  print $generator->portrait( headings => sub { uc shift } );

This module uses Spreadsheet::HTML to generate the tables. See
L<Spreadsheet::HTML> for further documentation on customizing
the table output.

=head1 METHODS

=over 4

=item C<connect( @database_credentials )>

Connects to the database. See L<DBI> for how to do that.
Optionally, create your own database handle and pass it:

  my $dbh = DBI->connect ( @db_creds );
  my $generator = DBIx::HTML->connect( $dbh );

  # do stuff and then finally ...
  $dbh->disconnect;

DBIx::HTML will not disconnect your database handle.

=item C<do( $query )>

Executes the SQL query, fetches the results and stores them internally.

=back

=head1 SPREADSHEET::HTML METHODS

All methods from Spreadsheet::HTML are delegated. Simply call
any one of the methods provided and supply your own arguments.
For example, to group table rows into respective <thead>, <tbody>
and <tfoot> sections:

  print $generator->portrait( tgroups => 2 ); # why 2? answer in the docs!

Or perhaps you want to wrap headings with <td> instead of <th>:

  print $generator->portrait( matrix => 1 );

Or have some fun:

  print $generator->conway;

See L<Spreadsheet::HTML> for full documentation for these methods and
the named parameters they accept as arguments.

=head1 SEE ALSO



( run in 0.661 second using v1.01-cache-2.11-cpan-3d66aa2751a )