From 55fdf72df80937a0393754ee948dac7a30a7cd9d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 19 Mar 2020 12:33:47 +0100 Subject: [PATCH] Bug 24883: Add a new yml2sql sub to C4::Installer It will allow us to generate sql code from the yml files --- C4/Installer.pm | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/C4/Installer.pm b/C4/Installer.pm index b9a0ce53b2..8854cf83df 100644 --- a/C4/Installer.pm +++ b/C4/Installer.pm @@ -480,6 +480,28 @@ sub process_yml_table { return { query => $query, placeholders => $placeholders, values => \@values }; } +sub yml2sql { + my ( $class, $filepath ) = @_; + my $dbh = C4::Context->dbh; + my $yaml = LoadFile( $filepath ); + my @queries; + for my $table ( @{ $yaml->{'tables'} } ) { + my $query_info = process_yml_table($table); + my $query = $query_info->{query}; + my $values = $query_info->{values}; + push @queries, $query . "\n" . join( + ",\n", + map { + '(' . join( ', ', map { $dbh->quote($_) } @$_ ) . ')' + } @$values + ); + } + for my $statement ( @{ $yaml->{'sql_statements'} } ) { + push @queries, $statement; + } + return join ";\n", @queries; +} + sub load_sql { my $self = shift; my $filename = shift; -- 2.20.1