# Genarating a single PK column subquery is trivial and supported
# by all RDBMS. However if we have a multicolumn PK, things get ugly.
# Look at _multipk_update_delete()
-sub subq_update_delete {
+sub _subq_update_delete {
my $self = shift;
my ($rs, $op, $values) = @_;
}
$sub_attrs->{group_by} ||= [ map { "$attrs->{alias}.$_" } ($source->primary_columns) ];
- $sub_attrs->{select} = $self->_grouped_count_select ($sub_attrs);
+ $sub_attrs->{select} = $self->_grouped_count_select ($source, $sub_attrs);
$attrs->{from} = [{
count_subq => $source->resultset_class->new ($source, $sub_attrs )->as_query
# choke in various ways.
#
sub _grouped_count_select {
- my ($self, $attrs) = @_;
- return $attrs->{group_by};
+ my ($self, $source, $rs_args) = @_;
+ return $rs_args->{group_by};
}
sub source_bind_attributes {
# MySql can not do subquery update/deletes, only way is slow per-row operations.
# This assumes you have set proper transaction isolation and use innodb.
-sub subq_update_delete {
+sub _subq_update_delete {
return shift->_per_row_update_delete (@_);
}
+# MySql chokes on things like:
+# COUNT(*) FROM (SELECT tab1.col, tab2.col FROM tab1 JOIN tab2 ... )
+# claiming that col is a duplicate column (it loses the table specifiers by
+# the time it gets to the *). Thus for any subquery count we select only the
+# primary keys of the main table in the inner query. This hopefully still
+# hits the indexes and keeps mysql happy.
+# (mysql does not care if the SELECT and the GROUP BY match)
+sub _grouped_count_select {
+ my ($self, $source, $rs_args) = @_;
+ my @pcols = map { join '.', $rs_args->{alias}, $_ } ($source->primary_columns);
+ return @pcols ? \@pcols : $rs_args->{group_by};
+}
+
1;
=head1 NAME
plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
unless ($dsn && $user);
-plan tests => 23;
+plan tests => 19;
my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
);
my $owners2 = $schema->resultset ('Owners')->search ({ id => { -in => $owners->get_column ('me.id')->as_query }});
for ($owners, $owners2) {
- lives_ok { is ($_->all, 2, 'Prefetched grouped search returns correct number of rows') }
- || skip ('No test due to exception', 1);
- lives_ok { is ($_->count, 2, 'Prefetched grouped search returns correct count') }
- || skip ('No test due to exception', 1);
+ is ($_->all, 2, 'Prefetched grouped search returns correct number of rows');
+ is ($_->count, 2, 'Prefetched grouped search returns correct count');
}
- TODO: {
- # try a ->prefetch direction (no select collapse)
- my $books = $schema->resultset ('BooksInLibrary')->search (
- { 'owner.name' => 'wiggle' },
- { prefetch => 'owner', distinct => 1 }
- );
-
- local $TODO = 'MySQL is crazy - there seems to be no way to make this work';
- # error thrown is:
- # Duplicate column name 'id' [for Statement "
- # SELECT COUNT( * )
- # FROM (
- # SELECT me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name
- # FROM books me
- # JOIN owners owner ON owner.id = me.owner
- # WHERE ( ( owner.name = ? AND source = ? ) )
- # GROUP BY me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name
- # ) count_subq
- # " with ParamValues: 0='wiggle', 1='Library']
- #
- # go fucking figure
-
- my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }});
- for ($books, $books2) {
- lives_ok { is ($_->all, 1, 'Prefetched grouped search returns correct number of rows') }
- || skip ('No test due to exception', 1);
- lives_ok { is ($_->count, 1, 'Prefetched grouped search returns correct count') }
- || skip ('No test due to exception', 1);
- }
+ # try a ->prefetch direction (no select collapse)
+ my $books = $schema->resultset ('BooksInLibrary')->search (
+ { 'owner.name' => 'wiggle' },
+ { prefetch => 'owner', distinct => 1 }
+ );
+ my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }});
+ for ($books, $books2) {
+ is ($_->all, 1, 'Prefetched grouped search returns correct number of rows');
+ is ($_->count, 1, 'Prefetched grouped search returns correct count');
}
}