Make sure MultiColumnIn quotes column names while munging literal sql
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / MultiColumnIn.pm
CommitLineData
4ce3b851 1package DBIx::Class::Storage::DBI::MultiColumnIn;
2
3use strict;
4use warnings;
5
6use base 'DBIx::Class::Storage::DBI';
7
8=head1 NAME
9
10DBIx::Class::Storage::DBI::MultiColumnIn - Storage component for RDBMS supporting multicolumn in clauses
11
12=head1 DESCRIPTION
13
14While ANSI SQL does not define a multicolumn in operator, many databases can
15in fact understand WHERE (cola, colb) IN ( SELECT subcol_a, subcol_b ... )
16The storage class for any such RDBMS should inherit from this class, in order
17to dramatically speed up update/delete operations on joined multipk resultsets.
18
19At this point the only overriden method is C<_multipk_update_delete()>
20
21=cut
22
23sub _multipk_update_delete {
24 my $self = shift;
25 my ($rs, $op, $values) = @_;
26
27 my $rsrc = $rs->result_source;
28 my @pcols = $rsrc->primary_columns;
29 my $attrs = $rs->_resolved_attrs;
30
31 # naive check - this is an internal method after all, we should know what we are doing
32 $self->throw_exception ('Number of columns selected by supplied resultset does not match number of primary keys')
33 if ( ref $attrs->{select} ne 'ARRAY' or @{$attrs->{select}} != @pcols );
34
35 # This is hideously ugly, but SQLA does not understand multicol IN expressions
9f6b5584 36 my $sqla = $self->_sql_maker;
4ce3b851 37 my ($sql, @bind) = @${$rs->as_query};
9f6b5584 38 $sql = sprintf ('(%s) IN %s', # the as_query stuff is already enclosed in ()s
39 join (', ', map { $sqla->_quote ($_) } @pcols),
4ce3b851 40 $sql,
41 );
42
43 return $self->$op (
44 $rsrc,
45 $op eq 'update' ? $values : (),
46 \[$sql, @bind],
47 );
48
49}
50
51=head1 AUTHORS
52
53See L<DBIx::Class/CONTRIBUTORS>
54
55=head1 LICENSE
56
57You may distribute this code under the same terms as Perl itself.
58
59=cut
60
611;