Revision 62041123
Von Sven Schöling vor etwa 8 Jahren hinzugefügt
SL/Controller/CsvImport.pm | ||
---|---|---|
2 | 2 |
|
3 | 3 |
use strict; |
4 | 4 |
|
5 |
use SL::DB; |
|
5 | 6 |
use SL::DB::Buchungsgruppe; |
6 | 7 |
use SL::DB::CsvImportProfile; |
7 | 8 |
use SL::DB::CsvImportReport; |
... | ... | |
515 | 516 |
|
516 | 517 |
$report->save(cascade => 1) or die $report->db->error; |
517 | 518 |
|
518 |
my $dbh = $::form->get_standard_dbh;
|
|
519 |
$dbh->begin_work;
|
|
519 |
SL::DB->client->with_transaction(sub {
|
|
520 |
my $dbh = SL::DB->client->dbh;
|
|
520 | 521 |
|
521 |
my $query = 'INSERT INTO csv_import_report_rows (csv_import_report_id, col, row, value) VALUES (?, ?, ?, ?)'; |
|
522 |
my $query2 = 'INSERT INTO csv_import_report_status (csv_import_report_id, row, type, value) VALUES (?, ?, ?, ?)'; |
|
522 |
my $query = 'INSERT INTO csv_import_report_rows (csv_import_report_id, col, row, value) VALUES (?, ?, ?, ?)';
|
|
523 |
my $query2 = 'INSERT INTO csv_import_report_status (csv_import_report_id, row, type, value) VALUES (?, ?, ?, ?)';
|
|
523 | 524 |
|
524 |
my $sth = $dbh->prepare($query); |
|
525 |
my $sth2 = $dbh->prepare($query2); |
|
525 |
my $sth = $dbh->prepare($query);
|
|
526 |
my $sth2 = $dbh->prepare($query2);
|
|
526 | 527 |
|
527 |
# save headers |
|
528 |
my (@headers, @info_methods, @raw_methods, @methods); |
|
529 |
|
|
530 |
for my $i (0 .. $#{ $self->info_headers->{headers} }) { |
|
531 |
next unless $self->info_headers->{used}->{ $self->info_headers->{methods}->[$i] }; |
|
532 |
push @headers, $self->info_headers->{headers}->[$i]; |
|
533 |
push @info_methods, $self->info_headers->{methods}->[$i]; |
|
534 |
} |
|
535 |
for my $i (0 .. $#{ $self->headers->{headers} }) { |
|
536 |
next unless $self->headers->{used}->{ $self->headers->{headers}->[$i] }; |
|
537 |
push @headers, $self->headers->{headers}->[$i]; |
|
538 |
push @methods, $self->headers->{methods}->[$i]; |
|
539 |
} |
|
540 |
for my $i (0 .. $#{ $self->raw_data_headers->{headers} }) { |
|
541 |
next unless $self->raw_data_headers->{used}->{ $self->raw_data_headers->{headers}->[$i] }; |
|
542 |
push @headers, $self->raw_data_headers->{headers}->[$i]; |
|
543 |
push @raw_methods, $self->raw_data_headers->{headers}->[$i]; |
|
544 |
} |
|
528 |
# save headers |
|
529 |
my (@headers, @info_methods, @raw_methods, @methods); |
|
545 | 530 |
|
546 |
$sth->execute($report->id, $_, 0, $headers[$_]) for 0 .. $#headers; |
|
531 |
for my $i (0 .. $#{ $self->info_headers->{headers} }) { |
|
532 |
next unless $self->info_headers->{used}->{ $self->info_headers->{methods}->[$i] }; |
|
533 |
push @headers, $self->info_headers->{headers}->[$i]; |
|
534 |
push @info_methods, $self->info_headers->{methods}->[$i]; |
|
535 |
} |
|
536 |
for my $i (0 .. $#{ $self->headers->{headers} }) { |
|
537 |
next unless $self->headers->{used}->{ $self->headers->{headers}->[$i] }; |
|
538 |
push @headers, $self->headers->{headers}->[$i]; |
|
539 |
push @methods, $self->headers->{methods}->[$i]; |
|
540 |
} |
|
541 |
for my $i (0 .. $#{ $self->raw_data_headers->{headers} }) { |
|
542 |
next unless $self->raw_data_headers->{used}->{ $self->raw_data_headers->{headers}->[$i] }; |
|
543 |
push @headers, $self->raw_data_headers->{headers}->[$i]; |
|
544 |
push @raw_methods, $self->raw_data_headers->{headers}->[$i]; |
|
545 |
} |
|
547 | 546 |
|
548 |
# col offsets |
|
549 |
my $o1 = @info_methods; |
|
550 |
my $o2 = $o1 + @methods; |
|
547 |
$sth->execute($report->id, $_, 0, $headers[$_]) for 0 .. $#headers; |
|
551 | 548 |
|
552 |
for my $row (0 .. $#{ $self->data }) {
|
|
553 |
$self->track_progress(progress => $row / @{ $self->data } * 100) if $row % 1000 == 0;
|
|
554 |
my $data_row = $self->{data}[$row];
|
|
549 |
# col offsets
|
|
550 |
my $o1 = @info_methods;
|
|
551 |
my $o2 = $o1 + @methods;
|
|
555 | 552 |
|
556 |
$sth->execute($report->id, $_, $row + 1, $data_row->{info_data}{ $info_methods[$_] }) for 0 .. $#info_methods;
|
|
557 |
$sth->execute($report->id, $o1 + $_, $row + 1, $data_row->{object}->${ \ $methods[$_] }) for 0 .. $#methods;
|
|
558 |
$sth->execute($report->id, $o2 + $_, $row + 1, $data_row->{raw_data}{ $raw_methods[$_] }) for 0 .. $#raw_methods;
|
|
553 |
for my $row (0 .. $#{ $self->data }) {
|
|
554 |
$self->track_progress(progress => $row / @{ $self->data } * 100) if $row % 1000 == 0;
|
|
555 |
my $data_row = $self->{data}[$row];
|
|
559 | 556 |
|
560 |
$sth2->execute($report->id, $row + 1, 'information', $_) for @{ $data_row->{information} || [] };
|
|
561 |
$sth2->execute($report->id, $row + 1, 'errors', $_) for @{ $data_row->{errors} || [] };
|
|
562 |
}
|
|
557 |
$sth->execute($report->id, $_, $row + 1, $data_row->{info_data}{ $info_methods[$_] }) for 0 .. $#info_methods;
|
|
558 |
$sth->execute($report->id, $o1 + $_, $row + 1, $data_row->{object}->${ \ $methods[$_] }) for 0 .. $#methods;
|
|
559 |
$sth->execute($report->id, $o2 + $_, $row + 1, $data_row->{raw_data}{ $raw_methods[$_] }) for 0 .. $#raw_methods;
|
|
563 | 560 |
|
564 |
$dbh->commit; |
|
561 |
$sth2->execute($report->id, $row + 1, 'information', $_) for @{ $data_row->{information} || [] }; |
|
562 |
$sth2->execute($report->id, $row + 1, 'errors', $_) for @{ $data_row->{errors} || [] }; |
|
563 |
} |
|
564 |
1; |
|
565 |
}) or do { die SL::DB->client->error }; |
|
565 | 566 |
|
566 | 567 |
return $report->id; |
567 | 568 |
} |
Auch abrufbar als: Unified diff
CsvImport: single-dbh