1 |
package A3C::SQL; |
2 |
|
3 |
use strict; |
4 |
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 |
13 |
|
14 |
A3C::SQL |
15 |
|
16 |
=head1 DESCRIPTION |
17 |
|
18 |
Issue SQL queries |
19 |
|
20 |
=head1 METHODS |
21 |
|
22 |
=head2 new |
23 |
|
24 |
my $sql = A3C::SQL->new({ query => 'select now()' }); |
25 |
|
26 |
As a alternative, if you don't want to use Jifty's database handle, |
27 |
specify it like this: |
28 |
|
29 |
my $sql = A3C::SQL->new({ |
30 |
query => 'select now()', |
31 |
dbh => $my_dbh, |
32 |
encoding => 'UTF-8', |
33 |
}); |
34 |
|
35 |
Mungling with C<encoding> is rearly needed, especially if using recent C<DBD::Pg> as driver. |
36 |
|
37 |
=head2 sth |
38 |
|
39 |
Execute query and return statement handle. Ususally you don't have to call this manually. |
40 |
|
41 |
=cut |
42 |
|
43 |
sub sth { |
44 |
my $self = shift; |
45 |
if ( ! $self->{_sth} ) { |
46 |
my $dbh = $self->dbh || Jifty->handle->dbh; |
47 |
my $sth = $dbh->prepare( $self->query ) or $dbh->errstr; |
48 |
my $t = time(); |
49 |
if ( $self->arguments ) { |
50 |
Jifty->log->debug( $self->sql . ' arguments: ' . dump( $self->arguments ) ); |
51 |
$sth->execute( $self->arguments ) or die $dbh->errstr; |
52 |
} else { |
53 |
$sth->execute or die $dbh->errstr; |
54 |
} |
55 |
$self->duration( time() - $t ); |
56 |
$self->{_sth} = $sth; |
57 |
} |
58 |
|
59 |
return $self->{_sth}; |
60 |
} |
61 |
|
62 |
=head2 next |
63 |
|
64 |
while ( my $row = $sql->next ) { |
65 |
print $row->now; |
66 |
} |
67 |
|
68 |
=cut |
69 |
|
70 |
sub next { |
71 |
my $self = shift; |
72 |
my $row = $self->sth->fetchrow_hashref; |
73 |
return unless defined $row; |
74 |
# $self->log->debug( dump( $row ) ); |
75 |
return A3C::SQL::row->new( $row, $self->encoding ); |
76 |
} |
77 |
|
78 |
=head2 count |
79 |
|
80 |
print $sql->count; |
81 |
|
82 |
=cut |
83 |
|
84 |
sub count { |
85 |
my $self = shift; |
86 |
return $self->sth->rows; |
87 |
} |
88 |
|
89 |
=head1 HELPERS |
90 |
|
91 |
This helpers are accessor to L<DBI> |
92 |
|
93 |
=head2 _column_names |
94 |
|
95 |
my @columns = $sql->_column_names; |
96 |
|
97 |
=cut |
98 |
|
99 |
sub _column_names { |
100 |
my $self = shift; |
101 |
return @{ $self->sth->{NAME} }; |
102 |
} |
103 |
|
104 |
package A3C::SQL::row; |
105 |
|
106 |
use Encode qw/decode/; |
107 |
use Data::Dump qw/dump/; |
108 |
use base qw/Jifty::Object/; |
109 |
|
110 |
our $AUTOLOAD; |
111 |
|
112 |
sub new { |
113 |
my $that = shift; |
114 |
my $class = ref($that) || $that; |
115 |
my $self = shift; |
116 |
bless $self, $class; |
117 |
$self->{__encoding} = shift || 'UTF-8'; |
118 |
return $self; |
119 |
} |
120 |
|
121 |
sub AUTOLOAD { |
122 |
my $self = shift; |
123 |
my $type = ref($self) or die "$self is not an object"; |
124 |
my $name = lc($AUTOLOAD); # DBI lowercase columns so we do same for accessors |
125 |
$name =~ s/.*://; |
126 |
my $v = $self->{$name}; |
127 |
Jifty->log->error("SQL: $name doesn't exist") unless defined $v; |
128 |
if ( ! Encode::is_utf8( $v ) ) { |
129 |
eval { $v = decode( $self->{__encoding}, $self->{$name} ) }; |
130 |
if ( $@ ) { |
131 |
warn "## column $name can't decode ",dump( $self->{$name} ); |
132 |
$v = $self->{$name}; |
133 |
} |
134 |
} |
135 |
return $v; |
136 |
} |
137 |
|
138 |
sub DESTROY {} |
139 |
|
140 |
1; |