perl - DBIx::Class::ResultSet 中列名的抽象

标签 perl catalyst dbix-class

我正在开发一个使用 DBIx::Class 的 Catalyst 应用程序访问 MySQL 数据库。该应用程序对文件执行数据质量检查。文件被加载到表FileContent中:

 FILE_ID LINE_NUMBER FOO BAR BAZ
 -----------------------------------
 1       1           A   99  Blah
 1       2           B   11  Blargh
 1       3           A   4   Sproing
 1       4           B   7   Sproing
 1       5           B   10  Doink

然后我有另一个表,Format,它定义了每列的验证操作:

 COL_ORDER COL_NAME VALIDATION
 1         FOO      regex:/^[AB]$/
 2         BAR      unique_number
 3         BAZ      regex:/\S/

我想要做的是逐行检查给定文件的 FileContent ,将 Format 中的所有规则应用于每一行:

my @lines  = $c->model('DB::FileContent')->search({file_id => 1});
my @format = $c->model('DB::Format')->search();

foreach my $line (@lines)
{
    foreach my $column (@format)
    {
        #Get the field matching this column from $line.
        #e.g. for first $column get $line->foo()  
    }
}

但是,我不确定如何最好地从与格式中当前列匹配的行中有效地获取列。访问列的正常方式是通过方法,例如 $line->foo。但是当 foo 是一个变量时我该怎么办?

我想我不想这样做:

eval "$line->${$column->col_name}";

我知道 get_column,但是这对于从行中获取单个值是否有效?

$line->get_column($column->col_name)

根据另一个表中的值检索列的最有效方法是什么?我可以使用列名称或列位置。预先感谢您的帮助。

最佳答案

首先尝试将所有验证规则放入哈希引用(或哈希)中,并且最重要的是确保 FileContent 项通过 DBIx::Class::ResultClass::HashRefInflator 膨胀到哈希引用中。这使得在循环浏览项目中的字段时访问它们变得更加方便。

像这样:

#------------------------------------------------------------------------------------------
# get all formats into a hashref
#------------------------------------------------------------------------------------------

my $format = $c->model('DB::Format')->search({}, { order_by => ['COL_ORDER'] } );
my @col_names = $format->get_column('COL_NAME')->all;
# @col_names now contains qw/FOO BAR BAZ/

my $formats;
@{$formats}{@col_names} = $format->get_column('VALIDATION')->all;

#------------------------------------------------------------------------------------------
# create an iterator over DB::FileContent, and make items inflate to hashrefs 
#------------------------------------------------------------------------------------------

my $file_content  = $c->model('DB::FileContent')->search({file_id => 1});
$file_content->result_class('DBIx::Class::ResultClass::HashRefInflator');
# this ensures that every item inflates into a hashref


# this way you can iterate over items, and don't have to put them all into an array 
while (my $hashref = $file_content->next) {
    # put relevant field values into an array
    my @values = @{$hashref}{@col_names};
}

关于perl - DBIx::Class::ResultSet 中列名的抽象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15481536/

相关文章:

perl - 哪一个 cpan 安装程序是正确的? (CPAN.pm/CPANPLUS/cpanminus)

perl - 将 Perl 的文件输入角度运算符重载为通用迭代器/生成器是否合适?

perl - 模板工具包和复杂变量

performance - Spark : Explicit caching can interfere with Catalyst optimizer's ability to optimize some queries?

perl eval block 抛出 execptions 没有错误

java - Perl Web 服务的 JAX-WS 客户端

perl - 使用 "perl -c"验证语法但忽略特定错误?

Perl Catalyst Controller 链

perl - 在 DBIx-Class 中使用预取时如何仅选择特定列?

sql - 如何在 DBIx::Class 结果集搜索中检索每个组中的最新记录?