3 Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
4 Copyright [2016-2024] EMBL-European Bioinformatics Institute
6 Licensed under the Apache License, Version 2.0 (the
"License");
7 you may not use
this file except in compliance with the License.
8 You may obtain a copy of the License at
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an
"AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License
for the specific language governing permissions and
16 limitations under the License.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
46 my $arr_ref = $helper->execute(
47 -SQL =>
'select name, age from tab where col =?',
49 my @row = @{ shift @_ };
50 return { name => $row[0], age => $row[1] };
55 print Dumper($arr_ref),
"\n";
56 # Prints out [name=>'name', age=>1] maybe ....
59 # For transactional work; only works if your MySQL table
60 # engine/database supports transactional work (such as InnoDB)
64 if ( $helper->execute_single_result(
65 -SQL =>
'select count(*) from tab'
68 return $helper->execute_update(
'delete from tab');
71 $helper->batch( -SQL =>
'insert into tab (?,?)',
72 -DATA => [ [ 1, 2 ], [ 1, 3 ], [ 1, 4 ] ] );
78 Easier database interaction
82 package Bio::EnsEMBL::Utils::SqlHelper;
91 use English qw( -no_match_vars ); #Used
for $PROCESS_ID
92 use Scalar::Util qw(weaken); #Used to not hold a strong ref to DBConnection
100 Returntype : Instance of helper
101 Exceptions : If the
object given as a DBConnection is not one or it
104 Description : Creates a
new instance of
this object.
106 my $dba =
get_dba(
'mydb'); # New DBAdaptor from somewhere
108 -DB_CONNECTION => $dba->dbc()
111 $helper->execute_update( -SQL =>
'update tab set flag=?',
117 my ( $class, @args ) = @_;
119 my ($db_connection) = rearrange([qw(db_connection)], @args);
121 my $self = bless( {}, ref($class) || $class );
122 throw(
'-DB_CONNECTION construction parameter was undefined.')
123 unless defined $db_connection;
124 $self->db_connection($db_connection);
131 =head2 db_connection()
133 Arg [1] :
Bio::
EnsEMBL::DBSQL::DBConnection $db_connection
134 Description : Sets and retrieves the DBConnection
135 Returntype :
Bio::
EnsEMBL::DBSQL::DBConnection
136 Exceptions : If the
object given as a DBConnection is not one or if an
137 attempt is made to set the value more than once
143 my ($self, $db_connection) = @_;
144 if(defined $db_connection) {
145 if(exists $self->{db_connection}) {
146 throw(
'Cannot reset the DBConnection object; already defined ');
148 assert_ref($db_connection,
'Bio::EnsEMBL::DBSQL::DBConnection',
'db_connection');
149 $self->{db_connection} = $db_connection;
150 weaken $self->{db_connection};
152 return $self->{db_connection};
155 # --------- SQL Methods
159 =head2 execute() - Execute a SQL statement with a custom row handler
161 Arg [SQL] :
string SQL to execute
162 Arg [CALLBACK] : CodeRef; The callback to use
for mapping a row to a data
163 point; leave blank
for a
default mapping to a 2D array
164 Arg [USE_HASHREFS] :
boolean If set to
true will cause HashRefs to be returned
165 to the callback & not ArrayRefs
166 Arg [PARAMS] : ArrayRef The binding parameters to the SQL statement
167 Arg [PREPARE_PARAMS] :
boolean Parameters to be passed onto the Statement Handle
170 rather than a 2D array
172 Exceptions : If errors occur in the execution of the SQL
175 my $arr_ref = $helper->execute(
176 -SQL =>
'select a,b,c from tab where col =?',
178 my @row = @{ shift @_ };
179 return { A => $row[0], B => $row[1], C => $row[2] };
184 my $arr_ref = $helper->execute(
185 -SQL =>
'select a,b,c from tab where col =?',
189 return { A => $row->{a}, B => $row->{b}, C => $row->{c} };
194 Uses a callback defined by the sub decalaration. Here we specify how
195 the calling code will deal with each row of a database
's result set. The
196 sub can return any type of Object/hash/data structure you require.
198 Should you not specify a callback then a basic one will be assigned to
199 you which will return a 2D array structure e.g.
201 my $arr_ref = $helper->execute(
202 -SQL => 'select a,b,c from tab where col =?
',
205 This is equivalent to DBI's selectall_arrayref() subroutine.
207 As an extension to this method you can write a closure subroutine which
208 takes in two parameters. The first is the array/hash reference & the
209 second is the statement handle used to execute. 99% of the time you will
210 not need it but there are occasions where you do need it. An example of
213 my $conn = get_conn();
#From somwewhere
214 my $arr_ref = $conn->execute(
215 -SQL =>
'select a,b,c from tab where col =?',
218 my ( $row, $sth ) = @_;
219 #Then do something with sth
220 return { A => $row->[0], B => $row->[1], C => $row->[2] };
224 Any arguments to bind to the incoming statement. This can be a set of scalars
225 or a 2D array
if you need to specify any kind of types of sql objects i.e.
227 use DBI qw(:sql_types);
229 my $conn = get_conn();
230 my $arr_ref = $conn->execute(
232 'select a,b,c from tab where col =? and num_col=? and other=?',
235 my @row = @{ shift @_ };
236 return { A => $row[0], B => $row[1], C => $row[2] };
238 -PARAMS => [
'1', SQL_VARCHAR ],
242 Here we
import DBI
's sql types into our package and then pass in
243 multiple anonymous array references as parameters. Each param is
244 tested in the input and if it is detected to be an ARRAY reference we
245 dereference the array and run DBI's bind_param method. In fact you can
246 see each part of the incoming paramaters array as the contents to call
247 C<bind_param> with. The only difference is the
package tracks the bind
250 We can get back a L<Bio::EnsEMBL::Utils::Iterator> object which can be used
251 to iterate over the results set without first materializing the data into
252 memory. An example would be:
254 my $iterator = $helper->execute(
255 -SQL => 'select a,b,c from tab where col =?',
258 while($iterator->has_next()) {
259 my $row = $iterator->next();
263 This is very useful
for very large datasets.
268 my ( $self, @args ) = @_;
269 my ($sql, $callback, $use_hashrefs, $params, $prepare_params, $iterator) =
270 rearrange([qw(sql callback use_hashrefs params prepare_params iterator)], @args);
273 #If no callback then we execute using a default one which returns a 2D array
274 if(!defined $callback) {
276 $callback = $self->_mappers()->{hash_ref};
279 $callback = $self->_mappers()->{array_ref};
283 return $self->_execute( $sql, $callback, $has_return, $use_hashrefs, $params, $prepare_params, $iterator );
288 =head2 execute_simple()
290 Arg [SQL] :
string $sql
291 Arg [PARAMS] : ArrayRef $params
292 Arg [CALLBACK] : CodeRef $callback
293 Returntype : ArrayRef of 1D elements
294 Exceptions : If errors occur in the execution of the SQL
296 Example : my $classification =
297 $helper->execute_simple(
298 -SQL => 'select meta_val from meta where meta_key =? order by meta_id',
299 -PARAMS => ['species.classification']
301 Description: Similar to execute() but without a sub-routine reference.
302 Using this code assumes you want an array of single scalar values
303 as returned by the given SQL statement.
307 my ( $self, @args ) = @_;
308 my ($sql, $params, $callback) = rearrange([qw(sql params callback)], @args);
310 my $use_hashrefs = 0;
311 $callback ||= $self->_mappers()->{first_element};
312 return $self->_execute($sql, $callback, $has_return, $use_hashrefs, $params);
317 =head2 execute_no_return()
319 Arg [SQL] :
string sql
320 Arg [CALLBACK] : CodeRef The callback to use for mapping a row to a data point;
321 we assume you are assigning into a data structure which
322 has requirements other than simple translation into an
324 Arg [USE_HASHREFS] :
boolean If set to true will cause HashRefs to be returned
325 to the callback and not ArrayRefs
326 Arg [PARAMS] : ArrayRef The binding parameters to the SQL statement
328 Exceptions : If errors occur in the execution of the SQL
330 Description: Whilst all other execute methods will return something; this assumes that the
331 given mapper subroutine will be performing the business of placing values
332 somewhere or doing something with them.
334 There is a huge temptation to nest queries using this method; do not! Execute
335 the values into an array using one of the other methods then
run your subqueries
336 on them; or make a better first query. SQL is flexible; so use it.
340 sub execute_no_return {
341 my ( $self, @args ) = @_;
342 my ($sql, $callback, $use_hashrefs, $params) = rearrange([qw(sql callback use_hashrefs params)], @args);
343 throw(
'No callback defined but this is a required parameter for execute_no_return()')
if ! $callback;
345 my $prepare_params = [];
346 $self->_execute( $sql, $callback, $has_return, $use_hashrefs, $params);
352 =head2 execute_into_hash()
354 Arg [SQL] :
string $sql
355 Arg [CALLBACK] : CodeRef The callback to use for mapping to a value in a hash
356 keyed by the first element in your result set;
357 leave blank for a default mapping to a scalar value
358 of the second element
359 Arg [PARAMS] : The binding parameters to the SQL statement
360 Returntype : HashRef keyed by column 1 & value is the return of callback
361 Exceptions : If errors occur in the execution of the SQL
363 Description: A variant of the execute methods but rather than returning a list of
364 mapped results, this will assume the first column of a returning
map and
365 the calling subroutine will
map the remainder of your return as the
368 This code can handle simple queries to hashes, complex value mappings
369 and repeated mappings for the same key.
375 my $sql = 'select key, one, two from table where something =?';
377 # Argument one is a row from the result, argument two is any previously seen value for the first column of the row
378 my ( $row, $value ) = @_;
379 #Ignore field 0 as that is being used for the key
380 my $obj = Some::Obj->new( one => $row->[1], two => $row->[2] );
384 my $hash = $helper->execute_into_hash( -SQL => $sql,
385 -CALLBACK => $mapper,
386 -PARAMS => [
'val'] );
388 #Or the default simplistic invocation
389 my $sql =
'select biotype, count(gene_id) from gene group by biotype';
390 my $biotype_hash = $conn->execute_into_hash( -SQL => $sql );
391 print $biotype_hash->{protein_coding} || 0,
"\n";
393 # More complicated mapping, result hash will be keyed on "meta_key"
394 # Hash will contain lists of values linked to their meta_key
395 my %args = ( -SQL =>
'select meta_key, meta_value from meta '
396 .
'where meta_key =? order by meta_id',
397 -PARAMS => [
'species.classification'] );
399 my $hash = $helper->execute_into_hash(
402 my ( $row, $value ) = @_;
403 $value = []
if !defined $value;
404 push( @{$value}, $row->[1] );
408 # Add new values to an already seen existing meta_key
409 $hash = $helper->execute_into_hash(
412 my ( $row, $value ) = @_;
413 if ( defined $value ) {
414 # Calling code is dealing with $row->[0], so we only have to handle the remaining columns
415 push( @{$value}, $row->[1] );
418 my $new_value = [ $row->[1] ];
424 sub execute_into_hash {
425 my ( $self, @args ) = @_;
426 my ($sql, $callback, $params) = rearrange([qw(sql callback params)], @args);
429 #If no callback then we execute using a default one which sets value to 2nd element
430 if(!defined $callback) {
431 $callback = $self->_mappers()->{second_element};
434 #Default mapper uses the 1st key + something else from the mapper
438 my $value = $hash->{$key};
439 my $new_value = $callback->($row, $value);
440 if(defined $new_value) {
441 $hash->{ $key } = $new_value;
446 $self->execute_no_return(
448 -CALLBACK => $mapper,
457 =head2 execute_single_result()
459 Arg [SQL] :
string $sql
460 Arg [CALLBACK] : CodeRef The callback to use for mapping a row to a data point;
461 leave blank for a default scalar mapping
462 Arg [USE_HASHREFS] :
boolean If set to true will cause HashRefs to be returned
463 to the callback & not ArrayRefs
464 Arg [PARAMS] : ArrayRef The binding parameters to the SQL statement
465 Arg [NO_ERROR] : Boolean Flag to indicate that the code should not
466 throw an error when row counts are not equal to 1
468 Exceptions : If errors occur in the execution of the SQL, if the query
469 returned more than 1 row and if we found no rows.
473 $helper->execute_single_result(
474 -SQL => 'select count(*) from meta where species_id =?',
477 Description : Very similar to execute() except it will raise an exception if we have more
478 or less than one row returned
481 sub execute_single_result {
482 my ( $self, @args ) = @_;
483 my ($sql, $callback, $use_hashrefs, $params, $no_error) = rearrange(
484 [qw(sql callback use_hashrefs params no_error)], @args);
486 my $results = $self->execute_simple(
488 -CALLBACK => $callback,
489 -USE_HASHREFS => $use_hashrefs,
493 my $result_count = scalar(@{$results});
494 if(! $no_error && $result_count != 1) {
495 $params = []
if ! $params;
496 my $type = ($result_count == 0) ?
'No' :
'Too many';
497 my $msg =
"${type} results returned. Expected 1 but got $result_count for query '${sql}' with params [";
498 $msg .= join(
',',
map {(defined $_) ? $_ :
'-undef-';} @{$params} );
502 return $results->[0]
if defined $results->[0];
510 Arg [CALLBACK] : CodeRef The callback used for transaction isolation; once
511 the subroutine exists the code will decide on rollback
513 Arg [RETRY] : integer the number of retries to attempt with this
514 transactional block. Defaults to 0.
515 Arg [PAUSE] : integer the time in seconds to pause in-between retries.
516 Defaults to 1. Fractions are allowed as use delegate to
517 Time::HiRes' sleep function
518 Arg [CONDITION] : CodeRef allows you to inspect the exception raised
519 and should your callback return true then the
520 retry will be attempted. If not given then all
521 exceptions mean attempt a retry (if specified)
522 Returntype : Return of the callback
523 Exceptions : If errors occur in the execution of the SQL
526 my $val = $helper->transaction(
533 # Or without named arguments
534 my $val = $helper->transaction(
541 # To allow retries (use sparingly, and see description)
542 my $val = $helper->transaction(
551 # Only retry when we have an error containing the phrase "deadlock"
552 my $val = $helper->transaction(
553 -RETRY => 3, -PAUSE => 2,
561 return ( $error =~ /deadlock/ ) ? 1 : 0;
566 Creates a transactional block which will ensure that the connection is
567 committed when your submmited subroutine has finished or will rollback
568 in the
event of an error occuring in your block.
570 The code will always force AutoCommit off but will restore it to its
571 previous setting. If your DBI/DBD driver does not support manual
572 commits then
this code will
break. The code will turn off the
573 disconnect_when_idle() method to allow transactions to work as
576 An effect of using REPEATABLE READ transaction isolation (InnoDB's
577 default) is that your data is as fresh as when you started your current
578 transaction. To ensure the freshest data use SELECT ... FROM ... LOCK
579 IN SHARE MODE> or SELECT ... FROM ... LOCK FOR UPDATE if you are
580 going to issue updates.
582 Creating a transaction within a transaction results in the commit
583 rollback statements occuring in the top level transaction. That way any
584 block of code which is meant to to be transaction can be wrapped in
585 this block ( assuming the same instance of SQLHelper is passed around and
588 You can also request the -RETRY of a transactional block of code which is
589 causing problems. This can indicate your programming model is broken,
592 The -RETRY argument indicates the number of times we attempt the transaction
593 and -PAUSE indicates the time in-between attempts. These retries will
594 only occur in the root transaction block i.e. you cannot influence the
595 retry system in a sub-transaction. You can influence if the retry is done with
596 the -CONDITION argument which accepts a Code reference (same as the
597 -CALLBACK parameter). This allows you to inspect the error thrown to
598 retry only in some situations e.g.
603 my ($self, @args) = @_;
605 my ($callback, $retry, $pause, $condition) = rearrange([qw(callback retry pause condition)], @args);
607 throw(
'-CALLBACK was not a CodeRef. Got a reference of type ['.ref($callback).
']. Check your parameters')
608 unless check_ref($callback,
'CODE');
611 $retry = 0 unless defined $retry;
612 $pause = 1 unless defined $pause;
613 if(! defined $condition) {
619 assert_ref($condition,
'CODE',
'-CONDITION');
621 my $dbc = $self->db_connection();
628 #If we were already in a transaction then we do not do any management of the
629 #session & wait for the parent transaction(s) to finish
630 my $perform_transaction = $self->_perform_transaction_code();
631 if($perform_transaction) {
632 ($original_dwi, $ac) = $self->_enable_transaction();
635 #If we were in a transaction then ignore any attempts at retry here
639 for(my $iteration = 0; $iteration <= $retry; $iteration++) {
641 $result = $callback->($dbc);
642 $dbc->db_handle()->commit()
if $perform_transaction;
645 #If we were allowed to deal with the error then we apply rollbacks & then
646 #retry or leave to the remainder of the code to throw
647 if($perform_transaction && $error) {
648 eval { $dbc->db_handle()->rollback(); };
649 #If we were not on our last iteration then warn & allow the retry
650 if($iteration != $retry) {
651 if($condition->($error)) {
652 warn(
"Encountered error on attempt ${iteration} of ${retry} and have issued a rollback. Will retry after sleeping for $pause second(s): $error");
653 Time::HiRes::sleep $pause;
656 last; #
break early
if condition of error was not matched
661 #Always break the loop if we had a successful attempt
665 if($perform_transaction) {
666 $self->_disable_transaction($original_dwi, $ac);
669 throw(
"ABORT: Transaction aborted because of error: ${error}")
if $error;
676 =head2 execute_update()
678 Arg [SQL] :
string $sql
679 Arg [CALLBACK] : CodeRef The callback to use for calling methods on the
680 DBI statement handle or DBConnection
object after an
682 Arg [PARAMS] : ArrayRef The binding parameters to the SQL statement
683 Arg [PREPARE_PARAMS] : ArrayRef Parameters to bind to the prepare() StatementHandle call
684 Returntype : integer - Number of rows affected
685 Exceptions : If errors occur in the execution of the SQL
687 Description: Used for performing updates but conforms to the normal execute statement subroutines.
689 use DBI qw(:sql_types);
690 $helper->execute_update(-SQL => 'update tab set name = ? where
id =?',
691 -PARAMS => [ 'andy', [ 1, SQL_INTEGER ] ] );
693 #If you need to do something a bit more advanced with your database then you can
694 #give the method a closure and this will be called after the execute has been
698 $helper->execute_update(
699 -SQL =>
'insert into tab (name) values(?)',
701 my ( $sth, $dbh, $rv ) = @_;
702 $obj->{
id} = $dbh->{mysql_insertid};
704 -PARAMS => [ $obj->name() ] );
706 #This lets us access the statement handle, the database handle and
707 #the return value from $sth->execute, to access other properties such as
708 #the last identifier inserted.
713 my ($self, @args) = @_;
714 my ($sql, $callback, $params, $prepare_params) = rearrange([qw(sql callback params prepare_params)], @args);
719 @prepare_params = @{$prepare_params}
if check_ref($prepare_params,
'ARRAY');
720 $sth = $self->db_connection()->prepare($sql, @prepare_params);
721 $self->_bind_params($sth, $params);
722 $rv = $sth->execute();
723 $callback->($sth, $self->db_connection()->db_handle(), $rv)
if $callback;
726 $self->_finish_sth($sth);
728 my $params = join
' ',
map { (defined $_) ? $_ : q{undef} } @{$params};
729 throw(
"Cannot apply sql '${sql}' with params '${params}': ${error}");
734 =head2 execute_with_sth()
736 Arg [SQL] :
string $sql
737 Arg [CALLBACK] : CodeRef The callback to use for working with the statement
738 handle once returned. This is not a mapper.
739 Arg [PARAMS] : ArrayRef The binding parameters to the SQL statement
740 Arg [PREPARE_PARAMS] : ArrayRef Used to pass parameters to the statement handle
742 Description : Run query without worrying statement handles and such.
743 Very similar to execute() except this gives you full control over the
744 lifecycle of the statement handle and how you wish to proceed with working
745 with a statement handle. This is for situations where you believe going through
746 the mappers causes too much of a slow-down (since we have to execute a
747 subroutine for every row in order to
map it correctly).
749 However please benchmark before adopting this method as it increases the
750 complexity of your code and the mapper slowness only becomes apparent when
751 working with very large numbers of rows.
752 Returntype : Anything you wish to return from the callback
753 Exceptions : If errors occur in the execution of the SQL
756 my $meta_count = $helper->execute_with_sth(
757 -SQL => 'select count(*) from meta where species_id =?',
762 $sth->bind_columns( \$count );
763 while ( $sth->fetch ) {
773 sub execute_with_sth {
774 my ($self, @args) = @_;
775 my ($sql, $callback, $params, $prepare_params) = rearrange([qw(sql callback params prepare_params)], @args);
776 my $sth = $self->_base_execute( $sql, $params, $prepare_params, $callback );
777 my $result = eval {$callback->($sth)};
779 $self->_finish_sth($sth);
780 die $error
if $error;
788 Arg [SQL] :
string $sql
789 Arg [CALLBACK] : CodeRef The callback to use for working with the statement
790 handle once returned; specify this or -DATA
791 Arg [DATA] : ArrayRef The data to insert; specify this or -CALLBACK
792 Arg [COMMIT_EVERY] : Integer defines the rate at which to issue commits to
793 the DB handle. This is important when working with
794 InnoDB databases since it affects the speed of rollback
795 (larger gaps inbetween commits means more to rollback).
797 Ignored if using the callback version.
798 Arg [PREPARE_PARAMS] : ArrayRef Used to pass parameters to the statement handle
800 Returntype : integer rows updated
801 Exceptions : If errors occur in the execution of the SQL
804 my $alotofdata = getitfromsomewhere();
806 -SQL => 'insert into table (one,two) values(?,?)',
808 my ( $sth, $dbc ) = @_;
809 foreach my $data (@alotofdata) {
810 $sth->execute( @{$data} );
814 #Or for a 2D array data driven approach
815 $helper->batch( -SQL =>
'insert into table (one,two) values(?,?)',
816 -DATA => $alotofdata );
819 Description: Takes in a sql statement and a code reference. Your SQL is converted into a
820 prepared statement and then given as the first parameter to the closure. The
821 second parameter is the DBH which created the statement. This is intended
822 to let you
do mass insertion into a database without the need to
823 re-preparing the same statement.
825 This can be combined with the transaction() code to provide a construct
826 which does batch insertion and is transactionally aware.
828 We can also use data based batch insertions i.e.
832 #Or if using the DBI types:
833 # [ [ [ 1, SQL_INTEGER ], [ 2, SQL_INTEGER ] ],
834 # [ [ 3, SQL_INTEGER ], [ 4, SQL_INTEGER ] ] ];
836 my $alotofdata = getitfromsomewhere();
837 $helper->batch( -SQL =>
'insert into table (one,two) values(?,?)',
838 -DATA => $alotofdata );
840 This does exactly the same as the previous example.
842 All batch statements will
return the value the callback computes. If you are
843 using the previous example with a data array then the code will
return the
844 number affected rows by the query.
849 my ($self, @args) = @_;
850 my ($sql, $callback, $data, $commit_every, $prepare_params) =
851 rearrange([qw(sql callback data commit_every prepare_params)], @args);
853 if(! defined $callback && ! defined $data) {
854 throw(
'You need to define a callback for insertion work or the 2D data array');
858 if(defined $callback) {
859 $result = $self->_callback_batch($sql, $callback, $prepare_params);
862 $result = $self->_data_batch($sql, $data, $commit_every, $prepare_params);
864 return $result
if defined $result;
868 #------- Internal methods
870 my $default_mappers = {
871 first_element => sub {
875 second_element => sub {
881 return [@{$row}]; #copy of array done because DBI
's array is read only
885 return {%{$row}}; #applying same logic as above
891 return $default_mappers;
894 sub _perform_transaction_code {
896 return $self->{_transaction_active}->{$PROCESS_ID} ? 0 : 1;
899 sub _enable_transaction {
901 my $dbc = $self->db_connection();
902 my $original_dwi = $dbc->disconnect_when_inactive();
903 $dbc->disconnect_when_inactive(0);
904 my $ac = $dbc->db_handle()->{'AutoCommit
'};
905 $dbc->db_handle()->{'AutoCommit
'} = 0;
906 $self->{_transaction_active}->{$PROCESS_ID} = 1;
907 return ($original_dwi, $ac);
910 sub _disable_transaction {
911 my ($self, $original_dwi, $ac) = @_;
912 my $dbc = $self->db_connection();
913 $dbc->db_handle()->{'AutoCommit
'} = $ac;
914 $dbc->disconnect_when_inactive($original_dwi);
915 delete $self->{_transaction_active}->{$PROCESS_ID};
920 my ( $self, $sth, $params ) = @_;
922 return if ! defined $params; #Return quickly if we had no data
924 if(! check_ref($params, 'ARRAY
')) {
925 throw(qq{The given parameters reference '${params}
' is not an ARRAY; wrap in an ArrayRef});
929 foreach my $param (@{$params}) {
930 if ( check_ref($param, 'ARRAY
') ) {
931 $sth->bind_param( $count, @{$param} );
934 $sth->bind_param( $count, $param );
942 my ( $self, $sql, $callback, $has_return, $use_hashrefs, $params, $prepare_params, $iterator ) = @_;
944 throw('Not given a mapper. _execute() must always been given a CodeRef') unless check_ref($callback, 'CODE');
946 my $sth = $self->_base_execute($sql, $params, $prepare_params);
950 $sth_processor = sub {
951 return unless $sth->{Active};
952 while( my $row = $sth->fetchrow_hashref() ) {
953 my $v = $callback->($row, $sth);
954 return $v
if $has_return;
956 $self->_finish_sth($sth);
961 $sth_processor = sub {
962 return unless $sth->{Active};
963 while( my $row = $sth->fetchrow_arrayref() ) {
964 my $v = $callback->($row, $sth);
965 return $v
if $has_return;
967 $self->_finish_sth($sth);
974 return $iter
if $iterator;
978 #Force iteration if we had no return since the caller is expecting this
985 my ( $self, $sql, $params, $prepare_params) = @_;
987 $params = [] unless $params;
989 my $conn = $self->db_connection;
994 @prepare_params = @{$prepare_params}
if check_ref($prepare_params,
'ARRAY');
995 $sth = $conn->prepare($sql, @prepare_params);
996 throw(
"Cannot continue as prepare() did not return a handle with prepare params '@prepare_params'")
998 $self->_bind_params( $sth, $params );
1004 throw(
"Cannot run '${sql}' with params '@{$params}' due to error: $error")
if $error;
1011 my ($self, $sth) = @_;
1012 eval { $sth->finish()
if defined $sth; };
1013 warn(
'Cannot finish() the statement handle: $@') if $@;
1017 sub _callback_batch {
1018 my ($self, $sql, $callback, $prepare_params) = @_;
1024 @prepare_params = @{$prepare_params}
if check_ref($prepare_params,
'ARRAY');
1025 $sth = $self->db_connection()->prepare($sql, @prepare_params);
1026 $closure_return = $callback->($sth, $self->db_connection());
1029 $self->_finish_sth($sth);
1030 throw(
"Problem detected during batch work: $error")
if $error;
1032 return $closure_return
if defined $closure_return;
1037 my ($self, $sql, $data, $commit_every, $prepare_params) = @_;
1040 assert_ref($data,
'ARRAY',
'-DATA');
1041 my $data_length = scalar(@{$data});
1042 return 0 unless $data_length > 0;
1043 my $first_row = $data->[0];
1044 throw(
'I expect to work with a 2D ArrayRef but this is not one') unless check_ref($first_row,
'ARRAY');
1046 my $callback = sub {
1047 my ($sth, $dbc) = @_;
1048 my $total_affected = 0;
1049 #Iterate over each data point
1050 for(my $data_index = 0; $data_index < $data_length; $data_index++) {
1051 my $row = $data->[$data_index];
1052 $self->_bind_params($sth, $row);
1053 my $affected = eval {$sth->execute()};
1055 throw(
"Problem working with $sql with params @{$row}: $@");
1057 my $num_affected = ($affected) ? $affected : 0; #Get around DBI
's 0E0
1058 $total_affected += $num_affected;
1060 #Lets us do a commit once every x rows apart from 0. We also finish
1061 #off with a commit if the code told us we were doing it
1063 if( ($data_index % $commit_every == 0) && $data_index != 0) {
1064 $dbc->db_handle()->commit();
1069 #finish off with a commit if the code told us we were doing it
1071 $dbc->db_handle()->commit();
1074 return $total_affected || 0;
1077 return $self->_callback_batch($sql, $callback, $prepare_params)