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

    简介 属性 clause 一个数组引用,存储where子中的所有where项,用于在调用to_string方法时生...

  • DBIx::Custom::Mapper

    简介 DBIx::Custom::Mapper模块是DBIx::Custom框架的参数映射器。 属性 param ...

  • DBIx::Custom::Model

    简介 DBIx::Custom::Model模块是DBIx::Custom框架中的模型对象。每个Model对象都对...

  • DBIx::Custom

    简介 DBIx::Custom是一个DBI的包装器,此模块具有以下功能: 可以更容易的执行insert、updat...

  • DBIx::Custom::Result

    简介 DBIx::Custom::Result模块是对 select 语句查询结果的封装。 属性 dbi DBIx...

  • DBIx::Custom::Order

    简介 DBIx::Custom::Order模块主要用于为查询生成order子句。 属性 dbi DBIx::Cu...

  • SQL之DBIx::Custom

    说明 写这篇文章的目的只是因为自己要用这个模块,在CPAN和Github上面有详细的介绍说明以及使用方式,但是每次...

  • DBIx::Custom框架实践方法

    简介 本文旨在说明在实际的软件开发过程中如何使用DBIx::Custom框架完成对数据库的操作。 扩展last_i...

  • Custom URL Schemes

    Custom URL Schemes Implementing a Custom URL SchemeDefini...

  • jupyter使用sublime快捷键

    添加sublime的快捷键 在~/.jupyter/custom下有custom.css custom.js fo...

网友评论

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

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