| 1 | package TableDataRole::Source::DBI; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 10 | 4 | our $DATE = '2024-05-14'; # DATE | |||||||||||||||||||||||||||
| 11 | 4 | our $DIST = 'TableDataRoles-Standard'; # DIST | |||||||||||||||||||||||||||
| 12 | 4 | our $VERSION = '0.025'; # VERSION | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | with 'TableDataRole::Spec::Basic'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=14 SCAR=29.7
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | my $dsn = delete $args{dsn}; | |||||||||||||||||||||||||||
| 20 | 1 | my $user = delete $args{user}; | |||||||||||||||||||||||||||
| 21 | 1 | my $password = delete $args{password}; | |||||||||||||||||||||||||||
| 22 | 1 | my $dbh = delete $args{dbh}; | |||||||||||||||||||||||||||
| 23 | 1 | ▶ | if (defined $dbh) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 24 | } elsif (defined $dsn) { | ||||||||||||||||||||||||||||
| 25 | 0 | require DBI; | |||||||||||||||||||||||||||
| 26 | 0 | $dbh = DBI->connect($dsn, $user, $password, {RaiseError=>1}); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | my $sth = delete $args{sth}; | |||||||||||||||||||||||||||
| 30 | 1 | my $sth_bind_params = delete $args{sth_bind_params}; | |||||||||||||||||||||||||||
| 31 | 1 | my $query = delete $args{query}; | |||||||||||||||||||||||||||
| 32 | 1 | my $table = delete $args{table}; | |||||||||||||||||||||||||||
| 33 | 1 | ▶ | if (defined $sth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||||||||||
| 35 | 1 | ▶ | die "You specify 'query' or 'table', but you don't specify ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | "dbh/dsn+user+password, so I cannot create a statement handle" | ||||||||||||||||||||||||||||
| 37 | unless $dbh; | ||||||||||||||||||||||||||||
| 38 | 1 | ▶ | if (defined $query) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 39 | } elsif (defined $table) { | ||||||||||||||||||||||||||||
| 40 | 1 | $query = "SELECT * FROM $table"; | |||||||||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||||||||
| 42 | 0 | die "Please specify 'sth', 'query', or 'table' argument"; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 1 | $sth = $dbh->prepare($query); | |||||||||||||||||||||||||||
| 45 | 1 | ▶ | $sth->execute(@{ $sth_bind_params // [] }); # to check query syntax | ||||||||||||||||||||||||||
|
Condition50%1/2
$sth_bind_params // []
MC/DC0%0/1
$sth_bind_params // []
$sth_bind_params
Truth table50%1/2
A
A= $sth_bind_params
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | my $row_count_sth = delete $args{row_count_sth}; | |||||||||||||||||||||||||||
| 49 | 1 | my $row_count_sth_bind_params = delete $args{row_count_sth_bind_params}; | |||||||||||||||||||||||||||
| 50 | 1 | my $row_count_query = delete $args{row_count_query}; | |||||||||||||||||||||||||||
| 51 | 1 | ▶ | if (defined $row_count_sth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 1 | ▶ | die "You specify 'row_count_query' or 'table', but you don't specify ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | "dbh/dsn+user+password, so I cannot create a statement handle" | ||||||||||||||||||||||||||||
| 55 | unless $dbh; | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | if (defined $row_count_query) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 57 | } elsif (defined $table) { | ||||||||||||||||||||||||||||
| 58 | 1 | $row_count_query = "SELECT COUNT(*) FROM $table"; | |||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||
| 60 | 0 | die "For getting row count, please specify 'row_count_sth', ". | |||||||||||||||||||||||||||
| 61 | "'row_count_query', or 'table' argument"; | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 1 | $row_count_sth = $dbh->prepare($row_count_query); | |||||||||||||||||||||||||||
| 64 | 1 | ▶ | $sth->execute(@{ $row_count_sth_bind_params // [] }); # to check query syntax | ||||||||||||||||||||||||||
|
Condition50%1/2
$row_count_sth_bind_params // []
MC/DC0%0/1
$row_count_sth_bind_params // []
$row_count_sth_bind_params
Truth table50%1/2
A
A= $row_count_sth_bind_params
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | die "Unknown argument(s): ". join(", ", sort keys %args) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | if keys %args; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | bless { | |||||||||||||||||||||||||||
| 71 | #dbh => $dbh, | ||||||||||||||||||||||||||||
| 72 | sth => $sth, | ||||||||||||||||||||||||||||
| 73 | sth_bind_params => $sth_bind_params, | ||||||||||||||||||||||||||||
| 74 | row_count_sth => $row_count_sth, | ||||||||||||||||||||||||||||
| 75 | row_count_sth_bind_params => $row_count_sth_bind_params, | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | pos => 0, | ||||||||||||||||||||||||||||
| 78 | }, $class; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 6 | ▶ | sub _get_row { | ||||||||||||||||||||||||||
|
Subroutine
_get_row: called CC=5 SCAR=17.1
| |||||||||||||||||||||||||||||
| 82 | # get a hashref row from sth, and empty the buffer | ||||||||||||||||||||||||||||
| 83 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | 6 | ▶ | if ($self->{buffer}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 3 | my $row = delete $self->{buffer}; | |||||||||||||||||||||||||||
| 86 | 3 | ▶ | if (!ref($row) && $row == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $row and $row == -1
MC/DC0%0/2
not ref $row and $row == -1
not ref $row
$row == -1
Truth table33%1/3
not(A) and B
A= not ref $rowB= $row == -1
| |||||||||||||||||||||||||||||
| 87 | 0 | return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef | |||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||
| 89 | 3 | return $row; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | 3 | my $row = $self->{sth}->fetchrow_hashref; | |||||||||||||||||||||||||||
| 93 | 3 | ▶ | return undef unless $row; ## no critic: Subroutines::ProhibitExplicitReturnUndef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 3 | return $row; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 4 | ▶ | sub _peek_row { | ||||||||||||||||||||||||||
|
Subroutine
_peek_row: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 99 | # get a row from iterator, put it in buffer. will return the existing buffer | ||||||||||||||||||||||||||||
| 100 | # content if it exists. | ||||||||||||||||||||||||||||
| 101 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 102 | 4 | ▶ | unless ($self->{buffer}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 4 | ▶ | $self->{buffer} = $self->{sth}->fetchrow_hashref // -1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"sth"}->fetchrow_hashref // -1
MC/DC100%1/1
$self->{"sth"}->fetchrow_hashref // -1
$self->{"sth"}->fetchrow_hashref
Truth table100%2/2
A
A= $self->{"sth"}->fetchrow_hashref
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 4 | ▶ | if (!ref($self->{buffer}) && $self->{buffer} == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $self->{"buffer"} and $self->{"buffer"} == -1
MC/DC50%1/2
not ref $self->{"buffer"} and $self->{"buffer"} == -1
not ref $self->{"buffer"}
$self->{"buffer"} == -1
Truth table66%2/3
not(A) and B
A= not ref $self->{"buffer"}B= $self->{"buffer"} == -1
| |||||||||||||||||||||||||||||
| 106 | 1 | return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef | |||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||
| 108 | 3 | return $self->{buffer}; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | sub get_column_count { | ||||||||||||||||||||||||||
|
Subroutine
get_column_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | 1 | $self->{sth}{NUM_OF_FIELDS}; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 7 | ▶ | sub get_column_names { | ||||||||||||||||||||||||||
|
Subroutine
get_column_names: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | 7 | ▶ | wantarray ? @{ $self->{sth}{NAME_lc} } : $self->{sth}{NAME_lc}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 4 | ▶ | sub has_next_item { | ||||||||||||||||||||||||||
|
Subroutine
has_next_item: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 4 | ▶ | $self->_peek_row ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 5 | ▶ | sub get_next_item { | ||||||||||||||||||||||||||
|
Subroutine
get_next_item: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 129 | 5 | my $row_hashref = $self->_get_row; | |||||||||||||||||||||||||||
| 130 | 5 | ▶ | die "StopIteration" unless $row_hashref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 5 | $self->{pos}++; | |||||||||||||||||||||||||||
| 132 | 5 | my $row_aryref = []; | |||||||||||||||||||||||||||
| 133 | 5 | my $column_names = $self->get_column_names; | |||||||||||||||||||||||||||
| 134 | 5 | for (0..$#{$column_names}) { | |||||||||||||||||||||||||||
| 135 | 10 | $row_aryref->[$_] = $row_hashref->{ $column_names->[$_] }; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 5 | $row_aryref; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1 | ▶ | sub get_next_row_hashref { | ||||||||||||||||||||||||||
|
Subroutine
get_next_row_hashref: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 141 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 1 | my $row = $self->_get_row; | |||||||||||||||||||||||||||
| 143 | 1 | ▶ | die "StopIteration" unless $row; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 1 | $self->{pos}++; | |||||||||||||||||||||||||||
| 145 | 1 | $row; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1 | ▶ | sub get_row_count { | ||||||||||||||||||||||||||
|
Subroutine
get_row_count: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 149 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 150 | 1 | ▶ | $self->{row_count_sth}->execute(@{ $self->{row_count_sth_bind_params} // [] }); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"row_count_sth_bind_params"} // []
MC/DC0%0/1
$self->{"row_count_sth_bind_params"} // []
$self->{"row_count_sth_bind_params"}
Truth table50%1/2
A
A= $self->{"row_count_sth_bind_params"}
| |||||||||||||||||||||||||||||
| 151 | 1 | my ($row_count) = $self->{row_count_sth}->fetchrow_array; | |||||||||||||||||||||||||||
| 152 | 1 | $row_count; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 3 | ▶ | sub reset_iterator { | ||||||||||||||||||||||||||
|
Subroutine
reset_iterator: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | 3 | ▶ | $self->{sth}->execute(@{ $self->{sth_bind_params} // [] }); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"sth_bind_params"} // []
MC/DC0%0/1
$self->{"sth_bind_params"} // []
$self->{"sth_bind_params"}
Truth table50%1/2
A
A= $self->{"sth_bind_params"}
| |||||||||||||||||||||||||||||
| 158 | 3 | delete $self->{buffer}; | |||||||||||||||||||||||||||
| 159 | 3 | $self->{pos} = 0; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub get_iterator_pos { | ||||||||||||||||||||||||||
|
Subroutine
get_iterator_pos: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 164 | 0 | $self->{pos}; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4 | 1; | |||||||||||||||||||||||||||
| 168 | # ABSTRACT: Role to access table data from DBI | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | __END__ | ||||||||||||||||||||||||||||