DBIx::Custom::Result

简介

DBIx::Custom::Result模块是对 select 语句查询结果的封装。

# Result
my $result = $dbi->select(table => 'book');

# Fetch a row and put it into array reference
while (my $row = $result->fetch) {
  my $author = $row->[0];
  my $title  = $row->[1];
}

# Fetch only a first row and put it into array reference
my $row = $result->fetch_one;

# Fetch all rows and put them into array of array reference
my $rows = $result->fetch_all;

# Fetch a row and put it into hash reference
while (my $row = $result->fetch_hash) {
  my $title  = $row->{title};
  my $author = $row->{author};
}

# Fetch only a first row and put it into hash reference
my $row = $result->fetch_hash_one;
my $row = $result->one; # Alias for "fetch_hash_one"

# Fetch all rows and put them into array of hash reference
my $rows = $result->fetch_hash_all;
my $rows = $result->all; # Alias for "fetch_hash_all"

属性

dbi

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

DBIx::Custom对象。

sth

my $sth = $reuslt->sth
$result = $result->sth($sth);

DBI的Statement 句柄。

stash

my $stash = $result->stash;
my $foo = $result->stash->{foo};
$result->stash->{foo} = $foo;

用于存储数据的hashref。

方法

DBIx::Custom::Result模块了Object::Simple中的所有方法,并实现了以下方法。

all

my $rows = $result->all;

和 fetch_hash_all 功能完全一样,其实这个方法只是fetch_hash_all方法的别名。

fetch

my $row = $result->fetch;

从查询结果中获取一行并以数组引用的方式返回。

fetch_all

my $rows = $result->fetch_all;

从查询结果中以数组引用的格式获取所有行并将其放入数组引用中返回。实现代码如下:

sub fetch_all {
  my $self = shift;

  # Fetch all rows
  my $rows = [];
  while(my $row = $self->fetch) { push @$rows, $row}

  return $rows;
}

fetch_one

my $row = $result->fetch_one;

仅从查询结果中获取第一行,并将其以数组引用的方式返回,然后结束Statement 句柄。实现代码如下:

sub fetch_one {
  my $self = shift;

  # Fetch
  my $row = $self->fetch;
  return unless $row;

  # Finish statement handle
  $self->sth->finish;

  return $row;
}

fetch_hash

my $row = $result->fetch_hash;

从查询结果中获取一行并以哈希引用的方式返回。

fetch_hash_all

my $rows = $result->fetch_hash_all;

从查询结果中以哈希引用的格式获取所有行并将其放入数组引用中返回。实现代码如下:

sub fetch_hash_all {
  my $self = shift;

  # Fetch all rows as hash
  my $rows = [];
  while(my $row = $self->fetch_hash) { push @$rows, $row }

  return $rows;
}

fetch_hash_one

my $row = $result->fetch_hash_one;

仅从查询结果中获取第一行,并将其以哈希引用的方式返回,然后结束Statement 句柄。实现代码如下:

sub fetch_hash_one {
  my $self = shift;

  # Fetch hash
  my $row = $self->fetch_hash;
  return unless $row;

  # Finish statement handle
  $self->sth->finish;

  return $row;
}

fetch_hash_multi

my $rows = $result->fetch_hash_multi(5);

从查询结果中以哈希引用的格式获取多行(由参数指定)并将其放入数组引用中返回。实现代码如下:

sub fetch_hash_multi {
  my ($self, $count) = @_;

  # Fetch multiple rows
  croak 'Row count must be specified ' . _subname  unless $count;

  return if $self->{_finished};

  my $rows = [];
  for (my $i = 0; $i < $count; $i++) {
    my $row = $self->fetch_hash;
    unless ($row) {
      $self->{_finished} = 1;
      last;
    }
    push @$rows, $row;
  }

  return unless @$rows;
  return $rows;
}

fetch_multi

my $rows = $result->fetch_multi(5);

从查询结果中以数组引用的格式获取多行(由参数指定)并将其放入数组引用中返回。实现代码如下:

sub fetch_multi {
  my ($self, $count) = @_;

  # Row count not specified
  croak 'Row count must be specified ' . _subname   unless $count;

  return if $self->{_finished};

  # Fetch multi rows
  my $rows = [];
  for (my $i = 0; $i < $count; $i++) {
    my $row = $self->fetch;
    unless ($row) {
      $self->{_finished} = 1;
      last;
    }
    push @$rows, $row;
  }

  return unless @$rows;
  return $rows;
}

filter

$result->filter(title  => sub { uc $_[0] }, author => 'to_upper');
$result->filter([qw/title author/] => 'to_upper');

在这个方法中,你可以使用subroutine或在DBIx::Custom中使用 register_filter 方法设置的“过滤器”名称,为查询结果中的数据列设置过滤器。这里设置的过滤器,会在DBIx::Custom早使用type_rule方法设置的过滤器之后执行。

flat

my @list = $result->flat;

从查询结果中以数组方式获取所有行,并将他们合并到一个列表中返回。

my @list = $dbi->select(['id', 'title'])->flat; ## (1, 'Perl', 2, 'Ruby')

您使用此方法可以轻松创建键值对列表(哈希结构)。

my %titles = $dbi->select(['id', 'title'])->flat;

此方法的实现代码如下:

sub flat {
  my $self = shift;

  my @flat;
  while (my $row = $self->fetch) {
    push @flat, @$row;
  }
  return @flat;
}

kv

my $key_value = $result->kv;

获取键值对。

my $books = $dbi->select(['id', 'title', 'author'])->kv;

如果调用all方法返回以下数据:

[
  {id => 1, title => 'Perl', author => 'Ken'},
  {id => 2, title => 'Ruby', author => 'Taro'}
]

而 kv 方法返回以下数据。

{
  1 => {title => 'Perl', author => 'Ken'},
  2 => {title => 'Ruby', author => 'Taro'}
}

在查询语句中第一列的值会被作为kv方法返回结果中的键。kv 方法的实现代码如下:

sub kv {
  my ($self, %opt) = @_;

  my $key_name = $self->{sth}{NAME}[0];
  my $kv = {};
  while (my $row = $self->fetch_hash) {
    my $key_value = delete $row->{$key_name};
    next unless defined $key_value;
    if ($opt{multi}) {
      _deprecate('0.28', "DBIx::Custom::Result::kv method's "
        . 'multi option is DEPRECATED. use kvs method instead');
      $kv->{$key_value} ||= [];
      push @{$kv->{$key_value}}, $row;
    }
    else { $kv->{$key_value} = $row }
  }

  return $kv;
}

kvs

my $key_values = $result->kvs;

获取键值对,与kv方法不同的是,在这里获取的结果中 value是以数组引用的格式存在的。如果一个key可能对应多个value时应该使用此方法,而不是kv方法。

my $books = $dbi->select(['author', 'title', 'price'])->kvs;

如果调用all方法返回以下数据:

[
  {author => 'Ken', title => 'Perl', price => 1000},
  {author => 'Ken', title => 'Good', price => 2000},
  {author => 'Taro', title => 'Ruby', price => 3000}
  {author => 'Taro', title => 'Sky', price => 4000}
]

kvs 方法返回以下数据。

{
  Ken => [
    {title => 'Perl', price => 1000},
    {title => 'Good', price => 2000}
  ],
  Taro => [
    {title => 'Ruby', price => 3000},
    {title => 'Sky', price => 4000}
  ]
}

kvs方法的实现代码如下:

sub kvs {
  my ($self, %opt) = @_;

  my $key_name = $self->{sth}{NAME}[0];
  my $kv = {};
  while (my $row = $self->fetch_hash) {
    my $key_value = delete $row->{$key_name};
    next unless defined $key_value;
    $kv->{$key_value} ||= [];
    push @{$kv->{$key_value}}, $row;
  }

  return $kv;
}

header

my $header = $result->header;

获取表头,也就是查询结果中每一列的名称。

one

my $row = $result->one;

与 fetch_hash_one 方法的功能完全相同。

type_rule

感觉官方文档写的有点问题,所以暂时不做翻译。

type_rule_off

$result = $result->type_rule_off;

关闭 type_rule中的from1和from2过滤规则,默认情况下是开着的。

type_rule_on

$result = $result->type_rule_on;

打开 type_rule中的from1和from2过滤规则,默认情况下是开着的。

type_rule1_off

$result = $result->type_rule1_off;

关闭 type_rule中的from1过滤规则,默认情况下是开着的。

type_rule1_on

$result = $result->type_rule1_on;

打开 type_rule中的from1过滤规则,默认情况下是开着的。

type_rule2_off

$result = $result->type_rule2_off;

关闭 type_rule中的from2过滤规则,默认情况下是开着的。

type_rule2_on

$result = $result->type_rule2_on;

打开 type_rule中的from2过滤规则,默认情况下是开着的。

value

my $value = $result->value;

从查询结果中获取第一行中第一列的值。

my $count = $dbi->select('count(*)', table => 'book')->value;
## 等价于
my $count = $dbi->select('count(*)')->fetch_one->[0];

value的实现代码如下:

sub value {
  my $self = shift;
  my $row = $self->fetch_one;
  my $value = $row ? $row->[0] : undef;
  return $value;
}

values

my $values = $result->values;

从查询结果中获取所有行中第一列的值。

my $tables = $dbi->select('show tables')->values;
## 等价于
my $rows = $dbi->select('show tables')->fetch_all;
my $tables = [map { $_->[0] } @$rows];

values的实现代码如下:

sub values {
  my $self = shift;

  my $values = [];
  my $rows = $self->fetch_all;
  push @$values, $_->[0] for @$rows;
  return $values;
}

1

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,268评论 19 139
  • 简介 DBIx::Custom是一个DBI的包装器,此模块具有以下功能: 可以更容易的执行insert、updat...
    JSON_NULL阅读 614评论 0 3
  • PHP常用函数大全 usleep() 函数延迟代码执行若干微秒。 unpack() 函数从二进制字符串对数据进行解...
    上街买菜丶迷倒老太阅读 1,407评论 0 20
  • 瞬间“小满足” 幸福感是朋友圈最热门的话题之一,我一小友曾恨恨地说,刷烂了的心灵鸡汤,腐蚀了...
    大图图阅读 209评论 0 0
  • 人因梦想而伟大。这句话越长大越被抛弃的彻底,因为我们后来都不相信梦想,或者说,梦想变成了物质基础。如果没有了...
    昼夜思阅读 241评论 0 0