File: ProxyResultSetUpdate.pm

package info (click to toggle)
libdbix-class-helpers-perl 2.033002-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 972 kB
  • ctags: 358
  • sloc: perl: 4,654; sql: 167; makefile: 7
file content (97 lines) | stat: -rw-r--r-- 2,363 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package DBIx::Class::Helper::Row::ProxyResultSetUpdate;
$DBIx::Class::Helper::Row::ProxyResultSetUpdate::VERSION = '2.033002';
# ABSTRACT: Efficiently reuse ResultSet updates from results

use strict;
use warnings;

use parent 'DBIx::Class::Helper::Row::SelfResultSet', 'DBIx::Class::Row';

sub update {
  my ($self, $upd) = @_;

  $self->set_inflated_columns($upd) if $upd;

  my %to_update = $self->get_dirty_columns
    or return $self;

  $self->throw_exception( "Not in database" ) unless $self->in_storage;

  # copied directly from DBIx::Class::Row except for this line
  my $rows = $self->self_rs->update(\%to_update);
  if ($rows == 0) {
    $self->throw_exception( "Can't update ${self}: row not found" );
  } elsif ($rows > 1) {
    $self->throw_exception("Can't update ${self}: updated more than one row");
  }
  $self->{_dirty_columns} = {};
  $self->{related_resultsets} = {};
  delete $self->{_column_data_in_storage};
  return $self;
}

1;

__END__

=pod

=head1 NAME

DBIx::Class::Helper::Row::ProxyResultSetUpdate - Efficiently reuse ResultSet updates from results

=head1 SYNOPSIS

ResultSet:

 package MyApp::Schema::ResultSet::Foo;

 use parent 'DBIx::Class::ResultSet';

 sub update {
    my ($self, $data) = @_;

    die 'you fool!' if $data->{name} eq 'fool';

    return $self->next::method($data);
 }

Result:

 package MyApp::Schema::Result::Foo;

 use parent 'DBIx::Class::Core';

 __PACKAGE__->load_components(qw( Helper::Row::ProxyResultSetUpdate ));

 ...

or with L<DBIx::Class::Candy>:

 package MyApp::Schema::Result::Foo;

 use DBIx::Class::Candy -components => ['Helper::Row::ProxyResultSetMethod'];

 ...

=head1 DESCRIPTION

This module makes reusing resultset updates from a result trivially easy.
Often the only way that people share update methods is by overriding update
in their resultset to use L<DBIx::Class::ResultSet/update_all>.  Unfortunately,
that can end up being wildly inefficient.  Instead, if you can write your
update in terms of the resultset, you can make your code much faster and more
efficient.

=head1 AUTHOR

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut