| 1 | package DBIx::Class::Helper::Row::ProxyResultSetUpdate; | ||||||||||||||
| 2 | 54 | $DBIx::Class::Helper::Row::ProxyResultSetUpdate::VERSION = '2.033001'; | |||||||||||||
| 3 | # ABSTRACT: Efficiently reuse ResultSet updates from results | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 54 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 54 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 54 | ▶ | use parent 'DBIx::Class::Helper::Row::SelfResultSet', 'DBIx::Class::Row'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | sub update { | ||||||||||||||
| 11 | 2 | ▶ | my ($self, $upd) = @_; | ||||||||||||
|
Subroutine
update: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 2 | ▶ | $self->set_inflated_columns($upd) if $upd; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 2 | ▶ | my %to_update = $self->get_dirty_columns | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 16 | or return $self; | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 2 | ▶ | $self->throw_exception( "Not in database" ) unless $self->in_storage; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | # copied directly from DBIx::Class::Row except for this line | ||||||||||||||
| 21 | 2 | my $rows = $self->self_rs->update(\%to_update); | |||||||||||||
| 22 | 2 | ▶ | if ($rows == 0) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 23 | 1 | $self->throw_exception( "Can't update ${self}: row not found" ); | |||||||||||||
| 24 | } elsif ($rows > 1) { | ||||||||||||||
| 25 | 0 | $self->throw_exception("Can't update ${self}: updated more than one row"); | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | 1 | $self->{_dirty_columns} = {}; | |||||||||||||
| 28 | 1 | $self->{related_resultsets} = {}; | |||||||||||||
| 29 | 1 | delete $self->{_column_data_in_storage}; | |||||||||||||
| 30 | 1 | return $self; | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 54 | 1; | |||||||||||||
| 34 | |||||||||||||||
| 35 | __END__ | ||||||||||||||
| 36 | |||||||||||||||
| 37 | =pod | ||||||||||||||
| 38 | |||||||||||||||
| 39 | =head1 NAME | ||||||||||||||
| 40 | |||||||||||||||
| 41 | DBIx::Class::Helper::Row::ProxyResultSetUpdate - Efficiently reuse ResultSet updates from results | ||||||||||||||
| 42 | |||||||||||||||
| 43 | =head1 SYNOPSIS | ||||||||||||||
| 44 | |||||||||||||||
| 45 | ResultSet: | ||||||||||||||
| 46 | |||||||||||||||
| 47 | package MyApp::Schema::ResultSet::Foo; | ||||||||||||||
| 48 | |||||||||||||||
| 49 | use parent 'DBIx::Class::ResultSet'; | ||||||||||||||
| 50 | |||||||||||||||
| 51 | sub update { | ||||||||||||||
| 52 | my ($self, $data) = @_; | ||||||||||||||
| 53 | |||||||||||||||
| 54 | die 'you fool!' if $data->{name} eq 'fool'; | ||||||||||||||
| 55 | |||||||||||||||
| 56 | return $self->next::method($data); | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | Result: | ||||||||||||||
| 60 | |||||||||||||||
| 61 | package MyApp::Schema::Result::Foo; | ||||||||||||||
| 62 | |||||||||||||||
| 63 | use parent 'DBIx::Class::Core'; | ||||||||||||||
| 64 | |||||||||||||||
| 65 | __PACKAGE__->load_components(qw( Helper::Row::ProxyResultSetUpdate )); | ||||||||||||||
| 66 | |||||||||||||||
| 67 | ... | ||||||||||||||
| 68 | |||||||||||||||
| 69 | or with L<DBIx::Class::Candy>: | ||||||||||||||
| 70 | |||||||||||||||
| 71 | package MyApp::Schema::Result::Foo; | ||||||||||||||
| 72 | |||||||||||||||
| 73 | use DBIx::Class::Candy -components => ['Helper::Row::ProxyResultSetMethod']; | ||||||||||||||
| 74 | |||||||||||||||
| 75 | ... | ||||||||||||||
| 76 | |||||||||||||||
| 77 | =head1 DESCRIPTION | ||||||||||||||
| 78 | |||||||||||||||
| 79 | This module makes reusing resultset updates from a result trivially easy. | ||||||||||||||
| 80 | Often the only way that people share update methods is by overriding update | ||||||||||||||
| 81 | in their resultset to use L<DBIx::Class::ResultSet/update_all>. Unfortunately, | ||||||||||||||
| 82 | that can end up being wildly inefficient. Instead, if you can write your | ||||||||||||||
| 83 | update in terms of the resultset, you can make your code much faster and more | ||||||||||||||
| 84 | efficient. | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =head1 AUTHOR | ||||||||||||||
| 87 | |||||||||||||||
| 88 | Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com> | ||||||||||||||
| 89 | |||||||||||||||
| 90 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 91 | |||||||||||||||
| 92 | This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt. | ||||||||||||||
| 93 | |||||||||||||||
| 94 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 95 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 96 | |||||||||||||||
| 97 | =cut | ||||||||||||||