rename method and add docs
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / AutoCast.pm
CommitLineData
d047d650 1package DBIx::Class::Storage::DBI::AutoCast;
2
3use strict;
4use warnings;
5
6use base qw/DBIx::Class::Storage::DBI/;
7use mro 'c3';
8
9__PACKAGE__->mk_group_accessors('simple' => 'auto_cast' );
10
11=head1 NAME
12
13DBIx::Class::Storage::DBI::AutoCast
14
15=head1 SYNOPSIS
16
17 $schema->storage->auto_cast(1);
18
19=head1 DESCRIPTION
20
ce012195 21In some combinations of RDBMS and DBD drivers (e.g. FreeTDS and Sybase)
22statements with values bound to columns or conditions that are not strings will
23throw implicit type conversion errors.
d047d650 24
25As long as a column L<data_type|DBIx::Class::ResultSource/add_columns> is
ce012195 26defined, and it resolves to a base RDBMS native type via L</_map_data_type> as
27defined in your Storage driver, the placeholder for this column will be
28converted to:
d047d650 29
ce012195 30 CAST(? as $mapped_type)
d047d650 31
32=cut
33
34sub _prep_for_execute {
35 my $self = shift;
36 my ($op, $extra_bind, $ident, $args) = @_;
37
38 my ($sql, $bind) = $self->next::method (@_);
39
40# If we're using ::NoBindVars, there are no binds by this point so this code
41# gets skippeed.
42 if ($self->auto_cast && @$bind) {
43 my $new_sql;
44 my @sql_part = split /\?/, $sql;
45 my $col_info = $self->_resolve_column_info($ident,[ map $_->[0], @$bind ]);
46
47 foreach my $bound (@$bind) {
48 my $col = $bound->[0];
ce012195 49 my $type = $self->_map_data_type($col_info->{$col}{data_type});
d047d650 50
51 foreach my $data (@{$bound}[1..$#$bound]) { # <--- this will multiply the amount of ?'s no...?
52 $new_sql .= shift(@sql_part) .
ce012195 53 ($type ? "CAST(? AS $type)" : '?');
d047d650 54 }
55 }
56 $new_sql .= join '', @sql_part;
57 $sql = $new_sql;
58 }
59
60 return ($sql, $bind);
61}
62
63
64=head1 AUTHORS
65
66See L<DBIx::Class/CONTRIBUTORS>
67
68=head1 LICENSE
69
70You may distribute this code under the same terms as Perl itself.
71
72=cut
73
741;