【问题标题】:Perl DBI: get array of columns that are in index foo of table barPerl DBI:获取表栏索引 foo 中的列数组
【发布时间】:2014-02-09 04:47:00
【问题描述】:

我有一个列应该是全文索引的数组:

my @cols = ('col_1', 'col_2', 'col_3');

我也有对应的已经连接的dbi…:

my $dbh = DBI->connect(…);

... 包含一个带有全文索引 foo 的表 bar。

由于我无法修改现有索引的列,因此如果数组 @cols 的元素与索引中的列foo中的列数组的元素不同,我必须删除它并重新创建它> table bar,这正是我不知道如何得到的。

有人知道怎么弄吗?

【问题讨论】:

  • 你用的是什么数据库引擎?
  • MySQL,但由于 DBI 是一个抽象层,只要引擎支持某个特性,它应该就让它变得无关紧要。

标签: sql perl dbi


【解决方案1】:

简短的版本是使用DBI's statistics_info method,现在似乎已在DBDs 中广泛实施:

my $table = 'bar';
my $index = 'foo';
my @applicable_columns;
# $dbh already defined
my $schema = undef;
my $catalog = undef;
my $sth = $dbh->statistics_info( $catalog, $schema, $table, undef, 1 );
$sth->execute;
while ( my $row = $sth->fetchrow_hashref ) {
    next unless $row->{INDEX_NAME} eq $index;
    $applicable_columns[$row->{ORDINAL_POSITION}] = $row->{COLUMN_NAME};
}
$sth->finish;
shift @applicable_columns; # since starts from 1, index 0 will be undef

以上代码部分抄自DBIx::Class::Schema::Loader::DBI。

一个预期的插件:正在从DBIx::Class::Schema::Loader::DBI 子类中获取代码以正确实现SQL::Translator::Parser::DBI 子类(然后使DBIx::Class::Schema::Loader 使用生成的SQL::Translator::Schema),这也将提供良好的通用代码回答这类问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2013-04-29
    • 1970-01-01
    相关资源
    最近更新 更多