MSSQL/Storage/Sybase.pm

filter: Statementstmt 91.4% 235 / 257 Branchbran 89.6% 147 / 164 Conditioncond 76.1% 83 / 109 MC/DCmcdc 58.3% 35 / 60 Subroutinesub 92.6% 38 / 41 Podpod 66.6% 2 / 3 totaltotal 85.1% 540 / 634 CC 128 SCAR 24.7
CC
128
Cov
88%
_rewrite_binds
32.5
_execute_sth
19.9
_bulk_text_values
10.1
CRAP 11.9
« MSSQL/Storage/ODBC.pm ↑ Summary MSSQL/Diff/Column.pm »
1 package DBIO::MSSQL::Storage::Sybase;
2 # ABSTRACT: Support for Microsoft SQL Server via DBD::Sybase
3  
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 our $VERSION = '0.900007';
8  
9 16 ▶use base qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
10 DBIO::Sybase::Storage
11 DBIO::MSSQL::Storage
12 16 /;
13 16 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 16 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use DBIO::Storage::Composed ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use DBIO::Storage::DBI::NoBindVars ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use DBIO::Util 'is_plain_value';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 16 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 16 __PACKAGE__->datetime_parser_type(
24 'DBIO::MSSQL::Storage::Sybase::DateTime::Format'
25 );
26  
27 # DBIO::Sybase::Storage precedes DBIO::MSSQL::Storage in this class's
28 # linearisation and names DBIO::Sybase::Deploy (ASE: a `text` column is a TEXT
29 # there, not the NVARCHAR(MAX) of DBIO::MSSQL::DDL). Over DBD::Sybase
30 # $schema->deploy and the diff must still be this driver's (karr k81).
31 1 ▶sub dbio_deploy_class { 'DBIO::MSSQL::Deploy' }
Subroutine dbio_deploy_class: called CC=1 SCAR=0.0
Pod undocumented
32  
33 # Deliberately without next::method. DBIO::Sybase::Storage precedes
34 # DBIO::MSSQL::Storage in this class's linearisation, and the hook there is
35 # the ASE hook's: it would move the storage into the ASE class, which a
36 # declared MSSQL storage never becomes.
37 34 ▶sub _adapt_to_access_path {
Subroutine _adapt_to_access_path: called CC=3 SCAR=12.0
38 34 my ($self, $dbd) = @_;
39  
40 # A storage pinned to this class and connected through another DBD: core
41 # reports that, and there is no DBD::Sybase client library to ask.
42 34 ▶ return if defined $dbd and $dbd ne 'Sybase';
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbd and $dbd ne "Sybase" 0 34 34
Condition33%1/3
defined $dbd and $dbd ne "Sybase"
!ll&&!rl&&r
0340
MC/DC0%0/2
defined $dbd and $dbd ne "Sybase"
defined $dbd $dbd ne "Sybase"
Truth table33%1/3
A and B
A= defined $dbdB= $dbd ne "Sybase"
ABresult
0X0
100
111
43  
44 34 $self->_adapt_to_placeholder_support;
45  
46 34 return;
47 }
48  
49 # The one decision this class makes about itself, for a declared storage
50 # (_adapt_to_access_path).
51 # True when the storage moved into the NoBindVars class.
52 34 ▶sub _adapt_to_placeholder_support {
Subroutine _adapt_to_placeholder_support: called CC=5 SCAR=16.1
53 34 my $self = shift;
54  
55 # This class itself, or a class synthesised over it: a storage pinned here
56 # with storage layers registered on the schema is an instance of a
57 # DBIO::Storage::Composed class whose base is this one (karr k48). The
58 # layers go back over the NoBindVars class when core recomposes after the
59 # hook (DBIO::Storage::DBI::_adapt_declared_storage). A storage already in
60 # a NoBindVars class has made the decision.
61 34 my $class = ref $self;
62 34 ▶ if (my $composition = DBIO::Storage::Composed->composition_of($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $composition = "DBIO::Storage::Composed"->composition_of($class)) 2 32 34
63 2 $class = $composition->{base};
64 }
65 34 ▶ return if $class ne __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $class ne __PACKAGE__ 1 33 34
66  
67 33 ▶ if (not $self->_use_typeless_placeholders) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_use_typeless_placeholders) 15 18 33
68 15 ▶ carp_once <<'EOF' unless $ENV{DBIO_MSSQL_FREETDS_LOWVER_NOWARN};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"DBIO_MSSQL_FREETDS_LOWVER_NOWARN"} 0 15 15
69 Placeholders do not seem to be supported in your configuration of
70 DBD::Sybase/FreeTDS.
71  
72 This means you are taking a large performance hit, as caching of prepared
73 statements is disabled.
74  
75 Make sure to configure your server with "tds version" of 8.0 or 7.0 in
76 /etc/freetds/freetds.conf .
77  
78 To turn off this warning, set the DBIO_MSSQL_FREETDS_LOWVER_NOWARN environment
79 variable.
80 EOF
81 require
82 15 DBIO::MSSQL::Storage::Sybase::NoBindVars;
83 15 bless $self,
84 'DBIO::MSSQL::Storage::Sybase::NoBindVars';
85 15 return 1;
86 }
87  
88 18 return;
89 }
90  
91 34 ▶sub _init {
Subroutine _init: called CC=4 SCAR=14.0
92 34 my $self = shift;
93  
94 34 $self->next::method(@_);
95  
96 # work around massively broken freetds versions after 0.82
97 # - no sth caching
98 #
99 # NOT "no scope_identity" (karr k39): the workaround of the old FreeTDS
100 # versions turned SELECT SCOPE_IDENTITY() off and read @@identity, the last
101 # identity of the SESSION, which a trigger that inserts into another
102 # IDENTITY table overwrites -- create() returned the other table's key
103 # (1000 for the row 1). SCOPE_IDENTITY() works over FreeTDS 1.3.17
104 # (measured live, t/60 part C, with a trigger); the base class always asked
105 # it. Only that version is measured: an older FreeTDS that really cannot
106 # take the trailing SELECT would show as a missing key, and its
107 # workaround would be a deliberate choice for that version, not for
108 # every one above 0.82.
109 #
110 # warn about the fact as well, do not provide a mechanism to shut it up
111 #
112 # Parse as a dotted version so modern FreeTDS strings like "1.3.17" are not
113 # coerced to a number (which truncates at the second dot and warns "isn't
114 # numeric"). An undeterminable version (0/undef) keeps the historic 999
115 # sentinel so it is still treated as a new/buggy build.
116 34 ▶ if ($self->_using_freetds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_using_freetds) 32 2 34
117 32 ▶ my $ver = $self->_using_freetds_version || 999;
Condition50%1/2
$self->_using_freetds_version || 999
l!l
320
MC/DC0%0/1
$self->_using_freetds_version || 999
$self->_using_freetds_version
Truth table50%1/2
A
A= $self->_using_freetds_version
Aresult
11
00
118 32 ▶ if (version->parse("v$ver") > version->parse('v0.82')) {
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse("v$ver") > "version"->parse("v0.82")) 32 0 32
119 32 carp_once(
120 "Your DBD::Sybase was compiled against buggy FreeTDS version $ver. "
121 . 'Statement caching does not work and will be disabled.'
122 );
123  
124 32 $self->disable_sth_caching(1);
125 }
126 }
127 }
128  
129 # invoked only if DBD::Sybase is compiled against FreeTDS
130 33 ▶sub _set_autocommit_stmt {
Subroutine _set_autocommit_stmt: called CC=2 SCAR=7.2
131 33 my ($self, $on) = @_;
132  
133 33 ▶ return 'SET IMPLICIT_TRANSACTIONS ' . ($on ? 'OFF' : 'ON');
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 33 0 33
134 }
135  
136 0 ▶sub _get_server_version {
Subroutine _get_server_version: not called CC=2 SCAR=17.9
137 0 my $self = shift;
138  
139 0 my $product_version = $self->_get_dbh->selectrow_hashref('master.dbo.xp_msver ProductVersion');
140  
141 0 ▶ if ((my $version = $product_version->{Character_Value}) =~ /^(\d+)\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $version = $product_version->{'Character_Value'}) =~ /^(\d+)\./) { } 0 0 0
142 0 return $version;
143 }
144 else {
145 0 $self->throw_exception(
146 "MSSQL Version Retrieval Failed, Your ProductVersion's Character_Value is missing or malformed!"
147 );
148 }
149 }
150  
151 # karr k40. What FreeTDS declares every placeholder as when it prepares a
152 # statement for SQL Server ("@P1 varchar(4000)", read in its protocol dump):
153 # the server cuts a longer value to this many bytes, without an error.
154 16 my $PLACEHOLDER_BYTES = 4000;
155  
156 # A string as DBD::Sybase sends it: the bytes of Perl's internal form, for a
157 # statement and for a bound value alike (measured: a character string and
158 # the bytes of its UTF-8 form are stored as the same thing).
159 115 ▶sub _wire_bytes {
Subroutine _wire_bytes: called CC=2 SCAR=6.9
160 115 my $string = shift;
161 115 ▶ utf8::encode($string) if utf8::is_utf8($string);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($string) 10 105 115
162 115 return $string;
163 }
164  
165 # The Transact-SQL literal for a value that cannot be bound, from its bytes.
166 31 ▶sub _long_value_literal {
Subroutine _long_value_literal: called CC=3 SCAR=11.0
167 31 my $value = shift;
168  
169 # A plain literal is the varchar the placeholder was declared as. Beyond
170 # ASCII it would go through the code page of the database, which stores a
171 # character it lacks as "?" without an error; a Unicode literal does not.
172 31 ▶ my $n = $value =~ /[^\x00-\x7F]/ ? 'N' : '';
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /[^\x00-\x7F]/ ? : 9 22 31
173  
174 31 $value =~ s/'/''/g;
175  
176 # Two things the server reads in a literal and not in a bound value: a
177 # backslash before a line break continues the literal in the next line
178 # (both are dropped), and a NUL ends the statement text. The literal is
179 # closed after such a backslash and around a NUL, and the pieces joined.
180 31 my $pieces = ( $value =~ s/\\(?=[\r\n])/\\' + $n'/g )
181 + ( $value =~ s/\0/' + ${n}CHAR(0) + $n'/g );
182  
183 31 ▶ return "$n'$value'" unless $pieces;
Branch: 1 branch
BranchTrueFalseTotal
unless $pieces 28 3 31
184  
185 # Joined onto a MAX value: pieces joined to each other alone are cut at
186 # 8000 bytes.
187 3 return "(CAST($n'' AS ${n}VARCHAR(MAX)) + $n'$value')";
188 }
189  
190 # True for a bind value the server would cut, were it bound.
191 163 ▶sub _over_placeholder_size {
Subroutine _over_placeholder_size: called CC=8 SCAR=20.9
192 163 my $value = shift;
193  
194 163 ▶ return 0 if not defined $value or ref $value eq 'SCALAR'; # NULL, bind_inout
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value eq "SCALAR" 3 160 163
Condition66%2/3
not defined $value or ref $value eq "SCALAR"
l!l&&r!l&&!r
30160
MC/DC50%1/2
not defined $value or ref $value eq "SCALAR"
not defined $value ref $value eq "SCALAR"
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value eq "SCALAR"
ABresult
1X1
011
000
195  
196 # an object is bound as its string form (DBIO::Storage::DBI::_bind_sth_params)
197 160 ▶ $value = "$value" if length ref $value and is_plain_value $value;
Branch: 1 branch
BranchTrueFalseTotal
if length ref $value and &is_plain_value($value) 4 156 160
Condition66%2/3
length ref $value and &is_plain_value($value)
!ll&&!rl&&r
15604
MC/DC50%1/2
length ref $value and &is_plain_value($value)
length ref $value &is_plain_value($value)
Truth table66%2/3
A and B
A= length ref $valueB= &is_plain_value($value)
ABresult
0X0
100
111
198  
199 # characters are never more than their bytes
200 160 ▶ return 1 if length $value > $PLACEHOLDER_BYTES;
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $PLACEHOLDER_BYTES 33 127 160
201 127 ▶ return 0 unless utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($value) 121 6 127
202 6 ▶ return length _wire_bytes($value) > $PLACEHOLDER_BYTES ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length _wire_bytes($value) > $PLACEHOLDER_BYTES ? : 1 5 6
203 }
204  
205 # The string a bind value is sent as, or undef for one that is no string to
206 # look at: NULL, bind_inout, and an object that is not a plain value (core
207 # binds that untouched).
208 251 ▶sub _bind_string {
Subroutine _bind_string: called CC=5 SCAR=16.2
209 251 my $value = shift;
210  
211 251 ▶ return undef if not defined $value or ref $value eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value eq "SCALAR" 6 245 251
Condition66%2/3
not defined $value or ref $value eq "SCALAR"
l!l&&r!l&&!r
60245
MC/DC50%1/2
not defined $value or ref $value eq "SCALAR"
not defined $value ref $value eq "SCALAR"
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value eq "SCALAR"
ABresult
1X1
011
000
212  
213 245 ▶ if (length ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ref $value) 5 240 245
214 5 ▶ return undef unless is_plain_value $value;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_plain_value($value) 0 5 5
215 5 $value = "$value";
216 }
217  
218 245 return $value;
219 }
220  
221 # karr k49. A string with a character beyond ASCII does not arrive when it is
222 # bound, at any length: FreeTDS sends it as a CHAR of its length in client
223 # bytes, converted to the code page of the server, which the server pads with
224 # blanks (a-umlaut arrives as itself and a blank, the euro sign with two); a
225 # character the code page lacks is refused by the client library after the
226 # row has been written, with blanks for the whole value. Nothing the
227 # statement could say about the placeholder changes that, and rewriting the
228 # value is not this class's to decide, so it is refused -- before anything is
229 # sent. "Beyond ASCII" is a question of characters: a byte string with a high
230 # byte is as much beyond it as a character string.
231 139 ▶sub _non_ascii_bind {
Subroutine _non_ascii_bind: called CC=3 SCAR=11.0
232 139 my $string = _bind_string(shift);
233  
234 139 ▶ return ( defined $string and $string =~ /[^\x00-\x7F]/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $string && $string =~ /[^\x00-\x7F]/ ? : 19 120 139
Condition100%3/3
defined $string and $string =~ /[^\x00-\x7F]/
!ll&&!rl&&r
311719
MC/DC100%2/2
defined $string and $string =~ /[^\x00-\x7F]/
defined $string $string =~ /[^\x00-\x7F]/
Truth table100%3/3
A and B
A= defined $stringB= $string =~ /[^\x00-\x7F]/
ABresult
0X0
100
111
235 }
236  
237 # What is told, never the value.
238 19 ▶sub _refuse_non_ascii {
Subroutine _refuse_non_ascii: called CC=4 SCAR=13.9
239 19 my ($self, $column, $position, $of) = @_;
240  
241 19 ▶ $self->throw_exception(sprintf
Branch: 3 branches
BranchTrueFalseTotal
defined $position ? : 17 2 19
defined $column ? : 15 2 17
defined $column ? : 2 0 2
242 'Cannot send this statement through DBD::Sybase: %s has a character '
243 . 'beyond ASCII. FreeTDS sends a bound string as a fixed-length CHAR in '
244 . "the bytes of the client's character set, converted to the code page of "
245 . 'the server, and the server pads it with blanks (an a-umlaut arrives '
246 . 'as itself and a blank); a character the code page lacks is refused by '
247 . 'the client library after the row has been written, with blanks for '
248 . 'the value. Nothing the statement says about the placeholder changes '
249 . 'that, so the value is not sent. Connect through DBD::ODBC instead '
250 . '(a dbi:ODBC: DSN, class DBIO::MSSQL::Storage::ODBC), which carries '
251 . 'the string whole into an NCHAR, NVARCHAR or NTEXT column when the ODBC '
252 . "driver's client character set is UTF-8 (ClientCharset=UTF-8), and into "
253 . 'a VARCHAR column as far as the code page of the database has the '
254 . 'characters.',
255 ( defined $position
256 ? "bind value $position of $of"
257 . ( defined $column ? " (column '$column')" : '' )
258 : ( defined $column ? "a value for the column '$column'" : 'a bind value' )
259 ),
260 );
261 }
262  
263 # karr k55. DBD::Sybase binds an empty string as one blank (it has a space for
264 # "" in its code, an inheritance from ASE, which has no empty string). The
265 # server gets the same byte for '' and for ' '. So an empty value is not
266 # bound: the statement gets the literal '' where its placeholder stood, as for
267 # a value over the placeholder size. Nothing of a value is in the statement.
268 112 ▶sub _empty_bind {
Subroutine _empty_bind: called CC=3 SCAR=11.0
269 112 my $string = _bind_string(shift);
270  
271 112 ▶ return ( defined $string and not length $string ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $string && !length($string) ? : 18 94 112
Condition100%3/3
defined $string and not length $string
!ll&&!rl&&r
39118
MC/DC100%2/2
defined $string and not length $string
defined $string not length $string
Truth table100%3/3
A and not(B)
A= defined $stringB= not length $string
ABresult
0X0
100
111
272 }
273  
274 # karr k50. A value for a binary column cannot be bound as it is either: it
275 # is varchar to the server, which refuses to make VARBINARY of that (Msg 257)
276 # -- and DBD::Sybase ignores a bind type for anything but an EXEC. What can be
277 # bound is its hex form, which is ASCII and therefore arrives as it was sent,
278 # to a placeholder the statement turns back into the bytes:
279 #
280 # CONVERT(VARBINARY(MAX), ?, 1) bound: "0x" and the hex digits
281 #
282 # Style 1, with the "0x": the bound string is then never empty, which
283 # DBD::Sybase would bind as a blank, or as NULL (karr k55).
284 #
285 # One placeholder carries 4000 bytes, so 1999 bytes of the value; a longer
286 # value is bound in pieces, which the statement joins. MAX, because pieces of
287 # a sized type are cut to 8000 bytes when joined, without an error; and joined
288 # as a balanced tree, because joined one after the other the server copies
289 # what it has so far for every piece (measured: 3 MB in 77 s that way, in
290 # 1.1 s this way).
291 16 my $PIECE_BYTES = 1999;
292  
293 # What one statement takes: the server counts 2100 parameters and the handle
294 # of the prepared statement is one of them (Msg 8003 from 2100 placeholders).
295 16 my $MAX_PLACEHOLDERS = 2099;
296  
297 # How many placeholders a binary value takes: one per piece, one for an empty
298 # value and one for NULL.
299 36 ▶sub _piece_count {
Subroutine _piece_count: called CC=3 SCAR=11.0
300 36 my $bytes = shift;
301  
302 36 ▶ return 1 unless defined $bytes and length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and length $bytes 4 32 36
Condition100%3/3
defined $bytes and length $bytes
!ll&&!rl&&r
1332
MC/DC100%2/2
defined $bytes and length $bytes
defined $bytes length $bytes
Truth table100%3/3
A and B
A= defined $bytesB= length $bytes
ABresult
0X0
100
111
303 32 return int( ( length($bytes) + $PIECE_BYTES - 1 ) / $PIECE_BYTES );
304 }
305  
306 # The strings bound to them.
307 35 ▶sub _hex_pieces {
Subroutine _hex_pieces: called CC=3 SCAR=11.0
308 35 my $bytes = shift;
309  
310 35 ▶ return (undef) unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 1 34 35
311 34 ▶ return ('0x') unless length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless length $bytes 3 31 34
312 31 return map { '0x' . uc unpack 'H*', $_ } unpack "(a$PIECE_BYTES)*", $bytes;
313 }
314  
315 # The expression that stands where the "?" of a binary value stood.
316 4351 ▶sub _hex_placeholders {
Subroutine _hex_placeholders: called CC=2 SCAR=6.9
317 4351 my $count = shift;
318  
319 4351 ▶ return 'CONVERT(VARBINARY(MAX), ?, 1)' if $count == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 1 2193 2158 4351
320  
321 2158 my $left = int( ( $count + 1 ) / 2 );
322  
323 2158 return '(' . _hex_placeholders($left) . ' + '
324 . _hex_placeholders( $count - $left ) . ')';
325 }
326  
327 # The statement to prepare and what to bind to it, when a bind value cannot
328 # go the way it came: a value for a binary column (karr k50) is bound as its
329 # hex pieces, a value over the placeholder size (karr k40) is in the
330 # statement as a literal. undef when there is neither. On the NoBindVars
331 # class there are no binds to look at.
332 188 ▶sub _rewrite_binds {
Subroutine _rewrite_binds: called CC=32 SCAR=34.8
333 188 my ($self, $sql, $bind) = @_;
334  
335 188 my (%literal, %binary, %empty);
336  
337 188 for my $i (0 .. $#$bind) {
338 175 my ($attrs, $value) = @{ $bind->[$i] };
339  
340 175 ▶ if (
Branch: 4 branches
BranchTrueFalseTotal
if (ref $attrs eq 'HASH' and $self->_is_binary_data_type($attrs->{'sqlt_datatype'}) and ref $value ne 'SCALAR') { } 39 136 175
elsif (_over_placeholder_size($value)) { } 31 105 136
elsif (_non_ascii_bind($value)) { } 17 88 105
elsif (_empty_bind($value)) { } 16 72 88
Condition66%4/6
ref $attrs eq "HASH" and $self->_is_binary_data_type($attrs->{"sqlt_datatype"}) and ref $value ne "SCALAR"
!ll&&!rl&&r
136039
ref $attrs eq "HASH" and $self->_is_binary_data_type($attrs->{"sqlt_datatype"})
!ll&&!rl&&r
013639
MC/DC33%1/3
ref $attrs eq "HASH" and $self->_is_binary_data_type($attrs->{"sqlt_datatype"}) and ref $value ne "SCALAR"
ref $attrs eq "HASH" $self->_is_binary_data_type($attrs->{"sqlt_datatype"}) ref $value ne "SCALAR"
Truth table50%2/4
A and B and C
A= ref $attrs eq "HASH"B= $self->_is_binary_data_type($attrs->{"sqlt_datatype"})C= ref $value ne "SCALAR"
ABCresult
0XX0
10X0
1100
1111
341 ref $attrs eq 'HASH'
342 and $self->_is_binary_data_type( $attrs->{sqlt_datatype} )
343 and ref $value ne 'SCALAR' # bind_inout
344 ) {
345 $binary{$i}
346 39 = $self->_binary_value_bytes( $value, $attrs->{dbic_colname} );
347 }
348 elsif (_over_placeholder_size($value)) {
349 31 $literal{$i} = _long_value_literal(_wire_bytes("$value"));
350 }
351 elsif (_non_ascii_bind($value)) {
352 17 $self->_refuse_non_ascii( $attrs->{dbic_colname}, $i + 1, scalar @$bind );
353 }
354 elsif (_empty_bind($value)) {
355 16 $empty{$i} = 1;
356 }
357 }
358  
359 170 ▶ return undef unless %literal or %binary or %empty;
Branch: 1 branch
BranchTrueFalseTotal
unless %literal or %binary or %empty 89 81 170
Condition100%6/6
%literal or %binary or %empty
l!l&&r!l&&!r
651689
%literal or %binary
l!l&&r!l&&!r
2936105
MC/DC100%3/3
%literal or %binary or %empty
%literal %binary %empty
Truth table100%4/4
A or B or C
A= %literalB= %binaryC= %empty
ABCresult
1XX1
01X1
0011
0000
360  
361 # DBD::Sybase sends a statement that begins with EXEC as a remote procedure
362 # call and reads nothing of it but the name and the placeholders: what
363 # stands where a placeholder stood is dropped, and the procedure is handed
364 # the hex string -- or, for an empty value, a blank.
365 81 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if %empty and $sql =~ /\A\s*exec/i 2 79 81
Condition100%3/3
%empty and $sql =~ /\A\s*exec/i
!ll&&!rl&&r
65142
MC/DC100%2/2
%empty and $sql =~ /\A\s*exec/i
%empty $sql =~ /\A\s*exec/i
Truth table100%3/3
A and B
A= %emptyB= $sql =~ /\A\s*exec/i
ABresult
0X0
100
111
366 'Cannot send this statement through DBD::Sybase: it has an empty string '
367 . 'as a bind value, which DBD::Sybase binds as one blank and which is '
368 . "therefore written into the statement as the literal '' -- and "
369 . 'DBD::Sybase sends a statement that begins with EXEC as a remote '
370 . 'procedure call, without what stands around a placeholder: the '
371 . "procedure would be handed a blank. Write '' into the statement "
372 . 'yourself, or connect through DBD::ODBC (class '
373 . 'DBIO::MSSQL::Storage::ODBC), which binds an empty string as it is.'
374 ) if %empty and $sql =~ /\A\s*exec/i;
375  
376 79 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if %binary and $sql =~ /\A\s*exec/i 1 78 79
Condition100%3/3
%binary and $sql =~ /\A\s*exec/i
!ll&&!rl&&r
42361
MC/DC100%2/2
%binary and $sql =~ /\A\s*exec/i
%binary $sql =~ /\A\s*exec/i
Truth table100%3/3
A and B
A= %binaryB= $sql =~ /\A\s*exec/i
ABresult
0X0
100
111
377 'Cannot send this statement through DBD::Sybase: it has a bind value for '
378 . 'a binary column, which is bound as its hex form to a placeholder that '
379 . 'the statement turns back into bytes -- and DBD::Sybase sends a '
380 . 'statement that begins with EXEC as a remote procedure call, without '
381 . 'what stands around a placeholder. Hand the procedure the value in a '
382 . 'form it converts itself.'
383 ) if %binary and $sql =~ /\A\s*exec/i;
384  
385 # Put together as bytes: a byte string joined to a character string would
386 # be encoded a second time, and arrive as something else than it does bound.
387 # The placeholders are the free "?" of the statement: one in a string
388 # literal or in a quoted name is part of the text.
389 78 my @part = DBIO::Storage::DBI::NoBindVars::split_at_placeholders(_wire_bytes($sql));
390  
391 78 ▶ $self->throw_exception(sprintf
Branch: 5 branches
BranchTrueFalseTotal
unless $#part == @$bind 4 74 78
%literal ? : 1 3 4
%binary ? : 1 2 3
$#part == 1 ? : 0 4 4
@$bind == 1 ? : 4 0 4
392 'Cannot send this statement through DBD::Sybase: it has %s -- but the '
393 . 'statement has %d placeholder%s and %d bind value%s, and the two numbers '
394 . 'have to agree for the statement to be rewritten.',
395 ( %literal
396 ? "a bind value of more than $PLACEHOLDER_BYTES bytes, which the "
397 . "server would cut to $PLACEHOLDER_BYTES without an error if it "
398 . 'were bound, and which therefore goes into the statement as a '
399 . 'literal'
400 : %binary
401 ? 'a bind value for a binary column, whose placeholder has to be '
402 . 'rewritten for the server to take the value'
403 : "an empty string as a bind value, which DBD::Sybase would bind as "
404 . "a blank and which therefore goes into the statement as ''"
405 ),
406 $#part, ( $#part == 1 ? '' : 's' ), scalar @$bind, ( @$bind == 1 ? '' : 's' ),
407 ) unless $#part == @$bind;
408  
409 # counted before a value is turned into its hex form: a statement that is
410 # refused costs nothing
411 74 my %count = map { $_ => _piece_count( $binary{$_} ) } keys %binary;
412  
413 74 my $placeholders = @$bind - keys(%literal) - keys(%empty);
414 74 $placeholders += $_ - 1 for values %count;
415  
416 74 ▶ if (%count and $placeholders > $MAX_PLACEHOLDERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (%count and $placeholders > $MAX_PLACEHOLDERS) 1 73 74
Condition100%3/3
%count and $placeholders > $MAX_PLACEHOLDERS
!ll&&!rl&&r
39341
MC/DC100%2/2
%count and $placeholders > $MAX_PLACEHOLDERS
%count $placeholders > $MAX_PLACEHOLDERS
Truth table100%3/3
A and B
A= %countB= $placeholders > $MAX_PLACEHOLDERS
ABresult
0X0
100
111
417 0 my ($largest) = sort { $count{$b} <=> $count{$a} } keys %count;
418  
419 $self->throw_exception(sprintf
420 'Cannot send this statement through DBD::Sybase: it would have %d '
421 . 'placeholders, and the server takes %d. A value for a binary column is '
422 . 'bound in pieces of %d bytes, one placeholder each; the largest of '
423 . 'this statement%s has %d bytes and needs %d. One statement carries '
424 . 'about %d bytes of binary values, less what its other bind values '
425 . 'take.',
426 $placeholders, $MAX_PLACEHOLDERS, $PIECE_BYTES,
427 ( defined $bind->[$largest][0]{dbic_colname}
428 ? " (column '$bind->[$largest][0]{dbic_colname}')" : '' ),
429 length( defined $binary{$largest} ? $binary{$largest} : '' ),
430 1 ▶ $count{$largest},
Branch: 2 branches
BranchTrueFalseTotal
defined $bind->[$largest][0]{'dbic_colname'} ? : 1 0 1
defined $binary{$largest} ? : 1 0 1
431 $MAX_PLACEHOLDERS * $PIECE_BYTES,
432 );
433 }
434  
435 73 my $rewritten = shift @part;
436 $rewritten .= (
437 exists $literal{$_} ? $literal{$_}
438 : exists $empty{$_} ? "''"
439 : exists $binary{$_} ? _hex_placeholders( $count{$_} )
440 : '?'
441 73 ▶ ) . $part[$_] for 0 .. $#$bind;
Branch: 3 branches
BranchTrueFalseTotal
exists $literal{$_} ? : 30 79 109
exists $empty{$_} ? : 12 67 79
exists $binary{$_} ? : 35 32 67
442  
443 return {
444 sql => $rewritten,
445  
446 # per bind value: nothing (it is in the statement), its hex pieces, or
447 # its own position
448 bound => [ map {
449 73 ( exists $literal{$_} or exists $empty{$_} ) ? ()
450 109 ▶ : exists $binary{$_} ? [ _hex_pieces( $binary{$_} ) ]
Branch: 2 branches
BranchTrueFalseTotal
exists $literal{$_} || exists $empty{$_} ? : 42 67 109
exists $binary{$_} ? : 35 32 67
Condition100%3/3
exists $literal{$_} or exists $empty{$_}
l!l&&r!l&&!r
301267
MC/DC100%2/2
exists $literal{$_} or exists $empty{$_}
exists $literal{$_} exists $empty{$_}
Truth table100%3/3
A or B
A= exists $literal{$_}B= exists $empty{$_}
ABresult
1X1
011
000
451 : $_
452 } 0 .. $#$bind ],
453  
454 # a statement with a value in it does not come again
455 literal => scalar keys %literal,
456 };
457 }
458  
459 # Core runs a statement in three steps -- trace it, prepare it, bind its
460 # values (_dbh_execute; _dbh_prepare without the execute) -- and the prepare,
461 # the one step that has to be given another statement, is not handed the
462 # values. So what is to be prepared is worked out here, ahead of the three,
463 # and kept for the last two. The trace gets the statement and the values as
464 # they were, through redact_bind_value (ADR 0025) like any other: put into
465 # the statement before the trace, a long value would pass that hook by.
466 #
467 # Not after the prepare either: a second prepare while a prepared statement
468 # with placeholders is alive makes DBD::Sybase open a second connection.
469 188 ▶sub _dbh_execute {
Subroutine _dbh_execute: called CC=1 SCAR=0.0
470 188 my $self = shift;
471  
472 188 local $self->{_rewritten_binds} = $self->_rewrite_binds(@_[1, 2]);
473  
474 162 return $self->next::method(@_);
475 }
476  
477 0 ▶sub _dbh_prepare {
Subroutine _dbh_prepare: not called CC=1 SCAR=6.9
478 0 my $self = shift;
479  
480 0 local $self->{_rewritten_binds} = $self->_rewrite_binds(@_[1, 2]);
481  
482 0 return $self->next::method(@_);
483 }
484  
485 165 ▶sub _prepare_sth {
Subroutine _prepare_sth: called CC=3 SCAR=11.0
486 165 my ($self, $dbh, $sql) = @_;
487  
488 my $rewritten = $self->{_rewritten_binds}
489 165 ▶ or return $self->next::method($dbh, $sql);
Branch: 1 branch
BranchTrueFalseTotal
my $rewritten = $self->{"_rewritten_binds"} or return $self->next::method($dbh, $sql) 73 92 165
490  
491 # A statement with a value in it does not come again: never into the
492 # statement cache, whatever the setting.
493 73 ▶ local $self->{disable_sth_caching} = 1 if $rewritten->{literal};
Branch: 1 branch
BranchTrueFalseTotal
if $rewritten->{"literal"} 28 45 73
494  
495 73 return $self->next::method($dbh, $rewritten->{sql});
496 }
497  
498 162 ▶sub _bind_sth_params {
Subroutine _bind_sth_params: called CC=4 SCAR=13.9
499 162 my ($self, $sth, $bind, $bind_attrs) = @_;
500  
501 162 ▶ if (my $rewritten = $self->{_rewritten_binds}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rewritten = $self->{"_rewritten_binds"}) 73 89 162
502 73 my (@bind, @attrs);
503  
504 73 for my $bound (@{ $rewritten->{bound} }) {
505 67 ▶ if (ref $bound) { # the hex pieces of a binary value
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bound) { } 35 32 67
506 35 push @bind, map { [ {}, $_ ] } @$bound;
507 35 push @attrs, (undef) x @$bound;
508 }
509 else {
510 32 push @bind, $bind->[$bound];
511 32 push @attrs, $bind_attrs->[$bound];
512 }
513 }
514  
515 73 ($bind, $bind_attrs) = (\@bind, \@attrs);
516 }
517  
518 162 return $self->next::method($sth, $bind, $bind_attrs);
519 }
520  
521 # populate in void context: core prepares one statement from the first row
522 # and executes it once per row, with every value bound. A value over the
523 # placeholder size in any row cannot go that way, and neither can a value
524 # for a binary column, whose placeholders are as many as its size asks for.
525 # Such a data set is inserted row by row -- in one transaction, as the fast
526 # path does it.
527 14 ▶sub _insert_bulk {
Subroutine _insert_bulk: called CC=7 SCAR=19.5
528 14 my ($self, $source, $cols, $data) = @_;
529  
530 # No placeholders: core's fast path prepares one statement and executes it
531 # once per row, and throws when the values were interpolated into it
532 # instead (karr k57). Row by row, in one transaction, as the fast path does
533 # it. What the other branches below check is about binds, of which this
534 # class has none: core's literal is what it is.
535 14 ▶ return $self->_insert_rows_in_transaction($source, $cols, $data)
Branch: 1 branch
BranchTrueFalseTotal
if $self->isa("DBIO::Storage::DBI::NoBindVars") 3 11 14
536 if $self->isa('DBIO::Storage::DBI::NoBindVars');
537  
538 # refuses a value that is no binary value before the first row is sent
539 11 my (undef, $binary)
540 = $self->_bulk_binary_values_as_bytes($source, $cols, $data);
541  
542 # refuses a value beyond ASCII (karr k49) before the first row is sent. A
543 # binary value is not text and not looked at: it is in $binary, and its
544 # data set goes row by row anyway.
545 10 my @values = $self->_bulk_text_values($source, $cols, $data);
546  
547 10 for my $value (@values) {
548 34 ▶ $self->_refuse_non_ascii( $value->[0] ) if _non_ascii_bind( $value->[1] );
Branch: 1 branch
BranchTrueFalseTotal
if _non_ascii_bind($value->[1]) 2 32 34
549 }
550  
551 return $self->next::method($source, $cols, $data)
552 unless $binary
553 8 ▶ or grep { _over_placeholder_size( $_->[1] ) or _empty_bind( $_->[1] ) }
Branch: 1 branch
BranchTrueFalseTotal
unless $binary or grep {_over_placeholder_size($_->[1]) or _empty_bind($_->[1]);} @values 3 5 8
Condition100%6/6
$binary or grep {_over_placeholder_size($_->[1]) or _empty_bind($_->[1]);} @values
l!l&&r!l&&!r
233
_over_placeholder_size($_->[1]) || _empty_bind($_->[1])
l!l&&r!l&&!r
3222
MC/DC100%2/2
$binary or grep {_over_placeholder_size($_->[1]) or _empty_bind($_->[1]);} @values
$binary grep {_over_placeholder_size($_->[1]) or _empty_bind($_->[1]);} @values
MC/DC100%2/2
_over_placeholder_size($_->[1]) || _empty_bind($_->[1])
_over_placeholder_size($_->[1]) _empty_bind($_->[1])
Truth table100%3/3
A or B
A= $binaryB= grep {_over_placeholder_size($_->[1]) or _empty_bind($_->[1]);} @values
ABresult
1X1
011
000
Truth table100%3/3
A || B
A= _over_placeholder_size($_->[1])B= _empty_bind($_->[1])
ABresult
1X1
011
000
554 @values;
555  
556 5 return $self->_insert_rows_in_transaction($source, $cols, $data);
557 }
558  
559 8 ▶sub _insert_rows_in_transaction {
Subroutine _insert_rows_in_transaction: called CC=2 SCAR=6.9
560 8 my ($self, $source, $cols, $data) = @_;
561  
562 8 my $guard = $self->txn_scope_guard;
563  
564 8 for my $row (@$data) {
565 20 my %row;
566 20 @row{@$cols} = @$row;
567 20 $self->insert($source, \%row);
568 }
569  
570 7 $guard->commit;
571  
572 7 return scalar @$data;
573 }
574  
575 # The values of a populate data set that are text, each as [ column, value ]:
576 # a literal with binds, \[ $sql, @bind ], gives the values of its binds.
577 # Binary columns have none; with a binary value the data set is row by row
578 # in any case.
579 10 ▶sub _bulk_text_values {
Subroutine _bulk_text_values: called CC=6 SCAR=23.1
580 10 my ($self, $source, $cols, $data) = @_;
581  
582 10 my $colinfos = $source->columns_info($cols);
583 my @binary = map {
584 10 ▶ $self->_is_binary_data_type( $colinfos->{ $cols->[$_] }{data_type} ) ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_binary_data_type($$colinfos{$cols->[$_]}{'data_type'}) ? : 3 14 17
585 } 0 .. $#$cols;
586  
587 return map {
588 10 my $row = $_;
589 map {
590 24 my $i = $_;
591 0 ( ref $row->[$i] eq 'REF' and ref ${ $row->[$i] } eq 'ARRAY' )
592 0 ▶ ? ( map { [ $_->[0]{dbic_colname} // $cols->[$i], $_->[1] ] }
Condition0%0/3
$_->[0]{"dbic_colname"} // $cols->[$i]
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->[0]{"dbic_colname"} // $cols->[$i]
$_->[0]{"dbic_colname"} $cols->[$i]
Truth table0%0/3
A // B
A= $_->[0]{"dbic_colname"}B= $cols->[$i]
ABresult
1X1
011
000
593 0 grep { not $self->_is_binary_data_type( $_->[0]{sqlt_datatype} ) }
594 41 ▶ @{ $self->_resolve_bindattrs(
Branch: 1 branch
BranchTrueFalseTotal
ref $row->[$i] eq 'REF' && ref ${$row->[$i];} eq 'ARRAY' ? : 0 41 41
Condition33%1/3
ref $row->[$i] eq "REF" and ref ${$row->[$i];} eq "ARRAY"
!ll&&!rl&&r
4100
MC/DC0%0/2
ref $row->[$i] eq "REF" and ref ${$row->[$i];} eq "ARRAY"
ref $row->[$i] eq "REF" ref ${$row->[$i];} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $row->[$i] eq "REF"B= ref ${$row->[$i];} eq "ARRAY"
ABresult
0X0
100
111
595 0 ▶ $source, [ @{ ${ $row->[$i] } }[ 1 .. $#{ ${ $row->[$i] } } ] ],
Branch: 1 branch
BranchTrueFalseTotal
$binary[$i] ? : 7 34 41
596 $colinfos ) } )
597 : $binary[$i] ? ()
598 : [ $cols->[$i], $row->[$i] ];
599 } 0 .. $#$row;
600 } @$data;
601 }
602  
603 # karr k56. Reading a binary column. DBD::Sybase hands back a BINARY(n) or a
604 # VARBINARY(n) value as its hex digits, an IMAGE value too unless
605 # syb_binary_images is on -- and it has a buffer of ONE byte for a
606 # VARBINARY(MAX) column (the client library describes that as 2147483647
607 # bytes, and twice that plus two, for the hex digits, comes out as 0 in 32
608 # bits). The fetch of a row with a value in such a column fails, and the DBD
609 # drops a row whose fetch fails, without an error: the row seems not to exist.
610 #
611 # The one form in which this DBD carries the bytes of a binary value as they
612 # are is an IMAGE column with syb_binary_images on. So a column of a result
613 # source whose data type is a binary one is asked for as that:
614 #
615 # CAST([me].[body] AS IMAGE)
616 #
617 # Which entry of a select list is such a column is what its data type says,
618 # as for a bind value (_is_binary_data_type) -- not what comes back. Only an
619 # entry that is the column itself: a function, an expression and literal SQL
620 # stay what they are.
621 #
622 # Done here and not in _select_args, which _select_args_to_query (as_query)
623 # shares: a subquery keeps the column as it is. An IMAGE cannot be compared,
624 # and a condition on what a subquery selects is a comparison.
625 66 ▶sub _select {
Subroutine _select: called CC=3 SCAR=11.0
626 66 my $self = shift;
627  
628 66 my ($op, $ident, $select, $where, $attrs, @limit) = $self->_select_args(@_);
629  
630 66 ▶ my ($rewritten, $binary) = $self->_long_columns_guarded($ident, $select)
Branch: 1 branch
BranchTrueFalseTotal
() = $self->_long_columns_guarded($ident, $select) or return $self->_execute($op, $ident, $select, $where, $attrs, @limit) 47 19 66
631 or return $self->_execute($op, $ident, $select, $where, $attrs, @limit);
632  
633 # for _execute_sth below: this statement's IMAGE columns are to be bytes
634 47 ▶ local $self->{_binary_select} = $binary ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$binary ? : 42 5 47
635  
636 47 return $self->_execute(
637 $op, $ident, $rewritten, $where,
638 # the limit dialect builds its subqueries from the list in the attributes
639 { %$attrs, select => $rewritten },
640 @limit,
641 );
642 }
643  
644 # karr k47. The kind of column whose value the session's TEXTSIZE can cut, by
645 # the type it has ON THE SERVER (_server_data_type: what DBIO::MSSQL::DDL
646 # deploys the declared type as -- a 'text' column is an NVARCHAR(MAX), not a
647 # TEXT): 'binary' (read as an IMAGE, k56, whatever its size), 'ntext' (of
648 # which a CASE cannot return one), 'varchar_max' and 'nvarchar_max'. The
649 # sized VARCHAR(n) and NVARCHAR(n) are not subject to TEXTSIZE (measured: 8000
650 # bytes whole under a TEXTSIZE of 100), the XML type is not either (50007
651 # characters under 32768). A real TEXT cannot be declared (the portable
652 # 'text' is NVARCHAR(MAX)), so it has no kind.
653 185 ▶sub _long_column_kind {
Subroutine _long_column_kind: called CC=9 SCAR=22.0
654 185 my ($self, $info) = @_;
655  
656 185 my $type = $info->{data_type};
657 185 ▶ return undef unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 3 182 185
658  
659 182 ▶ return 'binary' if $self->_is_binary_data_type($type);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_binary_data_type($type) 52 130 182
660  
661 130 my $server = $self->_server_data_type($info);
662  
663 130 ▶ my ($name, $length) = $server =~ /\A \s* (\w+) \s* (?: \( \s* (\w+|-1) \s* \) )? \s* \z/x
Branch: 1 branch
BranchTrueFalseTotal
() = $server =~ /\A \s* (\w+) \s* (?: \( \s* (\w+|-1) \s* \) )? \s* \z/x or return undef 130 0 130
664 or return undef;
665  
666 130 ▶ return 'ntext' if $name eq 'ntext';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "ntext" 3 127 130
667  
668 127 ▶ return "${name}_max"
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "varchar" || $name eq "nvarchar" and defined $length and $length =~ /\A (?: -1 | max ) \z/xi 13 114 127
Condition100%9/9
$name eq "varchar" || $name eq "nvarchar" and defined $length and $length =~ /\A (?: -1 | max ) \z/xi
!ll&&!rl&&r
783613
$name eq "varchar" || $name eq "nvarchar" and defined $length
!ll&&!rl&&r
69949
$name eq "varchar" or $name eq "nvarchar"
l!l&&r!l&&!r
35569
MC/DC100%4/4
$name eq "varchar" || $name eq "nvarchar" and defined $length and $length =~ /\A (?: -1 | max ) \z/xi
$name eq "varchar" $name eq "nvarchar" defined $length $length =~ /\A (?: -1 | max ) \z/xi
Truth table85%6/7
A or B and C and D
A= $name eq "varchar"B= $name eq "nvarchar"C= defined $lengthD= $length =~ /\A (?: -1 | max ) \z/xi
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
669 if ( $name eq 'varchar' or $name eq 'nvarchar' )
670 and defined $length and $length =~ /\A (?: -1 | max ) \z/xi;
671  
672 114 return undef;
673 }
674  
675 # The select list with every column that the session's TEXTSIZE can cut asked
676 # for in a form that fails when it does, and every column of a binary data
677 # type as an IMAGE (karr k56); and whether there is a binary one. Nothing when
678 # there is no such column.
679 #
680 # karr k47. SET TEXTSIZE (the FreeTDS class sets it from LongReadLen, 32768
681 # bytes unless the connect info says otherwise) cuts a longer value of a TEXT,
682 # NTEXT, IMAGE, VARCHAR(MAX), NVARCHAR(MAX) or VARBINARY(MAX) column on the
683 # way back WITHOUT AN ERROR -- exactly at TEXTSIZE bytes: 32768 characters of
684 # a VARCHAR(MAX), 16384 of an NVARCHAR(MAX). The client cannot tell a value
685 # that was cut from one that is that long, the server can:
686 #
687 # CASE WHEN DATALENGTH(col) > @@TEXTSIZE
688 # THEN CONVERT(<type>, CONVERT(INT, 'message')) ELSE col END
689 #
690 # The conversion of the message to an integer is what fails, with Msg 245,
691 # and the server's text carries the message. Exact: a value of exactly
692 # TEXTSIZE bytes is whole and is not refused. Server side, so there is no
693 # fetch site that could miss it (cursor, single, get_column, ...).
694 #
695 # Only an entry that is the column itself, as for k56; not in a subquery.
696 66 ▶sub _long_columns_guarded {
Subroutine _long_columns_guarded: called CC=9 SCAR=22.0
697 66 my ($self, $ident, $select) = @_;
698  
699 66 ▶ return unless grep { defined $_ and not length ref $_ } @$select;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_ and not length ref $_;} @$select 9 57 66
Condition66%2/3
defined $_ && !length(ref $_)
!ll&&!rl&&r
012172
MC/DC50%1/2
defined $_ && !length(ref $_)
defined $_ !length(ref $_)
Truth table66%2/3
A && not(B)
A= defined $_B= !length(ref $_)
ABresult
0X0
100
111
700  
701 # of every column of every source in the FROM, as core resolves the type
702 # of a bind value: asked by name, a selected name that is no column of its
703 # source would throw
704 57 my $colinfos = $self->_resolve_column_info($ident);
705 57 my ($found, $binary);
706  
707 my @select = map {
708 57 my $kind = ( defined $_ and not length ref $_ and $colinfos->{$_} )
709 175 ▶ ? $self->_long_column_kind( $colinfos->{$_} )
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && !length(ref $_) && $colinfos->{$_} ? : 172 3 175
Condition66%4/6
defined $_ and not length ref $_ and $colinfos->{$_}
!ll&&!rl&&r
30172
defined $_ and not length ref $_
!ll&&!rl&&r
03172
MC/DC33%1/3
defined $_ and not length ref $_ and $colinfos->{$_}
defined $_ not length ref $_ $colinfos->{$_}
Truth table50%2/4
A and not(B) and C
A= defined $_B= not length ref $_C= $colinfos->{$_}
ABCresult
0XX0
10X0
1100
1111
710 : undef;
711  
712 175 ▶ if ($kind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind) { } 60 115 175
713 60 $found = 1;
714 60 ▶ $binary = 1 if $kind eq 'binary';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "binary" 50 10 60
715 60 \( $self->_guarded_long_column($_, $colinfos->{$_}, $kind) );
716 }
717 else {
718 115 $_;
719 }
720 } @$select;
721  
722 57 ▶ return $found ? ( \@select, $binary ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 47 10 57
723 }
724  
725 # What the failing conversion carries to the caller; _execute_sth reads it.
726 16 my $TEXTSIZE_EXCEEDED = 'DBIO::MSSQL TEXTSIZE guard: column';
727  
728 # The failing conversion of the guard reaches the caller as the server's
729 # Msg 245, from the execute or -- when the row that does not fit comes after
730 # others -- from a fetch. Every error of this storage goes through
731 # throw_exception, so the guard's message is put into words here, whichever
732 # call it came from.
733 72 ▶sub throw_exception {
Subroutine throw_exception: called CC=4 SCAR=13.9
Pod documented
734 72 my ($self, $message, @rest) = @_;
735  
736 72 ▶ if ( defined $message and not ref $message
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message and not ref $message and $message =~ /\Q$TEXTSIZE_EXCEEDED\E '([^']*)' holds a value longer than the session's TEXTSIZE, (\d+) bytes/) 1 71 72
Condition83%5/6
defined $message and not ref $message and $message =~ /\Q$TEXTSIZE_EXCEEDED\E '([^']*)' holds a value longer than the session's TEXTSIZE, (\d+) bytes/
!ll&&!rl&&r
33381
defined $message and not ref $message
!ll&&!rl&&r
03339
MC/DC66%2/3
defined $message and not ref $message and $message =~ /\Q$TEXTSIZE_EXCEEDED\E '([^']*)' holds a value longer than the session's TEXTSIZE, (\d+) bytes/
defined $message not ref $message $message =~ /\Q$TEXTSIZE_EXCEEDED\E '([^']*)' holds a value longer than the session's TEXTSIZE, (\d+) bytes/
Truth table75%3/4
A and not(B) and C
A= defined $messageB= not ref $messageC= $message =~ /\Q$TEXTSIZE_EXCEEDED\E '([^']*)' holds a value longer than the session's TEXTSIZE, (\d+) bytes/
ABCresult
0XX0
10X0
1100
1111
737 and $message =~ /\Q$TEXTSIZE_EXCEEDED\E '([^']*)' holds a value longer than the session's TEXTSIZE, (\d+) bytes/
738 ) {
739 1 $message = sprintf
740 'Cannot read this result through DBD::Sybase: the column \'%s\' holds a '
741 . 'value longer than the TEXTSIZE of the session, %d bytes. DBD::Sybase '
742 . 'would return it cut to that size, without an error. The FreeTDS '
743 . 'class sets TEXTSIZE from LongReadLen of the connect info (32768 '
744 . 'unless that says otherwise): connect with a LongReadLen of at '
745 . 'least the longest value, in bytes. An empty value of a binary '
746 . 'column costs the client LongReadLen bytes of memory (see '
747 . '"VALUES LONGER THAN TEXTSIZE").',
748 $1, $2;
749 }
750  
751 72 return $self->next::method($message, @rest);
752 }
753  
754 65 ▶sub _guarded_long_column {
Subroutine _guarded_long_column: called CC=4 SCAR=13.9
755 65 my ($self, $name, $info, $kind) = @_;
756  
757 65 my $column = $self->sql_maker->_quote($name);
758  
759 65 ( my $label = $name ) =~ s/'/''/g;
760 # Short: it is in the text of every statement that reads the column. The
761 # exception that reaches the caller says the rest (throw_exception).
762 65 my $message = "'$TEXTSIZE_EXCEEDED ''$label'' holds a value longer than "
763 . "the session''s TEXTSIZE, ' + CONVERT(VARCHAR(20), \@\@TEXTSIZE) "
764 . "+ ' bytes'";
765  
766 65 my $fail = "CONVERT(INT, $message)";
767  
768 65 my $too_long = "DATALENGTH($column) > \@\@TEXTSIZE";
769  
770 65 ▶ return "CAST(CASE WHEN $too_long THEN CONVERT(VARBINARY(MAX), $fail) "
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "binary" 50 15 65
771 . "ELSE CAST($column AS VARBINARY(MAX)) END AS IMAGE)"
772 if $kind eq 'binary';
773  
774 # A CASE returns one type, the higher of its branches. For an NTEXT that is
775 # the MAX type it is cast to (a CASE cannot return the NTEXT itself).
776 15 ▶ return "CASE WHEN $too_long THEN CONVERT(NVARCHAR(MAX), $fail) "
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "ntext" 3 12 15
777 . "ELSE CAST($column AS NVARCHAR(MAX)) END"
778 if $kind eq 'ntext';
779  
780 # VARCHAR(MAX) and NVARCHAR(MAX) must stay the type they are: the TEXTSIZE
781 # cuts the value as the type the statement returns, and a CASE between a
782 # VARCHAR and an NVARCHAR branch would hand it over as an NVARCHAR, cut at
783 # half as many characters than the guard looks for (measured: a VARCHAR(MAX)
784 # value of 20000 characters came back as 16384, without an error). The type
785 # is known (the server's, _server_data_type), so the failing branch is of
786 # that type and the CASE returns the column itself. No concatenation: under
787 # CONCAT_NULL_YIELDS_NULL OFF that turns a NULL into '' (k47). Here a NULL
788 # has no length, the condition is unknown and the ELSE hands the NULL over.
789 12 ▶ my $type = $kind eq 'nvarchar_max' ? 'NVARCHAR(MAX)' : 'VARCHAR(MAX)';
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'nvarchar_max' ? : 9 3 12
790  
791 12 return "CASE WHEN $too_long THEN CONVERT($type, $fail) ELSE $column END";
792 }
793  
794 # What a result column of unlimited binary size is to DBI on this DBD: the
795 # type and the size the client library describes VARBINARY(MAX) with.
796 16 my $SQL_BINARY = -2;
797 16 my $UNLIMITED_BINARY = 2147483647;
798  
799 # And SQL_VARIANT: a CHAR of 8009 (karr k59).
800 16 my $SQL_CHAR = 1;
801 16 my $SQL_VARIANT_PRECISION = 8009;
802  
803 # Two things around the execute of a statement.
804 #
805 # syb_binary_images, for a statement _select has rewritten and for nothing
806 # else: the DBD decides how an IMAGE column is fetched when it describes the
807 # result, which is in the execute. The handle is as it was afterwards, so what
808 # hand-written SQL and dbh_do read from an IMAGE column is what it was.
809 #
810 # And a result column this DBD cannot read is refused, loudly. That is every
811 # VARBINARY(MAX) column _select did not rewrite -- a function, an expression,
812 # literal SQL, a column whose data type is not declared as a binary one.
813 # Decided by how the handle describes the column, before a row is fetched
814 # and whatever the rows hold: with NULLs and empty values alone the result
815 # would be right, with the first value it would be short.
816 162 ▶sub _execute_sth {
Subroutine _execute_sth: called CC=18 SCAR=29.9
817 162 my ($self, $dbh, $sth) = @_;
818  
819 162 ▶ local $dbh->{syb_binary_images} = 1 if $self->{_binary_select};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_binary_select"} 40 122 162
820  
821 162 my @executed = $self->next::method($dbh, $sth);
822  
823 # nothing to look at for an INSERT, an UPDATE, a DELETE, DDL
824 158 ▶ return @executed unless $sth->{NUM_OF_FIELDS};
Branch: 1 branch
BranchTrueFalseTotal
unless $sth->{"NUM_OF_FIELDS"} 105 53 158
825  
826 53 ▶ my ($types, $sizes) = map { $sth->{$_} || [] } qw(TYPE PRECISION);
Condition100%2/2
$sth->{$_} || []
l!l
1042
MC/DC100%1/1
$sth->{$_} || []
$sth->{$_}
Truth table100%2/2
A
A= $sth->{$_}
Aresult
11
00
827  
828 # karr k59. A SQL_VARIANT column is described as a CHAR of 8009 (8000 and
829 # the 9 bytes of its header; no CHAR, VARCHAR, NCHAR or NVARCHAR has that
830 # precision: the client library gives 4 times the length). A variant with a
831 # binary base type loses its row, silently -- or comes back as hex digits
832 # when other rows were fetched before it (measured). What it holds is not
833 # known before the rows are.
834 my ($variant) = grep {
835 53 ▶ ( $types->[$_] || 0 ) == $SQL_CHAR
Condition71%5/7
($types->[$_] || 0) == $SQL_CHAR && ($sizes->[$_] || 0) == $SQL_VARIANT_PRECISION
!ll&&!rl&&r
7462
$types->[$_] || 0
l!l
820
$sizes->[$_] || 0
l!l
80
MC/DC0%0/2
($types->[$_] || 0) == $SQL_CHAR && ($sizes->[$_] || 0) == $SQL_VARIANT_PRECISION
$types->[$_] $sizes->[$_]
Truth table33%1/3
A && B
A= $types->[$_]B= $sizes->[$_]
ABresult
0X0
100
111
836 and ( $sizes->[$_] || 0 ) == $SQL_VARIANT_PRECISION
837 } 0 .. $#$types;
838  
839 53 ▶ if (defined $variant) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $variant) 2 51 53
840 2 ▶ my $name = ( $sth->{NAME} || [] )->[$variant];
Condition50%1/2
$sth->{'NAME'} || []
l!l
20
MC/DC0%0/1
$sth->{'NAME'} || []
$sth->{'NAME'}
Truth table50%1/2
A
A= $sth->{'NAME'}
Aresult
11
00
841  
842 2 $sth->finish;
843  
844 2 ▶ $self->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
defined $name && length $name ? : 2 0 2
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
002
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
845 'Cannot read this result through DBD::Sybase: its column %d%s is '
846 . 'SQL_VARIANT, and DBD::Sybase drops the row of a value with a binary '
847 . 'base type without an error (the row seems not to exist), or returns '
848 . 'its hex digits as text when rows were fetched before it. Which of '
849 . 'the two cannot be told before the rows are. Select a form of the '
850 . 'value that does not depend on its base type: '
851 . 'CONVERT(VARCHAR(8000), <expression>) for text, numbers and dates, '
852 . 'CONVERT(VARCHAR(MAX), CONVERT(VARBINARY(8000), <expression>), 2) for '
853 . 'the hex digits of a binary value; SQL_VARIANT_PROPERTY(<expression>, '
854 . "'BaseType') says which. DBD::ODBC reads the column.",
855 $variant + 1,
856 ( defined $name and length $name ) ? " ('$name')" : '',
857 );
858 }
859  
860 my ($unreadable) = grep {
861 51 ▶ ( $types->[$_] || 0 ) == $SQL_BINARY
Branch: 1 branch
BranchTrueFalseTotal
my($unreadable) = grep({($types->[$_] || 0) == $SQL_BINARY and ($sizes->[$_] || 0) == $UNLIMITED_BINARY;} 0 .. $types->$#*) or return @executed 5 46 51
Condition71%5/7
($types->[$_] || 0) == $SQL_BINARY && ($sizes->[$_] || 0) == $UNLIMITED_BINARY
!ll&&!rl&&r
7125
$types->[$_] || 0
l!l
780
$sizes->[$_] || 0
l!l
70
MC/DC0%0/2
($types->[$_] || 0) == $SQL_BINARY && ($sizes->[$_] || 0) == $UNLIMITED_BINARY
$types->[$_] $sizes->[$_]
Truth table33%1/3
A && B
A= $types->[$_]B= $sizes->[$_]
ABresult
0X0
100
111
862 and ( $sizes->[$_] || 0 ) == $UNLIMITED_BINARY
863 } 0 .. $#$types
864 or return @executed;
865  
866 5 ▶ my $name = ( $sth->{NAME} || [] )->[$unreadable];
Condition50%1/2
$sth->{'NAME'} || []
l!l
50
MC/DC0%0/1
$sth->{'NAME'} || []
$sth->{'NAME'}
Truth table50%1/2
A
A= $sth->{'NAME'}
Aresult
11
00
867  
868 5 $sth->finish;
869  
870 5 ▶ $self->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
defined $name && length $name ? : 4 1 5
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
014
MC/DC50%1/2
defined $name and length $name
defined $name length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
871 'Cannot read this result through DBD::Sybase: its column %d%s is '
872 . 'VARBINARY(MAX), and DBD::Sybase drops every row that has a value of '
873 . 'one byte or more in such a column, without an error -- the rows would '
874 . 'seem not to exist. A column of a result source is read when its '
875 . 'data_type is declared as a binary one and it is selected as the column '
876 . 'itself, not through a function or literal SQL: the storage then asks '
877 . 'for it in a form DBD::Sybase carries. For anything else, select the '
878 . 'hex digits of '
879 . 'the value, CONVERT(VARCHAR(MAX), <expression>, 2): they arrive up to '
880 . 'the TEXTSIZE of the session in characters, half as many bytes, and are '
881 . 'cut beyond that without an error.',
882 $unreadable + 1,
883 ( defined $name and length $name ) ? " ('$name')" : '',
884 );
885 }
886  
887  
888 0 ▶sub connect_call_datetime_setup {
Subroutine connect_call_datetime_setup: not called CC=2 SCAR=17.9
Pod documented
889 0 my $self = shift;
890 0 my $dbh = $self->_get_dbh;
891  
892 0 ▶ if ($dbh->can('syb_date_fmt')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->can('syb_date_fmt')) { } 0 0 0
893 # amazingly, this works with FreeTDS
894 0 $dbh->syb_date_fmt('ISO_strict');
895 }
896 else{
897 0 carp_once
898 'Your DBD::Sybase is too old to support '
899 . 'DBIO::InflateColumn::DateTime, please upgrade!';
900 }
901 }
902  
903  
904 16 1;
905  
906 __END__
« MSSQL/Storage/ODBC.pm ↑ Summary MSSQL/Diff/Column.pm »