Bug 24883: Move to a flat array with all values to an array of arrayref
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 19 Mar 2020 11:20:23 +0000 (12:20 +0100)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 27 Mar 2020 12:11:18 +0000 (12:11 +0000)
For the following SQL query:
INSERT INTO cities(city_name, city_country) VALUES ('Madrid', 'Spain'), ('Buenos Aires', 'Argentina');

We move from:
[ 'Madrid', 'Spain', 'Buenos Aires', 'Argentina' ]
to:
[ [ 'Madrid', 'Spain' ], [ 'Buenos Aires', 'Argentina' ] ]

Which make more sense to split, build and construct the queries

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

C4/Installer.pm

index c0ff508..ec56c0e 100644 (file)
@@ -496,12 +496,12 @@ sub process_yml_table {
     my $placeholders = '(' . join ( ",", map { "?" } @columns ) . ')'; # '(?,..,?)' string
     my @values;
     foreach my $row ( @rows ) {
-        push @values, map {
+        push @values, [ map {
                         my $col = $_;
                         ( @multiline and grep { $_ eq $col } @multiline )
                         ? join "\r\n", @{$row->{$col}}                # join multiline values
                         : $row->{$col};
-                     } @columns;
+                     } @columns ];
     }
     return { query => $query, placeholders => $placeholders, values => \@values };
 }
@@ -535,8 +535,9 @@ sub load_sql {
                     my $placeholders = $query_info->{placeholders};
                     my $values       = $query_info->{values};
                     # Doing only 1 INSERT query for the whole table
-                    $query .= join ', ', ( $placeholders ) x scalar @values;
-                    $dbh->do( $query, undef, @values );
+                    my @all_rows_values = map { @$_ } @$values;
+                    $query .= join ', ', ( $placeholders ) x scalar @$values;
+                    $dbh->do( $query, undef, @all_rows_values );
                 }
                 for my $statement ( @{ $yaml->{'sql_statements'} } ) {               # extra SQL statements
                     $dbh->do($statement);