mirror of
https://github.com/DBD-SQLite/DBD-SQLite
synced 2025-06-08 06:38:12 -04:00
resolved #81536
This commit is contained in:
parent
dcfcf4bb04
commit
8fae2f1dee
2 changed files with 80 additions and 1 deletions
|
@ -401,12 +401,41 @@ sub primary_key_info {
|
|||
my $quoted_tbname = $dbh->quote_identifier($tbname);
|
||||
my $t_sth = $dbh->prepare("PRAGMA $quoted_dbname.table_info($quoted_tbname)");
|
||||
$t_sth->execute;
|
||||
my @pk;
|
||||
while(my $col = $t_sth->fetchrow_hashref) {
|
||||
next unless $col->{pk};
|
||||
push @pk, $col->{name};
|
||||
}
|
||||
|
||||
# If there're multiple primary key columns, we need to
|
||||
# find their order from one of the auto-generated unique
|
||||
# indices (note that single column integer primary key
|
||||
# doesn't create an index).
|
||||
if (@pk > 1) {
|
||||
my $indices = $dbh->selectall_arrayref("PRAGMA $quoted_dbname.index_list($quoted_tbname)", {Slice => +{}});
|
||||
for my $index (@$indices) {
|
||||
next unless $index->{unique};
|
||||
my $quoted_idxname = $dbh->quote_identifier($index->{name});
|
||||
my $cols = $dbh->selectall_arrayref("PRAGMA $quoted_dbname.index_info($quoted_idxname)", {Slice => +{}});
|
||||
my %seen;
|
||||
if (@pk == grep { !$seen{$_}++ } (@pk, map { $_->{name} } @$cols)) {
|
||||
for (@$cols) {
|
||||
push @pk_info, {
|
||||
TABLE_SCHEM => $dbname,
|
||||
TABLE_NAME => $tbname,
|
||||
COLUMN_NAME => $_->{name},
|
||||
KEY_SEQ => scalar @pk_info + 1,
|
||||
PK_NAME => 'PRIMARY KEY',
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
push @pk_info, {
|
||||
TABLE_SCHEM => $dbname,
|
||||
TABLE_NAME => $tbname,
|
||||
COLUMN_NAME => $col->{name},
|
||||
COLUMN_NAME => $pk[0],
|
||||
KEY_SEQ => scalar @pk_info + 1,
|
||||
PK_NAME => 'PRIMARY KEY',
|
||||
};
|
||||
|
|
50
t/rt_81536_multi_column_primary_key_info.t
Normal file
50
t/rt_81536_multi_column_primary_key_info.t
Normal file
|
@ -0,0 +1,50 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use strict;
|
||||
BEGIN {
|
||||
$| = 1;
|
||||
$^W = 1;
|
||||
}
|
||||
|
||||
use t::lib::Test qw/connect_ok/;
|
||||
use Test::More;
|
||||
use Test::NoWarnings;
|
||||
|
||||
plan tests => 10 + 1;
|
||||
|
||||
# single column integer primary key
|
||||
{
|
||||
my $dbh = connect_ok();
|
||||
$dbh->do("create table foo (id integer primary key, type text)");
|
||||
|
||||
my $sth = $dbh->primary_key_info(undef, undef, 'foo');
|
||||
my @pk_info;
|
||||
while(my $row = $sth->fetchrow_hashref) { push @pk_info, $row };
|
||||
is @pk_info => 1, "found 1 pks";
|
||||
is $pk_info[0]{COLUMN_NAME} => 'id', "first pk name is id";
|
||||
}
|
||||
|
||||
# single column not-integer primary key
|
||||
{
|
||||
my $dbh = connect_ok();
|
||||
$dbh->do("create table foo (id text primary key, type text)");
|
||||
|
||||
my $sth = $dbh->primary_key_info(undef, undef, 'foo');
|
||||
my @pk_info;
|
||||
while(my $row = $sth->fetchrow_hashref) { push @pk_info, $row };
|
||||
is @pk_info => 1, "found 1 pks";
|
||||
is $pk_info[0]{COLUMN_NAME} => 'id', "first pk name is id";
|
||||
}
|
||||
|
||||
# multi-column primary key
|
||||
{
|
||||
my $dbh = connect_ok();
|
||||
$dbh->do("create table foo (id id, type text, primary key(type, id))");
|
||||
|
||||
my $sth = $dbh->primary_key_info(undef, undef, 'foo');
|
||||
my @pk_info;
|
||||
while(my $row = $sth->fetchrow_hashref) { push @pk_info, $row };
|
||||
is @pk_info => 2, "found 1 pks";
|
||||
is $pk_info[0]{COLUMN_NAME} => 'type', "first pk name is type";
|
||||
is $pk_info[1]{COLUMN_NAME} => 'id', "second pk name is id";
|
||||
}
|
Loading…
Add table
Reference in a new issue