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