blib/lib/oEdtk/lib/params_Import.pl untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 72 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/perl
2  
3 use strict;
4 use warnings;
5  
6 use Text::CSV;
7 use oEdtk::Main;
8 use oEdtk::Config qw(config_read);
9 use oEdtk::DBAdmin qw(db_connect);
10  
11 if ( $#ARGV < 1 or $ARGV[0] =~ /-h/i ) {
12 die "\nUsage: $0 <table> <csv> [INI_SECTION]\n"
13 . "\n\t\twhere INI_SECTION is a configuration section in edtk.ini\n"
14 . "\t\t\t(usefull to load params in Prod, Dev, or other environnement)\n\n";
15 exit 1;
16 }
17  
18 my ( $table, $file ) = @ARGV;
19 my $section = shift || "";
20 my $DBI = 'EDTK_DBI_PARAM';
21 $DBI = 'EDTK_DBI_DSN' if ( $section ne "" );
22  
23 open( my $fh, "<", $file ) or die "ERROR: can't open $file: $!\n";
24 my $csv = Text::CSV->new( { sep_char => ';', binary => 1 } );
25  
26 my $cfg = config_read( 'EDTK_DB', $section );
27 my $dbh = db_connect( $cfg, $DBI, { AutoCommit => 0, RaiseError => 1 } );
28  
29 # Set the column names.
30 $csv->column_names( $csv->getline($fh) );
31  
32 eval {
33 if ( $dbh->{'Driver'}->{'Name'} eq 'SQLite' ) {
34 $dbh->do("DELETE FROM $table");
35 }
36 else {
37 $dbh->do("TRUNCATE TABLE $table");
38 }
39  
40 while ( my $row = $csv->getline_hr($fh) ) {
41 my $sql =
42 "INSERT INTO $table ("
43 . join( ', ', keys(%$row) )
44 . ") VALUES ("
45 . join( ', ', ('?') x keys(%$row) ) . ")";
46 $dbh->do( $sql, undef, values(%$row) );
47 }
48 $dbh->commit;
49 };
50 if ($@) {
51 warn "ERROR: $@\n";
52 eval { $dbh->rollback };
53 }
54  
55 $dbh->disconnect;
56 close($fh);