Merge 'trunk' into 'sybase'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / NoBindVars.pm
CommitLineData
3885cff6 1package DBIx::Class::Storage::DBI::NoBindVars;
2
3use strict;
4use warnings;
5
6use base 'DBIx::Class::Storage::DBI';
2ad62d97 7use mro 'c3';
3885cff6 8
b43345f2 9=head1 NAME
10
11DBIx::Class::Storage::DBI::NoBindVars - Sometime DBDs have poor to no support for bind variables
12
13=head1 DESCRIPTION
14
15This class allows queries to work when the DBD or underlying library does not
16support the usual C<?> placeholders, or at least doesn't support them very
17well, as is the case with L<DBD::Sybase>
18
19=head1 METHODS
20
b33697ef 21=head2 connect_info
b43345f2 22
b33697ef 23We 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.
b43345f2 24
25=cut
26
b33697ef 27sub connect_info {
28 my $self = shift;
d944c5ae 29 my $retval = $self->next::method(@_);
b33697ef 30 $self->disable_sth_caching(1);
31 $retval;
b43345f2 32}
33
d5130dd2 34=head2 _prep_for_execute
b43345f2 35
d5130dd2 36Manually subs in the values for the usual C<?> placeholders.
b43345f2 37
38=cut
39
d5130dd2 40sub _prep_for_execute {
41 my $self = shift;
b50a5275 42
0c449973 43 my ($op, $extra_bind, $ident, $args) = @_;
b50a5275 44
d944c5ae 45 my ($sql, $bind) = $self->next::method(@_);
46
47 # stringify args, quote via $dbh, and manually insert
48
b4474f31 49 my @sql_part = split /\?/, $sql;
50 my $new_sql;
51
434cace9 52 my $alias2src = $self->_resolve_ident_sources($ident);
a49fe312 53
d944c5ae 54 foreach my $bound (@$bind) {
b50a5275 55 my $col = shift @$bound;
b55e97a7 56
6636ad53 57 my $name_sep = $self->_sql_maker_opts->{name_sep} || '.';
a49fe312 58
59 $col =~ s/^([^\Q${name_sep}\E]*)\Q${name_sep}\E//;
17d750d7 60 my $alias = $1 || 'me';
b55e97a7 61
434cace9 62 my $rsrc = $alias2src->{$alias};
a49fe312 63
bbdc039b 64 my $datatype = $rsrc && $rsrc->column_info($col)->{data_type};
b55e97a7 65
d944c5ae 66 foreach my $data (@$bound) {
7d17f469 67 $data = ''.$data if ref $data;
6636ad53 68
7d17f469 69 $data = $self->_dbh->quote($data)
70 if $self->should_quote_value($datatype, $data);
6636ad53 71
7d17f469 72 $new_sql .= shift(@sql_part) . $data;
d944c5ae 73 }
74 }
b4474f31 75 $new_sql .= join '', @sql_part;
d5130dd2 76
01c04b1b 77 return ($new_sql, []);
3885cff6 78}
79
7d17f469 80=head2 should_quote_value
0c1bedfc 81
148e3b50 82This method is called by L</_prep_for_execute> for every column in
83order to determine if its value should be quoted or not. The arguments
84are the current column data type and the actual bind value. The return
85value is interpreted as: true - do quote, false - do not quote. You should
86override this in you Storage::DBI::<database> subclass, if your RDBMS
87does not like quotes around certain datatypes (e.g. Sybase and integer
88columns). The default method always returns true (do quote).
0c1bedfc 89
90 WARNING!!!
91
148e3b50 92 Always validate that the bind-value is valid for the current datatype.
93 Otherwise you may very well open the door to SQL injection attacks.
0c1bedfc 94
95=cut
96
7d17f469 97sub should_quote_value { 1 }
148e3b50 98
3885cff6 99=head1 AUTHORS
100
101Brandon Black <blblack@gmail.com>
b43345f2 102
7762b22c 103Trym Skaar <trym@tryms.no>
3885cff6 104
105=head1 LICENSE
106
107You may distribute this code under the same terms as Perl itself.
108
109=cut
b43345f2 110
1111;