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