line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package TablesRole::Source::DBI; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
4
|
|
|
|
|
|
|
our $DATE = '2020-11-10'; # DATE |
5
|
|
|
|
|
|
|
our $DIST = 'TablesRoles-Standard'; # DIST |
6
|
|
|
|
|
|
|
our $VERSION = '0.006'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
505
|
use 5.010001; |
|
1
|
|
|
|
|
4
|
|
9
|
1
|
|
|
1
|
|
7
|
use Role::Tiny; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
10
|
1
|
|
|
1
|
|
161
|
use Role::Tiny::With; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
668
|
|
11
|
|
|
|
|
|
|
with 'TablesRole::Spec::Basic'; |
12
|
|
|
|
|
|
|
with 'TablesRole::Util::CSV'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
1
|
|
|
1
|
1
|
52246
|
my ($class, %args) = @_; |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
|
|
5
|
my $dsn = delete $args{dsn}; |
18
|
1
|
|
|
|
|
3
|
my $user = delete $args{user}; |
19
|
1
|
|
|
|
|
2
|
my $password = delete $args{password}; |
20
|
1
|
|
|
|
|
3
|
my $dbh = delete $args{dbh}; |
21
|
1
|
50
|
|
|
|
8
|
if (defined $dbh) { |
|
|
0
|
|
|
|
|
|
22
|
|
|
|
|
|
|
} elsif (defined $dsn) { |
23
|
0
|
|
|
|
|
0
|
require DBI; |
24
|
0
|
|
|
|
|
0
|
$dbh = DBI->connect($dsn, $user, $password, {RaiseError=>1}); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
|
|
4
|
my $sth = delete $args{sth}; |
28
|
1
|
|
|
|
|
3
|
my $sth_bind_params = delete $args{sth_bind_params}; |
29
|
1
|
|
|
|
|
3
|
my $query = delete $args{query}; |
30
|
1
|
|
|
|
|
3
|
my $table = delete $args{table}; |
31
|
1
|
50
|
|
|
|
5
|
if (defined $sth) { |
32
|
|
|
|
|
|
|
} else { |
33
|
1
|
50
|
|
|
|
6
|
die "You specify 'query' or 'table', but you don't specify ". |
34
|
|
|
|
|
|
|
"dbh/dsn+user+password, so I cannot create a statement handle" |
35
|
|
|
|
|
|
|
unless $dbh; |
36
|
1
|
50
|
|
|
|
7
|
if (defined $query) { |
|
|
50
|
|
|
|
|
|
37
|
|
|
|
|
|
|
} elsif (defined $table) { |
38
|
1
|
|
|
|
|
4
|
$query = "SELECT * FROM $table"; |
39
|
|
|
|
|
|
|
} else { |
40
|
0
|
|
|
|
|
0
|
die "Please specify 'sth', 'query', or 'table' argument"; |
41
|
|
|
|
|
|
|
} |
42
|
1
|
|
|
|
|
25
|
$sth = $dbh->prepare($query); |
43
|
1
|
|
50
|
|
|
124
|
$sth->execute(@{ $sth_bind_params // [] }); # to check query syntax |
|
1
|
|
|
|
|
73
|
|
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
5
|
my $row_count_sth = delete $args{row_count_sth}; |
47
|
1
|
|
|
|
|
4
|
my $row_count_sth_bind_params = delete $args{row_count_sth_bind_params}; |
48
|
1
|
|
|
|
|
3
|
my $row_count_query = delete $args{row_count_query}; |
49
|
1
|
50
|
|
|
|
5
|
if (defined $row_count_sth) { |
50
|
|
|
|
|
|
|
} else { |
51
|
1
|
50
|
|
|
|
5
|
die "You specify 'row_count_query' or 'table', but you don't specify ". |
52
|
|
|
|
|
|
|
"dbh/dsn+user+password, so I cannot create a statement handle" |
53
|
|
|
|
|
|
|
unless $dbh; |
54
|
1
|
50
|
|
|
|
6
|
if (defined $row_count_query) { |
|
|
50
|
|
|
|
|
|
55
|
|
|
|
|
|
|
} elsif (defined $table) { |
56
|
1
|
|
|
|
|
3
|
$row_count_query = "SELECT COUNT(*) FROM $table"; |
57
|
|
|
|
|
|
|
} else { |
58
|
0
|
|
|
|
|
0
|
die "For getting row count, please specify 'row_count_sth', ". |
59
|
|
|
|
|
|
|
"'row_count_query', or 'table' argument"; |
60
|
|
|
|
|
|
|
} |
61
|
1
|
|
|
|
|
8
|
$row_count_sth = $dbh->prepare($row_count_query); |
62
|
1
|
|
50
|
|
|
79
|
$sth->execute(@{ $row_count_sth_bind_params // [] }); # to check query syntax |
|
1
|
|
|
|
|
56
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
1
|
50
|
|
|
|
8
|
die "Unknown argument(s): ". join(", ", sort keys %args) |
66
|
|
|
|
|
|
|
if keys %args; |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
13
|
bless { |
69
|
|
|
|
|
|
|
#dbh => $dbh, |
70
|
|
|
|
|
|
|
sth => $sth, |
71
|
|
|
|
|
|
|
sth_bind_params => $sth_bind_params, |
72
|
|
|
|
|
|
|
row_count_sth => $row_count_sth, |
73
|
|
|
|
|
|
|
row_count_sth_bind_params => $row_count_sth_bind_params, |
74
|
|
|
|
|
|
|
}, $class; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub get_column_count { |
78
|
1
|
|
|
1
|
0
|
4
|
my $self = shift; |
79
|
1
|
|
|
|
|
12
|
$self->{sth}{NUM_OF_FIELDS}; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub get_column_names { |
83
|
2
|
|
|
2
|
0
|
5
|
my $self = shift; |
84
|
2
|
50
|
|
|
|
9
|
wantarray ? @{ $self->{sth}{NAME_lc} } : $self->{sth}{NAME_lc}; |
|
2
|
|
|
|
|
50
|
|
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub get_row_arrayref { |
88
|
5
|
|
|
5
|
0
|
27
|
my $self = shift; |
89
|
5
|
|
|
|
|
56
|
$self->{sth}->fetchrow_arrayref; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub get_row_count { |
93
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
94
|
1
|
|
50
|
|
|
3
|
$self->{row_count_sth}->execute(@{ $self->{row_count_sth_bind_params} // [] }); |
|
1
|
|
|
|
|
18
|
|
95
|
1
|
|
|
|
|
10
|
my ($row_count) = $self->{row_count_sth}->fetchrow_array; |
96
|
1
|
|
|
|
|
6
|
$row_count; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub get_row_hashref { |
100
|
2
|
|
|
2
|
0
|
5
|
my $self = shift; |
101
|
2
|
|
|
|
|
63
|
$self->{sth}->fetchrow_hashref; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub reset_iterator { |
105
|
3
|
|
|
3
|
0
|
8
|
my $self = shift; |
106
|
3
|
|
50
|
|
|
8
|
$self->{sth}->execute(@{ $self->{sth_bind_params} // [] }); |
|
3
|
|
|
|
|
172
|
|
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
# ABSTRACT: Role to access table data from DBI |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__END__ |