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