Skip to content

Instantly share code, notes, and snippets.

@buzztaiki
Created February 17, 2012 02:37
Show Gist options
  • Save buzztaiki/1850059 to your computer and use it in GitHub Desktop.
Save buzztaiki/1850059 to your computer and use it in GitHub Desktop.
From c1606c90445679a04856665942cd6b398d441ff7 Mon Sep 17 00:00:00 2001
From: Taiki Sugawara <buzz.taiki@gmail.com>
Date: Fri, 17 Feb 2012 11:32:58 +0900
Subject: [PATCH] Encode utf-8 string.
---
edbi-bridge.pl | 34 ++++++++++++++++++++++++++--------
1 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/edbi-bridge.pl b/edbi-bridge.pl
index bcc960c..8f50976 100644
--- a/edbi-bridge.pl
+++ b/edbi-bridge.pl
@@ -4,10 +4,27 @@ use strict;
use RPC::EPC::Service;
use Data::Dumper;
use DBI;
+use Encode;
our $dbh = undef;
our $sth = undef;
+sub edbi_encode {
+ my ($str) = @_;
+ return encode_utf8($_) if Encode::is_utf8($str);
+ return $str;
+}
+
+sub edbi_encode_row {
+ my ($row) = @_;
+ return [map {edbi_encode($_)} @$row];
+}
+
+sub edbi_encode_rows {
+ my ($rows) = @_;
+ return [map {edbi_encode_row($_)} @$rows];
+}
+
sub edbi_connect {
my ($args) = @_;
my ($data_source,$username,$auth) = @$args;
@@ -32,7 +49,8 @@ sub edbi_select_all {
return undef unless $dbh;
my ($args) = @_;
my ($sql, $params) = @$args;
- my $rows = $dbh->selectall_arrayref($sql, undef, @$params); return $rows;
+ my $rows = $dbh->selectall_arrayref($sql, undef, @$params);
+ return edbi_encode_rows($rows);
}
sub edbi_prepare {
@@ -58,7 +76,7 @@ sub edbi_fetch {
return undef unless $sth;
my ($num) = @_;
if ($num eq undef) {
- return $sth->fetchall_arrayref();
+ return edbi_encode_rows($sth->fetchall_arrayref());
} else {
my $ret = [];
for (my $i = 0; $i < $num; $i++) {
@@ -66,7 +84,7 @@ sub edbi_fetch {
last if $row eq undef;
push @$ret, $row;
}
- return $ret;
+ return edbi_encode_rows($ret);
}
}
@@ -103,7 +121,7 @@ sub edbi_disconnect {
sub edbi_status {
return undef unless $dbh;
- return [$dbh->err, $dbh->errstr, $dbh->state];
+ return [$dbh->err, edbi_encode($dbh->errstr), edbi_encode($dbh->state)];
}
sub edbi_table_info {
@@ -114,7 +132,7 @@ sub edbi_table_info {
my ($args) = @_;
my ($catalog, $schema, $table, $type) = @$args;
$sth = $dbh->table_info( $catalog, $schema, $table, $type );
- return [$sth->{NAME}, $sth->fetchall_arrayref()];
+ return [$sth->{NAME}, edbi_encode_rows($sth->fetchall_arrayref())];
}
sub edbi_column_info {
@@ -125,7 +143,7 @@ sub edbi_column_info {
my ($args) = @_;
my ($catalog, $schema, $table, $column) = @$args;
$sth = $dbh->column_info( $catalog, $schema, $table, $column );
- return [$sth->{NAME}, $sth->fetchall_arrayref()];
+ return [$sth->{NAME}, edbi_encode_rows($sth->fetchall_arrayref())];
}
sub edbi_primary_key_info {
@@ -137,7 +155,7 @@ sub edbi_primary_key_info {
my ($catalog, $schema, $table) = @$args;
$sth = $dbh->primary_key_info( $catalog, $schema, $table );
return undef unless $sth;
- return [$sth->{NAME}, $sth->fetchall_arrayref()];
+ return [$sth->{NAME}, edbi_encode_rows($sth->fetchall_arrayref())];
}
sub edbi_foreign_key_info {
@@ -150,7 +168,7 @@ sub edbi_foreign_key_info {
$sth = $dbh->foreign_key_info( $pkcatalog, $pkschema, $pktable,
$fkcatalog, $fkschema, $fktable );
return undef unless $sth;
- return [$sth->{NAME}, $sth->fetchall_arrayref()];
+ return [$sth->{NAME}, edbi_encode_rows($sth->fetchall_arrayref())];
}
sub main {
--
1.7.5.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment