Sybase/Storage/ASE/LOBWriter.pm

filter: Statementstmt 74.0% 100 / 135 Branchbran 62.5% 45 / 72 Conditioncond 77.7% 14 / 18 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 65.0% 13 / 20 Podpod n/a totaltotal 70.3% 180 / 256 CC 53 SCAR 29.0
CC
53
Cov
71%
__ANON__
72.0
_insert_blobs
30.0
__ANON__
20.0
CRAP 18.2
« Sybase/Introspect/Views.pm ↑ Summary Sybase/Diff/ForeignKey.pm »
1 package DBIO::Sybase::Storage::ASE::LOBWriter;
2 # ABSTRACT: LOB (TEXT/IMAGE) handling mixin for Sybase ASE
3  
4 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 our $VERSION = '0.900007';
8  
9 35 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # No requires - these methods expect the consuming class to have:
14 # _is_lob_column, _get_dbh, _blob_log_on_update, _using_freetds
15  
16 1429 ▶sub _is_lob_type {
Subroutine _is_lob_type: called CC=2 SCAR=7.0
17 1429 my ($self, $type) = @_;
18 1429 ▶ return unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 1429 1429
19 1429 $type = lc $type;
20 1429 return $type =~ /^(?:text|ntext|image|blob|clob)$/;
21 }
22  
23 # FreeTDS cannot write the content of a LOB: ct_data_info fails ("text pointer
24 # is not set or is undefined", live on FreeTDS 1.3.17), and it does so after
25 # the INSERT went out with its placeholder, so the row stood in the table
26 # without the value. Refused before anything is sent (karr k97). Only the
27 # content: NULL, '' and literal SQL need no write.
28 29 ▶sub _refuse_lob_content {
Subroutine _refuse_lob_content: called CC=2 SCAR=6.9
29 29 my ($self, $source, $col) = @_;
30 29 ▶ return unless $self->_using_freetds;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_using_freetds 27 2 29
31 2 DBIO::Exception->throw(sprintf
32 'DBIO::Sybase: table %s, column %s: writing a value into a TEXT or IMAGE '
33 . 'column is refused: FreeTDS cannot write the content of a TEXT or IMAGE '
34 . 'column (ct_data_info fails). Use a DBD::Sybase built against the Sybase '
35 . 'Open Client libraries, or a VARCHAR column',
36 $source->name, $col);
37 }
38  
39 # Make sure blobs are not bound as placeholders, and return any non-empty ones
40 # as a hash.
41 #
42 # A value that already is literal SQL, a SCALAR ref, is neither replaced nor
43 # returned: it stays in the statement as it is (\'DEFAULT' of an empty insert,
44 # karr k80).
45 #
46 # What is left in place of a value is literal SQL, a SCALAR ref: NULL for
47 # undef, the empty string literal '' for every other value. It has to be the
48 # literal and not the two-character string "''": a string is a value, bound or
49 # (NoBindVars) quoted into the statement, and it stays the content of every
50 # LOB nothing is written to afterwards -- the ones set to '' (karr k72).
51 # ASE has no empty value: it stores '' as one space in a TEXT column and as
52 # the byte 0x20 in an IMAGE column.
53 230 ▶sub _remove_blob_cols {
Subroutine _remove_blob_cols: called CC=12 SCAR=24.8
54 230 my ($self, $source, $fields) = @_;
55  
56 # before anything is taken out of the caller's hash
57 230 for my $col (sort keys %$fields) {
58 505 ▶ next unless $self->_is_lob_column($source, $col);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_lob_column($source, $col) 146 359 505
59 359 ▶ next if ref $fields->{$col} eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fields->{$col} eq "SCALAR" 12 347 359
60 $self->_refuse_lob_content($source, $col)
61 347 ▶ if defined $fields->{$col} and $fields->{$col} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $fields->{$col} and $fields->{$col} ne "" 151 196 347
Condition100%3/3
defined $fields->{$col} and $fields->{$col} ne ""
!ll&&!rl&&r
9898151
MC/DC100%2/2
defined $fields->{$col} and $fields->{$col} ne ""
defined $fields->{$col} $fields->{$col} ne ""
Truth table100%3/3
A and B
A= defined $fields->{$col}B= $fields->{$col} ne ""
ABresult
0X0
100
111
62 }
63  
64 229 my %blob_cols;
65  
66 229 for my $col (keys %$fields) {
67 504 ▶ if ($self->_is_lob_column($source, $col)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_lob_column($source, $col)) 358 146 504
68 # literal SQL (karr k80): stays as it is, it is no content
69 358 ▶ next if ref $fields->{$col} eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fields->{$col} eq "SCALAR" 12 346 358
70  
71 346 my $blob_val = delete $fields->{$col};
72 346 ▶ if (not defined $blob_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $blob_val) { } 98 248 346
73 98 $fields->{$col} = \'NULL';
74 }
75 else {
76 248 $fields->{$col} = \"''";
77 248 ▶ $blob_cols{$col} = $blob_val unless $blob_val eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $blob_val eq "" 150 98 248
78 }
79 }
80 }
81  
82 229 ▶ return %blob_cols ? \%blob_cols : undef;
Branch: 1 branch
BranchTrueFalseTotal
%blob_cols ? : 109 120 229
83 }
84  
85 # same for _insert_bulk, with the same two literals. Core's _insert_bulk takes
86 # a SCALAR ref in the first row as literal SQL for that column: it is no bind
87 # of the statement, nothing is read from the rows for it, and every other row
88 # has to carry the same literal ("Inconsistent literal SQL value" otherwise).
89 #
90 # So a column has ONE placeholder for all its rows (karr k81): NULL where no
91 # row has a value, the empty literal '' where at least one has. In the second
92 # case the rows that were undef are NULL only after the INSERT: they are
93 # recorded and set by _null_blobs_array, one UPDATE a row. The alternative,
94 # grouping the rows by literal into several _insert_bulk calls, would split
95 # the slice and break the assumption _insert_bulk makes to find the identities
96 # of the rows (the last one minus the number of rows, consecutive); here the
97 # slice stays one INSERT in the order it came in.
98 #
99 # Returns the content to write, by row and column, or undef for none. In list
100 # context the second value is the undef cells to set to NULL afterwards, by
101 # row and column (a true value in the cell), or undef for none.
102 53 ▶sub _remove_blob_cols_array {
Subroutine _remove_blob_cols_array: called CC=17 SCAR=28.3
103 53 my ($self, $source, $cols, $data) = @_;
104  
105 53 my (@blob_cols, @null_cols);
106  
107 53 for my $i (0..$#$cols) {
108 98 ▶ next unless $self->_is_lob_column($source, $cols->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_lob_column($source, $cols->[$i]) 24 74 98
109 $self->_refuse_lob_content($source, $cols->[$i]) if grep {
110 74 ▶ ref $_->[$i] ne 'SCALAR' and defined $_->[$i] and $_->[$i] ne ''
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_->[$i] ne "SCALAR" and defined $_->[$i] and $_->[$i] ne "";} @$data 38 36 74
Condition100%6/6
ref $_->[$i] ne 'SCALAR' && defined $_->[$i] && $_->[$i] ne ''
!ll&&!rl&&r
634856
ref $_->[$i] ne "SCALAR" and defined $_->[$i]
!ll&&!rl&&r
1053104
MC/DC100%3/3
ref $_->[$i] ne 'SCALAR' && defined $_->[$i] && $_->[$i] ne ''
ref $_->[$i] ne "SCALAR" defined $_->[$i] $_->[$i] ne ''
Truth table100%4/4
A and B && C
A= ref $_->[$i] ne "SCALAR"B= defined $_->[$i]C= $_->[$i] ne ''
ABCresult
0XX0
10X0
1100
1111
111 } @$data;
112 }
113  
114 52 for my $i (0..$#$cols) {
115 96 my $col = $cols->[$i];
116  
117 96 ▶ if ($self->_is_lob_column($source, $col)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_lob_column($source, $col)) 73 23 96
118 # one placeholder for the column: '' as soon as a row has a value
119 my $has_value = grep {
120 73 ▶ ref $_->[$i] ne 'SCALAR' and defined $_->[$i]
Condition100%3/3
ref $_->[$i] ne 'SCALAR' && defined $_->[$i]
!ll&&!rl&&r
1052103
MC/DC100%2/2
ref $_->[$i] ne 'SCALAR' && defined $_->[$i]
ref $_->[$i] ne 'SCALAR' defined $_->[$i]
Truth table100%3/3
A && B
A= ref $_->[$i] ne 'SCALAR'B= defined $_->[$i]
ABresult
0X0
100
111
121 } @$data;
122  
123 73 for my $j (0..$#$data) {
124 # literal SQL (karr k80): the cell stays as it is
125 165 ▶ next if ref $data->[$j][$i] eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data->[$j][$i] eq "SCALAR" 10 155 165
126  
127 155 my $blob_val = delete $data->[$j][$i];
128 155 ▶ if (not defined $blob_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $blob_val) { } 52 103 155
129 52 ▶ if ($has_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_value) { } 34 18 52
130 34 $data->[$j][$i] = \"''";
131 34 $null_cols[$j][$i] = 1;
132 }
133 else {
134 18 $data->[$j][$i] = \'NULL';
135 }
136 }
137 else {
138 103 $data->[$j][$i] = \"''";
139 103 ▶ $blob_cols[$j][$i] = $blob_val
Branch: 1 branch
BranchTrueFalseTotal
unless $blob_val eq "" 55 48 103
140 unless $blob_val eq '';
141 }
142 }
143 }
144 }
145  
146 52 ▶ my @ret = (
Branch: 2 branches
BranchTrueFalseTotal
@blob_cols ? : 30 22 52
@null_cols ? : 21 31 52
147 (@blob_cols ? \@blob_cols : undef),
148 (@null_cols ? \@null_cols : undef),
149 );
150 52 ▶ return wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 48 4 52
151 }
152  
153 # The cells _remove_blob_cols_array left the empty literal in where the caller
154 # gave undef, set to NULL; $data has the primary key values of the rows.
155 19 ▶sub _null_blobs_array {
Subroutine _null_blobs_array: called CC=3 SCAR=11.0
156 19 my ($self, $source, $null_cols, $cols, $data) = @_;
157  
158 19 my @primary_cols = $source->primary_columns;
159  
160 19 for my $j (0..$#$data) {
161 51 ▶ next unless $null_cols->[$j];
Branch: 1 branch
BranchTrueFalseTotal
unless $null_cols->[$j] 22 29 51
162  
163 29 my %row;
164 29 @row{ @$cols } = @{ $data->[$j] };
165  
166 $self->update(
167 $source,
168 31 { map { ($cols->[$_] => undef) } grep { $null_cols->[$j][$_] } 0..$#$cols },
169 29 { map { ($_ => $row{$_}) } @primary_cols },
170 );
171 }
172 }
173  
174 16 ▶sub _update_blobs {
Subroutine _update_blobs: called CC=4 SCAR=13.9
175 16 my ($self, $source, $blob_cols, $where) = @_;
176  
177 my @primary_cols = try
178 16 ▶ { $source->_pri_cols_or_die }
Subroutine __ANON__: called CC=1 SCAR=0.0
179 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 0 $self->throw_exception("Cannot update TEXT/IMAGE column(s): $_")
181 16 };
182  
183 16 my @pks_to_update;
184 16 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $where eq 'HASH' and @primary_cols == grep({defined $where->{$_};} @primary_cols)) { } 8 8 16
Condition66%2/3
ref $where eq "HASH" and @primary_cols == grep({defined $where->{$_};} @primary_cols)
!ll&&!rl&&r
808
MC/DC50%1/2
ref $where eq "HASH" and @primary_cols == grep({defined $where->{$_};} @primary_cols)
ref $where eq "HASH" @primary_cols == grep({defined $where->{$_};} @primary_cols)
Truth table66%2/3
A and B
A= ref $where eq "HASH"B= @primary_cols == grep({defined $where->{$_};} @primary_cols)
ABresult
0X0
100
111
185 ref $where eq 'HASH'
186 and
187 8 @primary_cols == grep { defined $where->{$_} } @primary_cols
188 ) {
189 8 my %row_to_update;
190 8 @row_to_update{@primary_cols} = @{$where}{@primary_cols};
191 8 @pks_to_update = \%row_to_update;
192 }
193 else {
194 8 my $cursor = $self->select ($source, \@primary_cols, $where, {});
195 @pks_to_update = map {
196 8 my %row; @row{@primary_cols} = @$_; \%row
197 } $cursor->all;
198 }
199  
200 16 for my $ident (@pks_to_update) {
201 16 $self->_insert_blobs($source, $blob_cols, $ident);
202 }
203 }
204  
205 0 ▶sub _insert_blobs {
Subroutine _insert_blobs: not called CC=5 SCAR=34.0
206 0 my ($self, $source, $blob_cols, $row) = @_;
207 0 my $dbh = $self->_get_dbh;
208  
209 0 my $table = $source->name;
210  
211 0 my %row = %$row;
212 my @primary_cols = try
213 0 ▶ { $source->_pri_cols_or_die }
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 0 $self->throw_exception("Cannot update TEXT/IMAGE column(s): $_")
216 0 };
217  
218 $self->throw_exception('Cannot update TEXT/IMAGE column(s) without primary key values')
219 0 ▶ if ((grep { defined $row{$_} } @primary_cols) != @primary_cols);
Branch: 1 branch
BranchTrueFalseTotal
if grep({defined $row{$_};} @primary_cols) != @primary_cols 0 0 0
220  
221 # if we are 2-phase inserting a blob - there is nothing to retrieve anymore,
222 # regardless of the previous state of the flag
223 local $self->{_perform_autoinc_retrieval}
224 0 ▶ if $self->_perform_autoinc_retrieval;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_perform_autoinc_retrieval 0 0 0
225  
226 0 for my $col (keys %$blob_cols) {
227 0 my $blob = $blob_cols->{$col};
228  
229 0 my %where = map { ($_, $row{$_}) } @primary_cols;
230  
231 0 my $cursor = $self->select ($source, [$col], \%where, {});
232 0 $cursor->next;
233 0 my $sth = $cursor->sth;
234  
235 0 ▶ if (not $sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
236 # the key columns, never their values
237 0 $self->throw_exception(sprintf
238 "Could not find row in table '%s' for blob update (key columns: %s)",
239 $table, join(', ', @primary_cols),
240 );
241 }
242  
243 0 ▶ try {
Subroutine __ANON__: not called CC=8 SCAR=42.8
244 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$sth->fetch and do { do { $sth->func("CS_GET", 1, "ct_data_info") or die $sth->errstr } } 0 0 0
245 0 ▶ $sth->func('CS_GET', 1, 'ct_data_info') or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->func("CS_GET", 1, "ct_data_info") or die $sth->errstr 0 0 0
246 } while $sth->fetch;
247  
248 0 ▶ $sth->func('ct_prepare_send') or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->func("ct_prepare_send") or die $sth->errstr 0 0 0
249  
250 0 my $log_on_update = $self->_blob_log_on_update;
251 0 ▶ $log_on_update = 1 if not defined $log_on_update;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $log_on_update 0 0 0
252  
253 0 ▶ $sth->func('CS_SET', 1, {
Branch: 1 branch
BranchTrueFalseTotal
$sth->func("CS_SET", 1, {"total_txtlen", length $blob, "log_on_update", $log_on_update}, "ct_data_info") or die $sth->errstr 0 0 0
254 total_txtlen => length($blob),
255 log_on_update => $log_on_update
256 }, 'ct_data_info') or die $sth->errstr;
257  
258 0 ▶ $sth->func($blob, length($blob), 'ct_send_data') or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->func($blob, length $blob, "ct_send_data") or die $sth->errstr 0 0 0
259  
260 0 ▶ $sth->func('ct_finish_send') or die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->func("ct_finish_send") or die $sth->errstr 0 0 0
261 }
262 0 ▶ catch {
Subroutine __ANON__: not called CC=4 SCAR=30.0
263 # the key values and the content are the values of this operation
264 my $error = ref $_ ? $_ : $self->_mask_error_values(
265 0 ▶ $_, [ grep { defined and !ref } values %row, $blob ],
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
Condition0%0/3
defined $_ && !ref($_)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table0%0/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
266 );
267 0 ▶ if ($self->_using_freetds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_using_freetds) { } 0 0 0
268 0 $self->throw_exception (
269 "TEXT/IMAGE operation failed, probably because you are using FreeTDS: $error"
270 );
271 }
272 else {
273 0 $self->throw_exception($error);
274 }
275 }
276 0 ▶ finally {
Subroutine __ANON__: not called CC=2 SCAR=17.9
277 0 ▶ $sth->finish if $sth;
Branch: 1 branch
BranchTrueFalseTotal
if $sth 0 0 0
278 0 };
279 }
280 }
281  
282 28 ▶sub _insert_blobs_array {
Subroutine _insert_blobs_array: called CC=4 SCAR=13.9
283 28 my ($self, $source, $blob_cols, $cols, $data) = @_;
284  
285 28 for my $i (0..$#$data) {
286 66 my $datum = $data->[$i];
287  
288 66 my %row;
289 66 @row{ @$cols } = @$datum;
290  
291 66 my %blob_vals;
292 66 for my $j (0..$#$cols) {
293 162 ▶ if (exists $blob_cols->[$i][$j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $blob_cols->[$i][$j]) 52 110 162
294 52 $blob_vals{ $cols->[$j] } = $blob_cols->[$i][$j];
295 }
296 }
297  
298 66 $self->_insert_blobs ($source, \%blob_vals, \%row);
299 }
300 }
301  
302 35 1;
303  
304 __END__
« Sybase/Introspect/Views.pm ↑ Summary Sybase/Diff/ForeignKey.pm »