4 |
use warnings; |
use warnings; |
5 |
|
|
6 |
use base qw(Jifty::Object Class::Accessor::Fast); |
use base qw(Jifty::Object Class::Accessor::Fast); |
7 |
__PACKAGE__->mk_accessors( qw(query arguments dbh) ); |
__PACKAGE__->mk_accessors( qw(query arguments dbh encoding) ); |
8 |
|
|
9 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
10 |
|
|
28 |
my $sql = A3C::SQL->new({ |
my $sql = A3C::SQL->new({ |
29 |
query => 'select now()', |
query => 'select now()', |
30 |
dbh => $my_dbh, |
dbh => $my_dbh, |
31 |
|
encoding => 'UTF-8', |
32 |
}); |
}); |
33 |
|
|
34 |
|
Mungling with C<encoding> is rearly needed, especially if using recent C<DBD::Pg> as driver. |
35 |
|
|
36 |
=head2 sth |
=head2 sth |
37 |
|
|
38 |
Execute query and return statement handle. Ususally you don't have to call this manually. |
Execute query and return statement handle. Ususally you don't have to call this manually. |
68 |
my $self = shift; |
my $self = shift; |
69 |
my $row = $self->sth->fetchrow_hashref; |
my $row = $self->sth->fetchrow_hashref; |
70 |
return unless defined $row; |
return unless defined $row; |
71 |
return A3C::SQL::row->new( $row ); |
return A3C::SQL::row->new( $row, $self->encoding ); |
72 |
} |
} |
73 |
|
|
74 |
=head2 count |
=head2 count |
110 |
my $class = ref($that) || $that; |
my $class = ref($that) || $that; |
111 |
my $self = shift; |
my $self = shift; |
112 |
bless $self, $class; |
bless $self, $class; |
113 |
|
$self->{__encoding} = shift || 'UTF-8'; |
114 |
return $self; |
return $self; |
115 |
} |
} |
116 |
|
|
119 |
my $type = ref($self) or die "$self is not an object"; |
my $type = ref($self) or die "$self is not an object"; |
120 |
my $name = $AUTOLOAD; |
my $name = $AUTOLOAD; |
121 |
$name =~ s/.*://; |
$name =~ s/.*://; |
122 |
Jifty->log->error("SQL: $name doesn't exist") unless defined($self->{$name}); |
my $v = $self->{$name}; |
123 |
return decode('UTF-8', $self->{$name}); |
Jifty->log->error("SQL: $name doesn't exist") unless defined $v; |
124 |
|
if ( ! Encode::is_utf8( $v ) ) { |
125 |
|
eval { $v = decode( $self->{__encoding}, $self->{$name} ) }; |
126 |
|
if ( $@ ) { |
127 |
|
warn "## column $name can't decode ",dump( $self->{$name} ); |
128 |
|
$v = $self->{$name}; |
129 |
|
} |
130 |
|
} |
131 |
|
return $v; |
132 |
} |
} |
133 |
|
|
134 |
sub DESTROY {} |
sub DESTROY {} |