From f9fa530207bea1ce922979c469bf7ec4353c2a1c Mon Sep 17 00:00:00 2001 From: Kenichi Ishigaki Date: Wed, 16 Sep 2009 04:16:36 +0000 Subject: [PATCH] DBD-SQLite: applied a new patch by VLYON (#49716) --- lib/DBD/SQLite.pm | 5 +++-- t/16_column_info.t | 35 +++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/DBD/SQLite.pm b/lib/DBD/SQLite.pm index 51e6d54..8329042 100644 --- a/lib/DBD/SQLite.pm +++ b/lib/DBD/SQLite.pm @@ -488,13 +488,14 @@ END_SQL } my %col = ( + TABLE_SCHEM => $schema, TABLE_NAME => $table, COLUMN_NAME => $col_info->{name}, ORDINAL_POSITION => $position, ); my $type = $col_info->{type}; - if ( $type =~ s/(\w+)\((\d+)(?:,(\d+))?\)/$1/ ) { + if ( $type =~ s/(\w+) ?\((\d+)(?:,(\d+))?\)/$1/ ) { $col{COLUMN_SIZE} = $2; $col{DECIMAL_DIGITS} = $3; } @@ -513,7 +514,7 @@ END_SQL $col{IS_NULLABLE} = 'YES'; } - push @cols, \%col; ++ push @cols, \%col; } $sth_columns->finish; } diff --git a/t/16_column_info.t b/t/16_column_info.t index 888b6d4..9115658 100644 --- a/t/16_column_info.t +++ b/t/16_column_info.t @@ -7,11 +7,12 @@ BEGIN { } use t::lib::Test; -use Test::More tests => 10; +use Test::More tests => 12; use Test::NoWarnings; my $dbh = DBI->connect('dbi:SQLite:dbname=:memory:',undef,undef,{RaiseError => 1}); +# 1. Create a table ok( $dbh->do(<<'END_SQL'), 'Created test table' ); CREATE TABLE test ( id INTEGER PRIMARY KEY NOT NULL, @@ -19,6 +20,7 @@ ok( $dbh->do(<<'END_SQL'), 'Created test table' ); ); END_SQL +# 2. Create a temporary table ok( $dbh->do(<<'END_SQL'), 'Created temp test table' ); CREATE TEMP TABLE test2 ( id INTEGER PRIMARY KEY NOT NULL, @@ -26,14 +28,27 @@ ok( $dbh->do(<<'END_SQL'), 'Created temp test table' ); ); END_SQL +# 3. Attach a memory database +ok( $dbh->do('ATTACH DATABASE ":memory:" AS db3'), 'ATTACH DATABASE ":memory:" AS db3' ); + +# 4. Create a table on the attached database +ok( $dbh->do(<<'END_SQL'), 'CREATE TABLE db3.three' ); + CREATE TABLE db3.three ( + id INTEGER NOT NULL, + name CHAR (64) NOT NULL + ) +END_SQL + +# 5. No errors from column_info() my $sth = $dbh->column_info(undef, undef, 'test', undef); is $@, '', 'No error creating the table'; +# 6. Get column information ok $sth, 'We can get column information'; my %expected = ( TYPE_NAME => [qw( INTEGER VARCHAR )], - COLUMN_NAME => [qw( ID NAME )], + COLUMN_NAME => [qw( id name )], ); SKIP: { @@ -41,23 +56,27 @@ SKIP: { my $info = $sth->fetchall_arrayref({}); + # 7. Found 2 columns is( scalar @$info, 2, 'We got information on two columns' ); foreach my $item (qw( TYPE_NAME COLUMN_NAME )) { - my @info = map { uc $_->{$item} } (@$info); + my @info = map { $_->{$item} } (@$info); is_deeply( \@info, $expected{$item}, "We got the right info in $item" ); } - $info = $dbh->column_info(undef, undef, 'test%', '%a%')->fetchall_arrayref({}); + $info = $dbh->column_info(undef, undef, 't%', '%a%')->fetchall_arrayref({}); - is( scalar @$info, 2, 'We matched information from multiple databases' ); + # 10. Found 3 columns + is( scalar @$info, 3, 'We matched information from multiple databases' ); - my @fields = qw( TYPE_NAME COLUMN_NAME COLUMN_SIZE ); + my @fields = qw( TABLE_SCHEM TYPE_NAME COLUMN_NAME COLUMN_SIZE NULLABLE ); my @info = map [ @$_{@fields} ], @$info; my $expected = [ - [ 'VARCHAR', 'name', 255 ], - [ 'INTEGER', 'flag', undef ] + [ 'db3', 'CHAR', 'name', 64, 0 ], + [ 'main', 'VARCHAR', 'name', 255, 1 ], + [ 'temp', 'INTEGER', 'flag', undef, 1 ] # TODO: column_info should always return a valid COLUMN_SIZE ]; + # 11. Correct info retrieved is_deeply( \@info, $expected, 'We got the right info from multiple databases' ); }