Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 8e926112

Von Sven Schöling vor etwa 8 Jahren hinzugefügt

  • ID 8e926112ea0e2f73c096c82987ae29f7078049c3
  • Vorgänger 3fc69ae2
  • Nachfolger d9abca44

CsvImport: mehr single-dbh

Unterschiede anzeigen:

SL/Controller/CsvImport.pm
587 587

  
588 588
  $report->save(cascade => 1) or die $report->db->error;
589 589

  
590
  my $dbh = $::form->get_standard_dbh;
591
  $dbh->begin_work;
592

  
593
  my $query  = 'INSERT INTO csv_import_report_rows (csv_import_report_id, col, row, value) VALUES (?, ?, ?, ?)';
594
  my $query2 = 'INSERT INTO csv_import_report_status (csv_import_report_id, row, type, value) VALUES (?, ?, ?, ?)';
595

  
596
  my $sth = $dbh->prepare($query);
597
  my $sth2 = $dbh->prepare($query2);
590
  SL::DB->client->with_transaction(sub {
591
    my $dbh = SL::DB->client->dbh;
598 592

  
599
  # save headers
600
  my ($headers, $info_methods, $raw_methods, $methods);
593
    my $query  = 'INSERT INTO csv_import_report_rows (csv_import_report_id, col, row, value) VALUES (?, ?, ?, ?)';
594
    my $query2 = 'INSERT INTO csv_import_report_status (csv_import_report_id, row, type, value) VALUES (?, ?, ?, ?)';
601 595

  
602
  for my $i (0 .. $#{ $self->worker->profile }) {
603
    my $row_ident = $self->worker->profile->[$i]->{row_ident};
596
    my $sth = $dbh->prepare($query);
597
    my $sth2 = $dbh->prepare($query2);
604 598

  
605
    for my $i (0 .. $#{ $self->info_headers->{$row_ident}->{headers} }) {
606
      next unless                            $self->info_headers->{$row_ident}->{used}->{ $self->info_headers->{$row_ident}->{methods}->[$i] };
607
      push @{ $headers->{$row_ident} },      $self->info_headers->{$row_ident}->{headers}->[$i];
608
      push @{ $info_methods->{$row_ident} }, $self->info_headers->{$row_ident}->{methods}->[$i];
609
    }
610
    for my $i (0 .. $#{ $self->headers->{$row_ident}->{headers} }) {
611
      next unless                       $self->headers->{$row_ident}->{used}->{ $self->headers->{$row_ident}->{headers}->[$i] };
612
      push @{ $headers->{$row_ident} }, $self->headers->{$row_ident}->{headers}->[$i];
613
      push @{ $methods->{$row_ident} }, $self->headers->{$row_ident}->{methods}->[$i];
599
    # save headers
600
    my ($headers, $info_methods, $raw_methods, $methods);
601

  
602
    for my $i (0 .. $#{ $self->worker->profile }) {
603
      my $row_ident = $self->worker->profile->[$i]->{row_ident};
604

  
605
      for my $i (0 .. $#{ $self->info_headers->{$row_ident}->{headers} }) {
606
        next unless                            $self->info_headers->{$row_ident}->{used}->{ $self->info_headers->{$row_ident}->{methods}->[$i] };
607
        push @{ $headers->{$row_ident} },      $self->info_headers->{$row_ident}->{headers}->[$i];
608
        push @{ $info_methods->{$row_ident} }, $self->info_headers->{$row_ident}->{methods}->[$i];
609
      }
610
      for my $i (0 .. $#{ $self->headers->{$row_ident}->{headers} }) {
611
        next unless                       $self->headers->{$row_ident}->{used}->{ $self->headers->{$row_ident}->{headers}->[$i] };
612
        push @{ $headers->{$row_ident} }, $self->headers->{$row_ident}->{headers}->[$i];
613
        push @{ $methods->{$row_ident} }, $self->headers->{$row_ident}->{methods}->[$i];
614
      }
615

  
616
      for my $i (0 .. $#{ $self->raw_data_headers->{$row_ident}->{headers} }) {
617
      next unless                           $self->raw_data_headers->{$row_ident}->{used}->{ $self->raw_data_headers->{$row_ident}->{headers}->[$i] };
618
      push @{ $headers->{$row_ident} },     $self->raw_data_headers->{$row_ident}->{headers}->[$i];
619
      push @{ $raw_methods->{$row_ident} }, $self->raw_data_headers->{$row_ident}->{headers}->[$i];
614 620
    }
615 621

  
616
    for my $i (0 .. $#{ $self->raw_data_headers->{$row_ident}->{headers} }) {
617
    next unless                           $self->raw_data_headers->{$row_ident}->{used}->{ $self->raw_data_headers->{$row_ident}->{headers}->[$i] };
618
    push @{ $headers->{$row_ident} },     $self->raw_data_headers->{$row_ident}->{headers}->[$i];
619
    push @{ $raw_methods->{$row_ident} }, $self->raw_data_headers->{$row_ident}->{headers}->[$i];
620
  }
621

  
622
  }
623

  
624
  for my $i (0 .. $#{ $self->worker->profile }) {
625
    my $row_ident = $self->worker->profile->[$i]->{row_ident};
626
    $sth->execute($report->id, $_, $i, $headers->{$row_ident}->[$_]) for 0 .. $#{ $headers->{$row_ident} };
627
  }
628

  
629
  # col offsets
630
  my ($off1, $off2);
631
  for my $i (0 .. $#{ $self->worker->profile }) {
632
    my $row_ident = $self->worker->profile->[$i]->{row_ident};
633
    my $n_info_methods = $info_methods->{$row_ident} ? scalar @{ $info_methods->{$row_ident} } : 0;
634
    my $n_methods      = $methods->{$row_ident} ?      scalar @{ $methods->{$row_ident} }      : 0;
622
    }
635 623

  
636
    $off1->{$row_ident} = $n_info_methods;
637
    $off2->{$row_ident} = $off1->{$row_ident} + $n_methods;
638
  }
624
    for my $i (0 .. $#{ $self->worker->profile }) {
625
      my $row_ident = $self->worker->profile->[$i]->{row_ident};
626
      $sth->execute($report->id, $_, $i, $headers->{$row_ident}->[$_]) for 0 .. $#{ $headers->{$row_ident} };
627
    }
639 628

  
640
  my $n_header_rows = scalar @{ $self->worker->profile };
629
    # col offsets
630
    my ($off1, $off2);
631
    for my $i (0 .. $#{ $self->worker->profile }) {
632
      my $row_ident = $self->worker->profile->[$i]->{row_ident};
633
      my $n_info_methods = $info_methods->{$row_ident} ? scalar @{ $info_methods->{$row_ident} } : 0;
634
      my $n_methods      = $methods->{$row_ident} ?      scalar @{ $methods->{$row_ident} }      : 0;
635

  
636
      $off1->{$row_ident} = $n_info_methods;
637
      $off2->{$row_ident} = $off1->{$row_ident} + $n_methods;
638
    }
641 639

  
642
  for my $row (0 .. $#{ $self->data }) {
643
    $self->track_progress(progress => $row / @{ $self->data } * 100) if $row % 1000 == 0;
644
    my $data_row = $self->{data}[$row];
645
    my $row_ident = $data_row->{raw_data}{datatype};
640
    my $n_header_rows = scalar @{ $self->worker->profile };
646 641

  
647
    my $o1 = $off1->{$row_ident};
648
    my $o2 = $off2->{$row_ident};
642
    for my $row (0 .. $#{ $self->data }) {
643
      $self->track_progress(progress => $row / @{ $self->data } * 100) if $row % 1000 == 0;
644
      my $data_row = $self->{data}[$row];
645
      my $row_ident = $data_row->{raw_data}{datatype};
649 646

  
650
    $sth->execute($report->id,       $_, $row + $n_header_rows, $data_row->{info_data}{ $info_methods->{$row_ident}->[$_] }) for 0 .. $#{ $info_methods->{$row_ident} };
651
    $sth->execute($report->id, $o1 + $_, $row + $n_header_rows, $data_row->{object}->${ \ $methods->{$row_ident}->[$_] })    for 0 .. $#{ $methods->{$row_ident} };
652
    $sth->execute($report->id, $o2 + $_, $row + $n_header_rows, $data_row->{raw_data}{ $raw_methods->{$row_ident}->[$_] })   for 0 .. $#{ $raw_methods->{$row_ident} };
647
      my $o1 = $off1->{$row_ident};
648
      my $o2 = $off2->{$row_ident};
653 649

  
654
    $sth2->execute($report->id, $row + $n_header_rows, 'information', $_) for @{ $data_row->{information} || [] };
655
    $sth2->execute($report->id, $row + $n_header_rows, 'errors', $_)      for @{ $data_row->{errors}      || [] };
656
  }
650
      $sth->execute($report->id,       $_, $row + $n_header_rows, $data_row->{info_data}{ $info_methods->{$row_ident}->[$_] }) for 0 .. $#{ $info_methods->{$row_ident} };
651
      $sth->execute($report->id, $o1 + $_, $row + $n_header_rows, $data_row->{object}->${ \ $methods->{$row_ident}->[$_] })    for 0 .. $#{ $methods->{$row_ident} };
652
      $sth->execute($report->id, $o2 + $_, $row + $n_header_rows, $data_row->{raw_data}{ $raw_methods->{$row_ident}->[$_] })   for 0 .. $#{ $raw_methods->{$row_ident} };
657 653

  
658
  $dbh->commit;
654
      $sth2->execute($report->id, $row + $n_header_rows, 'information', $_) for @{ $data_row->{information} || [] };
655
      $sth2->execute($report->id, $row + $n_header_rows, 'errors', $_)      for @{ $data_row->{errors}      || [] };
656
    }
657
    1;
658
  }) or do { die SL::DB->client->error };
659 659

  
660 660
  return $report->id;
661 661
}

Auch abrufbar als: Unified diff