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) ); |
8 |
|
|
9 |
|
use Data::Dump qw/dump/; |
10 |
|
|
11 |
=head1 NAME |
=head1 NAME |
12 |
|
|
13 |
A3C::SQL |
A3C::SQL |
22 |
|
|
23 |
my $sql = A3C::SQL->new({ query => 'select now()' }); |
my $sql = A3C::SQL->new({ query => 'select now()' }); |
24 |
|
|
25 |
=cut |
As a alternative, if you don't want to use Jifty's database handle, |
26 |
|
specify it like this: |
27 |
|
|
28 |
use base qw(Jifty::Object Class::Accessor::Fast); |
my $sql = A3C::SQL->new({ |
29 |
__PACKAGE__->mk_accessors( qw(query arguments) ); |
query => 'select now()', |
30 |
|
dbh => $my_dbh, |
31 |
use Data::Dump qw/dump/; |
}); |
32 |
|
|
33 |
=head2 sth |
=head2 sth |
34 |
|
|
39 |
sub sth { |
sub sth { |
40 |
my $self = shift; |
my $self = shift; |
41 |
if ( ! $self->{_sth} ) { |
if ( ! $self->{_sth} ) { |
42 |
my $dbh = Jifty->handle->dbh; |
my $dbh = $self->dbh || Jifty->handle->dbh; |
43 |
my $sth = $dbh->prepare( $self->query ) or $dbh->errstr; |
my $sth = $dbh->prepare( $self->query ) or $dbh->errstr; |
44 |
if ( $self->arguments ) { |
if ( $self->arguments ) { |
45 |
Jifty->log->debug( $self->sql . ' arguments: ' . dump( $self->arguments ) ); |
Jifty->log->debug( $self->sql . ' arguments: ' . dump( $self->arguments ) ); |
46 |
$sth->execute( $self->arguments ) or $dbh->errstr; |
$sth->execute( $self->arguments ) or die $dbh->errstr; |
47 |
} else { |
} else { |
48 |
$sth->execute or $dbh->errstr; |
$sth->execute or die $dbh->errstr; |
49 |
} |
} |
50 |
$self->{_sth} = $sth; |
$self->{_sth} = $sth; |
51 |
} |
} |
79 |
return $self->sth->rows; |
return $self->sth->rows; |
80 |
} |
} |
81 |
|
|
82 |
|
=head1 HELPERS |
83 |
|
|
84 |
|
This helpers are accessor to L<DBI> |
85 |
|
|
86 |
|
=head2 _column_names |
87 |
|
|
88 |
|
my @columns = $sql->_column_names; |
89 |
|
|
90 |
|
=cut |
91 |
|
|
92 |
|
sub _column_names { |
93 |
|
my $self = shift; |
94 |
|
return @{ $self->sth->{NAME} }; |
95 |
|
} |
96 |
|
|
97 |
package A3C::SQL::row; |
package A3C::SQL::row; |
98 |
|
|
99 |
use Encode qw/decode/; |
use Encode qw/decode/; |
100 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
101 |
|
use base qw/Jifty::Object/; |
102 |
|
|
103 |
our $AUTOLOAD; |
our $AUTOLOAD; |
104 |
|
|
115 |
my $type = ref($self) or die "$self is not an object"; |
my $type = ref($self) or die "$self is not an object"; |
116 |
my $name = $AUTOLOAD; |
my $name = $AUTOLOAD; |
117 |
$name =~ s/.*://; |
$name =~ s/.*://; |
118 |
# warn "SQL: $name doesn't exist" unless defined($self->{$name}); |
Jifty->log->error("SQL: $name doesn't exist") unless defined($self->{$name}); |
119 |
return decode('UTF-8', $self->{$name}); |
return decode('UTF-8', $self->{$name}); |
120 |
} |
} |
121 |
|
|