1
0
Fork 0
mirror of https://github.com/DBD-SQLite/DBD-SQLite synced 2025-06-08 14:48:32 -04:00
This commit is contained in:
Kenichi Ishigaki 2012-09-03 15:04:52 +00:00
parent 5e7eda37c4
commit 26c1367218

View file

@ -362,36 +362,31 @@ END_SQL
sub primary_key_info { sub primary_key_info {
my ($dbh, $catalog, $schema, $table, $attr) = @_; my ($dbh, $catalog, $schema, $table, $attr) = @_;
# Escape the schema and table name unless ($schema) {
$schema =~ s/([\\_%])/\\$1/g if defined $schema; # for backward compat
my $escaped = $table; my $table_info = $dbh->table_info($catalog, $schema, $table);
$escaped =~ s/([\\_%])/\\$1/g; while(my $info = $table_info->fetchrow_hashref) {
$attr ||= {}; next unless uc $info->{TABLE_TYPE} eq 'TABLE';
$attr->{Escape} = '\\'; if ($info->{TABLE_NAME} eq $table) {
my $sth_tables = $dbh->table_info($catalog, $schema, $escaped, undef, $attr); $schema = $info->{TABLE_SCHEM};
last;
# This is a hack but much simpler than using pragma index_list etc }
# also the pragma doesn't list 'INTEGER PRIMARY KEY' autoinc PKs!
my @pk_info;
while ( my $row = $sth_tables->fetchrow_hashref ) {
my $sql = $row->{sqlite_sql} or next;
next unless $sql =~ /(.*?)\s*PRIMARY\s+KEY\s*(?:\(\s*(.*?)\s*\))?/si;
my @pk = split /\s*,\s*/, $2 || '';
unless ( @pk ) {
my $prefix = $1;
$prefix =~ s/.*create\s+table\s+.*?\(\s*//si;
$prefix = (split /\s*,\s*/, $prefix)[-1];
@pk = (split /\s+/, $prefix)[0]; # take first word as name
} }
my $key_seq = 0; }
foreach my $pk_field (@pk) {
$pk_field =~ s/(["'`])(.+)\1/$2/; # dequote # placeholder doesn't seem to work here
$pk_field =~ s/\[(.+)\]/$1/; # dequote my $quoted_table = $dbh->quote($table);
my $psth = $dbh->prepare("PRAGMA table_info($quoted_table)");
$psth->execute;
my @pk_info;
while(my $col = $psth->fetchrow_hashref) {
if ($col->{pk}) { # we now have this!
push @pk_info, { push @pk_info, {
TABLE_SCHEM => $row->{TABLE_SCHEM}, TABLE_SCHEM => $schema,
TABLE_NAME => $row->{TABLE_NAME}, TABLE_NAME => $table,
COLUMN_NAME => $pk_field, COLUMN_NAME => $col->{name},
KEY_SEQ => ++$key_seq, KEY_SEQ => scalar @pk_info + 1,
PK_NAME => 'PRIMARY KEY', PK_NAME => 'PRIMARY KEY',
}; };
} }