DBIx-XHTML_Table
view release on metacpan or search on metacpan
lib/DBIx/XHTML_Table.pm view on Meta::CPAN
my ($self,$ref) = @_;
$ref = undef if ref($ref) eq 'ARRAY' && scalar( @$ref ) < 1;
$ref = [@{$self->{'fields_arry'}}] unless defined $ref;
$ref = [$ref] unless ref $ref eq 'ARRAY';
return [map {$_ =~ /^\d+$/ ? $self->_lookup_name($_) || $_ : $_} @$ref];
}
sub _merge_attribs {
my ($hash1,$hash2) = @_;
return $hash1 unless $hash2;
return $hash2 unless $hash1;
return {%$hash2,%$hash1};
}
sub _lookup_name {
my ($self,$index) = @_;
return $self->{'fields_arry'}->[$index];
}
sub _lookup_index {
my ($self,$name) = @_;
return $self->{'fields_hash'}->{$name};
}
sub _reset_fields_hash {
my $self = shift;
my $i = 0;
$self->{fields_hash} = { map { $_ => $i++ } @{$self->{fields_arry}} };
}
# assigns a non-DBI supplied data table (2D array ref)
sub _do_black_magic {
my ($self,$ref,$headers) = @_;
croak "bad data" unless ref( $ref->[0] ) eq 'ARRAY';
$self->{'fields_arry'} = $headers ? [@$headers] : [ @{ shift @$ref } ];
$self->{'fields_hash'} = $self->_reset_fields_hash();
$self->{'rows'} = $ref;
}
# disconnect database handle if i created it
sub DESTROY {
my ($self) = @_;
unless ($self->{'keep_alive'}) {
$self->{'dbh'}->disconnect if defined $self->{'dbh'};
}
}
1;
__END__
=head1 NAME
DBIx::XHTML_Table - SQL query result set to XHTML table.
=head1 SYNOPSIS
use DBIx::XHTML_Table;
# database credentials - fill in the blanks
my ($data_source,$usr,$pass) = ();
my $table = DBIx::XHTML_Table->new($data_source,$usr,$pass);
$table->exec_query("
select foo from bar
where baz='qux'
order by foo
");
print $table->output();
# stackable method calls:
print DBIx::XHTML_Table
->new($data_source,$usr,$pass)
->exec_query('select foo,baz from bar')
->output();
# and much more - read on ...
=head1 DESCRIPTION
B<DBIx::XHTML_Table> is a DBI extension that creates an HTML
table from a database query result set. It was created to fill
the gap between fetching data from a database and transforming
that data into a web browser renderable table. DBIx::XHTML_Table is
intended for programmers who want the responsibility of presenting
(decorating) data, easily. This module is meant to be used in situations
where the concern for presentation and logic seperation is overkill.
Providing logic or editable data is beyond the scope of this module,
but it is capable of doing such.
=head1 CODE FREEZE
For the most part, no new functionality will be added to this module.
Only bug fixes and documentation corrections/additions. All new efforts
will be directed towards the rewrite of this distribution, B<DBIx::HTML>.
This distribution features a more flexible interface with fewer methods and
logically named argument parameters. At the core is an HTML attribute generator:
=over 4
=item * L<Tie::Hash::Attribute>
=back
Which is used by an HTML tag generator:
=over 4
=item * L<HTML::AutoTag>
=back
Which is used by an HTML table generator:
=over 4
=item * L<Spreadsheet::HTML>
=back
Which is finally wrapped by a DBI extension:
=over 4
=item * L<DBIx::HTML>
=back
=head1 WEBSITE
More documentation (tutorial, cookbook, FAQ, etc.) can be found at
http://www.unlocalhost.com/XHTML_Table/
=head1 GITHUB
https://github.com/jeffa/DBIx-XHTML_Table
=head1 CONSTRUCTOR
=over 4
=item B<style_1>
$obj_ref = new DBIx::XHTML_Table(@credentials[,$attribs])
Note - all optional arguments are denoted inside brackets.
The constructor will simply pass the credentials to the DBI::connect
method - read the DBI documentation as well as the docs for your
corresponding DBI driver module (DBD::Oracle, DBD::Sybase,
DBD::mysql, etc).
# MySQL example
my $table = DBIx::XHTML_Table->new(
'DBI:mysql:database:host', # datasource
'user', # user name
'password', # user password
) or die "couldn't connect to database";
The last argument, $attribs, is an optional hash reference
and should not be confused with the DBI::connect method's
similar 'attributes' hash reference.'
# valid example for last argument
my $attribs = {
table => {
border => 1,
cellspacing => 0,
rules => 'groups',
},
caption => 'Example',
td => {
style => 'text-align: right',
},
};
my $table = DBIx::XHTML_Table->new(
$data_source,$user,$pass,$attribs
) or die "couldn't connect to database";
But it is still experimental and unpleasantly limiting.
The purpose of $table_attribs is to bypass having to
call modify() multiple times. However, if you find
yourself calling modify() more than 4 or 5 times,
then DBIx::XHTML_Table might be the wrong tool. I recommend
HTML::Template or Template-Toolkit, both available at CPAN.
=item B<style_2>
$obj_ref = new DBIx::XHTML_Table($DBH[,$attribs])
The first style will result in the database handle being created
and destroyed 'behind the scenes'. If you need to keep the database
connection open after the XHTML_Table object is destroyed, then
create one yourself and pass it to the constructor:
my $dbh = DBI->connect(
$data_source,$usr,$passwd,
{RaiseError => 1},
);
my $table = DBIx::XHTML_Table->new($dbh);
# do stuff
$dbh->disconnect;
You can also use any class that isa() DBI::db object, such
as Apache::DBI or DBIx::Password objects:
( run in 0.633 second using v1.01-cache-2.11-cpan-5511b514fd6 )