Lines 485-490
error.
Link Here
|
485 |
|
485 |
|
486 |
=cut |
486 |
=cut |
487 |
|
487 |
|
|
|
488 |
sub process_yml_table { |
489 |
my ($table) = @_; |
490 |
my $table_name = ( keys %$table )[0]; # table name |
491 |
my @rows = @{ $table->{$table_name}->{rows} }; # |
492 |
my @columns = ( sort keys %{$rows[0]} ); # column names |
493 |
my $fields = join ",", map{sprintf("`%s`", $_)} @columns; # idem, joined |
494 |
my $query = "INSERT INTO $table_name ( $fields ) VALUES "; |
495 |
my @multiline = @{ $table->{$table_name}->{'multiline'} }; # to check multiline values; |
496 |
my $placeholders = '(' . join ( ",", map { "?" } @columns ) . ')'; # '(?,..,?)' string |
497 |
my @values; |
498 |
foreach my $row ( @rows ) { |
499 |
push @values, map { |
500 |
my $col = $_; |
501 |
( @multiline and grep { $_ eq $col } @multiline ) |
502 |
? join "\r\n", @{$row->{$col}} # join multiline values |
503 |
: $row->{$col}; |
504 |
} @columns; |
505 |
} |
506 |
return { query => $query, placeholders => $placeholders, values => \@values }; |
507 |
} |
508 |
|
488 |
sub load_sql { |
509 |
sub load_sql { |
489 |
my $self = shift; |
510 |
my $self = shift; |
490 |
my $filename = shift; |
511 |
my $filename = shift; |
Lines 509-532
sub load_sql {
Link Here
|
509 |
eval { |
530 |
eval { |
510 |
my $yaml = LoadFile( $filename ); # Load YAML |
531 |
my $yaml = LoadFile( $filename ); # Load YAML |
511 |
for my $table ( @{ $yaml->{'tables'} } ) { |
532 |
for my $table ( @{ $yaml->{'tables'} } ) { |
512 |
my $table_name = ( keys %$table )[0]; # table name |
533 |
my $query_info = process_yml_table($table); |
513 |
my @rows = @{ $table->{$table_name}->{rows} }; # |
534 |
my $query = $query_info->{query}; |
514 |
my @columns = ( sort keys %{$rows[0]} ); # column names |
535 |
my $placeholders = $query_info->{placeholders}; |
515 |
my $fields = join ",", map{sprintf("`%s`", $_)} @columns; # idem, joined |
536 |
my $values = $query_info->{values}; |
516 |
my $query = "INSERT INTO $table_name ( $fields ) VALUES "; |
|
|
517 |
my @multiline = @{ $table->{$table_name}->{'multiline'} }; # to check multiline values; |
518 |
my $placeholders = '(' . join ( ",", map { "?" } @columns ) . ')'; # '(?,..,?)' string |
519 |
my @values; |
520 |
foreach my $row ( @rows ) { |
521 |
push @values, map { |
522 |
my $col = $_; |
523 |
( @multiline and grep { $_ eq $col } @multiline ) |
524 |
? join "\r\n", @{$row->{$col}} # join multiline values |
525 |
: $row->{$col}; |
526 |
} @columns; |
527 |
} |
528 |
# Doing only 1 INSERT query for the whole table |
537 |
# Doing only 1 INSERT query for the whole table |
529 |
$query .= join ', ', ( $placeholders ) x scalar @rows; |
538 |
$query .= join ', ', ( $placeholders ) x scalar @values; |
530 |
$dbh->do( $query, undef, @values ); |
539 |
$dbh->do( $query, undef, @values ); |
531 |
} |
540 |
} |
532 |
for my $statement ( @{ $yaml->{'sql_statements'} } ) { # extra SQL statements |
541 |
for my $statement ( @{ $yaml->{'sql_statements'} } ) { # extra SQL statements |
533 |
- |
|
|