简体   繁体   English

在find()内部使用OR子句

[英]Using OR clause inside of find()

I need to fetch a single row from database table using DBIx::Class when one of the two descriptor columns is like a provided data. 当两个描述符列之一类似于提供的数据时,我需要使用DBIx :: Class从数据库表中获取一行。

My SQL select looks like this: 我的SQL选择看起来像这样:

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

The table has unique constraint what ensures me that only one row exists for the VALUE. 该表具有唯一的约束,这确保了VALUE仅存在一行。

How can I formulate this SQL using DBIx::Class? 如何使用DBIx :: Class编写此SQL?

The following code returns me the value 1 but not the correct data: 以下代码为我返回值1,但返回的数据不正确:

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

I should use the find method to return a single row but then I can not use the -or clause. 我应该使用find方法返回单行,但是然后我不能使用-or子句。

Any suggestion? 有什么建议吗?


Hi, I am back. 嗨,我回来了。 I decided to create myself the classes. 我决定创建自己的课程。 First the schema class: 首先是模式类:

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;

I am using a base Result class for my table: 我正在为表使用基本Result类:

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

Each table then uses a ResultSet base class: 每个表然后使用一个ResultSet基类:

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

and the Product class itself: 和产品类本身:

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;

The application uses this model class: 该应用程序使用以下模型类:

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;

The Root is this: 根源是这样的:

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;

Now, I am using this one_row from that helper class, I do get the wanted result but the SQL statement remains open till I shutdown the application. 现在,我正在使用该帮助程序类中的one_row ,确实得到了所需的结果,但是SQL语句保持打开状态,直到关闭应用程序为止。

@Alexander Hartmaier you said that I should avoid this. @亚历山大·哈特迈尔,你说我应该避免这种情况。 But how? 但是如何? What is wrong with this code? 此代码有什么问题?

Either by using the one_row method from the highly recommended DBIx::Class::Helper distribution or by copying its code. 通过使用强烈推荐的DBIx :: Class :: Helper发行版中的one_row方法或复制其代码。

first shouldn't be used because it keeps the database statement handle open. first不应该使用,因为它可以使数据库语句句柄保持打开状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM