| 1 | package DB::Batch::Do; | ||
| 2 | # encapsulate write functionality | ||
| 3 | # | ||
| 4 | # Copyright 2010, Chris Becker <clbecker@gmail.com> | ||
| 5 | # | ||
| 6 | # Original work sponsered by Shutterstock, LLC. http://shutterstock.com | ||
| 7 | # | ||
| 8 | # This program is free software; you can redistribute it and/or modify | ||
| 9 | # it under the same terms as Perl itself. | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my %args = @_; | ||
| 17 | my $self = bless \%args, $class; | ||
| 18 | return $self; | ||
| 19 | } | ||
| 20 | |||
| 21 | ## | ||
| 22 | # Execute a write to the database in batches | ||
| 23 | # execute the given query until all bind variables have been bound in batches | ||
| 24 | sub do_batch { | ||
| 25 | my $self = shift; | ||
| 26 | my %args = @_; | ||
| 27 | my $parent = $args{parent}; | ||
| 28 | my $query_str = $args{query}; | ||
| 29 | my $opts = $args{opts}; | ||
| 30 | my $dbi_binds = $args{dbi_binds} || []; | ||
| 31 | |||
| 32 | die 'query not defined' unless ($query_str); | ||
| 33 | |||
| 34 | my $dbh = $parent->get_dbh(); | ||
| 35 | my $log = $opts->{log}; | ||
| 36 | my $H = $opts->{output}; | ||
| 37 | |||
| 38 | my $query = $parent->get_query_object($query_str,$opts); | ||
| 39 | my $bindvalues = $parent->get_bindvalue_object($query,$opts); | ||
| 40 | my $key = $query->get_key(); # get hash key of query w/ placeholders | ||
| 41 | |||
| 42 | my @batch = $bindvalues->increment(); | ||
| 43 | |||
| 44 | while(@batch) { | ||
| 45 | |||
| 46 | my $bound_query = $query->bind_batch(\@batch); # bind current batch of data to placeholders | ||
| 47 | |||
| 48 | # run post hook subroutine if provided in opts | ||
| 49 | $parent->{__last_query_key__} = $key; | ||
| 50 | $parent->{__last_query__} = $bound_query; # accessed with Batch::Main->get_last_query() | ||
| 51 | |||
| 52 | if (defined $opts->{pre_hook} && ref $opts->{pre_hook} eq 'CODE') { | ||
| 53 | $opts->{pre_hook}->(); | ||
| 54 | } | ||
| 55 | |||
| 56 | $opts->{log}->info($query->bindstr($bound_query,@$dbi_binds)) if ($opts->{log}); | ||
| 57 | |||
| 58 | if (defined $H) { | ||
| 59 | |||
| 60 | # send the query to whatever was specified in $opts->{output} | ||
| 61 | # if nothing was specified then the query is executed on the db | ||
| 62 | # | ||
| 63 | # if a sub ref was passed in then the sub is called with args [query,(dbi bind values)] | ||
| 64 | # if an array ref was passed in, then the fully bound query is added to the array | ||
| 65 | # if a filehandle was passed in, then the fully bound query is printed to the handle | ||
| 66 | if (ref($H) eq 'CODE') { | ||
| 67 | $H->($bound_query,@_); | ||
| 68 | } elsif (ref($H) eq 'ARRAY') { | ||
| 69 | push @$H, $query->bindstr($bound_query,@$dbi_binds); | ||
| 70 | } elsif(ref(\$H) eq 'GLOB') { | ||
| 71 | print $H $query->bindstr($bound_query,@$dbi_binds); | ||
| 72 | } | ||
| 73 | |||
| 74 | } else { | ||
| 75 | $dbh->do($bound_query,undef,@$dbi_binds); | ||
| 76 | } | ||
| 77 | |||
| 78 | # run post hook subroutine if provided in opts | ||
| 79 | if (defined $opts->{post_hook} && ref $opts->{post_hook} eq 'CODE') { | ||
| 80 | $opts->{post_hook}->(); | ||
| 81 | } | ||
| 82 | |||
| 83 | @batch = $bindvalues->increment(); | ||
| 84 | |||
| 85 | last unless (@batch); | ||
| 86 | $parent->auto_throttle($opts); | ||
| 87 | $parent->throttle($opts); | ||
| 88 | } | ||
| 89 | $parent->clear($key); | ||
| 90 | } | ||
| 91 | |||
| 92 | 1; |