From 2f41bca4e8ef591f5fa5c9ee620c7aacc9409f71 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Fri, 2 Mar 2012 11:53:49 +0100 Subject: cleaner way to set restore the UTF-8 flag for the output of SELECT queries --- videoadd.pl | 27 ++++++++++++++------------- videodb-check.pl | 1 + videodb.rc | 1 - videomv.pl | 1 + videorm.pl | 1 + 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/videoadd.pl b/videoadd.pl index 1c8b0db..0d39eae 100755 --- a/videoadd.pl +++ b/videoadd.pl @@ -267,6 +267,7 @@ unless ( defined $ignoredb_flag ) { } $dbh = DBI->connect($dsn, $config{user}, $config{password}) or die "Error: Can't connect do database.\n"; + $dbh->do( "SET NAMES UTF8" ) or die "Error: Can't set names to UTF-8.\n"; } my $id; @@ -658,6 +659,15 @@ sub getfile { my $res = $dbh->selectall_hashref( $SELECT, 'id' ) or die "Can't select: $!\n"; + + # The database is using UTF-8, but the UTF-8 flag may be missing; + # restore it. + foreach my $id (keys $res) { + foreach my $k (keys $res->{$id}) { + Encode::_utf8_on($res->{$id}->{$k}); + } + } + # We have a new file to add to the database if (scalar (keys $res) > 0) { my $answer; @@ -672,7 +682,7 @@ sub getfile { my @options; foreach my $id (keys $res) { printf "%04d - ", $res->{$id}->{id} or die "Can't printf: $!"; - print &iconv($res->{$id}->{title}. " (" .$res->{$id}->{year}. ")\n", 'utf8') + print $res->{$id}->{title}. " (" .$res->{$id}->{year}. ")\n" or die "Can't print: $!"; push @options, sprintf ("%04d", $res->{$id}->{id}); } @@ -798,22 +808,13 @@ sub YNquestion { } # Convert to UTF-8 -# (Strings from the database are in UTF-8, but are lacking the UTF-8 flag) +# The input string should be in latin1 unless the UTF-8 flag is on. sub iconv { - my ($string, $enc) = @_; + my $string = $_[0]; return unless defined $string; unless (Encode::is_utf8($string)) { - my @encodings = qw/latin1 utf8/; - @encodings = $enc if defined $enc; - - my $decoded; - foreach my $enc (@encodings) { - $decoded = Encode::decode( $enc, $string); - next if $@ or not $decoded; - last if utf8::valid($decoded); - } - $string = $decoded; + $string = Encode::decode( 'latin1', $string, 1 ); } # The UTF-8 flag should be on now diff --git a/videodb-check.pl b/videodb-check.pl index d998af4..dae4cdb 100755 --- a/videodb-check.pl +++ b/videodb-check.pl @@ -145,6 +145,7 @@ if (defined $config{dbi_misc}) { } my $dbh = DBI->connect($dsn, $config{user}, $config{password}) or die "Can't connect do database\n"; +$dbh->do( "SET NAMES UTF8" ) or die "Error: Can't set names to UTF-8.\n"; ################################################################################ diff --git a/videodb.rc b/videodb.rc index 032bded..94a938f 100644 --- a/videodb.rc +++ b/videodb.rc @@ -11,7 +11,6 @@ user => 'username', # Optional extra connection options dbi_misc => { mysql_ssl => 1 , mysql_ssl_ca_file => "/etc/ssl/certs/cacert.org.pem" - , mysql_enable_utf8 => 1 }, password => '******', prefix => 'videodb_', diff --git a/videomv.pl b/videomv.pl index 2bc3313..dd66375 100755 --- a/videomv.pl +++ b/videomv.pl @@ -390,6 +390,7 @@ unless (defined $ignoredb_flag) { } $dbh = DBI->connect($dsn, $config{user}, $config{password}) or die "Can't connect do database"; + $dbh->do( "SET NAMES UTF8" ) or die "Error: Can't set names to UTF-8.\n"; # Lookup for the file in the videodb database $RES = diff --git a/videorm.pl b/videorm.pl index 3acf4f3..bbdc5c2 100755 --- a/videorm.pl +++ b/videorm.pl @@ -172,6 +172,7 @@ if (defined $config{dbi_misc}) { } my $dbh = DBI->connect($dsn, $config{user}, $config{password}) or die "Can't connect do database.\n"; +$dbh->do( "SET NAMES UTF8" ) or die "Error: Can't set names to UTF-8.\n"; # Lookup for the file in the videodb database my $res = $dbh->selectall_arrayref ( "SELECT id FROM $config{prefix}videodata WHERE filename = ?", -- cgit v1.2.3