--- sql2xls.cgi 2008/11/03 23:19:40 21 +++ sql2xls.cgi 2008/11/06 10:49:02 23 @@ -118,7 +118,24 @@ my $date_format = $workbook->add_format(num_format => $xls_date_format); -my $dbh = DBI->connect($dsn . $database,$user,$passwd, { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr; +our $dbh; +sub use_database { + $dbh->disconnect if $dbh; + my $database = shift || return; + print STDERR "## connect to $database\n" if $debug; + $dbh = DBI->connect($dsn . $database,$user,$passwd, { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr; + if ( $db_encoding ) { + if ( $dsn =~ m{Pg} ) { + $dbh->do( qq{ set client_encoding = '$db_encoding'; } ); + } elsif ( $dsn =~ m{mysql} ) { + $dbh->do( qq{ set names '$db_encoding'; } ); + } else { + warn "Don't know how to set encoding to $db_encoding for $dsn"; + } + } +} + +use_database( $database ); sub _c { return shift unless $db_encoding; @@ -144,9 +161,7 @@ while() { chomp; if (/^\\c\s+(\S+)/) { - $dbh->disconnect if $dbh; - print STDERR "## connect to $1\n" if $debug; - $dbh = DBI->connect($dsn . $1,$user,$passwd, { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr; + use_database( $1 ); } elsif (/^--(.+)/) { $comment.=$1; } else {