BatchUpdate.pm untested

filter: Statementstmt 0.0% 0 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 27 CC 1 SCAR 6.9
CC
1
Cov
0%
batch_update
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package DBIx::Class::BatchUpdate;
2 $DBIx::Class::BatchUpdate::VERSION = '1.003';
3 use base qw(DBIx::Class);
4 use strict;
5 use warnings;
6 use true;
7  
8 use DBIx::Class::BatchUpdate::Update;
9  
10  
11  
12 sub batch_update {
13 my $self = shift;
14 my ($rows) = @_;
15 return DBIx::Class::BatchUpdate::Update->new({
16 resultset => $self,
17 rows => $rows,
18 })->update();
19 }
20  
21  
22  
23 =head1 NAME
24  
25 DBIx::Class::BatchUpdate - Update batches of DBIC rows with as few queries as possible
26  
27  
28 =head1 SYNOPSIS
29  
30 # In your result class, e.g. MySchema::ResultSet::Book
31 __PACKAGE__->load_components("BatchUpdate");
32  
33  
34 # In your code, update loads of row objects and keep track of them
35 $book_row1->is_out_of_print(1);
36 $book_row2->is_out_of_print(1);
37 $book_row3->is_out_of_print(1);
38 $book_row3->price(42);
39 my $book_rows = [ $book_row1, $book_row2, $book_row3 ];
40  
41 # Batch update all rows in as few UPDATE statements as possible
42 $schema->resultset("Book")->batch_update($book_rows);
43  
44 # SQL queries
45 # 1 UPDATE for all the rows with is_out_of_print: 1
46 # 1 UPDATE for all the rows with is_out_of_print: 1, price: 42
47  
48  
49 # Alternatively, create your own BatchUpdate::Update object:
50 use DBIx::Class::BatchUpdate::Update;
51  
52 DBIx::Class::BatchUpdate::Update->new({
53 rows => $rows,
54 })->update();
55  
56  
57 =head1 DESCRIPTION
58  
59 This module is for when you have loads of DBIC rows to update as part
60 of some large scale processing, and you want to avoid making
61 individual calls to $row->update for each of them. If the number of
62 dirty rows is large, the many round-trips to the database will be
63 quite time consuming.
64  
65 So instead of calling $row->update you collect all the dirty row
66 objects (of the same Result class) for later and then let
67 DBIx::Class::BatchUpdate update the database with as few queries as
68 possible.
69  
70 This means that if the same columns have been set to the same value in
71 all the rows, this will be done in a single query. The more different
72 combinations of columns and values there are in rows, the more queries
73 are required.
74  
75  
76 =head1 USAGE
77  
78 =head2 As a DBIC component
79  
80 # In your result class, e.g. MySchema::ResultSet::Book
81 __PACKAGE__->load_components("BatchUpdate");
82  
83 Adding the DBIC component to a ResultSet class enables you to call
84 ->batch_update on the resultset.
85  
86 It is even more useful to put this in your base class for all the
87 ResultSet classes, so it's available for all resultsets.
88  
89  
90 =head3 $resultset->batch_update($rows)
91  
92 Make UPDATE queries on the $resultset to update all the dirty columns
93 in the arrayref $rows.
94  
95 Make the fewest number of queries given the different values to
96 update.
97  
98 The $rows must all be of the same ResultSet class as $resultset. $rows
99 may well be an empty arrayref. All the $rows must already exist in the
100 database and have an ->id.
101  
102  
103  
104 =head2 As a regular module
105  
106 =head3 $batch_update->update($rows)
107  
108 Example:
109  
110 use DBIx::Class::BatchUpdate::Update;
111 DBIx::Class::BatchUpdate::Update->new({ rows => $rows })->update();
112  
113 This is functionally the same as the above.
114  
115 Note that all $rows must be of the same Result class.
116  
117  
118  
119 =head1 DEVELOPMENT
120  
121 =head2 Author
122  
123 Johan Lindstrom, C<< <johanl [AT] cpan.org> >>
124  
125  
126 =head2 Contributors
127  
128  
129  
130 =head2 Source code
131  
132 L<https://github.com/jplindstrom/p5-DBIx-Class-BatchUpdate>
133  
134  
135 =head2 Bug reports
136  
137 Please report any bugs or feature requests on GitHub:
138  
139 L<https://github.com/jplindstrom/p5-DBIx-Class-BatchUpdate/issues>.
140  
141  
142 =head2 Caveats
143  
144  
145 =head1 COPYRIGHT & LICENSE
146  
147 Copyright 2016- Broadbean Technologies, All Rights Reserved.
148  
149 This program is free software; you can redistribute it and/or modify it
150 under the same terms as Perl itself.
151  
152  
153 =head1 ACKNOWLEDGEMENTS
154  
155 Thanks to Broadbean for providing time to open source this during one
156 of the regular Hack-days.
157  
158 =cut