Lines 459-464
error.
Link Here
|
459 |
|
459 |
|
460 |
=cut |
460 |
=cut |
461 |
|
461 |
|
|
|
462 |
sub process_yml_table { |
463 |
my ($table) = @_; |
464 |
my $table_name = ( keys %$table )[0]; # table name |
465 |
my @rows = @{ $table->{$table_name}->{rows} }; # |
466 |
my @columns = ( sort keys %{$rows[0]} ); # column names |
467 |
my $fields = join ",", map{sprintf("`%s`", $_)} @columns; # idem, joined |
468 |
my $query = "INSERT INTO $table_name ( $fields ) VALUES "; |
469 |
my @multiline = @{ $table->{$table_name}->{'multiline'} }; # to check multiline values; |
470 |
my $placeholders = '(' . join ( ",", map { "?" } @columns ) . ')'; # '(?,..,?)' string |
471 |
my @values; |
472 |
foreach my $row ( @rows ) { |
473 |
push @values, map { |
474 |
my $col = $_; |
475 |
( @multiline and grep { $_ eq $col } @multiline ) |
476 |
? join "\r\n", @{$row->{$col}} # join multiline values |
477 |
: $row->{$col}; |
478 |
} @columns; |
479 |
} |
480 |
return { query => $query, placeholders => $placeholders, values => \@values }; |
481 |
} |
482 |
|
462 |
sub load_sql { |
483 |
sub load_sql { |
463 |
my $self = shift; |
484 |
my $self = shift; |
464 |
my $filename = shift; |
485 |
my $filename = shift; |
Lines 483-506
sub load_sql {
Link Here
|
483 |
eval { |
504 |
eval { |
484 |
my $yaml = LoadFile( $filename ); # Load YAML |
505 |
my $yaml = LoadFile( $filename ); # Load YAML |
485 |
for my $table ( @{ $yaml->{'tables'} } ) { |
506 |
for my $table ( @{ $yaml->{'tables'} } ) { |
486 |
my $table_name = ( keys %$table )[0]; # table name |
507 |
my $query_info = process_yml_table($table); |
487 |
my @rows = @{ $table->{$table_name}->{rows} }; # |
508 |
my $query = $query_info->{query}; |
488 |
my @columns = ( sort keys %{$rows[0]} ); # column names |
509 |
my $placeholders = $query_info->{placeholders}; |
489 |
my $fields = join ",", map{sprintf("`%s`", $_)} @columns; # idem, joined |
510 |
my $values = $query_info->{values}; |
490 |
my $query = "INSERT INTO $table_name ( $fields ) VALUES "; |
|
|
491 |
my @multiline = @{ $table->{$table_name}->{'multiline'} }; # to check multiline values; |
492 |
my $placeholders = '(' . join ( ",", map { "?" } @columns ) . ')'; # '(?,..,?)' string |
493 |
my @values; |
494 |
foreach my $row ( @rows ) { |
495 |
push @values, map { |
496 |
my $col = $_; |
497 |
( @multiline and grep { $_ eq $col } @multiline ) |
498 |
? join "\r\n", @{$row->{$col}} # join multiline values |
499 |
: $row->{$col}; |
500 |
} @columns; |
501 |
} |
502 |
# Doing only 1 INSERT query for the whole table |
511 |
# Doing only 1 INSERT query for the whole table |
503 |
$query .= join ', ', ( $placeholders ) x scalar @rows; |
512 |
$query .= join ', ', ( $placeholders ) x scalar @values; |
504 |
$dbh->do( $query, undef, @values ); |
513 |
$dbh->do( $query, undef, @values ); |
505 |
} |
514 |
} |
506 |
for my $statement ( @{ $yaml->{'sql_statements'} } ) { # extra SQL statements |
515 |
for my $statement ( @{ $yaml->{'sql_statements'} } ) { # extra SQL statements |
507 |
- |
|
|