blib/lib/PDL/IO/DBI.pm

filter: Statementstmt 21.5% 59 / 274 Branchbran 1.5% 3 / 196 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 70.8% 17 / 24 Podpod 100% 2 / 2 totaltotal 14.2% 81 / 568 CC 137 SCAR 64.7
CC
137
Cov
12%
_init_2D
1122.0
rdbi1D
1061.7
_init_1D
552.0
CRAP 644.8
↑ Summary
1 package PDL::IO::DBI;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 our @EXPORT_OK = qw(rdbi1D rdbi2D);
8 5 our %EXPORT_TAGS = (all => \@EXPORT_OK);
9  
10 5 our $VERSION = '0.012';
11  
12 5 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use constant NO64BITINT => ($Config{ivsize} < 8) ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$PDL::IO::DBI::Config{'ivsize'} < 8 ? : 0 5 5
14 0 ▶use constant NODATETIME => eval { require PDL::DateTime; require Time::Moment; 1 } ? 0 : 1;
Subroutine BEGIN: called CC=2 SCAR=7.9
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require PDL::DateTime; require Time::Moment; 1 } } ? : 0 5 5
15 5 ▶use constant DEBUG => $ENV{PDL_IO_DBI_DEBUG} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PDL_IO_DBI_DEBUG'} ? : 0 5 5
16  
17 5 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 $Carp::Internal{ (__PACKAGE__) }++;
23  
24 5 my %pck = (
25 byte => "C",
26 short => "s",
27 ushort => "S",
28 long => "l",
29 longlong => "q",
30 float => "f",
31 double => "d",
32 );
33  
34 5 my %tmap = (
35 DBI::SQL_TINYINT => byte, # -6
36 DBI::SQL_BIGINT => longlong, # -5
37 DBI::SQL_NUMERIC => double, # 2
38 DBI::SQL_DECIMAL => double, # 3
39 DBI::SQL_INTEGER => long, # 4
40 DBI::SQL_SMALLINT => short, # 5
41 DBI::SQL_FLOAT => double, # 6
42 DBI::SQL_REAL => float, # 7
43 DBI::SQL_DOUBLE => double, # 8
44 DBI::SQL_DATETIME => '_dt_', # 9 == DBI::SQL_DATE
45 #DBI::SQL_INTERVAL => longlong, # 10 == DBI::SQL_TIME
46 DBI::SQL_TIMESTAMP => '_dt_', # 11
47 DBI::SQL_BOOLEAN => byte, # 16
48 DBI::SQL_TYPE_DATE => '_dt_', # 91
49 #DBI::SQL_TYPE_TIME # 92
50 DBI::SQL_TYPE_TIMESTAMP => '_dt_', # 93
51 #DBI::SQL_TYPE_TIME_WITH_TIMEZONE # 94
52 DBI::SQL_TYPE_TIMESTAMP_WITH_TIMEZONE => '_dt_', # 95
53 #DBI::SQL_INTERVAL_YEAR 101
54 #DBI::SQL_INTERVAL_MONTH 102
55 #DBI::SQL_INTERVAL_DAY 103
56 #DBI::SQL_INTERVAL_HOUR 104
57 #DBI::SQL_INTERVAL_MINUTE 105
58 #DBI::SQL_INTERVAL_SECOND 106
59 #DBI::SQL_INTERVAL_YEAR_TO_MONTH 107
60 #DBI::SQL_INTERVAL_DAY_TO_HOUR 108
61 #DBI::SQL_INTERVAL_DAY_TO_MINUTE 109
62 #DBI::SQL_INTERVAL_DAY_TO_SECOND 110
63 #DBI::SQL_INTERVAL_HOUR_TO_MINUTE 111
64 #DBI::SQL_INTERVAL_HOUR_TO_SECOND 112
65 #DBI::SQL_INTERVAL_MINUTE_TO_SECOND 113
66 ################## DBD::SQLite uses text values instead of numerical constants corresponding to DBI::SQL_*
67 'BIGINT' => longlong, # 8 bytes, -9223372036854775808 .. 9223372036854775807
68 'INT8' => longlong, # 8 bytes
69 'INTEGER' => long, # 4 bytes, -2147483648 .. 2147483647
70 'INT' => long, # 4 bytes
71 'INT4' => long, # 4 bytes
72 'MEDIUMINT' => long, # 3 bytes, -8388608 .. 8388607
73 'SMALLINT' => short, # 2 bytes, -32768 .. 32767
74 'INT2' => short, # 2 bytes
75 'TINYINT' => byte, # 1 byte, MySQL: -128 .. 127, MSSQL+Pg: 0 to 255
76 'REAL' => float, # 4 bytes
77 'FLOAT' => double, # 8 bytes
78 'NUMERIC' => double,
79 'DECIMAL' => double,
80 'DOUBLE' => double,
81 'DOUBLE PRECISION' => double,
82 'BOOLEAN' => byte,
83 'SMALLSERIAL' => short, # 2 bytes, 1 to 32767
84 'SERIAL' => long, # 4 bytes, 1 to 2147483647
85 'BIGSERIAL' => longlong, # 8 bytes, 1 to 9223372036854775807
86 'DATETIME' => '_dt_',
87 'DATE' => '_dt_',
88 'TIMESTAMP' => '_dt_',
89 );
90  
91 # https://www.sqlite.org/datatype3.html
92 # http://dev.mysql.com/doc/refman/5.7/en/integer-types.html
93 # http://www.postgresql.org/docs/9.6/static/datatype-numeric.html
94 # http://msdn.microsoft.com/en-us/library/ff848794.aspx
95  
96 0 ▶sub _dt_to_double {
Subroutine _dt_to_double: not called CC=4 SCAR=30.0
97 0 my $str = shift;
98 0 ▶ $str .= 'T00Z' if $str =~ m/^\d{4}-\d\d-\d\d$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\d{4}-\d\d-\d\d$/ 0 0 0
99 0 ▶ $str .= 'Z' if $str !~ m/(?:UTC|GMT|Z|[\+\-]\d\d(?:\:\d\d)?)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /(?:UTC|GMT|Z|[\+\-]\d\d(?:\:\d\d)?)$/ 0 0 0
100 0 ▶ my $t = eval { Time::Moment->from_string($str, lenient=>1) } or die "INVALID DATETIME: $str"; #XXX-FIXME PDL::dt2ll / dt2dbl ?
Branch: 1 branch
BranchTrueFalseTotal
my $t = eval { do { "Time::Moment"->from_string($str, "lenient", 1) } } or die "INVALID DATETIME: $str" 0 0 0
101 0 return $t->epoch * 1.0 + $t->millisecond / 1_000;
102 }
103  
104 0 ▶sub _dt_to_longlong {
Subroutine _dt_to_longlong: not called CC=4 SCAR=30.0
105 0 my $str = shift;
106 0 ▶ $str .= 'T00Z' if $str =~ m/^\d{4}-\d\d-\d\d$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\d{4}-\d\d-\d\d$/ 0 0 0
107 0 ▶ $str .= 'Z' if $str !~ m/(?:UTC|GMT|Z|[\+\-]\d\d(?:\:\d\d)?)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /(?:UTC|GMT|Z|[\+\-]\d\d(?:\:\d\d)?)$/ 0 0 0
108 0 ▶ my $t = eval { Time::Moment->from_string($str, lenient=>1) } or die "INVALID DATETIME: $str"; #XXX-FIXME PDL::dt2ll
Branch: 1 branch
BranchTrueFalseTotal
my $t = eval { do { "Time::Moment"->from_string($str, "lenient", 1) } } or die "INVALID DATETIME: $str" 0 0 0
109 0 return $t->epoch*1_000_000 + $t->microsecond;
110 }
111  
112 0 ▶sub rdbi1D {
Subroutine rdbi1D: not called CC=36 SCAR=69.7
Pod documented
113 0 my ($dbh, $sql, $bind_values, $O) = _proc_args(@_);
114  
115 # reuse_sth (if defined) is a scalar reference to a statement handle to be reused
116 0 my $reuse_sth = $O->{reuse_sth};
117 0 my $sth;
118 0 ▶ if ($reuse_sth && $$reuse_sth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reuse_sth and $$reuse_sth) { } 0 0 0
Condition0%0/3
$reuse_sth and $$reuse_sth
!ll&&!rl&&r
000
MC/DC0%0/2
$reuse_sth and $$reuse_sth
$reuse_sth $$reuse_sth
Truth table0%0/3
A and B
A= $reuse_sthB= $$reuse_sth
ABresult
0X0
100
111
119 0 $sth = $$reuse_sth;
120 }
121 else {
122 0 ▶ $sth = $dbh->prepare($sql) or croak "FATAL: prepare failed: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth = $dbh->prepare($sql) or croak('FATAL: prepare failed: ' . $dbh->errstr) 0 0 0
123 0 ▶ $sth->execute(@$bind_values) or croak "FATAL: execute failed: " . $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@$bind_values) or croak('FATAL: execute failed: ' . $sth->errstr) 0 0 0
124 0 ▶ $$reuse_sth = $sth if $reuse_sth;
Branch: 1 branch
BranchTrueFalseTotal
if $reuse_sth 0 0 0
125 }
126  
127 0 my ($c_type, $c_pack, $c_sizeof, $c_pdl, $c_bad, $c_dataref, $c_idx, $c_convert, $allocated, $cols) = _init_1D($sth->{TYPE}, $O);
128 0 ▶ warn "Initial size: '$allocated'\n" if $O->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $O->{"debug"} 0 0 0
129 0 my $null2bad = $O->{null2bad};
130 0 my $processed = 0;
131 0 my $headerline = $sth->{NAME_lc};
132  
133 0 ▶ warn "Fetching data (type=", join(',', @$c_type), ") ...\n" if $O->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $O->{"debug"} 0 0 0
134 0 ▶ while (my $data = $sth->fetchall_arrayref(undef, $O->{fetch_chunk})) { # limiting MaxRows
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $sth->fetchall_arrayref(undef, $O->{"fetch_chunk"})) 0 0 0
135 0 my $rows = scalar @$data;
136 0 ▶ if ($rows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows > 0) 0 0 0
137 0 $processed += $rows;
138 0 ▶ if ($allocated < $processed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allocated < $processed) 0 0 0
139 0 $allocated += $O->{reshape_inc};
140 0 ▶ warn "Reshape to: '$allocated'\n" if $O->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $O->{"debug"} 0 0 0
141 0 for (0..$cols-1) {
142 0 $c_pdl->[$_]->reshape($allocated);
143 0 $c_dataref->[$_] = $c_pdl->[$_]->get_dataref;
144 }
145 }
146 0 ▶ if ($null2bad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($null2bad) 0 0 0
147 0 for my $tmp (@$data) {
148 0 for (0..$cols-1) {
149 0 ▶ unless (defined $tmp->[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tmp->[$_]) 0 0 0
150 0 $tmp->[$_] = $c_bad->[$_];
151 0 $c_pdl->[$_]->badflag(1);
152 }
153 }
154 }
155 }
156 0 ▶ if (scalar @$c_convert > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$c_convert > 0) 0 0 0
157 0 for my $c (0..$cols-1) {
158 0 ▶ if (ref $c_convert->[$c] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c_convert->[$c] eq "CODE") 0 0 0
159 0 for my $r (0..$rows-1) {
160 0 $data->[$r]->[$c] = $c_convert->[$c]->($data->[$r]->[$c]);
161 }
162 }
163 }
164 }
165 0 for my $ci (0..$cols-1) {
166 0 my $bytes = '';
167 {
168 0 ▶ no warnings 'pack'; # intentionally disable all pack related warnings
Subroutine BEGIN: called CC=1 SCAR=0.2
169 5 ▶ no warnings 'numeric'; # disable: Argument ??? isn't numeric in pack
Subroutine BEGIN: called CC=1 SCAR=0.0
170 5 ▶ no warnings 'uninitialized'; # disable: Use of uninitialized value in pack
Subroutine BEGIN: called CC=1 SCAR=0.0
171 0 $bytes .= pack($c_pack->[$ci], $data->[$_][$ci]) for(0..$rows-1);
172 }
173 0 my $len = length $bytes;
174 0 my $expected_len = $c_sizeof->[$ci] * $rows;
175 0 ▶ croak "FATAL: len mismatch $len != $expected_len" if $len != $expected_len;
Branch: 1 branch
BranchTrueFalseTotal
if $len != $expected_len 0 0 0
176 0 substr(${$c_dataref->[$ci]}, $c_idx->[$ci], $len) = $bytes;
177 0 $c_idx->[$ci] += $expected_len;
178 }
179 }
180 0 ▶ last if $reuse_sth;
Branch: 1 branch
BranchTrueFalseTotal
if $reuse_sth 0 0 0
181 }
182 0 ▶ croak "FATAL: DB fetch failed: " . $sth->errstr if $sth->err;
Branch: 1 branch
BranchTrueFalseTotal
if $sth->err 0 0 0
183  
184 0 ▶ if ($processed != $allocated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($processed != $allocated) 0 0 0
185 0 ▶ warn "Reshape to: '$processed' (final)\n" if $O->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $O->{"debug"} 0 0 0
186 0 $c_pdl->[$_]->reshape($processed) for (0..$cols-1);
187 }
188 0 $c_pdl->[$_]->upd_data for (0..$cols-1);
189 0 ▶ if (ref $headerline eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $headerline eq "ARRAY") 0 0 0
190 0 for (0..$cols-1) {
191 0 ▶ $c_pdl->[$_]->hdr->{col_name} = $headerline->[$_] if $headerline->[$_] && $headerline->[$_] ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $headerline->[$_] and $headerline->[$_] ne "" 0 0 0
Condition0%0/3
$headerline->[$_] and $headerline->[$_] ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$headerline->[$_] and $headerline->[$_] ne ""
$headerline->[$_] $headerline->[$_] ne ""
Truth table0%0/3
A and B
A= $headerline->[$_]B= $headerline->[$_] ne ""
ABresult
0X0
100
111
192 };
193 }
194  
195 0 ▶ if ($processed == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($processed == 0) 0 0 0
196 0 ▶ if ($reuse_sth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reuse_sth) { } 0 0 0
197 # signal to callers that all chunks have been fetched
198 0 $$reuse_sth = undef;
199 }
200 else {
201 0 warn "rdbi1D: no data\n";
202 }
203 }
204  
205 0 return @$c_pdl;
206 }
207  
208 0 ▶sub rdbi2D {
Subroutine rdbi2D: not called CC=24 SCAR=61.0
Pod documented
209 0 my ($dbh, $sql, $bind_values, $O) = _proc_args(@_);
210  
211 0 ▶ croak 'FATAL: reuse_sth not supported yet for rdbi2D' if $O->{reuse_sth};
Branch: 1 branch
BranchTrueFalseTotal
if $O->{"reuse_sth"} 0 0 0
212 0 ▶ my $sth = $dbh->prepare($sql) or croak "FATAL: prepare failed: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare($sql) or croak('FATAL: prepare failed: ' . $dbh->errstr) 0 0 0
213 0 ▶ $sth->execute(@$bind_values) or croak "FATAL: execute failed: " . $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@$bind_values) or croak('FATAL: execute failed: ' . $sth->errstr) 0 0 0
214  
215 0 my ($c_type, $c_pack, $c_sizeof, $c_pdl, $c_bad, $c_dataref, $c_convert, $allocated, $cols) = _init_2D($sth->{TYPE}, $O);
216 0 ▶ warn "Initial size: '$allocated'\n" if $O->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $O->{"debug"} 0 0 0
217 0 my $null2bad = $O->{null2bad};
218 0 my $processed = 0;
219 0 my $c_idx = 0;
220 0 my $pck = "$c_pack\[$cols\]";
221  
222 0 ▶ warn "Fetching data (type=$c_type) ...\n" if $O->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $O->{"debug"} 0 0 0
223 0 ▶ while (my $data = $sth->fetchall_arrayref(undef, $O->{fetch_chunk})) { # limiting MaxRows
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $sth->fetchall_arrayref(undef, $O->{"fetch_chunk"})) 0 0 0
224 0 my $rows = scalar @$data;
225 0 ▶ if ($rows > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows > 0) 0 0 0
226 0 $processed += $rows;
227 0 ▶ if ($allocated < $processed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allocated < $processed) 0 0 0
228 0 $allocated += $O->{reshape_inc};
229 0 ▶ warn "Reshape to: '$allocated'\n" if $O->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $O->{"debug"} 0 0 0
230 0 $c_pdl->reshape($cols, $allocated);
231 0 $c_dataref = $c_pdl->get_dataref;
232 }
233 0 my $bytes = '';
234 0 ▶ if ($null2bad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($null2bad) 0 0 0
235 0 for my $tmp (@$data) {
236 0 for (@$tmp) {
237 0 ▶ unless (defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_) 0 0 0
238 0 $_ = $c_bad;
239 0 $c_pdl->badflag(1);
240 }
241 }
242 }
243 }
244 0 ▶ if (scalar @$c_convert > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$c_convert > 0) 0 0 0
245 0 for my $c (0..$cols-1) {
246 0 ▶ if (ref $c_convert->[$c] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c_convert->[$c] eq "CODE") 0 0 0
247 0 for my $r (0..$rows-1) {
248 0 $data->[$r]->[$c] = $c_convert->[$c]->($data->[$r]->[$c]);
249 }
250 }
251 }
252 }
253 {
254 0 ▶ no warnings 'pack'; # intentionally disable all pack related warnings
Subroutine BEGIN: called CC=1 SCAR=0.2
255 5 ▶ no warnings 'numeric'; # disable: Argument ??? isn't numeric in pack
Subroutine BEGIN: called CC=1 SCAR=0.0
256 5 ▶ no warnings 'uninitialized'; # disable: Use of uninitialized value in pack
Subroutine BEGIN: called CC=1 SCAR=0.0
257 0 $bytes .= pack($pck, @$_) for (@$data);
258 }
259 0 my $len = length $bytes;
260 0 my $expected_len = $c_sizeof * $cols * $rows;
261 0 ▶ croak "FATAL: len mismatch $len != $expected_len" if $len != $expected_len;
Branch: 1 branch
BranchTrueFalseTotal
if $len != $expected_len 0 0 0
262 0 substr($$c_dataref, $c_idx, $len) = $bytes;
263 0 $c_idx += $len;
264 }
265 }
266 0 ▶ croak "FATAL: DB fetch failed: " . $sth->errstr if $sth->err;
Branch: 1 branch
BranchTrueFalseTotal
if $sth->err 0 0 0
267 0 ▶ if ($processed != $allocated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($processed != $allocated) 0 0 0
268 0 ▶ warn "Reshape to: '$processed' (final)\n" if $O->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $O->{"debug"} 0 0 0
269 0 $c_pdl->reshape($cols, $processed); # allocate the exact size
270 }
271 0 $c_pdl->upd_data;
272  
273 0 ▶ warn "rdbi2D: no data\n" unless $processed > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $processed > 0 0 0 0
274  
275 0 return $c_pdl->transpose;
276 }
277  
278 0 ▶sub _proc_args {
Subroutine _proc_args: not called CC=16 SCAR=56.1
279 0 ▶ my $options = ref $_[-1] eq 'HASH' ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 0 0 0
280 0 my ($dsn_or_dbh, $sql, $bind_values) = @_;
281  
282 0 ▶ croak "FATAL: no SQL query" unless $sql;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql 0 0 0
283 0 ▶ croak "FATAL: no DBH or DSN" unless defined $dsn_or_dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dsn_or_dbh 0 0 0
284 0 my $reuse_sth = $options->{reuse_sth};
285 0 ▶ if ($reuse_sth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reuse_sth) 0 0 0
286 croak "FATAL: reuse_sth must either be false, a reference to a false value, or a reference to a statement handle"
287 0 ▶ if $$reuse_sth && !eval { $$reuse_sth->isa('DBI::st') };
Branch: 1 branch
BranchTrueFalseTotal
if $$reuse_sth and not eval { do { $$reuse_sth->isa("DBI::st") } } 0 0 0
Condition0%0/3
$$reuse_sth and not eval { do { $$reuse_sth->isa("DBI::st") } }
!ll&&!rl&&r
000
MC/DC0%0/2
$$reuse_sth and not eval { do { $$reuse_sth->isa("DBI::st") } }
$$reuse_sth not eval { do { $$reuse_sth->isa("DBI::st") } }
Truth table0%0/3
A and not(B)
A= $$reuse_sthB= not eval { do { $$reuse_sth->isa("DBI::st") } }
ABresult
0X0
100
111
288 }
289 0 my $O = { %$options }; # make a copy
290  
291 # handle defaults for optional parameters
292 0 ▶ $O->{fetch_chunk} = 8_000 unless defined $O->{fetch_chunk};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $O->{"fetch_chunk"} 0 0 0
293 0 ▶ my $alloc = $reuse_sth ? $O->{fetch_chunk} : 80_000;
Branch: 1 branch
BranchTrueFalseTotal
$reuse_sth ? : 0 0 0
294 0 ▶ $O->{reshape_inc} = $alloc unless defined $O->{reshape_inc};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $O->{"reshape_inc"} 0 0 0
295 0 ▶ $O->{type} = 'auto' unless defined $O->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $O->{"type"} 0 0 0
296 0 ▶ $O->{debug} = DEBUG unless defined $O->{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $O->{"debug"} 0 0 0
297  
298 # reshape_inc cannot be lower than fetch_chunk
299 0 ▶ $O->{reshape_inc} = $O->{fetch_chunk} if $O->{reshape_inc} < $O->{fetch_chunk};
Branch: 1 branch
BranchTrueFalseTotal
if $O->{"reshape_inc"} < $O->{"fetch_chunk"} 0 0 0
300  
301 0 ▶ $bind_values = [] unless ref $bind_values eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bind_values eq "ARRAY" 0 0 0
302  
303 # launch db query
304 0 ▶ my $dbh = ref $dsn_or_dbh ? $dsn_or_dbh : DBI->connect($dsn_or_dbh) or croak "FATAL: connect failed: " . $DBI::errstr;
Branch: 2 branches
BranchTrueFalseTotal
my $dbh = ref $dsn_or_dbh ? $dsn_or_dbh : "DBI"->connect($dsn_or_dbh) or croak('FATAL: connect failed: ' . $DBI::errstr) 0 0 0
ref $dsn_or_dbh ? : 0 0 0
305  
306 0 return ($dbh, $sql, $bind_values, $O);
307 }
308  
309 0 ▶sub _init_1D {
Subroutine _init_1D: not called CC=23 SCAR=63.1
310 0 my ($sql_types, $O) = @_;
311  
312 0 ▶ croak "FATAL: no columns" unless ref $sql_types eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sql_types eq "ARRAY" 0 0 0
313 0 my $cols = scalar @$sql_types;
314 0 ▶ croak "FATAL: no columns" unless $cols > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols > 0 0 0 0
315  
316 0 my @c_type;
317 my @c_pack;
318 0 my @c_sizeof;
319 0 my @c_pdl;
320 0 my @c_bad;
321 0 my @c_dataref;
322 0 my @c_idx;
323 0 my @c_convert;
324  
325 0 ▶ if (ref $O->{type} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $O->{'type'} eq 'ARRAY') { } 0 0 0
326 0 @c_type = @{$O->{type}};
327 }
328 else {
329 0 $c_type[$_] = $O->{type} for (0..$cols-1);
330 }
331 0 ▶ for (0..$cols-1) { $c_type[$_] = 'auto' if !$c_type[$_] }
Branch: 1 branch
BranchTrueFalseTotal
unless $c_type[$_] 0 0 0
332  
333 0 ▶ my @detected_type = map { $sql_types->[$_] ? $tmap{$sql_types->[$_]} : undef } (0..$cols-1);
Branch: 1 branch
BranchTrueFalseTotal
$sql_types->[$_] ? : 0 0 0
334 0 ▶ if ($O->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($O->{"debug"}) 0 0 0
335 0 ▶ $detected_type[$_] or warn "column $_ has unknown type '$sql_types->[$_]' gonna use Double\n" for (0..$cols-1);
Branch: 1 branch
BranchTrueFalseTotal
$detected_type[$_] or warn "column $_ has unknown type '$sql_types->[$_]' gonna use Double\n" 0 0 0
336 }
337 0 my $allocated = $O->{reshape_inc};
338  
339 0 my @c_dt;
340 0 for (0..$cols-1) {
341 0 ▶ if ($detected_type[$_] && $detected_type[$_] eq '_dt_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($detected_type[$_] and $detected_type[$_] eq "_dt_") 0 0 0
Condition0%0/3
$detected_type[$_] and $detected_type[$_] eq "_dt_"
!ll&&!rl&&r
000
MC/DC0%0/2
$detected_type[$_] and $detected_type[$_] eq "_dt_"
$detected_type[$_] $detected_type[$_] eq "_dt_"
Truth table0%0/3
A and B
A= $detected_type[$_]B= $detected_type[$_] eq "_dt_"
ABresult
0X0
100
111
342 0 ▶ if (!NODATETIME && ($c_type[$_] eq 'auto' || $c_type[$_] eq 'datetime')) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { $c_type[$_] eq longlong() }) { } 0 0 0
343 croak "PDL::DateTime not installed" if NODATETIME;
344 $c_convert[$_] = \&_dt_to_longlong;
345 $c_type[$_] = longlong;
346 $c_dt[$_] = 'datetime';
347 }
348 elsif ($c_type[$_] eq longlong) {
349 0 $c_convert[$_] = \&_dt_to_longlong;
350 }
351 else {
352 0 $c_convert[$_] = \&_dt_to_double;
353 0 $c_type[$_] = double;
354 }
355 }
356 0 ▶ $c_type[$_] = $detected_type[$_] if !defined $c_type[$_] || $c_type[$_] eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $c_type[$_] or $c_type[$_] eq "auto" 0 0 0
Condition0%0/3
not defined $c_type[$_] or $c_type[$_] eq "auto"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $c_type[$_] or $c_type[$_] eq "auto"
not defined $c_type[$_] $c_type[$_] eq "auto"
Truth table0%0/3
not(A) or B
A= not defined $c_type[$_]B= $c_type[$_] eq "auto"
ABresult
1X1
011
000
357 0 ▶ $c_type[$_] = double if !$c_type[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless $c_type[$_] 0 0 0
358 0 $c_pack[$_] = $pck{$c_type[$_]};
359 0 ▶ croak "FATAL: your perl does not support 64bitint (avoid using type longlong)" if $c_pack[$_] eq 'q' && NO64BITINT;
Branch: 1 branch
BranchTrueFalseTotal
if $c_pack[$_] eq "q" and 0 0 0 0
Condition0%0/2
$c_pack[$_] eq "q" and 0
!ll
00
MC/DC0%0/1
$c_pack[$_] eq "q" and 0
$c_pack[$_] eq "q"
Truth table0%0/2
A
A= $c_pack[$_] eq "q"
Aresult
00
11
360 0 ▶ croak "FATAL: invalid type '$c_type[$_]' for column $_" if !$c_pack[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless $c_pack[$_] 0 0 0
361 0 $c_sizeof[$_] = length pack($c_pack[$_], 1);
362 0 ▶ $c_pdl[$_] = $c_dt[$_] ? PDL::DateTime->new(zeroes(longlong, $allocated)) : zeroes($c_type[$_], $allocated);
Branch: 1 branch
BranchTrueFalseTotal
$c_dt[$_] ? : 0 0 0
363 0 $c_dataref[$_] = $c_pdl[$_]->get_dataref;
364 0 $c_bad[$_] = $c_pdl[$_]->badvalue;
365 0 $c_idx[$_] = 0;
366 0 my $big = PDL::Core::howbig($c_pdl[$_]->get_datatype);
367 0 ▶ croak "FATAL: column $_ mismatch (type=$c_type[$_], sizeof=$c_sizeof[$_], big=$big)" if $big != $c_sizeof[$_];
Branch: 1 branch
BranchTrueFalseTotal
if $big != $c_sizeof[$_] 0 0 0
368 }
369  
370 0 return (\@c_type, \@c_pack, \@c_sizeof, \@c_pdl, \@c_bad, \@c_dataref, \@c_idx, \@c_convert, $allocated, $cols);
371 }
372  
373 0 ▶sub _init_2D {
Subroutine _init_2D: not called CC=33 SCAR=70.2
374 0 my ($sql_types, $O) = @_;
375  
376 0 ▶ croak "FATAL: no columns" unless ref $sql_types eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sql_types eq "ARRAY" 0 0 0
377 0 my $cols = scalar @$sql_types;
378 0 ▶ croak "FATAL: no columns" unless $cols > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols > 0 0 0 0
379  
380 0 my $c_type = $O->{type};
381 0 ▶ if (!$c_type || $c_type eq 'auto') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c_type or $c_type eq "auto") 0 0 0
Condition0%0/3
not $c_type or $c_type eq "auto"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $c_type or $c_type eq "auto"
not $c_type $c_type eq "auto"
Truth table0%0/3
not(A) or B
A= not $c_typeB= $c_type eq "auto"
ABresult
1X1
011
000
382 # try to guess the best type
383 0 ▶ my @detected_type = map { $sql_types->[$_] ? $tmap{$sql_types->[$_]} : undef } (0..$cols-1);
Branch: 1 branch
BranchTrueFalseTotal
$sql_types->[$_] ? : 0 0 0
384 0 ▶ if ($O->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($O->{"debug"}) 0 0 0
385 0 ▶ $detected_type[$_] or warn "column $_ has unknown type '$sql_types->[$_]' gonna use Double\n" for (0..$cols-1);
Branch: 1 branch
BranchTrueFalseTotal
$detected_type[$_] or warn "column $_ has unknown type '$sql_types->[$_]' gonna use Double\n" 0 0 0
386 }
387 0 for (0..$#detected_type) {
388 # DATETIME is auto-detected as double
389 0 ▶ $detected_type[$_] = 'double' if $detected_type[$_] && $detected_type[$_] eq '_dt_';
Branch: 1 branch
BranchTrueFalseTotal
if $detected_type[$_] and $detected_type[$_] eq "_dt_" 0 0 0
Condition0%0/3
$detected_type[$_] and $detected_type[$_] eq "_dt_"
!ll&&!rl&&r
000
MC/DC0%0/2
$detected_type[$_] and $detected_type[$_] eq "_dt_"
$detected_type[$_] $detected_type[$_] eq "_dt_"
Truth table0%0/3
A and B
A= $detected_type[$_]B= $detected_type[$_] eq "_dt_"
ABresult
0X0
100
111
390 0 ▶ my $dt = $detected_type[$_] || 'double';
Condition0%0/2
$detected_type[$_] || 'double'
l!l
00
MC/DC0%0/1
$detected_type[$_] || 'double'
$detected_type[$_]
Truth table0%0/2
A
A= $detected_type[$_]
Aresult
11
00
391 0 ▶ $c_type = double if $dt eq double;
Branch: 1 branch
BranchTrueFalseTotal
if $dt eq double() 0 0 0
392 0 ▶ $c_type = float if $dt eq float && $c_type ne double;
Branch: 1 branch
BranchTrueFalseTotal
if $dt eq float() and $c_type ne double() 0 0 0
Condition0%0/3
$dt eq float() and $c_type ne double()
!ll&&!rl&&r
000
MC/DC0%0/2
$dt eq float() and $c_type ne double()
$dt eq float() $c_type ne double()
Truth table0%0/3
A and B
A= $dt eq float()B= $c_type ne double()
ABresult
0X0
100
111
393 0 ▶ $c_type = longlong if $dt eq longlong && $c_type !~ /^(double|float)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $dt eq longlong() and not $c_type =~ /^(double|float)$/ 0 0 0
Condition0%0/3
$dt eq longlong() and not $c_type =~ /^(double|float)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$dt eq longlong() and not $c_type =~ /^(double|float)$/
$dt eq longlong() not $c_type =~ /^(double|float)$/
Truth table0%0/3
A and not(B)
A= $dt eq longlong()B= not $c_type =~ /^(double|float)$/
ABresult
0X0
100
111
394 0 ▶ $c_type = long if $dt eq long && $c_type !~ /^(double|float|longlong)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $dt eq long() and not $c_type =~ /^(double|float|longlong)$/ 0 0 0
Condition0%0/3
$dt eq long() and not $c_type =~ /^(double|float|longlong)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$dt eq long() and not $c_type =~ /^(double|float|longlong)$/
$dt eq long() not $c_type =~ /^(double|float|longlong)$/
Truth table0%0/3
A and not(B)
A= $dt eq long()B= not $c_type =~ /^(double|float|longlong)$/
ABresult
0X0
100
111
395 0 ▶ $c_type = short if $dt eq short && $c_type !~ /^(double|float|longlong|long)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $dt eq short() and not $c_type =~ /^(double|float|longlong|long)$/ 0 0 0
Condition0%0/3
$dt eq short() and not $c_type =~ /^(double|float|longlong|long)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$dt eq short() and not $c_type =~ /^(double|float|longlong|long)$/
$dt eq short() not $c_type =~ /^(double|float|longlong|long)$/
Truth table0%0/3
A and not(B)
A= $dt eq short()B= not $c_type =~ /^(double|float|longlong|long)$/
ABresult
0X0
100
111
396 0 ▶ $c_type = byte if $dt eq byte && $c_type !~ /^(double|float|longlong|long|short)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $dt eq byte() and not $c_type =~ /^(double|float|longlong|long|short)$/ 0 0 0
Condition0%0/3
$dt eq byte() and not $c_type =~ /^(double|float|longlong|long|short)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$dt eq byte() and not $c_type =~ /^(double|float|longlong|long|short)$/
$dt eq byte() not $c_type =~ /^(double|float|longlong|long|short)$/
Truth table0%0/3
A and not(B)
A= $dt eq byte()B= not $c_type =~ /^(double|float|longlong|long|short)$/
ABresult
0X0
100
111
397 }
398 0 ▶ croak "FATAL: type detection failed" if !$c_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $c_type 0 0 0
399 }
400 0 my $c_pack = $pck{$c_type};
401 0 ▶ croak "FATAL: your perl does not support 64bitint (avoid using type longlong)" if $c_pack eq 'q' && NO64BITINT;
Branch: 1 branch
BranchTrueFalseTotal
if $c_pack eq "q" and 0 0 0 0
Condition0%0/2
$c_pack eq "q" and 0
!ll
00
MC/DC0%0/1
$c_pack eq "q" and 0
$c_pack eq "q"
Truth table0%0/2
A
A= $c_pack eq "q"
Aresult
00
11
402 0 ▶ croak "FATAL: invalid type '$c_type' for column $_" if !$c_pack;
Branch: 1 branch
BranchTrueFalseTotal
unless $c_pack 0 0 0
403  
404 0 my @c_convert = ();
405 0 for (0..$cols-1) {
406 0 ▶ my $t = $tmap{$sql_types->[$_]} || '';
Condition0%0/2
$tmap{$sql_types->[$_]} || ''
l!l
00
MC/DC0%0/1
$tmap{$sql_types->[$_]} || ''
$tmap{$sql_types->[$_]}
Truth table0%0/2
A
A= $tmap{$sql_types->[$_]}
Aresult
11
00
407 0 ▶ if ($t eq '_dt_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "_dt_") 0 0 0
408 0 ▶ $c_convert[$_] = ($c_type eq 'longlong') ? \&_dt_to_longlong : \&_dt_to_double;
Branch: 1 branch
BranchTrueFalseTotal
$c_type eq 'longlong' ? : 0 0 0
409 }
410 }
411  
412 0 my $allocated = $O->{reshape_inc};
413 0 my $c_sizeof = length pack($c_pack, 1);
414 0 my $c_pdl = zeroes($c_type, $cols, $allocated);
415 0 my $c_dataref = $c_pdl->get_dataref;
416 0 my $c_bad = $c_pdl->badvalue;
417  
418 0 my $howbig = PDL::Core::howbig($c_pdl->get_datatype);
419 0 ▶ croak "FATAL: column $_ size mismatch (type=$c_type, sizeof=$c_sizeof, howbig=$howbig)" unless $howbig == $c_sizeof;
Branch: 1 branch
BranchTrueFalseTotal
unless $howbig == $c_sizeof 0 0 0
420  
421 0 return ($c_type, $c_pack, $c_sizeof, $c_pdl, $c_bad, $c_dataref, \@c_convert, $allocated, $cols);
422 }
423  
424 5 1;
425  
426 __END__
↑ Summary