I've been having a heck of a time getting the following DBIx::Class::ResultSet working and have tracked a couple of bugs through DBIx::Class::Storage::DBI (or SQL::Abstract, really). Here's the ResulSet:<br><br><span style="font-family: courier new,monospace;">
sub most_for_campaign {<br> my ($self, $limit) = @_;<br><br> return $self->search(undef,<br> { select => [ 'Id', 'first_name', 'last_name',<br> { count => 'Id' }
<br> ],<br> as => [ qw/ Id first_name last_name total / ],<br> rows => $limit,<br> group_by => [ qw/ Id first_name last_name / ],
<br> });<br>}<br><br></span>First of all, I'm using a MS SQL database with the DBD::Sybase driver.<br><br>Because of the way SQL::Abstract::Limit rewrites MS SQL queries, that turns into:<br><br><span style="font-family: courier new,monospace;">
SELECT * FROM<br>(<br> SELECT TOP 10 * FROM<br> (<br> SELECT TOP 10 Id, first_name, last_name, COUNT( Id ) FROM contacts me <br> ) AS foo<br> <br>) AS bar<br><br></span>which breaks MS SQL because there is no implicit name for the COUNT( Id ) "column." I fixed this with the following patch to DBIx::Class::Storage::
DBI.pm in _recurse_fields.<br><br><span style="font-family: courier new,monospace;">- .'( '.$self->_recurse_fields($fields->{$func}).' )';</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
+ .'( '.$self->_recurse_fields($fields->{$func}).' ) as _' . $func;</span><br style="font-family: courier new,monospace;"><br>The second issue I don't have an easy fix for.<br><br>DBIC::SQL::Abstract (inside DBIx::Class::Storage::DBI), which subclasses SQL::Abstract::Limit, overrides the select method. The $order parameter becomes a HASH ref instead of a string, and the group_by ARRAY ref gets stuck inside $order. DBIC::SQL::Abstract also overrides the SQL::Abstract::_order_by method to process the HASH ref and appends the contents of group_by to the query (assuming everything works well). There are a couple of problems with the SQL::Abstract::Limit::select method (before it calls the SQL::Abstract::select method). First, and the correct line is commented out, if $rows is not defined, the SQL::Abstract::Limit::select calls SQL::Abstract->new->select instead of $self->SUPER::select which would do cause the carefully overridden SQL::Abstract methods in DBIC::SQL::Abstract to be ignored. If $rows is defined (in my case), the call to $self->SUPER::select doesn't pass the $order parameter because, according the the comment, "with LIMIT parameters, get the basic SQL without the ORDER BY clause."
<br><br>Is the answer to always pass $order to the SQL::Abstract::select method? Should the SQL::Abstract->new->select line be changed to $self->SUPER::select above?<br><br>Thanks in advance for your help.<br><br>
-matt<br><br>