MySQL-TableInfo
view release on metacpan or search on metacpan
TableInfo.pm view on Meta::CPAN
package MySQL::TableInfo;
use strict;
use Carp;
our $VERSION = '1.01';
####
# takes care of plural forms of methods and 'get_' prefix
####################
#sub AUTOLOAD { }
####
# initializes the object with table information
####################
sub _init {
my ($self, $table) = @_;
$self->{"_data"} = $self->{"_dbh"}->selectall_arrayref(qq/SHOW COLUMNS FROM $table/);
unless ( $self->{"_data"} ) {
croak "Table '$table' doesn't seem to exist";
}
$self->{"table_info"} = { };
foreach my $row ( @{ $self->{"_data"} } ) {
$self->{"table_info"}->{ $row->[0] } = $row;
}
}
####
# constructor method
####################
sub new {
my ($class, $dbh, $table) = @_;
unless ($dbh && $table) {
croak <<'END_OF_USAGE';
new() was called with insufficient arguments
Usage:
MySQL::TableInfo->new($dbh, $table)
END_OF_USAGE
}
$class = ref($class) || $class;
my $self = {
_dbh => $dbh,
_data => [],
table_info => {},
};
bless $self => $class;
$self->_init($table);
return $self;
}
####
# desctructor method
####################
sub DESTROY { }
####
# gets the column name or names
####################
sub column {
my ($self, $column) = @_;
if ($column) {
return @{$self->{table_info}{$column}};
}
return keys %{$self->{table_info}};
}
####
# gets the size of the column
####################
sub size {
my ($self, $col) = @_;
if ( $self->{table_info}{$col}[1] =~ m/\((\d+)\)/ ) {
return $1;
}
return undef;
}
####
# gets the type of the column
####################
sub type {
my ($self, $col) = @_;
if ($self->{table_info}{$col}[1] =~ m/(\w+)/) {
return $1;
( run in 1.058 second using v1.01-cache-2.11-cpan-39bf76dae61 )