Commit | Line | Data |
d047d650 |
1 | package DBIx::Class::Storage::DBI::AutoCast; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use base qw/DBIx::Class::Storage::DBI/; |
7 | use mro 'c3'; |
8 | |
9 | __PACKAGE__->mk_group_accessors('simple' => 'auto_cast' ); |
10 | |
11 | =head1 NAME |
12 | |
d7d812cf |
13 | DBIx::Class::Storage::DBI::AutoCast - Storage component for RDBMS requiring explicit placeholder typing |
d047d650 |
14 | |
15 | =head1 SYNOPSIS |
16 | |
17 | $schema->storage->auto_cast(1); |
18 | |
19 | =head1 DESCRIPTION |
20 | |
ce012195 |
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. |
d047d650 |
24 | |
25 | As long as a column L<data_type|DBIx::Class::ResultSource/add_columns> is |
5529838f |
26 | defined and resolves to a base RDBMS native type via |
27 | L<_native_data_type|DBIx::Class::Storage::DBI/_native_data_type> as |
ce012195 |
28 | defined in your Storage driver, the placeholder for this column will be |
29 | converted to: |
d047d650 |
30 | |
ce012195 |
31 | CAST(? as $mapped_type) |
d047d650 |
32 | |
8384a713 |
33 | This option can also be enabled in |
34 | L<connect_info|DBIx::Class::Storage::DBI/connect_info> as: |
d867eeda |
35 | |
36 | on_connect_call => ['set_auto_cast'] |
37 | |
d047d650 |
38 | =cut |
39 | |
40 | sub _prep_for_execute { |
41 | my $self = shift; |
d047d650 |
42 | |
43 | my ($sql, $bind) = $self->next::method (@_); |
44 | |
45 | # If we're using ::NoBindVars, there are no binds by this point so this code |
4a0eed52 |
46 | # gets skipped. |
d047d650 |
47 | if ($self->auto_cast && @$bind) { |
48 | my $new_sql; |
0e773352 |
49 | my @sql_part = split /\?/, $sql, scalar @$bind + 1; |
50 | for (@$bind) { |
51 | my $cast_type = $self->_native_data_type($_->[0]{sqlt_datatype}); |
52 | $new_sql .= shift(@sql_part) . ($cast_type ? "CAST(? AS $cast_type)" : '?'); |
d047d650 |
53 | } |
0e773352 |
54 | $sql = $new_sql . shift @sql_part; |
d047d650 |
55 | } |
56 | |
57 | return ($sql, $bind); |
58 | } |
59 | |
d867eeda |
60 | =head2 connect_call_set_auto_cast |
61 | |
62 | Executes: |
63 | |
64 | $schema->storage->auto_cast(1); |
65 | |
66 | on connection. |
67 | |
68 | Used as: |
69 | |
70 | on_connect_call => ['set_auto_cast'] |
71 | |
8384a713 |
72 | in L<connect_info|DBIx::Class::Storage::DBI/connect_info>. |
d867eeda |
73 | |
74 | =cut |
75 | |
76 | sub connect_call_set_auto_cast { |
77 | my $self = shift; |
78 | $self->auto_cast(1); |
79 | } |
d047d650 |
80 | |
a2bd3796 |
81 | =head1 FURTHER QUESTIONS? |
d047d650 |
82 | |
a2bd3796 |
83 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. |
d047d650 |
84 | |
a2bd3796 |
85 | =head1 COPYRIGHT AND LICENSE |
d047d650 |
86 | |
a2bd3796 |
87 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> |
88 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can |
89 | redistribute it and/or modify it under the same terms as the |
90 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. |
d047d650 |
91 | |
92 | =cut |
93 | |
94 | 1; |