From: Matt S Trout Date: Tue, 24 Jan 2006 19:55:38 +0000 (+0000) Subject: Updated columns_info_for patch from zby X-Git-Tag: v0.05005~117^2~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=commitdiff_plain;h=103e3e038c76f0665051b5f9aeabee2e888c8a15 Updated columns_info_for patch from zby --- diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index b59710a..42f6d38 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -137,6 +137,8 @@ Daniel Westermann-Clark Alexander Hartmaier +Zbigniew Lukasiak + =head1 LICENSE You may distribute this code under the same terms as Perl itself. diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 4291c8b..efa4208 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -314,22 +314,24 @@ Returns database type info for a given table columns. sub columns_info_for { my ($self, $table) = @_; - my $sth = $self->dbh->prepare("SELECT * FROM $table WHERE 1=0"); - $sth->execute; my %result; - my @columns = @{$sth->{NAME}}; - for my $i ( 0 .. $#columns ){ - my $type = $sth->{TYPE}->[$i]; - my $info = $self->dbh->type_info($type); - my %column_info; - if ( $info ){ + if ( $self->dbh->can( 'column_info' ) ){ + my $sth = $self->dbh->column_info( undef, undef, $table, '%' ); + $sth->execute(); + while ( my $info = $sth->fetchrow_hashref() ){ + my %column_info; $column_info{data_type} = $info->{TYPE_NAME}; $column_info{size} = $info->{COLUMN_SIZE}; $column_info{is_nullable} = $info->{NULLABLE}; - }else{ - $column_info{data_type} = $type; + $result{$info->{COLUMN_NAME}} = \%column_info; + } + }else{ + my $sth = $self->dbh->prepare("SELECT * FROM $table WHERE 1=0"); + $sth->execute; + my @columns = @{$sth->{NAME}}; + for my $i ( 0 .. $#columns ){ + $result{$columns[$i]}{data_type} = $sth->{TYPE}->[$i]; } - $result{$columns[$i]} = \%column_info; } return \%result; } diff --git a/t/run/11mysql.tl b/t/run/11mysql.tl index ab5d2db..2411b96 100644 --- a/t/run/11mysql.tl +++ b/t/run/11mysql.tl @@ -16,7 +16,7 @@ my $dbh = MySQLTest->schema->storage->dbh; $dbh->do("DROP TABLE IF EXISTS artist;"); -$dbh->do("CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255));"); +$dbh->do("CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), charfield CHAR(10));"); #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', ''); @@ -43,15 +43,20 @@ is( $it->next, undef, "next past end of resultset ok" ); my $test_type_info = { 'artistid' => { - 'data_type' => 'integer', - 'is_nullable' => 1, - 'size' => 10 + 'data_type' => 'INT', + 'is_nullable' => 0, + 'size' => 11 }, 'name' => { - 'data_type' => 'varchar', + 'data_type' => 'VARCHAR', 'is_nullable' => 1, 'size' => 255 - } + }, + 'charfield' => { + 'data_type' => 'VARCHAR', + 'is_nullable' => 1, + 'size' => 10 + }, }; diff --git a/t/run/12pg.tl b/t/run/12pg.tl index c39dab5..22c4008 100644 --- a/t/run/12pg.tl +++ b/t/run/12pg.tl @@ -14,7 +14,7 @@ DBICTest::Schema->compose_connection('PgTest' => $dsn, $user, $pass); my $dbh = PgTest->schema->storage->dbh; -$dbh->do("CREATE TABLE artist (artistid serial PRIMARY KEY, name VARCHAR(255));"); +$dbh->do("CREATE TABLE artist (artistid serial PRIMARY KEY, name VARCHAR(255), charfield CHAR(10));"); PgTest::Artist->load_components('PK::Auto::Pg'); @@ -28,15 +28,20 @@ is($new->artistid, 2, "Auto-PK worked"); my $test_type_info = { 'artistid' => { - 'data_type' => 'int4', - 'is_nullable' => 1, - 'size' => 10 + 'data_type' => 'integer', + 'is_nullable' => 0, + 'size' => 4 }, 'name' => { - 'data_type' => 'text', + 'data_type' => 'character varying', + 'is_nullable' => 1, + 'size' => 255 + }, + 'charfield' => { + 'data_type' => 'character', 'is_nullable' => 1, - 'size' => 4096 - } + 'size' => 10 + }, }; my $type_info = PgTest->schema->storage->columns_info_for('artist');