File Coverage

blib/lib/Catmandu/Importer/DBI.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Catmandu::Importer::DBI;
2              
3 1     1   21082 use Catmandu::Sane;
  0            
  0            
4             use DBI;
5             use Moo;
6              
7             our $VERSION = '0.031';
8              
9             with 'Catmandu::Importer';
10              
11             has dsn => (is => 'ro' , required => 1);
12             has user => (is => 'ro');
13             has password => (is => 'ro');
14             has query => (is => 'ro' , required => 1);
15             has dbh => (
16             is => 'ro',
17             init_arg => undef,
18             lazy => 1,
19             builder => '_build_dbh',
20             );
21             has sth => (
22             is => 'ro',
23             init_arg => undef,
24             lazy => 1,
25             builder => '_build_sth',
26             );
27              
28             sub _build_dbh {
29             my $self = $_[0];
30             DBI->connect($self->dsn, $self->user, $self->password);
31             }
32              
33             sub _build_sth {
34             my $self = $_[0];
35             my $sth = $self->dbh->prepare($self->query);
36             $sth->execute;
37             $sth;
38             }
39              
40             sub generator {
41             my ($self) = @_;
42              
43             return sub {
44             $self->sth->fetchrow_hashref();
45             }
46             }
47              
48             sub DESTROY {
49             my ($self) = @_;
50             $self->sth->finish;
51             $self->dbh->disconnect;
52             }
53              
54             =head1 NAME
55              
56             Catmandu::Importer::DBI - Catmandu module to import data from any DBI source
57              
58             =head1 SYNOPSIS
59              
60             use Catmandu::Importer::DBI;
61              
62             my %attrs = (
63             dsn => 'dbi:mysql:foobar' ,
64             user => 'foo' ,
65             password => 'bar' ,
66             query => 'select * from table'
67             );
68              
69             my $importer = Catmandu::Importer::DBI->new(%attrs);
70              
71             # Optional set extra parameters on the database handle
72             # $importer->dbh->{LongReadLen} = 1024 * 64;
73              
74             $importer->each(sub {
75             my $row_hash = shift;
76             ...
77             });
78              
79              
80             # or
81              
82             $ catmandu convert DBI --dsn dbi:mysql:foobar --user foo --password bar --query "select * from table"
83              
84             =head1 AUTHORS
85              
86             Patrick Hochstenbach, C<< >>
87              
88             =head1 SEE ALSO
89              
90             L, L , L
91              
92             =cut
93              
94             1;