Revision 3fc69ae2
Von Sven Schöling vor mehr als 8 Jahren hinzugefügt
SL/Controller/CsvImport/Base.pm | ||
---|---|---|
3 | 3 |
use strict; |
4 | 4 |
|
5 | 5 |
use English qw(-no_match_vars); |
6 |
use List::Util qw(min); |
|
6 | 7 |
use List::MoreUtils qw(pairwise any); |
7 | 8 |
|
8 | 9 |
use SL::Helper::Csv; |
9 | 10 |
|
11 |
use SL::DB; |
|
10 | 12 |
use SL::DB::BankAccount; |
11 | 13 |
use SL::DB::Customer; |
12 | 14 |
use SL::DB::Language; |
... | ... | |
461 | 463 |
|
462 | 464 |
my $dbh = $data->[0]{object}->db; |
463 | 465 |
|
464 |
$dbh->begin_work; |
|
465 |
foreach my $entry_index (0 .. $#$data) { |
|
466 |
my $entry = $data->[$entry_index]; |
|
467 |
next if @{ $entry->{errors} }; |
|
468 |
|
|
469 |
my $object = $entry->{object_to_save} || $entry->{object}; |
|
470 |
|
|
471 |
my $ret; |
|
472 |
if (!eval { $ret = $object->save(cascade => !!$self->save_with_cascade()); 1 }) { |
|
473 |
push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $EVAL_ERROR); |
|
474 |
} elsif ( !$ret ) { |
|
475 |
push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $object->db->error); |
|
476 |
} else { |
|
477 |
$self->_save_history($object); |
|
478 |
$self->controller->num_imported($self->controller->num_imported + 1); |
|
479 |
} |
|
480 |
} continue { |
|
481 |
if ($entry_index % 100 == 0) { |
|
482 |
$dbh->commit; |
|
483 |
$self->controller->track_progress(progress => $entry_index/scalar(@$data) * 100); # scale from 45..95%; |
|
484 |
$dbh->begin_work; |
|
485 |
} |
|
466 |
my $last_index = $#$data; |
|
467 |
my $chunk_size = 100; # one transaction and progress update every 100 objects |
|
468 |
|
|
469 |
for my $chunk (0 .. $last_index / $chunk_size) { |
|
470 |
$self->controller->track_progress(progress => ($chunk_size * $chunk)/scalar(@$data) * 100); # scale from 45..95%; |
|
471 |
SL::DB->client->with_transaction(sub { |
|
472 |
foreach my $entry_index ($chunk_size * $chunk .. min( $last_index, $chunk_size * ($chunk + 1) - 1 )) { |
|
473 |
my $entry = $data->[$entry_index]; |
|
474 |
next if @{ $entry->{errors} }; |
|
475 |
|
|
476 |
my $object = $entry->{object_to_save} || $entry->{object}; |
|
477 |
|
|
478 |
my $ret; |
|
479 |
if (!eval { $ret = $object->save(cascade => !!$self->save_with_cascade()); 1 }) { |
|
480 |
push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $EVAL_ERROR); |
|
481 |
} elsif ( !$ret ) { |
|
482 |
push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $object->db->error); |
|
483 |
} else { |
|
484 |
$self->_save_history($object); |
|
485 |
$self->controller->num_imported($self->controller->num_imported + 1); |
|
486 |
} |
|
487 |
} |
|
488 |
}) or do { die SL::DB->client->error }; |
|
486 | 489 |
} |
487 |
$dbh->commit;
|
|
490 |
$self->controller->track_progress(progress => 100);
|
|
488 | 491 |
} |
489 | 492 |
|
490 | 493 |
sub field_lengths { |
Auch abrufbar als: Unified diff
CsvImport/Base: single-dbh