MySQL-TableInfo

 view release on metacpan or  search on metacpan

TableInfo.pm  view on Meta::CPAN

##---Note:----------
# currently not implemented
#-------------------
sub _valid_set {
    my ($self, $param) = @_;

    return 1;
}


#---
# dumps the object into __PACKAGE__.dmp file
# for debugging purposes
#-------------------
sub _dump {
    my $self = shift;

    require Data::Dumper;

    open DATA, ">".__PACKAGE__.".dmp" or die "Couldn't dump: $!\n";
    print DATA Dumper($self);
    close DATA;

}



1;

__END__
# Below is stub documentation for the library

=head1 NAME

MySQL::TableInfo - Perl extension for getting access into mysql's column information.

=head1 RATIONALE

The idea was taken from Paul DuBois' "MySQL and Perl for the Web" book. I searched the CPAN
but failed to find any module that does the similar task and thought of putting one
together and upload to CPAN. And  here it is.

=head1 NOTE

The library has been tested on MySQL version 3.23.40

=head1 SYNOPSIS

    use CGI;
    use DBI;
    use MySQL::TableInfo;

    my $CGI = new CGI:
    my $dbh = DBI->connect(....);
    my $table = new MySQL::TableInfo($dbh, "bio");

    print $CGI->header,
        $CGI->start_html("MySQL::TableInfo"),
        $CGI->start_form,
        $CGI->div("Do you have beard?"),
        $CGI->popup_menu(-name=>'has_beard',
                         -values=>[$table->enum('has_beard')],
                         -default=>$table->default('has_beard')),
    $CGI->end_form,
    $CGI->end_html;


=head1 DESCRIPTION

MySQL::TableInfo is a handy class for getting easy access to MySQL tables' descriptions
which is available via

    DESCRIBE table_name, SHOW COLUMNS FROM table_name

queries. It's also handy for constructing form based CGI applications to control HTML forms'
attributes such as C<VALUE>, C<SIZE>, C<MAXLENGTH>, C<TYPE> and so forth.
For example, if you have a ENUM('Yes', 'No') column in your mysql table, then you normally
would present it either as a group of radio buttons, or as a <SELECT> menu. If you modify
the column, and add one more option, ENUM('Yes', 'No', 'N/A'), then you will have to
re-write your html code accordingly. By using MySQL::TableInfo, you can avoide this double
troubles. Consider the following code:

    use CGI;
    use DBI;
    use MySQL::TableInfo;

    my $CGI = new CGI:
    my $dbh = DBI->connect(....);
    my $table = new MySQL::TableInfo($dbh, "bio");

    print $CGI->header, $CGI->start_html("MySQL::TableInfo");

    print $CGI->start_form,
        $CGI->div("Do you wear beard?"),
        $CGI->checkbox_group( -name=>'has_beard',
                              -values=>[$table->set('has_beard')],
                              -default=>$table->default('has_beard')),
    $CGI->end_form;

    print $CGI->end_html;

As you see, modifying 'has_beard' column, which is an enumeration column, whould
reflect in your CGI too.

=head1 METHODS

=over 4

=item C<new($dbh, 'table_name')>

constructor method. The two reguired arguments are database handle ($dbh) returned from DBI->connect(), and the name of the mysql table to work with. Since you create the $dbh with the database name, it is not required to pass the database name to C<...

    my $table = new MySQL::TableInfo($dbh, "database.table_name");

=item C<column([$column_name])>

if invoked with a column name returns an array consisting of all the column's attributes. If the argument is missing returns an array consisting table's all the columns. For example, the following code prints all the column names:

    foreach my $col ($table->column) {
        print "$col\n";
    }



( run in 0.702 second using v1.01-cache-2.11-cpan-364913b4093 )