1 |
package Frey::DBI; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
|
6 |
use DBI; |
7 |
|
8 |
has 'dsn' => ( |
9 |
is => 'ro', |
10 |
isa => 'dsn', |
11 |
default => 'DBI:Pg:dbname=template1', |
12 |
required => 1, |
13 |
); |
14 |
|
15 |
has 'query' => ( |
16 |
is => 'ro', |
17 |
isa => 'Str', |
18 |
required => 1, |
19 |
default => 'select 42 as meaning_of_life', |
20 |
); |
21 |
|
22 |
sub as_sponge { |
23 |
my ( $self ) = @_; |
24 |
|
25 |
warn "# dsn: ", $self->dsn; |
26 |
my $dbh = DBI->connect( $self->dsn, '', '', { RaiseError => 1 } ) || die $DBI::errstr; |
27 |
$dbh->do( qq{ set client_encoding='utf-8' } ) if $self->dsn =~ m{pg}i; |
28 |
|
29 |
warn "# query: ", $self->query; |
30 |
my $sth = $dbh->prepare( $self->query ); |
31 |
$sth->execute(); |
32 |
|
33 |
return { |
34 |
NAME => $sth->{NAME}, |
35 |
rows => $sth->fetchall_arrayref, |
36 |
}; |
37 |
} |
38 |
|
39 |
__PACKAGE__->meta->make_immutable; |
40 |
no Moose; |
41 |
|
42 |
1; |