Sybase/Storage/ASE/BulkInsert.pm

filter: Statementstmt 28.0% 25 / 89 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 61.5% 8 / 13 Podpod n/a totaltotal 22.9% 33 / 144 CC 20 SCAR 46.2
CC
20
Cov
20%
_insert_bulk
204.5
__ANON__
30.0
__ANON__
3.2
CRAP 101.1
« Sybase/DDL.pm ↑ Summary Sybase/Introspect/Columns.pm »
1 package DBIO::Sybase::Storage::ASE::BulkInsert;
2 # ABSTRACT: Bulk insert API for Sybase ASE
3  
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 4 ▶use Sub::Name();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # No requires - these methods expect the consuming class to have:
12 # _remove_blob_cols_array, _insert_blobs_array, _fetch_identity_sql,
13 # _autoinc_supplied_for_op, _bulk_storage, _get_dbh, _dbi_connect_info,
14 # _dbh_execute_for_fetch, sql_maker, txn_scope_guard, debug,
15 # _resolve_bindattrs, _query_end
16  
17 0 ▶sub _insert_bulk {
Subroutine _insert_bulk: not called CC=15 SCAR=53.2
18 0 my $self = shift;
19 0 my ($source, $cols, $data) = @_;
20  
21 0 my $columns_info = $source->columns_info;
22  
23 my ($identity_col) =
24 0 grep { $columns_info->{$_}{is_auto_increment} }
25 keys %$columns_info;
26  
27 # FIXME - this is duplication from DBI.pm. When refactored towards
28 # the LobWriter this can be folded back where it belongs.
29 local $self->{_autoinc_supplied_for_op}
30 0 = grep { $_ eq $identity_col } @$cols;
31  
32 my $use_bulk_api =
33 $self->_bulk_storage &&
34 0 ▶ $self->_get_dbh->{syb_has_blk};
Condition0%0/3
$self->_bulk_storage && $self->_get_dbh->{'syb_has_blk'}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_bulk_storage && $self->_get_dbh->{'syb_has_blk'}
$self->_bulk_storage $self->_get_dbh->{'syb_has_blk'}
Truth table0%0/3
A && B
A= $self->_bulk_storageB= $self->_get_dbh->{'syb_has_blk'}
ABresult
0X0
100
111
35  
36 0 ▶ 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 0 0
Condition0%0/3
not $use_bulk_api and ref $self->_dbi_connect_info->[0] eq "CODE"
!ll&&!rl&&r
000
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 table0%0/3
not(A) and B
A= not $use_bulk_apiB= ref $self->_dbi_connect_info->[0] eq "CODE"
ABresult
0X0
100
111
37 0 carp_unique( join ' ',
38 'Bulk API support disabled due to use of a CODEREF connect_info.',
39 'Reverting to regular array inserts.',
40 );
41 }
42  
43 0 ▶ if (not $use_bulk_api) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_bulk_api) 0 0 0
44 0 my $blob_cols = $self->_remove_blob_cols_array($source, $cols, $data);
45  
46 # next::method uses a txn anyway, but it ends too early in case we need to
47 # select max(col) to get the identity for inserting blobs.
48 0 ▶ ($self, my $guard) = $self->{transaction_depth} == 0 ?
Branch: 1 branch
BranchTrueFalseTotal
$self->{'transaction_depth'} == 0 ? : 0 0 0
49 ($self->_writer_storage, $self->_writer_storage->txn_scope_guard)
50 :
51 ($self, undef);
52  
53 0 $self->next::method(@_);
54  
55 0 ▶ if ($blob_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blob_cols) 0 0 0
56 0 ▶ if ($self->_autoinc_supplied_for_op) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_autoinc_supplied_for_op) { } 0 0 0
57 0 $self->_insert_blobs_array ($source, $blob_cols, $cols, $data);
58 }
59 else {
60 0 my @cols_with_identities = (@$cols, $identity_col);
61  
62 ## calculate identities
63 # XXX This assumes identities always increase by 1, which may or may not
64 # be true.
65 0 my ($last_identity) =
66 $self->_dbh->selectrow_array (
67 $self->_fetch_identity_sql($source, $identity_col)
68 );
69 0 my @identities = (($last_identity - @$data + 1) .. $last_identity);
70  
71 0 my @data_with_identities = map [@$_, shift @identities], @$data;
72  
73 0 $self->_insert_blobs_array (
74 $source, $blob_cols, \@cols_with_identities, \@data_with_identities
75 );
76 }
77 }
78  
79 0 ▶ $guard->commit if $guard;
Branch: 1 branch
BranchTrueFalseTotal
if $guard 0 0 0
80  
81 0 return;
82 }
83  
84 # otherwise, use the bulk API
85  
86 # rearrange @$data so that columns are in database order
87 # and so we submit a full column list
88 0 my %orig_order = map { $cols->[$_] => $_ } 0..$#$cols;
89  
90 0 my @source_columns = $source->columns;
91  
92 # bcp identity index is 1-based
93 0 my ($identity_idx) = grep { $source_columns[$_] eq $identity_col } (0..$#source_columns);
94 0 ▶ $identity_idx = defined $identity_idx ? $identity_idx + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $identity_idx ? : 0 0 0
95  
96 0 my @new_data;
97 0 for my $slice_idx (0..$#$data) {
98 push @new_data, [map {
99 # identity data will be 'undef' if not _autoinc_supplied_for_op()
100 # columns with defaults will also be 'undef'
101 0 exists $orig_order{$_}
102 0 ▶ ? $data->[$slice_idx][$orig_order{$_}]
Branch: 1 branch
BranchTrueFalseTotal
exists $orig_order{$_} ? : 0 0 0
103 : undef
104 } @source_columns];
105 }
106  
107 my $proto_bind = $self->_resolve_bindattrs(
108 $source,
109 [map {
110 0 [ { dbic_colname => $source_columns[$_], _bind_data_slice_idx => $_ }
111 => $new_data[0][$_] ]
112 } (0 ..$#source_columns) ],
113 $columns_info
114 );
115  
116 ## Set a client-side conversion error handler, straight from DBD::Sybase docs.
117 # This ignores any data conversion errors detected by the client side libs, as
118 # they are usually harmless.
119 my $orig_cslib_cb = DBD::Sybase::set_cslib_cb(
120 0 ▶ Sub::Name::subname _insert_bulk_cslib_errhandler => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
121 0 my ($layer, $origin, $severity, $errno, $errmsg, $osmsg, $blkmsg) = @_;
122  
123 0 ▶ return 1 if $errno == 36;
Branch: 1 branch
BranchTrueFalseTotal
if $errno == 36 0 0 0
124  
125 0 ▶ carp
Branch: 3 branches
BranchTrueFalseTotal
$errmsg ? : 0 0 0
$osmsg ? : 0 0 0
$blkmsg ? : 0 0 0
126 "Layer: $layer, Origin: $origin, Severity: $severity, Error: $errno" .
127 ($errmsg ? "\n$errmsg" : '') .
128 ($osmsg ? "\n$osmsg" : '') .
129 ($blkmsg ? "\n$blkmsg" : '');
130  
131 0 return 0;
132 0 });
133  
134 0 my $exception = '';
135 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=11.6
136 0 my $bulk = $self->_bulk_storage;
137  
138 0 my $guard = $bulk->txn_scope_guard;
139  
140 ## FIXME - once this is done - address the FIXME on finish() below
141 ## XXX get this to work instead of our own $sth
142 ## will require SQLMaker or *Hacks changes for ordered columns
143 # $bulk->next::method($source, \@source_columns, \@new_data, {
144 # syb_bcp_attribs => {
145 # identity_flag => $self->_autoinc_supplied_for_op ? 1 : 0,
146 # identity_column => $identity_idx,
147 # }
148 # });
149 0 my $sql = 'INSERT INTO ' .
150 $bulk->sql_maker->_quote($source->name) . ' (' .
151 # colname list is ignored for BCP, but does no harm
152 (join ', ', map $bulk->sql_maker->_quote($_), @source_columns) . ') '.
153 ' VALUES ('. (join ', ', ('?') x @source_columns) . ')';
154  
155 ## XXX there's a bug in the DBD::Sybase bulk support that makes $sth->finish for
156 ## a prepare_cached statement ineffective. Replace with ->sth when fixed, or
157 ## better yet the version above. Should be fixed in DBD::Sybase .
158 0 ▶ my $sth = $bulk->_get_dbh->prepare($sql,
Branch: 1 branch
BranchTrueFalseTotal
$self->_autoinc_supplied_for_op ? : 0 0 0
159 # 'insert', # op
160 {
161 syb_bcp_attribs => {
162 identity_flag => $self->_autoinc_supplied_for_op ? 1 : 0,
163 identity_column => $identity_idx,
164 }
165 }
166 );
167  
168 {
169 # FIXME the $sth->finish in _execute_array does a rollback for some
170 # reason. Disable it temporarily until we fix the SQLMaker thing above
171 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.2
172 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
173 0 ▶ local *{ref($sth).'::finish'} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
174  
175 0 $self->_dbh_execute_for_fetch(
176 $source, $sth, $proto_bind, \@source_columns, \@new_data
177 );
178 }
179  
180 0 $guard->commit;
181  
182 0 $bulk->_query_end($sql);
183 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
184 0 $exception = shift;
185 0 };
186  
187 0 DBD::Sybase::set_cslib_cb($orig_cslib_cb);
188  
189 0 ▶ if ($exception =~ /-Y option/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exception =~ /-Y option/) { } 0 0 0
elsif ($exception) { } 0 0 0
190 0 my $w = 'Sybase bulk API operation failed due to character set incompatibility, '
191 . 'reverting to regular array inserts. Try unsetting the LANG environment variable'
192 ;
193 0 ▶ $w .= "\n$exception" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
194 0 carp $w;
195  
196 0 $self->_bulk_storage(undef);
197 0 unshift @_, $self;
198 0 goto \&_insert_bulk;
199 }
200 elsif ($exception) {
201 # rollback makes the bulkLogin connection unusable
202 0 $self->_bulk_storage->disconnect;
203 0 $self->throw_exception($exception);
204 }
205 }
206  
207 4 1;
208  
209 __END__
210  
211 =pod
212  
213 =encoding UTF-8
214  
215 =head1 NAME
216  
217 DBIO::Sybase::Storage::ASE::BulkInsert - Bulk insert API for Sybase ASE
218  
219 =head1 VERSION
220  
221 version 0.900001
222  
223 =head1 AUTHOR
224  
225 DBIO & DBIx::Class Authors
226  
227 =head1 COPYRIGHT AND LICENSE
228  
229 Copyright (C) 2026 DBIO Authors
230 Portions Copyright (C) 2005-2025 DBIx::Class Authors
231 Based on DBIx::Class, heavily modified.
232  
233 This is free software; you can redistribute it and/or modify it under
234 the same terms as the Perl 5 programming language system itself.
235  
236 =cut
« Sybase/DDL.pm ↑ Summary Sybase/Introspect/Columns.pm »