| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 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 | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 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. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | |||
| 21 | =head1 CONTACT | ||
| 22 | |||
| 23 | Please email comments or questions to the public Ensembl | ||
| 24 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 25 | |||
| 26 | Questions may also be sent to the Ensembl help desk at | ||
| 27 | <http://www.ensembl.org/Help/Contact>. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Bio::EnsEMBL::Utils::SqlHelper | ||
| 34 | |||
| 35 | =head1 VERSION | ||
| 36 | |||
| 37 | $Revision$ | ||
| 38 | |||
| 39 | =head1 SYNOPSIS | ||
| 40 | |||
| 41 | use Bio::EnsEMBL::Utils::SqlHelper; | ||
| 42 | |||
| 43 | my $helper = | ||
| 44 | Bio::EnsEMBL::Utils::SqlHelper->new( -DB_CONNECTION => $dbc ); | ||
| 45 | |||
| 46 | my $arr_ref = $helper->execute( | ||
| 47 | -SQL => 'select name, age from tab where col =?', | ||
| 48 | -CALLBACK => sub { | ||
| 49 | my @row = @{ shift @_ }; | ||
| 50 | return { name => $row[0], age => $row[1] }; | ||
| 51 | }, | ||
| 52 | -PARAMS => ['A'] ); | ||
| 53 | |||
| 54 | use Data::Dumper; | ||
| 55 | print Dumper($arr_ref), "\n"; | ||
| 56 | # Prints out [name=>'name', age=>1] maybe .... | ||
| 57 | |||
| 58 | |||
| 59 | # For transactional work; only works if your MySQL table | ||
| 60 | # engine/database supports transactional work (such as InnoDB) | ||
| 61 | |||
| 62 | $helper->transaction( | ||
| 63 | -CALLBACK => sub { | ||
| 64 | if ( $helper->execute_single_result( | ||
| 65 | -SQL => 'select count(*) from tab' | ||
| 66 | ) ) | ||
| 67 | { | ||
| 68 | return $helper->execute_update('delete from tab'); | ||
| 69 | } else { | ||
| 70 | return | ||
| 71 | $helper->batch( -SQL => 'insert into tab (?,?)', | ||
| 72 | -DATA => [ [ 1, 2 ], [ 1, 3 ], [ 1, 4 ] ] ); | ||
| 73 | } | ||
| 74 | } ); | ||
| 75 | |||
| 76 | =head1 DESCRIPTION | ||
| 77 | |||
| 78 | Easier database interaction | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | package Bio::EnsEMBL::Utils::SqlHelper; | ||
| 83 | $Bio::EnsEMBL::Utils::SqlHelper::VERSION = '114.0.0'; | ||
| 84 | use warnings; | ||
| 85 | use strict; | ||
| 86 | |||
| 87 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 88 | use Bio::EnsEMBL::Utils::Scalar qw(assert_ref check_ref); | ||
| 89 | use Bio::EnsEMBL::Utils::Exception qw(throw); | ||
| 90 | use Bio::EnsEMBL::Utils::Iterator; | ||
| 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 | ||
| 93 | use Time::HiRes; | ||
| 94 | |||
| 95 | =pod | ||
| 96 | |||
| 97 | =head2 new() | ||
| 98 | |||
| 99 | Arg [DB_CONNECTION] : Bio::EnsEMBL::DBSQL::DBConnection $db_connection | ||
| 100 | Returntype : Instance of helper | ||
| 101 | Exceptions : If the object given as a DBConnection is not one or it | ||
| 102 | was undefined | ||
| 103 | Status : Stable | ||
| 104 | Description : Creates a new instance of this object. | ||
| 105 | Example : | ||
| 106 | my $dba = get_dba('mydb'); # New DBAdaptor from somewhere | ||
| 107 | my $helper = Bio::EnsEMBL::Utils::SqlHelper->new( | ||
| 108 | -DB_CONNECTION => $dba->dbc() | ||
| 109 | ); | ||
| 110 | |||
| 111 | $helper->execute_update( -SQL => 'update tab set flag=?', | ||
| 112 | -PARAMS => [1] ); | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub new { | ||
| 117 | my ( $class, @args ) = @_; | ||
| 118 | |||
| 119 | my ($db_connection) = rearrange([qw(db_connection)], @args); | ||
| 120 | |||
| 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); | ||
| 125 | |||
| 126 | return $self; | ||
| 127 | } | ||
| 128 | |||
| 129 | =pod | ||
| 130 | |||
| 131 | =head2 db_connection() | ||
| 132 | |||
| 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 | ||
| 138 | Status : Stable | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub db_connection { | ||
| 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 '); | ||
| 147 | } | ||
| 148 | assert_ref($db_connection, 'Bio::EnsEMBL::DBSQL::DBConnection', 'db_connection'); | ||
| 149 | $self->{db_connection} = $db_connection; | ||
| 150 | weaken $self->{db_connection}; | ||
| 151 | } | ||
| 152 | return $self->{db_connection}; | ||
| 153 | } | ||
| 154 | |||
| 155 | # --------- SQL Methods | ||
| 156 | |||
| 157 | =pod | ||
| 158 | |||
| 159 | =head2 execute() - Execute a SQL statement with a custom row handler | ||
| 160 | |||
| 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 | ||
| 168 | prepare call | ||
| 169 | Arg [ITERATOR] : boolean Request a Bio::EnsEMBL::Utils::Iterator | ||
| 170 | rather than a 2D array | ||
| 171 | Returntype : ArrayRef or Bio::EnsEMBL::Utils::Iterator | ||
| 172 | Exceptions : If errors occur in the execution of the SQL | ||
| 173 | Status : Stable | ||
| 174 | Example : | ||
| 175 | my $arr_ref = $helper->execute( | ||
| 176 | -SQL => 'select a,b,c from tab where col =?', | ||
| 177 | -CALLBACK => sub { | ||
| 178 | my @row = @{ shift @_ }; | ||
| 179 | return { A => $row[0], B => $row[1], C => $row[2] }; | ||
| 180 | }, | ||
| 181 | -PARAMS => ['A'] ); | ||
| 182 | |||
| 183 | #Or with hashrefs | ||
| 184 | my $arr_ref = $helper->execute( | ||
| 185 | -SQL => 'select a,b,c from tab where col =?', | ||
| 186 | -USE_HASHREFS => 1, | ||
| 187 | -CALLBACK => sub { | ||
| 188 | my $row = shift @_; | ||
| 189 | return { A => $row->{a}, B => $row->{b}, C => $row->{c} }; | ||
| 190 | }, | ||
| 191 | -PARAMS => ['A'] ); | ||
| 192 | |||
| 193 | Description: | ||
| 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. | ||
| 197 | |||
| 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. | ||
| 200 | |||
| 201 | my $arr_ref = $helper->execute( | ||
| 202 | -SQL => 'select a,b,c from tab where col =?', | ||
| 203 | -PARAMS => ['A'] ); | ||
| 204 | |||
| 205 | This is equivalent to DBI's selectall_arrayref() subroutine. | ||
| 206 | |||
| 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 | ||
| 211 | usage would be: | ||
| 212 | |||
| 213 | my $conn = get_conn(); #From somwewhere | ||
| 214 | my $arr_ref = $conn->execute( | ||
| 215 | -SQL => 'select a,b,c from tab where col =?', | ||
| 216 | -USE_HASHREFS => 1, | ||
| 217 | -CALLBACK => sub { | ||
| 218 | my ( $row, $sth ) = @_; | ||
| 219 | #Then do something with sth | ||
| 220 | return { A => $row->[0], B => $row->[1], C => $row->[2] }; | ||
| 221 | }, | ||
| 222 | -PARAMS => ['A'] ); | ||
| 223 | |||
| 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. | ||
| 226 | |||
| 227 | use DBI qw(:sql_types); | ||
| 228 | |||
| 229 | my $conn = get_conn(); | ||
| 230 | my $arr_ref = $conn->execute( | ||
| 231 | -SQL => | ||
| 232 | 'select a,b,c from tab where col =? and num_col=? and other=?', | ||
| 233 | -USE_HASHREFS => 1, | ||
| 234 | -CALLBACK => sub { | ||
| 235 | my @row = @{ shift @_ }; | ||
| 236 | return { A => $row[0], B => $row[1], C => $row[2] }; | ||
| 237 | }, | ||
| 238 | -PARAMS => [ '1', SQL_VARCHAR ], | ||
| 239 | [ 2, SQL_INTEGER ], | ||
| 240 | 'hello' ); | ||
| 241 | |||
| 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 | ||
| 248 | position for you. | ||
| 249 | |||
| 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: | ||
| 253 | |||
| 254 | my $iterator = $helper->execute( | ||
| 255 | -SQL => 'select a,b,c from tab where col =?', | ||
| 256 | -PARAMS => ['A'] | ||
| 257 | -ITERATOR => 1); | ||
| 258 | while($iterator->has_next()) { | ||
| 259 | my $row = $iterator->next(); | ||
| 260 | #Do something | ||
| 261 | } | ||
| 262 | |||
| 263 | This is very useful for very large datasets. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub execute { | ||
| 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); | ||
| 271 | my $has_return = 1; | ||
| 272 | |||
| 273 | #If no callback then we execute using a default one which returns a 2D array | ||
| 274 | if(!defined $callback) { | ||
| 275 | if($use_hashrefs) { | ||
| 276 | $callback = $self->_mappers()->{hash_ref}; | ||
| 277 | } | ||
| 278 | else { | ||
| 279 | $callback = $self->_mappers()->{array_ref}; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | return $self->_execute( $sql, $callback, $has_return, $use_hashrefs, $params, $prepare_params, $iterator ); | ||
| 284 | } | ||
| 285 | |||
| 286 | =pod | ||
| 287 | |||
| 288 | =head2 execute_simple() | ||
| 289 | |||
| 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 | ||
| 295 | Status : Stable | ||
| 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'] | ||
| 300 | ); | ||
| 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. | ||
| 304 | =cut | ||
| 305 | |||
| 306 | sub execute_simple { | ||
| 307 | my ( $self, @args ) = @_; | ||
| 308 | my ($sql, $params, $callback) = rearrange([qw(sql params callback)], @args); | ||
| 309 | my $has_return = 1; | ||
| 310 | my $use_hashrefs = 0; | ||
| 311 | $callback ||= $self->_mappers()->{first_element}; | ||
| 312 | return $self->_execute($sql, $callback, $has_return, $use_hashrefs, $params); | ||
| 313 | } | ||
| 314 | |||
| 315 | =pod | ||
| 316 | |||
| 317 | =head2 execute_no_return() | ||
| 318 | |||
| 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 | ||
| 323 | array | ||
| 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 | ||
| 327 | Returntype : None | ||
| 328 | Exceptions : If errors occur in the execution of the SQL | ||
| 329 | Status : Stable | ||
| 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. | ||
| 333 | |||
| 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. | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 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; | ||
| 344 | my $has_return = 0; | ||
| 345 | my $prepare_params = []; | ||
| 346 | $self->_execute( $sql, $callback, $has_return, $use_hashrefs, $params); | ||
| 347 | return; | ||
| 348 | } | ||
| 349 | |||
| 350 | =pod | ||
| 351 | |||
| 352 | =head2 execute_into_hash() | ||
| 353 | |||
| 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 | ||
| 362 | Status : Stable | ||
| 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 | ||
| 366 | hash's key. | ||
| 367 | |||
| 368 | This code can handle simple queries to hashes, complex value mappings | ||
| 369 | and repeated mappings for the same key. | ||
| 370 | |||
| 371 | |||
| 372 | |||
| 373 | Example: | ||
| 374 | |||
| 375 | my $sql = 'select key, one, two from table where something =?'; | ||
| 376 | my $mapper = sub { | ||
| 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] ); | ||
| 381 | return $obj; | ||
| 382 | }; | ||
| 383 | |||
| 384 | my $hash = $helper->execute_into_hash( -SQL => $sql, | ||
| 385 | -CALLBACK => $mapper, | ||
| 386 | -PARAMS => ['val'] ); | ||
| 387 | |||
| 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"; | ||
| 392 | |||
| 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'] ); | ||
| 398 | |||
| 399 | my $hash = $helper->execute_into_hash( | ||
| 400 | %args, | ||
| 401 | -CALLBACK => sub { | ||
| 402 | my ( $row, $value ) = @_; | ||
| 403 | $value = [] if !defined $value; | ||
| 404 | push( @{$value}, $row->[1] ); | ||
| 405 | return $value; | ||
| 406 | } ); | ||
| 407 | |||
| 408 | # Add new values to an already seen existing meta_key | ||
| 409 | $hash = $helper->execute_into_hash( | ||
| 410 | %args, | ||
| 411 | -CALLBACK => sub { | ||
| 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] ); | ||
| 416 | return; | ||
| 417 | } | ||
| 418 | my $new_value = [ $row->[1] ]; | ||
| 419 | return $new_value; | ||
| 420 | } ); | ||
| 421 | |||
| 422 | =cut | ||
| 423 | |||
| 424 | sub execute_into_hash { | ||
| 425 | my ( $self, @args ) = @_; | ||
| 426 | my ($sql, $callback, $params) = rearrange([qw(sql callback params)], @args); | ||
| 427 | my $hash = {}; | ||
| 428 | |||
| 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}; | ||
| 432 | } | ||
| 433 | |||
| 434 | #Default mapper uses the 1st key + something else from the mapper | ||
| 435 | my $mapper = sub { | ||
| 436 | my $row = shift @_; | ||
| 437 | my $key = $row->[0]; | ||
| 438 | my $value = $hash->{$key}; | ||
| 439 | my $new_value = $callback->($row, $value); | ||
| 440 | if(defined $new_value) { | ||
| 441 | $hash->{ $key } = $new_value; | ||
| 442 | } | ||
| 443 | return; | ||
| 444 | }; | ||
| 445 | |||
| 446 | $self->execute_no_return( | ||
| 447 | -SQL => $sql, | ||
| 448 | -CALLBACK => $mapper, | ||
| 449 | -PARAMS => $params | ||
| 450 | ); | ||
| 451 | |||
| 452 | return $hash; | ||
| 453 | } | ||
| 454 | |||
| 455 | =pod | ||
| 456 | |||
| 457 | =head2 execute_single_result() | ||
| 458 | |||
| 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 | ||
| 467 | Returntype : Scalar | ||
| 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. | ||
| 470 | Status : Stable | ||
| 471 | Example : | ||
| 472 | my $meta_count = | ||
| 473 | $helper->execute_single_result( | ||
| 474 | -SQL => 'select count(*) from meta where species_id =?', | ||
| 475 | -PARAMS => [1] ); | ||
| 476 | |||
| 477 | Description : Very similar to execute() except it will raise an exception if we have more | ||
| 478 | or less than one row returned | ||
| 479 | =cut | ||
| 480 | |||
| 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); | ||
| 485 | |||
| 486 | my $results = $self->execute_simple( | ||
| 487 | -SQL => $sql, | ||
| 488 | -CALLBACK => $callback, | ||
| 489 | -USE_HASHREFS => $use_hashrefs, | ||
| 490 | -PARAMS => $params | ||
| 491 | ); | ||
| 492 | |||
| 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} ); | ||
| 499 | $msg .= ']'; | ||
| 500 | throw($msg); | ||
| 501 | } | ||
| 502 | return $results->[0] if defined $results->[0]; | ||
| 503 | return; | ||
| 504 | } | ||
| 505 | |||
| 506 | =pod | ||
| 507 | |||
| 508 | =head2 transaction() | ||
| 509 | |||
| 510 | Arg [CALLBACK] : CodeRef The callback used for transaction isolation; once | ||
| 511 | the subroutine exists the code will decide on rollback | ||
| 512 | or commit. Required | ||
| 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 | ||
| 524 | Status : Stable | ||
| 525 | Example : | ||
| 526 | my $val = $helper->transaction( | ||
| 527 | -CALLBACK => sub { | ||
| 528 | my ($dbc) = @_; | ||
| 529 | #Do something | ||
| 530 | return 1; | ||
| 531 | } ); | ||
| 532 | |||
| 533 | # Or without named arguments | ||
| 534 | my $val = $helper->transaction( | ||
| 535 | sub { | ||
| 536 | my ($dbc) = @_; | ||
| 537 | #Do something | ||
| 538 | return 1; | ||
| 539 | } ); | ||
| 540 | |||
| 541 | # To allow retries (use sparingly, and see description) | ||
| 542 | my $val = $helper->transaction( | ||
| 543 | -RETRY => 3, | ||
| 544 | -PAUSE => 2, | ||
| 545 | -CALLBACK => sub { | ||
| 546 | my ($dbc) = @_; | ||
| 547 | #Do something | ||
| 548 | return 1; | ||
| 549 | } ); | ||
| 550 | |||
| 551 | # Only retry when we have an error containing the phrase "deadlock" | ||
| 552 | my $val = $helper->transaction( | ||
| 553 | -RETRY => 3, -PAUSE => 2, | ||
| 554 | -CALLBACK => sub { | ||
| 555 | my ($dbc) = @_; | ||
| 556 | #Do something | ||
| 557 | return 1; | ||
| 558 | }, | ||
| 559 | -CONDITION => sub { | ||
| 560 | my ($error) = @_; | ||
| 561 | return ( $error =~ /deadlock/ ) ? 1 : 0; | ||
| 562 | } | ||
| 563 | ); | ||
| 564 | |||
| 565 | Description : | ||
| 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. | ||
| 569 | |||
| 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 | ||
| 574 | expected. | ||
| 575 | |||
| 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. | ||
| 581 | |||
| 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 | ||
| 586 | used). | ||
| 587 | |||
| 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, | ||
| 590 | so use with care. | ||
| 591 | |||
| 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. | ||
| 599 | |||
| 600 | =cut | ||
| 601 | |||
| 602 | sub transaction { | ||
| 603 | my ($self, @args) = @_; | ||
| 604 | |||
| 605 | my ($callback, $retry, $pause, $condition) = rearrange([qw(callback retry pause condition)], @args); | ||
| 606 | |||
| 607 | throw('-CALLBACK was not a CodeRef. Got a reference of type ['.ref($callback).']. Check your parameters') | ||
| 608 | unless check_ref($callback, 'CODE'); | ||
| 609 | |||
| 610 | #Setup defaults | ||
| 611 | $retry = 0 unless defined $retry; | ||
| 612 | $pause = 1 unless defined $pause; | ||
| 613 | if(! defined $condition) { | ||
| 614 | $condition = sub { | ||
| 615 | return 1; | ||
| 616 | }; | ||
| 617 | } | ||
| 618 | |||
| 619 | assert_ref($condition, 'CODE', '-CONDITION'); | ||
| 620 | |||
| 621 | my $dbc = $self->db_connection(); | ||
| 622 | my $original_dwi; | ||
| 623 | my $ac; | ||
| 624 | |||
| 625 | my $error; | ||
| 626 | my $result; | ||
| 627 | |||
| 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(); | ||
| 633 | } | ||
| 634 | else { | ||
| 635 | #If we were in a transaction then ignore any attempts at retry here | ||
| 636 | $retry = 0; | ||
| 637 | } | ||
| 638 | |||
| 639 | for(my $iteration = 0; $iteration <= $retry; $iteration++) { | ||
| 640 | eval { | ||
| 641 | $result = $callback->($dbc); | ||
| 642 | $dbc->db_handle()->commit() if $perform_transaction; | ||
| 643 | }; | ||
| 644 | $error = $@; | ||
| 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; | ||
| 654 | } | ||
| 655 | else { | ||
| 656 | last; #break early if condition of error was not matched | ||
| 657 | } | ||
| 658 | } | ||
| 659 | } | ||
| 660 | |||
| 661 | #Always break the loop if we had a successful attempt | ||
| 662 | last if ! $error; | ||
| 663 | } | ||
| 664 | |||
| 665 | if($perform_transaction) { | ||
| 666 | $self->_disable_transaction($original_dwi, $ac); | ||
| 667 | } | ||
| 668 | |||
| 669 | throw("ABORT: Transaction aborted because of error: ${error}") if $error; | ||
| 670 | |||
| 671 | return $result; | ||
| 672 | } | ||
| 673 | |||
| 674 | =pod | ||
| 675 | |||
| 676 | =head2 execute_update() | ||
| 677 | |||
| 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 | ||
| 681 | update command | ||
| 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 | ||
| 686 | Status : Stable | ||
| 687 | Description: Used for performing updates but conforms to the normal execute statement subroutines. | ||
| 688 | Example : | ||
| 689 | use DBI qw(:sql_types); | ||
| 690 | $helper->execute_update(-SQL => 'update tab set name = ? where id =?', | ||
| 691 | -PARAMS => [ 'andy', [ 1, SQL_INTEGER ] ] ); | ||
| 692 | |||
| 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 | ||
| 695 | #issued i.e. | ||
| 696 | |||
| 697 | my $obj; | ||
| 698 | $helper->execute_update( | ||
| 699 | -SQL => 'insert into tab (name) values(?)', | ||
| 700 | -CALLBACK => sub { | ||
| 701 | my ( $sth, $dbh, $rv ) = @_; | ||
| 702 | $obj->{id} = $dbh->{mysql_insertid}; | ||
| 703 | }, | ||
| 704 | -PARAMS => [ $obj->name() ] ); | ||
| 705 | |||
| 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. | ||
| 709 | |||
| 710 | =cut | ||
| 711 | |||
| 712 | sub execute_update { | ||
| 713 | my ($self, @args) = @_; | ||
| 714 | my ($sql, $callback, $params, $prepare_params) = rearrange([qw(sql callback params prepare_params)], @args); | ||
| 715 | my $rv = 0; | ||
| 716 | my $sth; | ||
| 717 | eval { | ||
| 718 | my @prepare_params; | ||
| 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; | ||
| 724 | }; | ||
| 725 | my $error = $@; | ||
| 726 | $self->_finish_sth($sth); | ||
| 727 | if($error) { | ||
| 728 | my $params = join ' ', map { (defined $_) ? $_ : q{undef} } @{$params}; | ||
| 729 | throw("Cannot apply sql '${sql}' with params '${params}': ${error}"); | ||
| 730 | } | ||
| 731 | return $rv; | ||
| 732 | } | ||
| 733 | |||
| 734 | =head2 execute_with_sth() | ||
| 735 | |||
| 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 | ||
| 741 | prepare method | ||
| 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). | ||
| 748 | |||
| 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 | ||
| 754 | Status : Stable | ||
| 755 | Example : | ||
| 756 | my $meta_count = $helper->execute_with_sth( | ||
| 757 | -SQL => 'select count(*) from meta where species_id =?', | ||
| 758 | -PARAMS => [1], | ||
| 759 | -CALLBACK => sub { | ||
| 760 | my ($sth) = @_; | ||
| 761 | my $count; | ||
| 762 | $sth->bind_columns( \$count ); | ||
| 763 | while ( $sth->fetch ) { | ||
| 764 | print $count, "\n"; | ||
| 765 | } | ||
| 766 | return $count; | ||
| 767 | } ); | ||
| 768 | |||
| 769 | |||
| 770 | |||
| 771 | =cut | ||
| 772 | |||
| 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)}; | ||
| 778 | my $error = $@; | ||
| 779 | $self->_finish_sth($sth); | ||
| 780 | die $error if $error; | ||
| 781 | return $result; | ||
| 782 | } | ||
| 783 | |||
| 784 | =pod | ||
| 785 | |||
| 786 | =head2 batch() | ||
| 787 | |||
| 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). | ||
| 796 | |||
| 797 | Ignored if using the callback version. | ||
| 798 | Arg [PREPARE_PARAMS] : ArrayRef Used to pass parameters to the statement handle | ||
| 799 | prepare method | ||
| 800 | Returntype : integer rows updated | ||
| 801 | Exceptions : If errors occur in the execution of the SQL | ||
| 802 | Status : Stable | ||
| 803 | Example : | ||
| 804 | my $alotofdata = getitfromsomewhere(); | ||
| 805 | $helper->batch( | ||
| 806 | -SQL => 'insert into table (one,two) values(?,?)', | ||
| 807 | -CALLBACk => sub { | ||
| 808 | my ( $sth, $dbc ) = @_; | ||
| 809 | foreach my $data (@alotofdata) { | ||
| 810 | $sth->execute( @{$data} ); | ||
| 811 | } | ||
| 812 | } ); | ||
| 813 | |||
| 814 | #Or for a 2D array data driven approach | ||
| 815 | $helper->batch( -SQL => 'insert into table (one,two) values(?,?)', | ||
| 816 | -DATA => $alotofdata ); | ||
| 817 | |||
| 818 | |||
| 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. | ||
| 824 | |||
| 825 | This can be combined with the transaction() code to provide a construct | ||
| 826 | which does batch insertion and is transactionally aware. | ||
| 827 | |||
| 828 | We can also use data based batch insertions i.e. | ||
| 829 | |||
| 830 | #Needs to be like: | ||
| 831 | # [ [1,2], [3,4] ] | ||
| 832 | #Or if using the DBI types: | ||
| 833 | # [ [ [ 1, SQL_INTEGER ], [ 2, SQL_INTEGER ] ], | ||
| 834 | # [ [ 3, SQL_INTEGER ], [ 4, SQL_INTEGER ] ] ]; | ||
| 835 | |||
| 836 | my $alotofdata = getitfromsomewhere(); | ||
| 837 | $helper->batch( -SQL => 'insert into table (one,two) values(?,?)', | ||
| 838 | -DATA => $alotofdata ); | ||
| 839 | |||
| 840 | This does exactly the same as the previous example. | ||
| 841 | |||
| 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. | ||
| 845 | |||
| 846 | =cut | ||
| 847 | |||
| 848 | sub batch { | ||
| 849 | my ($self, @args) = @_; | ||
| 850 | my ($sql, $callback, $data, $commit_every, $prepare_params) = | ||
| 851 | rearrange([qw(sql callback data commit_every prepare_params)], @args); | ||
| 852 | |||
| 853 | if(! defined $callback && ! defined $data) { | ||
| 854 | throw('You need to define a callback for insertion work or the 2D data array'); | ||
| 855 | } | ||
| 856 | |||
| 857 | my $result; | ||
| 858 | if(defined $callback) { | ||
| 859 | $result = $self->_callback_batch($sql, $callback, $prepare_params); | ||
| 860 | } | ||
| 861 | else { | ||
| 862 | $result = $self->_data_batch($sql, $data, $commit_every, $prepare_params); | ||
| 863 | } | ||
| 864 | return $result if defined $result; | ||
| 865 | return; | ||
| 866 | } | ||
| 867 | |||
| 868 | #------- Internal methods | ||
| 869 | |||
| 870 | my $default_mappers = { | ||
| 871 | first_element => sub { | ||
| 872 | my ($row) = @_; | ||
| 873 | return $row->[0]; | ||
| 874 | }, | ||
| 875 | second_element => sub { | ||
| 876 | my ($row) = @_; | ||
| 877 | return $row->[1]; | ||
| 878 | }, | ||
| 879 | array_ref => sub { | ||
| 880 | my $row = shift @_; | ||
| 881 | return [@{$row}]; #copy of array done because DBI's array is read only | ||
| 882 | }, | ||
| 883 | hash_ref => sub { | ||
| 884 | my $row = shift @_; | ||
| 885 | return {%{$row}}; #applying same logic as above | ||
| 886 | } | ||
| 887 | }; | ||
| 888 | |||
| 889 | sub _mappers { | ||
| 890 | my ($self) = @_; | ||
| 891 | return $default_mappers; | ||
| 892 | } | ||
| 893 | |||
| 894 | sub _perform_transaction_code { | ||
| 895 | my ($self) = @_; | ||
| 896 | return $self->{_transaction_active}->{$PROCESS_ID} ? 0 : 1; | ||
| 897 | } | ||
| 898 | |||
| 899 | sub _enable_transaction { | ||
| 900 | my ($self) = @_; | ||
| 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); | ||
| 908 | } | ||
| 909 | |||
| 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}; | ||
| 916 | return; | ||
| 917 | } | ||
| 918 | |||
| 919 | sub _bind_params { | ||
| 920 | my ( $self, $sth, $params ) = @_; | ||
| 921 | |||
| 922 | return if ! defined $params; #Return quickly if we had no data | ||
| 923 | |||
| 924 | if(! check_ref($params, 'ARRAY')) { | ||
| 925 | throw(qq{The given parameters reference '${params}' is not an ARRAY; wrap in an ArrayRef}); | ||
| 926 | } | ||
| 927 | |||
| 928 | my $count = 1; | ||
| 929 | foreach my $param (@{$params}) { | ||
| 930 | if ( check_ref($param, 'ARRAY') ) { | ||
| 931 | $sth->bind_param( $count, @{$param} ); | ||
| 932 | } | ||
| 933 | else { | ||
| 934 | $sth->bind_param( $count, $param ); | ||
| 935 | } | ||
| 936 | $count++; | ||
| 937 | } | ||
| 938 | return; | ||
| 939 | } | ||
| 940 | |||
| 941 | sub _execute { | ||
| 942 | my ( $self, $sql, $callback, $has_return, $use_hashrefs, $params, $prepare_params, $iterator ) = @_; | ||
| 943 | |||
| 944 | throw('Not given a mapper. _execute() must always been given a CodeRef') unless check_ref($callback, 'CODE'); | ||
| 945 | |||
| 946 | my $sth = $self->_base_execute($sql, $params, $prepare_params); | ||
| 947 | |||
| 948 | my $sth_processor; | ||
| 949 | if($use_hashrefs) { | ||
| 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; | ||
| 955 | } | ||
| 956 | $self->_finish_sth($sth); | ||
| 957 | return; | ||
| 958 | }; | ||
| 959 | } | ||
| 960 | else { | ||
| 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; | ||
| 966 | } | ||
| 967 | $self->_finish_sth($sth); | ||
| 968 | return; | ||
| 969 | }; | ||
| 970 | } | ||
| 971 | |||
| 972 | my $iter = Bio::EnsEMBL::Utils::Iterator->new($sth_processor); | ||
| 973 | if($has_return) { | ||
| 974 | return $iter if $iterator; | ||
| 975 | return $iter->to_arrayref(); | ||
| 976 | } | ||
| 977 | else { | ||
| 978 | #Force iteration if we had no return since the caller is expecting this | ||
| 979 | $iter->each(sub {}); | ||
| 980 | } | ||
| 981 | return; | ||
| 982 | } | ||
| 983 | |||
| 984 | sub _base_execute { | ||
| 985 | my ( $self, $sql, $params, $prepare_params) = @_; | ||
| 986 | |||
| 987 | $params = [] unless $params; | ||
| 988 | |||
| 989 | my $conn = $self->db_connection; | ||
| 990 | |||
| 991 | my $sth; | ||
| 992 | eval { | ||
| 993 | my @prepare_params; | ||
| 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'") | ||
| 997 | unless $sth; | ||
| 998 | $self->_bind_params( $sth, $params ); | ||
| 999 | $sth->execute(); | ||
| 1000 | }; | ||
| 1001 | |||
| 1002 | my $error = $@; | ||
| 1003 | if($error) { | ||
| 1004 | throw("Cannot run '${sql}' with params '@{$params}' due to error: $error") if $error; | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | return $sth; | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | sub _finish_sth { | ||
| 1011 | my ($self, $sth) = @_; | ||
| 1012 | eval { $sth->finish() if defined $sth; }; | ||
| 1013 | warn('Cannot finish() the statement handle: $@') if $@; | ||
| 1014 | return; | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | sub _callback_batch { | ||
| 1018 | my ($self, $sql, $callback, $prepare_params) = @_; | ||
| 1019 | my $error; | ||
| 1020 | my $sth; | ||
| 1021 | my $closure_return; | ||
| 1022 | eval { | ||
| 1023 | my @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()); | ||
| 1027 | }; | ||
| 1028 | $error = $@; | ||
| 1029 | $self->_finish_sth($sth); | ||
| 1030 | throw("Problem detected during batch work: $error") if $error; | ||
| 1031 | |||
| 1032 | return $closure_return if defined $closure_return; | ||
| 1033 | return; | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | sub _data_batch { | ||
| 1037 | my ($self, $sql, $data, $commit_every, $prepare_params) = @_; | ||
| 1038 | |||
| 1039 | #Input checks | ||
| 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'); | ||
| 1045 | |||
| 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()}; | ||
| 1054 | if($@) { | ||
| 1055 | throw("Problem working with $sql with params @{$row}: $@"); | ||
| 1056 | } | ||
| 1057 | my $num_affected = ($affected) ? $affected : 0; #Get around DBI's 0E0 | ||
| 1058 | $total_affected += $num_affected; | ||
| 1059 | |||
| 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 | ||
| 1062 | if($commit_every) { | ||
| 1063 | if( ($data_index % $commit_every == 0) && $data_index != 0) { | ||
| 1064 | $dbc->db_handle()->commit(); | ||
| 1065 | } | ||
| 1066 | } | ||
| 1067 | } | ||
| 1068 | |||
| 1069 | #finish off with a commit if the code told us we were doing it | ||
| 1070 | if($commit_every) { | ||
| 1071 | $dbc->db_handle()->commit(); | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | return $total_affected || 0; | ||
| 1075 | }; | ||
| 1076 | |||
| 1077 | return $self->_callback_batch($sql, $callback, $prepare_params) | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | 1; |