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