3 |
use strict; |
use strict; |
4 |
use warnings; |
use warnings; |
5 |
|
|
6 |
|
use base qw(Jifty::Object Class::Accessor::Fast); |
7 |
|
__PACKAGE__->mk_accessors( qw(query arguments dbh encoding duration) ); |
8 |
|
|
9 |
|
use Data::Dump qw/dump/; |
10 |
|
use Time::HiRes qw/time/; |
11 |
|
|
12 |
=head1 NAME |
=head1 NAME |
13 |
|
|
14 |
A3C::SQL |
A3C::SQL |
23 |
|
|
24 |
my $sql = A3C::SQL->new({ query => 'select now()' }); |
my $sql = A3C::SQL->new({ query => 'select now()' }); |
25 |
|
|
26 |
=cut |
As a alternative, if you don't want to use Jifty's database handle, |
27 |
|
specify it like this: |
28 |
|
|
29 |
use base qw(Jifty::Object Class::Accessor::Fast); |
my $sql = A3C::SQL->new({ |
30 |
__PACKAGE__->mk_accessors( qw(query arguments) ); |
query => 'select now()', |
31 |
|
dbh => $my_dbh, |
32 |
|
encoding => 'UTF-8', |
33 |
|
}); |
34 |
|
|
35 |
use Data::Dump qw/dump/; |
Mungling with C<encoding> is rearly needed, especially if using recent C<DBD::Pg> as driver. |
36 |
|
|
37 |
=head2 sth |
=head2 sth |
38 |
|
|
43 |
sub sth { |
sub sth { |
44 |
my $self = shift; |
my $self = shift; |
45 |
if ( ! $self->{_sth} ) { |
if ( ! $self->{_sth} ) { |
46 |
my $dbh = Jifty->handle->dbh; |
my $dbh = $self->dbh || Jifty->handle->dbh; |
47 |
my $sth = $dbh->prepare( $self->query ) or $dbh->errstr; |
my $sth = $dbh->prepare( $self->query ) or $dbh->errstr; |
48 |
|
my $t = time(); |
49 |
if ( $self->arguments ) { |
if ( $self->arguments ) { |
50 |
Jifty->log->debug( $self->sql . ' arguments: ' . dump( $self->arguments ) ); |
Jifty->log->debug( $self->sql . ' arguments: ' . dump( $self->arguments ) ); |
51 |
$sth->execute( $self->arguments ) or $dbh->errstr; |
$sth->execute( $self->arguments ) or die $dbh->errstr; |
52 |
} else { |
} else { |
53 |
$sth->execute or $dbh->errstr; |
$sth->execute or die $dbh->errstr; |
54 |
} |
} |
55 |
|
$self->duration( time() - $t ); |
56 |
$self->{_sth} = $sth; |
$self->{_sth} = $sth; |
57 |
} |
} |
58 |
|
|
71 |
my $self = shift; |
my $self = shift; |
72 |
my $row = $self->sth->fetchrow_hashref; |
my $row = $self->sth->fetchrow_hashref; |
73 |
return unless defined $row; |
return unless defined $row; |
74 |
return A3C::SQL::row->new( $row ); |
return A3C::SQL::row->new( $row, $self->encoding ); |
75 |
} |
} |
76 |
|
|
77 |
=head2 count |
=head2 count |
85 |
return $self->sth->rows; |
return $self->sth->rows; |
86 |
} |
} |
87 |
|
|
88 |
|
=head1 HELPERS |
89 |
|
|
90 |
|
This helpers are accessor to L<DBI> |
91 |
|
|
92 |
|
=head2 _column_names |
93 |
|
|
94 |
|
my @columns = $sql->_column_names; |
95 |
|
|
96 |
|
=cut |
97 |
|
|
98 |
|
sub _column_names { |
99 |
|
my $self = shift; |
100 |
|
return @{ $self->sth->{NAME} }; |
101 |
|
} |
102 |
|
|
103 |
package A3C::SQL::row; |
package A3C::SQL::row; |
104 |
|
|
105 |
use Encode qw/decode/; |
use Encode qw/decode/; |
106 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
107 |
|
use base qw/Jifty::Object/; |
108 |
|
|
109 |
our $AUTOLOAD; |
our $AUTOLOAD; |
110 |
|
|
113 |
my $class = ref($that) || $that; |
my $class = ref($that) || $that; |
114 |
my $self = shift; |
my $self = shift; |
115 |
bless $self, $class; |
bless $self, $class; |
116 |
|
$self->{__encoding} = shift || 'UTF-8'; |
117 |
return $self; |
return $self; |
118 |
} |
} |
119 |
|
|
122 |
my $type = ref($self) or die "$self is not an object"; |
my $type = ref($self) or die "$self is not an object"; |
123 |
my $name = $AUTOLOAD; |
my $name = $AUTOLOAD; |
124 |
$name =~ s/.*://; |
$name =~ s/.*://; |
125 |
# warn "SQL: $name doesn't exist" unless defined($self->{$name}); |
my $v = $self->{$name}; |
126 |
return decode('UTF-8', $self->{$name}); |
Jifty->log->error("SQL: $name doesn't exist") unless defined $v; |
127 |
|
if ( ! Encode::is_utf8( $v ) ) { |
128 |
|
eval { $v = decode( $self->{__encoding}, $self->{$name} ) }; |
129 |
|
if ( $@ ) { |
130 |
|
warn "## column $name can't decode ",dump( $self->{$name} ); |
131 |
|
$v = $self->{$name}; |
132 |
|
} |
133 |
|
} |
134 |
|
return $v; |
135 |
} |
} |
136 |
|
|
137 |
sub DESTROY {} |
sub DESTROY {} |