summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem.moulin@ens-lyon.org>2012-03-02 11:53:49 +0100
committerGuilhem Moulin <guilhem.moulin@ens-lyon.org>2012-03-02 11:53:49 +0100
commit2f41bca4e8ef591f5fa5c9ee620c7aacc9409f71 (patch)
treea70932ff81e6bcc0a3da818f97959723c26f4b28
parent8aac36c6e2d16c7122eb1b18febba4b5b9374323 (diff)
cleaner way to set restore the UTF-8 flag for the output of SELECT queries
-rwxr-xr-xvideoadd.pl27
-rwxr-xr-xvideodb-check.pl1
-rw-r--r--videodb.rc1
-rwxr-xr-xvideomv.pl1
-rwxr-xr-xvideorm.pl1
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 = ?",