| 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 | warn "Usage: $0 <table> <csv>\n\n"; | ||
| 13 | warn "\tThis extracts database params into csv file.\n"; | ||
| 14 | exit 1; | ||
| 15 | } | ||
| 16 | |||
| 17 | my ( $table, $file ) = @ARGV; | ||
| 18 | |||
| 19 | my $cfg = config_read('EDTK_DB'); | ||
| 20 | my $dbh = db_connect( $cfg, 'EDTK_DBI_PARAM', { AutoCommit => 1, RaiseError => 1 } ); | ||
| 21 | |||
| 22 | open( my $fh, ">", $file ) or die "ERROR: can't open $file: $!\n"; | ||
| 23 | my $csv = Text::CSV->new( { sep_char => ";", binary => 1, eol => "\n" } ); | ||
| 24 | |||
| 25 | eval { | ||
| 26 | # Get the column names. | ||
| 27 | my $sth = $dbh->column_info( undef, undef, $table, '%' ); #bon sous Oracle | ||
| 28 | $sth->execute(); | ||
| 29 | my $cols = $dbh->selectcol_arrayref( $sth, { Columns => [4] } ); | ||
| 30 | |||
| 31 | $csv->print( $fh, $cols ); | ||
| 32 | |||
| 33 | $sth = $dbh->prepare("SELECT * FROM $table"); | ||
| 34 | $sth->execute(); | ||
| 35 | while ( my $row = $sth->fetchrow_hashref() ) { | ||
| 36 | my @vals = map { $row->{$_} } @$cols; | ||
| 37 | $csv->print( $fh, \@vals ); | ||
| 38 | } | ||
| 39 | }; | ||
| 40 | if ($@) { | ||
| 41 | warn "ERROR: $@\n"; | ||
| 42 | } | ||
| 43 | |||
| 44 | $dbh->disconnect; | ||
| 45 | close($fh); |