mirror of
https://github.com/DBD-SQLite/DBD-SQLite
synced 2025-06-07 14:19:10 -04:00
applied a patch to fix table_info issue from Vlyon (#46831)
This commit is contained in:
parent
c2a9d9e7e5
commit
ae41bd45c1
4 changed files with 177 additions and 19 deletions
|
@ -151,16 +151,28 @@ sub get_info {
|
|||
return $v;
|
||||
}
|
||||
|
||||
sub _attached_database_list {
|
||||
my $dbh = shift;
|
||||
my @attached;
|
||||
|
||||
my $sth_databases = $dbh->prepare( 'PRAGMA database_list' );
|
||||
$sth_databases->execute;
|
||||
while ( my $db_info = $sth_databases->fetchrow_hashref ) {
|
||||
push @attached, $db_info->{name} if $db_info->{seq} >= 2;
|
||||
}
|
||||
return @attached;
|
||||
}
|
||||
|
||||
# SQL/CLI (ISO/IEC JTC 1/SC 32 N 0595), 6.63 Tables
|
||||
# Based on DBD::Oracle's
|
||||
# See also http://www.ch-werner.de/sqliteodbc/html/sqliteodbc_8c.html#a117
|
||||
# See also http://www.ch-werner.de/sqliteodbc/html/sqlite3odbc_8c.html#a213
|
||||
sub table_info {
|
||||
my ($dbh, $cat_val, $sch_val, $tbl_val, $typ_val) = @_;
|
||||
|
||||
my @where = ();
|
||||
my $sql;
|
||||
if ( defined($cat_val) && $cat_val eq '%'
|
||||
&& defined($sch_val) && $sch_val eq ''
|
||||
if ( defined($cat_val) && $cat_val eq '%'
|
||||
&& defined($sch_val) && $sch_val eq ''
|
||||
&& defined($tbl_val) && $tbl_val eq '') { # Rule 19a
|
||||
$sql = <<'END_SQL';
|
||||
SELECT NULL TABLE_CAT
|
||||
|
@ -170,21 +182,28 @@ SELECT NULL TABLE_CAT
|
|||
, NULL REMARKS
|
||||
END_SQL
|
||||
}
|
||||
elsif ( defined($sch_val) && $sch_val eq '%'
|
||||
&& defined($cat_val) && $cat_val eq ''
|
||||
elsif ( defined($cat_val) && $cat_val eq ''
|
||||
&& defined($sch_val) && $sch_val eq '%'
|
||||
&& defined($tbl_val) && $tbl_val eq '') { # Rule 19b
|
||||
$sql = <<'END_SQL';
|
||||
SELECT NULL TABLE_CAT
|
||||
, NULL TABLE_SCHEM
|
||||
, t.tn TABLE_SCHEM
|
||||
, NULL TABLE_NAME
|
||||
, NULL TABLE_TYPE
|
||||
, NULL REMARKS
|
||||
FROM (
|
||||
SELECT 'main' tn
|
||||
UNION SELECT 'temp' tn
|
||||
END_SQL
|
||||
for my $db_name (_attached_database_list($dbh)) {
|
||||
$sql .= " UNION SELECT '$db_name' tn\n";
|
||||
}
|
||||
$sql .= ") t\n";
|
||||
}
|
||||
elsif ( defined($typ_val) && $typ_val eq '%'
|
||||
&& defined($cat_val) && $cat_val eq ''
|
||||
&& defined($sch_val) && $sch_val eq ''
|
||||
&& defined($tbl_val) && $tbl_val eq '') { # Rule 19c
|
||||
elsif ( defined($cat_val) && $cat_val eq ''
|
||||
&& defined($sch_val) && $sch_val eq ''
|
||||
&& defined($tbl_val) && $tbl_val eq ''
|
||||
&& defined($typ_val) && $typ_val eq '%') { # Rule 19c
|
||||
$sql = <<'END_SQL';
|
||||
SELECT NULL TABLE_CAT
|
||||
, NULL TABLE_SCHEM
|
||||
|
@ -205,26 +224,41 @@ SELECT *
|
|||
FROM
|
||||
(
|
||||
SELECT NULL TABLE_CAT
|
||||
, NULL TABLE_SCHEM
|
||||
, TABLE_SCHEM
|
||||
, tbl_name TABLE_NAME
|
||||
, TABLE_TYPE
|
||||
, NULL REMARKS
|
||||
, sql sqlite_sql
|
||||
FROM (
|
||||
SELECT tbl_name, upper(type) TABLE_TYPE, sql
|
||||
SELECT 'main' TABLE_SCHEM, tbl_name, upper(type) TABLE_TYPE, sql
|
||||
FROM sqlite_master
|
||||
WHERE type IN ( 'table','view')
|
||||
UNION ALL
|
||||
SELECT tbl_name, 'LOCAL TEMPORARY' TABLE_TYPE, sql
|
||||
SELECT 'temp' TABLE_SCHEM, tbl_name, 'LOCAL TEMPORARY' TABLE_TYPE, sql
|
||||
FROM sqlite_temp_master
|
||||
WHERE type IN ( 'table','view')
|
||||
END_SQL
|
||||
|
||||
for my $db_name (_attached_database_list($dbh)) {
|
||||
$sql .= <<"END_SQL";
|
||||
UNION ALL
|
||||
SELECT 'sqlite_master' tbl_name, 'SYSTEM TABLE' TABLE_TYPE, NULL sql
|
||||
SELECT '$db_name' TABLE_SCHEM, tbl_name, upper(type) TABLE_TYPE, sql
|
||||
FROM "$db_name".sqlite_master
|
||||
WHERE type IN ('table','view')
|
||||
END_SQL
|
||||
}
|
||||
|
||||
$sql .= <<'END_SQL';
|
||||
UNION ALL
|
||||
SELECT 'sqlite_temp_master' tbl_name, 'SYSTEM TABLE' TABLE_TYPE, NULL sql
|
||||
SELECT 'main' TABLE_SCHEM, 'sqlite_master' tbl_name, 'SYSTEM TABLE' TABLE_TYPE, NULL sql
|
||||
UNION ALL
|
||||
SELECT 'temp' TABLE_SCHEM, 'sqlite_temp_master' tbl_name, 'SYSTEM TABLE' TABLE_TYPE, NULL sql
|
||||
)
|
||||
)
|
||||
END_SQL
|
||||
if ( defined $sch_val ) {
|
||||
push @where, "TABLE_SCHEM LIKE '$sch_val'";
|
||||
}
|
||||
if ( defined $tbl_val ) {
|
||||
push @where, "TABLE_NAME LIKE '$tbl_val'";
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ END_SQL
|
|||
# Confirm fix for #34408: Primary key name wrong with newline in CREATE TABLE
|
||||
my $pkh = $dbh->primary_key_info( undef, undef, 'f' );
|
||||
my @pk = $pkh->fetchall_arrayref();
|
||||
is_deeply( \@pk, [ [ [ undef, undef, 'f', 'f1', 1, 'PRIMARY KEY' ] ] ], '->primary_key_info ok' );
|
||||
is_deeply( \@pk, [ [ [ undef, 'main', 'f', 'f1', 1, 'PRIMARY KEY' ] ] ], '->primary_key_info ok' );
|
||||
|
||||
my $sth = $dbh->prepare("SELECT f.f1, f.* FROM f");
|
||||
isa_ok( $sth, 'DBI::st' );
|
||||
|
|
|
@ -19,7 +19,7 @@ ok $dbh->do("create table meta2 (f1 varchar(2), f2 char(1), PRIMARY KEY (f1))");
|
|||
ok $dbh->do("create table meta3 (f2 char(1), f1 varchar(2) PRIMARY KEY)");
|
||||
$dbh->trace(0);
|
||||
$DBI::neat_maxlen = 4000;
|
||||
my $sth = $dbh->primary_key_info('', '', '%');
|
||||
my $sth = $dbh->primary_key_info(undef, undef, '%');
|
||||
ok $sth;
|
||||
my $pki = $sth->fetchall_hashref('TABLE_NAME');
|
||||
ok $pki;
|
||||
|
@ -28,7 +28,7 @@ ok keys %$pki == 3;
|
|||
ok $_->{COLUMN_NAME} eq 'f1' for values %$pki;
|
||||
|
||||
ok $dbh->do("create table meta4 (f1 varchar(2), f2 char(1), PRIMARY KEY (f1,f2))");
|
||||
$sth = $dbh->primary_key_info('', '', 'meta4');
|
||||
$sth = $dbh->primary_key_info(undef, undef, 'meta4');
|
||||
ok $sth;
|
||||
$pki = $sth->fetchall_hashref('COLUMN_NAME');
|
||||
ok $pki;
|
||||
|
@ -37,7 +37,7 @@ ok keys %$pki == 2;
|
|||
ok $pki->{f1}->{KEY_SEQ} == 1;
|
||||
ok $pki->{f2}->{KEY_SEQ} == 2;
|
||||
|
||||
my @pk = $dbh->primary_key('','','meta4');
|
||||
my @pk = $dbh->primary_key(undef, undef, 'meta4');
|
||||
ok @pk == 2;
|
||||
ok "@pk" eq "f1 f2";
|
||||
|
||||
|
|
124
t/35_table_info.t
Normal file
124
t/35_table_info.t
Normal file
|
@ -0,0 +1,124 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use strict;
|
||||
BEGIN {
|
||||
$| = 1;
|
||||
$^W = 1;
|
||||
}
|
||||
|
||||
use t::lib::Test;
|
||||
use Test::More tests => 18;
|
||||
use Test::NoWarnings;
|
||||
|
||||
my @schema_info = (
|
||||
[undef, 'main', undef, undef, undef],
|
||||
[undef, 'temp', undef, undef, undef]
|
||||
);
|
||||
my @systable_info = (
|
||||
[undef, 'main', 'sqlite_master', 'SYSTEM TABLE', undef, undef],
|
||||
[undef, 'temp', 'sqlite_temp_master', 'SYSTEM TABLE', undef, undef]
|
||||
);
|
||||
|
||||
# Create a database
|
||||
my $dbh = connect_ok();
|
||||
|
||||
# Check avalable schemas
|
||||
my $sth = $dbh->table_info('', '%', '');
|
||||
ok $sth, 'We can get table/schema information';
|
||||
my $info = $sth->fetchall_arrayref;
|
||||
is_deeply $info, \@schema_info, 'Correct table/schema information';
|
||||
|
||||
# Create a table
|
||||
ok( $dbh->do(<<'END_SQL'), 'CREATE TABLE one' );
|
||||
CREATE TABLE one (
|
||||
id INTEGER PRIMARY KEY NOT NULL,
|
||||
name CHAR (64) NOT NULL
|
||||
)
|
||||
END_SQL
|
||||
my $table1_info = [undef, 'main', 'one', 'TABLE', undef, 'CREATE TABLE one (
|
||||
id INTEGER PRIMARY KEY NOT NULL,
|
||||
name CHAR (64) NOT NULL
|
||||
)'];
|
||||
|
||||
# Create a temporary table
|
||||
ok( $dbh->do(<<'END_SQL'), 'CREATE TEMP TABLE two' );
|
||||
CREATE TEMP TABLE two (
|
||||
id INTEGER NOT NULL,
|
||||
name CHAR (64) NOT NULL
|
||||
)
|
||||
END_SQL
|
||||
my $table2_info = [undef, 'temp', 'two', 'LOCAL TEMPORARY', undef, 'CREATE TABLE two (
|
||||
id INTEGER NOT NULL,
|
||||
name CHAR (64) NOT NULL
|
||||
)'];
|
||||
|
||||
# Attach a memory database
|
||||
ok( $dbh->do('ATTACH DATABASE ":memory:" AS db3'), 'ATTACH DATABASE ":memory:" AS db3' );
|
||||
|
||||
# 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
|
||||
my $table3_info = [undef, 'db3', 'three', 'TABLE', undef, 'CREATE TABLE three (
|
||||
id INTEGER NOT NULL,
|
||||
name CHAR (64) NOT NULL
|
||||
)'];
|
||||
|
||||
# Get table_info for "one"
|
||||
$info = $dbh->table_info(undef, undef, 'one')->fetchall_arrayref;
|
||||
is_deeply $info, [$table1_info], 'Correct table_info for "one"';
|
||||
|
||||
# Get table_info for "main"."one"
|
||||
$info = $dbh->table_info(undef, 'main', 'one')->fetchall_arrayref;
|
||||
is_deeply $info, [$table1_info], 'Correct table_info for "main"."one"';
|
||||
|
||||
# Get table_info for "two"
|
||||
$info = $dbh->table_info(undef, undef, 'two')->fetchall_arrayref;
|
||||
is_deeply $info, [$table2_info], 'Correct table_info for "two"';
|
||||
|
||||
# Get table_info for "temp"."two"
|
||||
$info = $dbh->table_info(undef, 'temp', 'two')->fetchall_arrayref;
|
||||
is_deeply $info, [$table2_info], 'Correct table_info for "temp"."two"';
|
||||
|
||||
# Get table_info for "three"
|
||||
$info = $dbh->table_info(undef, undef, 'three')->fetchall_arrayref;
|
||||
is_deeply $info, [$table3_info], 'Correct table_info for "three"';
|
||||
|
||||
# Get table_info for "db3"."three"
|
||||
$info = $dbh->table_info(undef, 'db3', 'three')->fetchall_arrayref;
|
||||
is_deeply $info, [$table3_info], 'Correct table_info for "db3"."three"';
|
||||
|
||||
# Create another table "one" on the attached database
|
||||
ok( $dbh->do(<<'END_SQL'), 'CREATE TABLE db3.one' );
|
||||
CREATE TABLE db3.one (
|
||||
id INTEGER PRIMARY KEY NOT NULL,
|
||||
name CHAR (64) NOT NULL
|
||||
)
|
||||
END_SQL
|
||||
my $table4_info = [undef, 'db3', 'one', 'TABLE', undef, 'CREATE TABLE one (
|
||||
id INTEGER PRIMARY KEY NOT NULL,
|
||||
name CHAR (64) NOT NULL
|
||||
)'];
|
||||
|
||||
# Get table_info for both tables named "one"
|
||||
$info = $dbh->table_info(undef, undef, 'one')->fetchall_arrayref;
|
||||
is_deeply $info, [$table4_info, $table1_info], 'Correct table_info for both tables named "one"';
|
||||
|
||||
# Get table_info for the system tables
|
||||
$info = $dbh->table_info(undef, undef, undef, 'SYSTEM TABLE')->fetchall_arrayref;
|
||||
is_deeply $info, \@systable_info, 'Correct table_info for the system tables';
|
||||
|
||||
# Get table_info for all tables
|
||||
$info = $dbh->table_info()->fetchall_arrayref;
|
||||
is_deeply $info, [$table2_info, @systable_info, $table4_info, $table3_info, $table1_info],
|
||||
'Correct table_info for all tables';
|
||||
|
||||
#use Data::Dumper;
|
||||
#warn 'Catalog Names', substr Dumper($dbh->table_info('%', '', '')->fetchall_arrayref), 5;
|
||||
#warn 'Schema Names', substr Dumper($dbh->table_info('', '%', '')->fetchall_arrayref), 5;
|
||||
#warn 'Table Types', substr Dumper($dbh->table_info('', '', '', '%')->fetchall_arrayref), 5;
|
||||
#warn 'table_info', substr Dumper($info), 5;
|
||||
|
Loading…
Add table
Reference in a new issue