| Class-DBI-Plugin-Type documentation | Contained in the Class-DBI-Plugin-Type distribution. |
Class::DBI::Plugin::Type - Determine type information for columns
package Music::Artist;
use base 'Class::DBI';
use Class::DBI::Plugin::Type;
Music::Artist->table('artist');
Music::Artist->columns(All => qw/artistid name/);
print Music::Artist->column_type("artistid"); # integer
This module allows Class::DBI-based classes to query their columns
for data type information in a database-independent manner.
Simon Cozens, <simon@cpan.org>
Copyright 2004 by Simon Cozens
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This module was generously sponsored by the Perl Foundation.
| Class-DBI-Plugin-Type documentation | Contained in the Class-DBI-Plugin-Type distribution. |
package Class::DBI::Plugin::Type; use strict; use warnings; our $VERSION = '0.02'; sub import { no strict 'refs'; my $caller = caller(); #if ($caller->isa("Class::DBI::mysql") and # $caller->can("column_type")) { # return; # My work here is done #} return if $caller->can("sql_dummy"); $caller->set_sql(dummy => <<''); SELECT * FROM __TABLE__ WHERE 1=0 $caller->mk_classdata("_types"); *{$caller."::column_type"} = sub { my ($self, $column) = @_; if (!$self->_types) { my $sth = $self->sql_dummy; $sth->execute; my %hash; @hash{@{$sth->{NAME}}} = map { my $info = scalar $self->db_Main->type_info($_); if ($info) { $info->{TYPE_NAME} } else { $_ } # Typeless databases (SQLite) } @{$sth->{TYPE}}; $sth->finish; $self->_types(\%hash); } return $self->_types->{$column}; } } 1; __END__