首页 > 解决方案 > 在 find() 中使用 OR 子句

问题描述

当两个描述符列之一就像提供的数据时,我需要使用 DBIx::Class 从数据库表中获取一行。

我的 SQL 选择如下所示:

select id from products where (code = 'VALUE' or description like '%VALUE%')

该表具有唯一约束,可确保我只为 VALUE 存在一行。

如何使用 DBIx::Class 制定此 SQL?

以下代码返回值1但不是正确的数据:

my $param = 'VALUE';
my $res = $c->stash->{products_rs}->search(
    -or => [
        { code => { '=', $param } },{ description => { 'like', '%'.$param.'%' } }
    ],
);

我应该使用find方法返回单行,但是我不能使用-or子句。

有什么建议吗?


你好,我回来了。我决定自己创建课程。首先是模式类:

package prod::Schema;
use utf8;
use Moose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Schema';
__PACKAGE__->load_namespaces(
    default_resultset_class => 'ResultSet',
);
__PACKAGE__->meta->make_immutable(inline_constructor => 0);
1;

我正在为我的表使用基本 Result 类:

package prod::Schema::Result;
use strict;
use warnings;
use base qw( DBIx::Class::Core );
__PACKAGE__->load_components(
    "InflateColumn::DateTime", 
    "TimeStamp", 
    "EncodedColumn"
);
1;

然后每个表使用一个 ResultSet 基类:

package prod::Schema::ResultSet;
use strict;
use warnings;
use base qw( DBIx::Class::ResultSet );
__PACKAGE__->load_components('Helper::ResultSet::OneRow');
1;

和 Product 类本身:

package prod::Schema::Result::Products;
use strict;
use warnings;
use utf8;
use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'prod::Schema::Result';
__PACKAGE__->table("prod.products");
__PACKAGE__->add_columns("id",  {
    data_type => "uuid",
    default_value => \"uuid_generate_v4()",
    is_nullable => 0,
    size => 16,
}, "code", {
    data_type => "varchar", is_nullable => 0, size => 2
}, "name", {
    data_type => "varchar", is_nullable => 0, size => 128 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->add_unique_constraint("uk_products_code", ["code"]);
__PACKAGE__->meta->make_immutable;
1;

该应用程序使用此模型类:

package prod::Model::DB_T;
use strict;
use base 'Catalyst::Model::DBIC::Schema';
__PACKAGE__->config(
    schema_class => 'prod::Schema',
    connect_info => {
        dsn => 'dbi:Pg:dbname=p12;host=localhost',
        user => 'username',
        password => 'password',
        AutoCommit => q{1},
    }
);
1;

根是这样的:

package prod::Controller::Root;
use Moose;
use namespace::autoclean;
use Data::Dumper;
BEGIN { extends 'Catalyst::Controller' }
__PACKAGE__->config(namespace => '');
sub index :Path :Args(0) {
    my ($self, $c) = @_;
    my $Products = $c->model('DB_T::Products');
    my $product = $Products->search(
        -or => [
        { code => { '=', 'x' } },{ name => { 'like', '%'.'X'.'%' } }
        ],
    )->one_row;
    my $str = ''.$product->name;
    $c->response->body('product: '.$str);
}
sub default :Path {
    my ($self, $c) = @_;
    $c->response->body('Page not found');
    $c->response->status(404);
}
sub end : ActionClass('RenderView') {}
__PACKAGE__->meta->make_immutable;
1;

现在,我正在使用该帮助程序类中的one_row,我确实得到了想要的结果,但 SQL 语句保持打开状态,直到我关闭应用程序。

@Alexander Hartmaier 你说我应该避免这种情况。但是怎么做?这段代码有什么问题?

标签: perlcatalystdbix-class

解决方案


通过使用one_row强烈推荐的DBIx::Class::Helper发行版中的方法或复制其代码。

first不应使用,因为它使数据库语句句柄保持打开状态。


推荐阅读