| 1 | package DBIx::Class::Storage::DBI::AutoCast; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base qw/DBIx::Class::Storage::DBI/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | __PACKAGE__->mk_group_accessors('simple' => 'auto_cast' ); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | DBIx::Class::Storage::DBI::AutoCast - Storage component for RDBMS requiring explicit placeholder typing | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | $schema->storage->auto_cast(1); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | In some combinations of RDBMS and DBD drivers (e.g. FreeTDS and Sybase) | ||||||||||||||||||||||||||||
| 22 | statements with values bound to columns or conditions that are not strings will | ||||||||||||||||||||||||||||
| 23 | throw implicit type conversion errors. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | As long as a column L<data_type|DBIx::Class::ResultSource/add_columns> is | ||||||||||||||||||||||||||||
| 26 | defined and resolves to a base RDBMS native type via L</_native_data_type> as | ||||||||||||||||||||||||||||
| 27 | defined in your Storage driver, the placeholder for this column will be | ||||||||||||||||||||||||||||
| 28 | converted to: | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | CAST(? as $mapped_type) | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This option can also be enabled in | ||||||||||||||||||||||||||||
| 33 | L<connect_info|DBIx::Class::Storage::DBI/connect_info> as: | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | on_connect_call => ['set_auto_cast'] | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub _prep_for_execute { | ||||||||||||||||||||||||||||
| 40 | 25 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_prep_for_execute: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 25 | my ($sql, $bind) = $self->next::method (@_); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # If we're using ::NoBindVars, there are no binds by this point so this code | ||||||||||||||||||||||||||||
| 45 | # gets skipped. | ||||||||||||||||||||||||||||
| 46 | 25 | ▶ | if ($self->auto_cast && @$bind) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->auto_cast and @$bind
MC/DC50%1/2
$self->auto_cast and @$bind
$self->auto_cast
@$bind
Truth table66%2/3
A and B
A= $self->auto_castB= @$bind
| |||||||||||||||||||||||||||||
| 47 | 1 | my $new_sql; | |||||||||||||||||||||||||||
| 48 | 1 | my @sql_part = split /\?/, $sql, scalar @$bind + 1; | |||||||||||||||||||||||||||
| 49 | 1 | for (@$bind) { | |||||||||||||||||||||||||||
| 50 | 4 | my $cast_type = $self->_native_data_type($_->[0]{sqlt_datatype}); | |||||||||||||||||||||||||||
| 51 | 4 | ▶ | $new_sql .= shift(@sql_part) . ($cast_type ? "CAST(? AS $cast_type)" : '?'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 1 | $sql = $new_sql . shift @sql_part; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 25 | return ($sql, $bind); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head2 connect_call_set_auto_cast | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Executes: | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | $schema->storage->auto_cast(1); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | on connection. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Used as: | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | on_connect_call => ['set_auto_cast'] | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | in L<connect_info|DBIx::Class::Storage::DBI/connect_info>. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub connect_call_set_auto_cast { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
connect_call_set_auto_cast: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 0 | $self->auto_cast(1); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | See L<DBIx::Class/CONTRIBUTORS> | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | You may distribute this code under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 3 | 1; | |||||||||||||||||||||||||||