DBIx::Custom::Where

作者: JSON_NULL | 来源:发表于2017-12-07 14:47 被阅读11次

    简介

    DBIx::Custom::Where模块用于生成where子句。
    
    # Create DBIx::Custom::Where object
    my $where = $dbi->where;
    
    # Clause
    $where->clause(['and', 'title like :title', 'price = :price']);
    $where->clause(['and', ':title{like}', ':price{=}']);
    
    # Stringify where clause
    my $where_clause = "$where";
    my $where_clause = $where->to_string;
      # -> where title like :title and price = :price
    
    # Only price condition
    $where->clause(['and', ':title{like}', ':price{=}']);
    $where->param({price => 1900});
      # -> where price = :price
    
    # Only title condition
    $where->clause(['and', ':title{like}', ':price{=}']);
    $where->param({title => 'Perl'});
      # -> where title like :title
    
    # Nothing
    $where->clause(['and', ':title{like}', ':price{=}']);
    $where->param({});
      # => Nothing
    
    # or condition
    $where->clause(['or', ':title{like}', ':price{=}']);
      # -> where title = :title or price like :price
    
    # More than one parameter
    $where->clause(['and', ':price{>}', ':price{<}']);
    $where->param({price => [1000, 2000]});
      # -> where price > :price and price < :price
    
    # Only first condition
    $where->clause(['and', ':price{>}', ':price{<}']);
    $where->param({price => [1000, $dbi->not_exists]});
      # -> where price > :price
    
    # Only second condition
    $where->clause(['and', ':price{>}', ':price{<}']);
    $where->param({price => [$dbi->not_exists, 2000]});
      # -> where price < :price
    
    # More complex condition
    $where->clause(
      [
        'and',
        ':price{=}',
        ['or', ':title{=}', ':title{=}', ':title{=}']
      ]
    );
      # -> pirce = :price and (title = :title or title = :title or tilte = :title)
    
    # Using Full-qualified column name
    $where->clause(['and', ':book.title{like}', ':book.price{=}']);
      # -> book.title like :book.title and book.price = :book.price
    

    属性

    clause

    my $clause = $where->clause;
    $where = $where->clause(
      ['and',
        ':title{=}', 
        ['or', ':date{<}', ':date{>}']
      ]
    );
    

    一个数组引用,存储where子中的所有where项,用于在调用to_string方法时生成where子句。

    where title = :title and ( date < :date or date > :date )
    

    param

    my $param = $where->param;
    $where = $where->param({
      title => 'Perl',
      date => ['2010-11-11', '2011-03-05'],
    });
    

    用于生成where子句的参数。

    dbi

    my $dbi = $where->dbi;
    $where = $where->dbi($dbi);
    

    DBIx::Custom对象。

    join

    my $join = $where->join;
    $join = $where->join($join);
    

    边连接的信息,这个值会被添加到select方法的join选项中。

    方法

    DBIx::Custom::Where继承了Object::Simple中的所有方法,并且实现了一个to_string方法。

    $where->to_string;
    

    用于将当前DBIx::Custom::Where对象转化为where子句。

    重载的操作符

    use overload
      'bool'   => sub {1},
      '""'     => sub { shift->to_string },
      fallback => 1;
    

    clause 语法

    clause 必须是一个以andor 字符串为首元素的数组引用,里面的元素可以是字符串(后面称其为where项),也可以一个clause。也就是说clause是可以嵌套的。

    而对于clause中的where项的格式可以有以下几种:

    1. 没有占位符的一个条件式( x=y)
    2. 带有占位符的条件式(x= :y)
    3. 自带操作符的占位符(:x{>}),可用的操作符有:like,>,<,=。

    需要注意的是:当对DBIx::Custom::Where对象指定param属性时,在clause中存在而在parame中不存在的项,再调用to_string生成where子句时会被忽略。

    1

    相关文章

      网友评论

        本文标题:DBIx::Custom::Where

        本文链接:https://www.haomeiwen.com/subject/bjudixtx.html