--- lib/A3C/SQL.pm 2008/06/15 14:40:50 159 +++ lib/A3C/SQL.pm 2008/06/22 14:55:55 220 @@ -4,9 +4,10 @@ use warnings; use base qw(Jifty::Object Class::Accessor::Fast); -__PACKAGE__->mk_accessors( qw(query arguments dbh) ); +__PACKAGE__->mk_accessors( qw(query arguments dbh encoding duration) ); use Data::Dump qw/dump/; +use Time::HiRes qw/time/; =head1 NAME @@ -28,8 +29,11 @@ my $sql = A3C::SQL->new({ query => 'select now()', dbh => $my_dbh, + encoding => 'UTF-8', }); +Mungling with C is rearly needed, especially if using recent C as driver. + =head2 sth Execute query and return statement handle. Ususally you don't have to call this manually. @@ -41,12 +45,14 @@ if ( ! $self->{_sth} ) { my $dbh = $self->dbh || Jifty->handle->dbh; my $sth = $dbh->prepare( $self->query ) or $dbh->errstr; + my $t = time(); if ( $self->arguments ) { Jifty->log->debug( $self->sql . ' arguments: ' . dump( $self->arguments ) ); - $sth->execute( $self->arguments ) or $dbh->errstr; + $sth->execute( $self->arguments ) or die $dbh->errstr; } else { - $sth->execute or $dbh->errstr; + $sth->execute or die $dbh->errstr; } + $self->duration( time() - $t ); $self->{_sth} = $sth; } @@ -65,7 +71,7 @@ my $self = shift; my $row = $self->sth->fetchrow_hashref; return unless defined $row; - return A3C::SQL::row->new( $row ); + return A3C::SQL::row->new( $row, $self->encoding ); } =head2 count @@ -107,6 +113,7 @@ my $class = ref($that) || $that; my $self = shift; bless $self, $class; + $self->{__encoding} = shift || 'UTF-8'; return $self; } @@ -115,8 +122,16 @@ my $type = ref($self) or die "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; - Jifty->log->error("SQL: $name doesn't exist") unless defined($self->{$name}); - return decode('UTF-8', $self->{$name}); + my $v = $self->{$name}; + Jifty->log->error("SQL: $name doesn't exist") unless defined $v; + if ( ! Encode::is_utf8( $v ) ) { + eval { $v = decode( $self->{__encoding}, $self->{$name} ) }; + if ( $@ ) { + warn "## column $name can't decode ",dump( $self->{$name} ); + $v = $self->{$name}; + } + } + return $v; } sub DESTROY {}