line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::DBI; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
179785
|
use Catmandu::Sane; |
|
2
|
|
|
|
|
371575
|
|
|
2
|
|
|
|
|
17
|
|
4
|
2
|
|
|
2
|
|
2255
|
use DBI; |
|
2
|
|
|
|
|
18070
|
|
|
2
|
|
|
|
|
132
|
|
5
|
2
|
|
|
2
|
|
21
|
use Moo; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
15
|
|
6
|
2
|
|
|
2
|
|
1790
|
use MooX::Aliases; |
|
2
|
|
|
|
|
6289
|
|
|
2
|
|
|
|
|
12
|
|
7
|
2
|
|
|
2
|
|
841
|
use namespace::clean; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
14
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has data_source => (is => 'ro', required => 1, alias => 'dsn'); |
14
|
|
|
|
|
|
|
has username => (is => 'ro', alias => 'user'); |
15
|
|
|
|
|
|
|
has password => (is => 'ro', alias => 'pass'); |
16
|
|
|
|
|
|
|
has query => (is => 'ro', required => 1); |
17
|
|
|
|
|
|
|
has dbh => |
18
|
|
|
|
|
|
|
(is => 'ro', init_arg => undef, lazy => 1, builder => '_build_dbh',); |
19
|
|
|
|
|
|
|
has sth => |
20
|
|
|
|
|
|
|
(is => 'ro', init_arg => undef, lazy => 1, builder => '_build_sth',); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _build_dbh { |
23
|
0
|
|
|
0
|
|
|
my $self = $_[0]; |
24
|
0
|
|
|
|
|
|
DBI->connect( |
25
|
|
|
|
|
|
|
$self->dsn, |
26
|
|
|
|
|
|
|
$self->user, |
27
|
|
|
|
|
|
|
$self->password, |
28
|
|
|
|
|
|
|
{ |
29
|
|
|
|
|
|
|
AutoCommit => 1, |
30
|
|
|
|
|
|
|
RaiseError => 1, |
31
|
|
|
|
|
|
|
mysql_auto_reconnect => 1, |
32
|
|
|
|
|
|
|
mysql_enable_utf8 => 1, |
33
|
|
|
|
|
|
|
pg_utf8_strings => 1, |
34
|
|
|
|
|
|
|
sqlite_use_immediate_transaction => 1, |
35
|
|
|
|
|
|
|
sqlite_unicode => 1, |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _build_sth { |
41
|
0
|
|
|
0
|
|
|
my $self = $_[0]; |
42
|
0
|
|
|
|
|
|
my $sth = $self->dbh->prepare($self->query); |
43
|
0
|
|
|
|
|
|
$sth->execute; |
44
|
0
|
|
|
|
|
|
$sth; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub generator { |
48
|
|
|
|
|
|
|
my ($self) = @_; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
return sub { |
51
|
|
|
|
|
|
|
$self->sth->fetchrow_hashref(); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub DESTROY { |
56
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
57
|
0
|
|
|
|
|
|
$self->sth->finish; |
58
|
0
|
|
|
|
|
|
$self->dbh->disconnect; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 NAME |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Catmandu::Importer::DBI - Catmandu module to import data from any DBI source |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 LIMITATIONS |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Text columns are assumed to be utf-8. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 SYNOPSIS |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# From the command line |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$ catmandu convert DBI --dsn dbi:mysql:foobar --user foo --password bar --query "select * from table" |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# From Perl code |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
use Catmandu; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my %attrs = ( |
80
|
|
|
|
|
|
|
dsn => 'dbi:mysql:foobar' , |
81
|
|
|
|
|
|
|
user => 'foo' , |
82
|
|
|
|
|
|
|
password => 'bar' , |
83
|
|
|
|
|
|
|
query => 'select * from table' |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $importer = Catmandu->importer('DBI',%attrs); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Optional set extra parameters on the database handle |
89
|
|
|
|
|
|
|
# $importer->dbh->{LongReadLen} = 1024 * 64; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
$importer->each(sub { |
92
|
|
|
|
|
|
|
my $row_hash = shift; |
93
|
|
|
|
|
|
|
... |
94
|
|
|
|
|
|
|
}); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 DESCRIPTION |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
This L<Catmandu::Importer> can be used to access data stored in a relational database. |
99
|
|
|
|
|
|
|
Given a database handle and a SQL query an export of hits will be exported. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 CONFIGURATION |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=over |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item dsn |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Required. The connection parameters to the database. See L<DBI> for more information. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Examples: |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
dbi:mysql:foobar <= a local mysql database 'foobar' |
112
|
|
|
|
|
|
|
dbi:Pg:dbname=foobar;host=myserver.org;port=5432 <= a remote PostGres database |
113
|
|
|
|
|
|
|
dbi:SQLite:mydb.sqlite <= a local SQLLite file based database mydb.sqlite |
114
|
|
|
|
|
|
|
dbi:Oracle:host=myserver.org;sid=data01 <= a remote Oracle database |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Drivers for each database need to be available on your computer. Install then with: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
cpanm DBD::mysql |
119
|
|
|
|
|
|
|
cpanm DBD::Pg |
120
|
|
|
|
|
|
|
cpanm DBD::SQLite |
121
|
|
|
|
|
|
|
cpanm DBD::Oracle |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=item user |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Optional. A user name to connect to the database |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item password |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Optional. A password for connecting to the database |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item query |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Required. An SQL query to be executed against the datbase. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=back |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 SEE ALSO |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
L<Catmandu>, L<Catmandu::Importer> , L<Catmandu::Store::DBI> |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=cut |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
1; |