Sybase/Storage/ASE/BulkInsert.pm

filter: Statementstmt 57.1% 64 / 112 Branchbran 46.0% 23 / 50 Conditioncond 52.1% 12 / 23 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 69.2% 9 / 13 Podpod n/a totaltotal 52.8% 112 / 212 CC 37 SCAR 45.7
CC
37
Cov
54%
_insert_bulk
148.1
__ANON__
56.0
__ANON__
6.0
CRAP 96.2
« Sybase/Test/Conformance.pm ↑ Summary Sybase/DDL.pm »
1 package DBIO::Sybase::Storage::ASE::BulkInsert;
2 # ABSTRACT: Bulk insert API for Sybase ASE
3  
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 our $VERSION = '0.900007';
8  
9 34 ▶use Sub::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # No requires - these methods expect the consuming class to have:
15 # _remove_blob_cols_array, _null_blobs_array, _insert_blobs_array,
16 # _fetch_identity_sql,
17 # _autoinc_supplied_for_op, _bulk_storage, _get_dbh, _dbi_connect_info,
18 # _dbh_execute_for_fetch, sql_maker, txn_scope_guard, debug,
19 # _resolve_bindattrs, _query_end
20  
21 50 ▶sub _insert_bulk {
Subroutine _insert_bulk: called CC=29 SCAR=50.0
22 50 my $self = shift;
23  
24 # the writer and bulk storages are made by _init, at the first connect, and
25 # the bulk decision below reads the connection (karr k67)
26 50 $self->_get_dbh;
27  
28 50 my ($source, $cols, $data) = @_;
29  
30 # before any row is sent (karr k97, see ASE::_refuse_non_ascii_binds)
31 50 ▶ if ($self->_using_freetds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_using_freetds) 8 42 50
32 8 for my $j (0..$#$cols) {
33 21 ▶ my $type = $source->has_column($cols->[$j]) ? ($source->column_info($cols->[$j])->{data_type} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
$source->has_column($cols->[$j]) ? : 21 0 21
Condition50%1/2
$source->column_info($cols->[$j])->{"data_type"} // ""
l!l
210
MC/DC0%0/1
$source->column_info($cols->[$j])->{"data_type"} // ""
$source->column_info($cols->[$j])->{"data_type"}
Truth table50%1/2
A
A= $source->column_info($cols->[$j])->{"data_type"}
Aresult
11
00
34 21 ▶ next if $type =~ $DBIO::Sybase::Storage::ASE::BINARY_TYPE_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /$DBIO::Sybase::Storage::ASE::BINARY_TYPE_RE/ 6 15 21
35 15 for my $row (@$data) {
36 30 my $v = $row->[$j];
37 30 ▶ next unless defined $v and not ref $v and $v =~ /[^\x00-\x7F]/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and not ref $v and $v =~ /[^\x00-\x7F]/ 28 2 30
Condition83%5/6
defined $v and not ref $v and $v =~ /[^\x00-\x7F]/
!ll&&!rl&&r
2262
defined $v and not ref $v
!ll&&!rl&&r
2028
MC/DC66%2/3
defined $v and not ref $v and $v =~ /[^\x00-\x7F]/
defined $v not ref $v $v =~ /[^\x00-\x7F]/
Truth table75%3/4
A and not(B) and C
A= defined $vB= not ref $vC= $v =~ /[^\x00-\x7F]/
ABCresult
0XX0
10X0
1100
1111
38 2 $self->_throw_non_ascii($source->name, $cols->[$j], $j + 1);
39 }
40 }
41 }
42  
43 48 my $columns_info = $source->columns_info;
44  
45 my ($identity_col) =
46 48 grep { $columns_info->{$_}{is_auto_increment} }
47 keys %$columns_info;
48  
49 # FIXME - this is duplication from DBI.pm. When refactored towards
50 # the LobWriter this can be folded back where it belongs.
51 local $self->{_autoinc_supplied_for_op}
52 48 = grep { $_ eq $identity_col } @$cols;
53  
54 my $use_bulk_api =
55 $self->_bulk_storage &&
56 48 ▶ $self->_get_dbh->{syb_has_blk};
Condition66%2/3
$self->_bulk_storage && $self->_get_dbh->{'syb_has_blk'}
!ll&&!rl&&r
6420
MC/DC0%0/2
$self->_bulk_storage && $self->_get_dbh->{'syb_has_blk'}
$self->_bulk_storage $self->_get_dbh->{'syb_has_blk'}
Truth table66%2/3
A && B
A= $self->_bulk_storageB= $self->_get_dbh->{'syb_has_blk'}
ABresult
0X0
100
111
57  
58 48 ▶ if (! $use_bulk_api and ref($self->_dbi_connect_info->[0]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $use_bulk_api and ref $self->_dbi_connect_info->[0] eq "CODE") 0 48 48
Condition33%1/3
not $use_bulk_api and ref $self->_dbi_connect_info->[0] eq "CODE"
!ll&&!rl&&r
0480
MC/DC0%0/2
not $use_bulk_api and ref $self->_dbi_connect_info->[0] eq "CODE"
not $use_bulk_api ref $self->_dbi_connect_info->[0] eq "CODE"
Truth table33%1/3
not(A) and B
A= not $use_bulk_apiB= ref $self->_dbi_connect_info->[0] eq "CODE"
ABresult
0X0
100
111
59 0 carp_unique( join ' ',
60 'Bulk API support disabled due to use of a CODEREF connect_info.',
61 'Reverting to regular array inserts.',
62 );
63 }
64  
65 48 ▶ if (not $use_bulk_api) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_bulk_api) 48 0 48
66 # the rows are the caller's: the placeholders go into copies (karr k83)
67 48 $data = [ map { [ @$_ ] } @$data ];
68 48 @_ = ($source, $cols, $data);
69  
70 48 my ($blob_cols, $null_cols)
71 = $self->_remove_blob_cols_array($source, $cols, $data);
72  
73 # next::method uses a txn anyway, but it ends too early in case we need to
74 # select max(col) to get the identity for inserting blobs.
75 # read the flag before $self is the writer storage, which does not have it
76 # (the key of a row was taken from the last identity although the slice
77 # carried it; with the rows NULLed by key that would be the wrong rows)
78 48 my $autoinc_supplied = $self->_autoinc_supplied_for_op;
79  
80 48 ▶ ($self, my $guard) = $self->{transaction_depth} == 0 ?
Branch: 1 branch
BranchTrueFalseTotal
$self->{'transaction_depth'} == 0 ? : 48 0 48
81 ($self->_writer_storage, $self->_writer_storage->txn_scope_guard)
82 :
83 ($self, undef);
84  
85 48 $self->next::method(@_);
86  
87 # $null_cols: undef cells of a LOB column that has values in other rows;
88 # they went in as the empty literal and are set to NULL here (karr k81)
89 46 ▶ if ($blob_cols or $null_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blob_cols or $null_cols) 31 15 46
Condition100%3/3
$blob_cols or $null_cols
l!l&&r!l&&!r
28315
MC/DC100%2/2
$blob_cols or $null_cols
$blob_cols $null_cols
Truth table100%3/3
A or B
A= $blob_colsB= $null_cols
ABresult
1X1
011
000
90 31 ▶ if ($autoinc_supplied) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autoinc_supplied) { } 6 25 31
91 6 ▶ $self->_null_blobs_array ($source, $null_cols, $cols, $data)
Branch: 1 branch
BranchTrueFalseTotal
if $null_cols 5 1 6
92 if $null_cols;
93 6 ▶ $self->_insert_blobs_array ($source, $blob_cols, $cols, $data)
Branch: 1 branch
BranchTrueFalseTotal
if $blob_cols 5 1 6
94 if $blob_cols;
95 }
96 else {
97 25 my @cols_with_identities = (@$cols, $identity_col);
98  
99 ## calculate identities
100 # XXX This assumes identities always increase by 1, which may or may not
101 # be true.
102 25 my ($last_identity) =
103 $self->_dbh->selectrow_array (
104 $self->_fetch_identity_sql($source, $identity_col)
105 );
106 25 my @identities = (($last_identity - @$data + 1) .. $last_identity);
107  
108 25 my @data_with_identities = map [@$_, shift @identities], @$data;
109  
110 25 ▶ $self->_null_blobs_array (
Branch: 1 branch
BranchTrueFalseTotal
if $null_cols 14 11 25
111 $source, $null_cols, \@cols_with_identities, \@data_with_identities
112 ) if $null_cols;
113 25 ▶ $self->_insert_blobs_array (
Branch: 1 branch
BranchTrueFalseTotal
if $blob_cols 23 2 25
114 $source, $blob_cols, \@cols_with_identities, \@data_with_identities
115 ) if $blob_cols;
116 }
117 }
118  
119 46 ▶ $guard->commit if $guard;
Branch: 1 branch
BranchTrueFalseTotal
if $guard 46 0 46
120  
121 46 return;
122 }
123  
124 # otherwise, use the bulk API
125  
126 # rearrange @$data so that columns are in database order
127 # and so we submit a full column list
128 0 my %orig_order = map { $cols->[$_] => $_ } 0..$#$cols;
129  
130 0 my @source_columns = $source->columns;
131  
132 # bcp identity index is 1-based
133 0 my ($identity_idx) = grep { $source_columns[$_] eq $identity_col } (0..$#source_columns);
134 0 ▶ $identity_idx = defined $identity_idx ? $identity_idx + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $identity_idx ? : 0 0 0
135  
136 0 my @new_data;
137 0 for my $slice_idx (0..$#$data) {
138 push @new_data, [map {
139 # identity data will be 'undef' if not _autoinc_supplied_for_op()
140 # columns with defaults will also be 'undef'
141 0 exists $orig_order{$_}
142 0 ▶ ? $data->[$slice_idx][$orig_order{$_}]
Branch: 1 branch
BranchTrueFalseTotal
exists $orig_order{$_} ? : 0 0 0
143 : undef
144 } @source_columns];
145 }
146  
147 my $proto_bind = $self->_resolve_bindattrs(
148 $source,
149 [map {
150 0 [ { dbic_colname => $source_columns[$_], _bind_data_slice_idx => $_ }
151 => $new_data[0][$_] ]
152 } (0 ..$#source_columns) ],
153 $columns_info
154 );
155  
156 ## Set a client-side conversion error handler, straight from DBD::Sybase docs.
157 # This ignores any data conversion errors detected by the client side libs, as
158 # they are usually harmless.
159 my $orig_cslib_cb = DBD::Sybase::set_cslib_cb(
160 0 ▶ Sub::Util::set_subname _insert_bulk_cslib_errhandler => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
161 0 my ($layer, $origin, $severity, $errno, $errmsg, $osmsg, $blkmsg) = @_;
162  
163 0 ▶ return 1 if $errno == 36;
Branch: 1 branch
BranchTrueFalseTotal
if $errno == 36 0 0 0
164  
165 # the texts of the client library hold the value it could not convert
166 0 ▶ my $values = [ map { grep { defined and !ref } @$_ } @new_data ];
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
167 my ($errmsg_m, $osmsg_m, $blkmsg_m) = map {
168 0 ▶ $_ ? $self->_mask_error_values($_, $values) : $_
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
169 } ($errmsg, $osmsg, $blkmsg);
170  
171 0 ▶ carp
Branch: 3 branches
BranchTrueFalseTotal
$errmsg_m ? : 0 0 0
$osmsg_m ? : 0 0 0
$blkmsg_m ? : 0 0 0
172 "Layer: $layer, Origin: $origin, Severity: $severity, Error: $errno" .
173 ($errmsg_m ? "\n$errmsg_m" : '') .
174 ($osmsg_m ? "\n$osmsg_m" : '') .
175 ($blkmsg_m ? "\n$blkmsg_m" : '');
176  
177 0 return 0;
178 0 });
179  
180 0 my $exception = '';
181 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=11.6
182 0 my $bulk = $self->_bulk_storage;
183  
184 0 my $guard = $bulk->txn_scope_guard;
185  
186 ## FIXME - once this is done - address the FIXME on finish() below
187 ## XXX get this to work instead of our own $sth
188 ## will require SQLMaker or *Hacks changes for ordered columns
189 # $bulk->next::method($source, \@source_columns, \@new_data, {
190 # syb_bcp_attribs => {
191 # identity_flag => $self->_autoinc_supplied_for_op ? 1 : 0,
192 # identity_column => $identity_idx,
193 # }
194 # });
195 0 my $sql = 'INSERT INTO ' .
196 $bulk->sql_maker->_quote($source->name) . ' (' .
197 # colname list is ignored for BCP, but does no harm
198 (join ', ', map $bulk->sql_maker->_quote($_), @source_columns) . ') '.
199 ' VALUES ('. (join ', ', ('?') x @source_columns) . ')';
200  
201 ## XXX there's a bug in the DBD::Sybase bulk support that makes $sth->finish for
202 ## a prepare_cached statement ineffective. Replace with ->sth when fixed, or
203 ## better yet the version above. Should be fixed in DBD::Sybase .
204 0 ▶ my $sth = $bulk->_get_dbh->prepare($sql,
Branch: 1 branch
BranchTrueFalseTotal
$self->_autoinc_supplied_for_op ? : 0 0 0
205 # 'insert', # op
206 {
207 syb_bcp_attribs => {
208 identity_flag => $self->_autoinc_supplied_for_op ? 1 : 0,
209 identity_column => $identity_idx,
210 }
211 }
212 );
213  
214 {
215 # FIXME the $sth->finish in _execute_array does a rollback for some
216 # reason. Disable it temporarily until we fix the SQLMaker thing above
217 0 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.2
218 34 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
219 0 ▶ local *{ref($sth).'::finish'} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
220  
221 0 $self->_dbh_execute_for_fetch(
222 $source, $sth, $proto_bind, \@source_columns, \@new_data
223 );
224 }
225  
226 0 $guard->commit;
227  
228 0 $bulk->_query_end($sql);
229 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
230 # a text, with the values of the rows taken out; an exception object as it is
231 $exception = $self->_mask_error_values(
232 0 ▶ $_, [ map { grep { defined and !ref } @$_ } @new_data ],
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
233 );
234 0 };
235  
236 0 DBD::Sybase::set_cslib_cb($orig_cslib_cb);
237  
238 0 ▶ if ($exception =~ /-Y option/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exception =~ /-Y option/) { } 0 0 0
elsif ($exception) { } 0 0 0
239 0 my $w = 'Sybase bulk API operation failed due to character set incompatibility, '
240 . 'reverting to regular array inserts. Try unsetting the LANG environment variable'
241 ;
242 0 ▶ $w .= "\n$exception" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
243 0 carp $w;
244  
245 0 $self->_bulk_storage(undef);
246 0 unshift @_, $self;
247 0 goto \&_insert_bulk;
248 }
249 elsif ($exception) {
250 # rollback makes the bulkLogin connection unusable
251 0 $self->_bulk_storage->disconnect;
252 0 $self->throw_exception($exception);
253 }
254 }
255  
256 34 1;
257  
258 __END__
« Sybase/Test/Conformance.pm ↑ Summary Sybase/DDL.pm »