blib/lib/Teng/Iterator.pm

filter: Statementstmt 31.1% 19 / 61 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 60.0% 6 / 10 Podpod 100% 3 / 3 totaltotal 18.7% 28 / 149 CC 27 SCAR 52.1
CC
27
Cov
16%
_apply_sql_types
342.0
next
42.0
all
30.0
CRAP 182.4
↑ Summary blib/lib/Teng/Plugin/Lookup.pm »
1 package Teng::Iterator;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use Class::Accessor::Lite (
7 3 rw => [qw/suppress_object_creation apply_sql_types guess_sql_types/],
8 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use DBI qw(:sql_types);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub new {
12 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
13  
14 0 return bless \%args, $class;
15 }
16  
17 sub next {
18 0 ▶ my $self = shift;
Subroutine next: not called CC=6 SCAR=37.4
Pod documented
19  
20 0 my $row;
21 0 ▶ if ($self->{sth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sth'}) { } 0 0 0
22 0 $row = $self->{sth}->fetchrow_hashref;
23 0 ▶ $self->{select_columns} ||= $self->{sth}->{$self->{teng}->{fields_case}};
Condition0%0/3
$self->{"select_columns"} ||= $self->{"sth"}{$self->{"teng"}{"fields_case"}}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"select_columns"} ||= $self->{"sth"}{$self->{"teng"}{"fields_case"}}
$self->{"select_columns"} $self->{"sth"}{$self->{"teng"}{"fields_case"}}
Truth table0%0/3
A ||= B
A= $self->{"select_columns"}B= $self->{"sth"}{$self->{"teng"}{"fields_case"}}
ABresult
1X1
011
000
24 0 ▶ unless ( $row ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row) 0 0 0
25 0 $self->{sth}->finish;
26 0 $self->{sth} = undef;
27 0 return;
28 }
29 } else {
30 0 return;
31 }
32  
33 0 ▶ if ($self->{suppress_object_creation}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'suppress_object_creation'}) { } 0 0 0
34 0 return $row;
35 } else {
36 0 ▶ $self->_apply_sql_types($row) if $self->{apply_sql_types};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"apply_sql_types"} 0 0 0
37 return $self->{row_class}->new(
38 {
39 sql => $self->{sql},
40 row_data => $row,
41 teng => $self->{teng},
42 table => $self->{table},
43 table_name => $self->{table_name},
44 select_columns => $self->{select_columns},
45 }
46 0 );
47 }
48 }
49  
50 sub _apply_sql_types {
51 0 ▶ my ($self, $row) = @_;
Subroutine _apply_sql_types: not called CC=18 SCAR=58.3
52  
53 0 foreach my $column (keys %$row) {
54 0 my $type = $self->{table}->{sql_types}->{$column};
55 0 ▶ if (defined $type) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $type) { } 0 0 0
elsif ($self->{'guess_sql_types'}) { } 0 0 0
56 0 ▶ if ( $type == SQL_BIGINT
Branch: 2 branches
BranchTrueFalseTotal
if ($type == &SQL_BIGINT() or $type == &SQL_BIT() or $type == &SQL_TINYINT() or $type == &SQL_NUMERIC() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_DECIMAL() or $type == &SQL_FLOAT() or $type == &SQL_REAL() or $type == &SQL_DOUBLE()) { } 0 0 0
elsif ($type == &SQL_BOOLEAN()) { } 0 0 0
Condition0%0/27
$type == &SQL_BIGINT() or $type == &SQL_BIT() or $type == &SQL_TINYINT() or $type == &SQL_NUMERIC() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_DECIMAL() or $type == &SQL_FLOAT() or $type == &SQL_REAL() or $type == &SQL_DOUBLE()
l!l&&r!l&&!r
000
$type == &SQL_BIGINT() or $type == &SQL_BIT() or $type == &SQL_TINYINT() or $type == &SQL_NUMERIC() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_DECIMAL() or $type == &SQL_FLOAT() or $type == &SQL_REAL()
l!l&&r!l&&!r
000
$type == &SQL_BIGINT() or $type == &SQL_BIT() or $type == &SQL_TINYINT() or $type == &SQL_NUMERIC() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_DECIMAL() or $type == &SQL_FLOAT()
l!l&&r!l&&!r
000
$type == &SQL_BIGINT() or $type == &SQL_BIT() or $type == &SQL_TINYINT() or $type == &SQL_NUMERIC() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_DECIMAL()
l!l&&r!l&&!r
000
$type == &SQL_BIGINT() or $type == &SQL_BIT() or $type == &SQL_TINYINT() or $type == &SQL_NUMERIC() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT()
l!l&&r!l&&!r
000
$type == &SQL_BIGINT() or $type == &SQL_BIT() or $type == &SQL_TINYINT() or $type == &SQL_NUMERIC() or $type == &SQL_INTEGER()
l!l&&r!l&&!r
000
$type == &SQL_BIGINT() or $type == &SQL_BIT() or $type == &SQL_TINYINT() or $type == &SQL_NUMERIC()
l!l&&r!l&&!r
000
$type == &SQL_BIGINT() or $type == &SQL_BIT() or $type == &SQL_TINYINT()
l!l&&r!l&&!r
000
$type == &SQL_BIGINT() or $type == &SQL_BIT()
l!l&&r!l&&!r
000
MC/DC0%0/10
$type == &SQL_BIGINT() or $type == &SQL_BIT() or $type == &SQL_TINYINT() or $type == &SQL_NUMERIC() or $type == &SQL_INTEGER() or $type == &SQL_SMALLINT() or $type == &SQL_DECIMAL() or $type == &SQL_FLOAT() or $type == &SQL_REAL() or $type == &SQL_DOUBLE()
$type == &SQL_BIGINT() $type == &SQL_BIT() $type == &SQL_TINYINT() $type == &SQL_NUMERIC() $type == &SQL_INTEGER() $type == &SQL_SMALLINT() $type == &SQL_DECIMAL() $type == &SQL_FLOAT() $type == &SQL_REAL() $type == &SQL_DOUBLE()
Truth table0%0/11
A or B or C or D or E or F or G or H or I or J
A= $type == &SQL_BIGINT()B= $type == &SQL_BIT()C= $type == &SQL_TINYINT()D= $type == &SQL_NUMERIC()E= $type == &SQL_INTEGER()F= $type == &SQL_SMALLINT()G= $type == &SQL_DECIMAL()H= $type == &SQL_FLOAT()I= $type == &SQL_REAL()J= $type == &SQL_DOUBLE()
ABCDEFGHIJresult
1XXXXXXXXX1
01XXXXXXXX1
001XXXXXXX1
0001XXXXXX1
00001XXXXX1
000001XXXX1
0000001XXX1
00000001XX1
000000001X1
00000000011
00000000000
57 or $type == SQL_BIT
58 or $type == SQL_TINYINT
59 or $type == SQL_NUMERIC
60 or $type == SQL_INTEGER
61 or $type == SQL_SMALLINT
62 or $type == SQL_DECIMAL
63 or $type == SQL_FLOAT
64 or $type == SQL_REAL
65 or $type == SQL_DOUBLE
66 ) {
67 0 $row->{$column} += 0;
68 } elsif ($type == SQL_BOOLEAN) {
69 0 ▶ if ($self->{teng}->{boolean_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'teng'}{'boolean_value'}) { } 0 0 0
70 0 ▶ if ($row->{$column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{$column}) { } 0 0 0
71 0 $row->{$column} = $self->{teng}->{boolean_value}->{true};
72 } else {
73 0 $row->{$column} = $self->{teng}->{boolean_value}->{false};
74 }
75 } else {
76 0 $row->{$column} += 0;
77 }
78 } else {
79 0 $row->{$column} .= '';
80 }
81 } elsif ($self->{guess_sql_types}) {
82 0 ▶ if (looks_like_number($row->{$column})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($row->{$column})) { } 0 0 0
83 0 $row->{$column} += 0;
84 } else {
85 0 $row->{$column} .= '';
86 }
87 }
88 }
89 }
90  
91 sub all {
92 0 ▶ my $self = shift;
Subroutine all: not called CC=5 SCAR=34.0
Pod documented
93  
94 0 my $result = [];
95  
96 0 ▶ if ($self->{sth}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sth"}) 0 0 0
97 0 ▶ $self->{select_columns} ||= $self->{sth}->{$self->{teng}->{fields_case}};
Condition0%0/3
$self->{"select_columns"} ||= $self->{"sth"}{$self->{"teng"}{"fields_case"}}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"select_columns"} ||= $self->{"sth"}{$self->{"teng"}{"fields_case"}}
$self->{"select_columns"} $self->{"sth"}{$self->{"teng"}{"fields_case"}}
Truth table0%0/3
A ||= B
A= $self->{"select_columns"}B= $self->{"sth"}{$self->{"teng"}{"fields_case"}}
ABresult
1X1
011
000
98 0 $result = $self->{sth}->fetchall_arrayref(+{});
99 0 $self->{sth}->finish;
100 0 $self->{sth} = undef;
101  
102 0 ▶ if (!$self->{suppress_object_creation}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"suppress_object_creation"}) 0 0 0
103 $result = [map {
104 0 $self->{row_class}->new(
105 {
106 sql => $self->{sql},
107 row_data => $_,
108 teng => $self->{teng},
109 table => $self->{table},
110 table_name => $self->{table_name},
111 select_columns => $self->{select_columns},
112 }
113 )
114 0 } @$result];
115 }
116 }
117  
118 0 ▶ return wantarray ? @$result : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
119 }
120  
121 3 1;
122  
123 __END__
124 =head1 NAME
125  
126 Teng::Iterator - Iterator for Teng
127  
128 =head1 DESCRIPTION
129  
130 This is an iterator class for L<Teng>.
131  
132 =head1 SYNOPSIS
133  
134 my $itr = Your::Model->search('user',{});
135
136 my @rows = $itr->all; # get all rows
137  
138 # do iteration
139 while (my $row = $itr->next) {
140 ...
141 }
142  
143 =head1 METHODS
144  
145 =over
146  
147 =item $itr = Teng::Iterator->new()
148  
149 Create new Teng::Iterator's object. You may not call this method directly.
150  
151 =item my $row = $itr->next();
152  
153 Get next row data.
154  
155 =item my @ary = $itr->all;
156  
157 Get all row data in array.
158  
159 =item $itr->suppress_object_creation($bool)
160  
161 Set row object creation mode.
162  
163 =item $itr->apply_sql_types($bool)
164  
165 Set column type application mode.
166  
167 If column has SQL type and it is numeric, regard it as number and add 0 to the value.
168 If column has SQL type and it isn't numeric, regard it as string and add '' to the value.
169 If column doesn't have SQL type, the value won't be changed.
170  
171 =item $itr->guess_sql_types($bool)
172  
173 If this is true, this implies apply_sql_types also true.
174 If column has no SQL type, it guesses SQL type with its value.
175 When column value likes numeric, regard it as number and add 0 to the value.
176 If not, regard it as string and add '' to the value.
177  
178 =back
179  
180 =cut
181  
↑ Summary blib/lib/Teng/Plugin/Lookup.pm »