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