#!/usr/bin/perl
use warnings;
use strict;
use DBI;
use Data::Dump qw(dump);
our $dsn = 'DBI:mysql:dbname=';
our $database = '';
our $user = '';
our $passwd = '';
require 'config.pl' if -e 'config.pl';
my $table = 'borrowers';
my $schema = $database;
my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
open(my $load, '>', 'load.xml');
sub dump_table {
my $table = shift;
my @cols = @_;
@cols = ( '*' ) unless @cols;
my $sql = 'select ' . join(',', @cols) . ' from ' . $table;
warn "# sql $sql\n";
my $sth = $dbh->prepare( $sql );
$sth->execute();
warn "got ", $sth->rows, " rows\n";
my @columns = @{ $sth->{NAME} };
warn "columns ", dump @columns;
print $load qq|\n|;
print $load qq|
| foreach @columns;
print $load qq|\n|;
my $file = "$table.csv";
print $load qq|\n|;
foreach my $i ( 0 .. $#columns ) {
print $load qq|
|;
}
print $load qq|\n|;
open(my $fh, '>', $file) || die "$file: $!";
while (my $row = $sth->fetchrow_arrayref) {
print $fh join(',', map { ! defined $_ ? '' : m/^\d+$/ ? $_ : "\"$_\"" } @$row), "\n";
}
close($fh);
warn "$file ", -s $file, " bytes\n";
}
print $load qq|\n|;
dump_table 'borrowers' => qw(
borrowernumber
cardnumber
surname
firstname
email
);
dump_table 'borrower_attributes' => qw(
borrowernumber
code
attribute
);
print $load qq|\n|;
#$dbh->commit;