Minaor speedup
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / NoBindVars.pm
1 package DBIx::Class::Storage::DBI::NoBindVars;
2
3 use strict;
4 use warnings;
5
6 use base 'DBIx::Class::Storage::DBI';
7 use mro 'c3';
8
9 =head1 NAME 
10
11 DBIx::Class::Storage::DBI::NoBindVars - Sometime DBDs have poor to no support for bind variables
12
13 =head1 DESCRIPTION
14
15 This class allows queries to work when the DBD or underlying library does not
16 support the usual C<?> placeholders, or at least doesn't support them very
17 well, as is the case with L<DBD::Sybase>
18
19 =head1 METHODS
20
21 =head2 connect_info
22
23 We can't cache very effectively without bind variables, so force the C<disable_sth_caching> setting to be turned on when the connect info is set.
24
25 =cut
26
27 sub connect_info {
28     my $self = shift;
29     my $retval = $self->next::method(@_);
30     $self->disable_sth_caching(1);
31     $retval;
32 }
33
34 =head2 _prep_for_execute
35
36 Manually subs in the values for the usual C<?> placeholders.
37
38 =cut
39
40 sub _prep_for_execute {
41   my $self = shift;
42
43   my ($sql, $bind) = $self->next::method(@_);
44
45   # stringify bind args, quote via $dbh, and manually insert
46   #my ($op, $extra_bind, $ident, $args) = @_;
47   my $ident = $_[2];
48
49   my @sql_part = split /\?/, $sql;
50   my $new_sql;
51
52   my $col_info = $self->_resolve_column_info($ident, [ map $_->[0], @$bind ]);
53
54   foreach my $bound (@$bind) {
55     my $col = shift @$bound;
56
57     my $datatype = $col_info->{$col}{data_type};
58
59     foreach my $data (@$bound) {
60       $data = ''.$data if ref $data;
61
62       $data = $self->transform_unbound_value($datatype, $data)
63         if $datatype;
64
65       $data = $self->_dbh->quote($data)
66         if (!$datatype || $self->should_quote_value($datatype, $data));
67
68       $new_sql .= shift(@sql_part) . $data;
69     }
70   }
71   $new_sql .= join '', @sql_part;
72
73   return ($new_sql, []);
74 }
75
76 =head2 should_quote_value
77
78 This method is called by L</_prep_for_execute> for every column in
79 order to determine if its value should be quoted or not. The arguments
80 are the current column data type and the actual bind value. The return
81 value is interpreted as: true - do quote, false - do not quote. You should
82 override this in you Storage::DBI::<database> subclass, if your RDBMS
83 does not like quotes around certain datatypes (e.g. Sybase and integer
84 columns). The default method always returns true (do quote).
85
86  WARNING!!!                     
87
88  Always validate that the bind-value is valid for the current datatype.
89  Otherwise you may very well open the door to SQL injection attacks.
90
91 =cut                            
92
93 sub should_quote_value { 1 }
94
95 =head2 transform_unbound_value
96
97 Given a datatype and the value to be inserted directly into a SQL query, returns
98 the necessary SQL fragment to represent that value.
99
100 =cut
101
102 sub transform_unbound_value { $_[2] }
103
104 =head1 AUTHORS
105
106 Brandon Black <blblack@gmail.com>
107
108 Trym Skaar <trym@tryms.no>
109
110 =head1 LICENSE
111
112 You may distribute this code under the same terms as Perl itself.
113
114 =cut
115
116 1;