cleanups
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI.pm
index 4e4d4e7..63997aa 100644 (file)
@@ -927,6 +927,22 @@ sub _fix_bind_params {
         } @bind;
 }
 
+sub _flatten_bind_params {
+    my ($self, @bind) = @_;
+
+    ### Turn @bind from something like this:
+    ###   ( [ "artist", 1 ], [ "cdid", 1, 3 ] )
+    ### to this:
+    ###   ( 1, 1, 3 )
+    return
+        map {
+            if ( defined( $_ && $_->[1] ) ) {
+                @{$_}[ 1 .. $#$_ ];
+            }
+            else { undef; }
+        } @bind;
+}
+
 sub _query_start {
     my ( $self, $sql, @bind ) = @_;
 
@@ -1217,8 +1233,12 @@ sub _select_args_to_query {
 
   # my ($sql, $prepared_bind) = $self->_prep_for_execute($op, $bind, $ident, [ $select, $cond, $order, $rows, $offset ]);
   my ($sql, $prepared_bind) = $self->_prep_for_execute($op, $bind, $ident, \@args);
+  $prepared_bind ||= [];
 
-  return \[ "($sql)", @{ $prepared_bind || [] }];
+  return wantarray
+    ? ($sql, $prepared_bind, $bind_attrs)
+    : \[ "($sql)", @$prepared_bind ]
+  ;
 }
 
 sub _select_args {
@@ -1302,13 +1322,20 @@ sub _resolve_ident_sources {
   return $alias2source;
 }
 
-sub count {
+sub _copy_attributes_for_count {
   my ($self, $source, $attrs) = @_;
+  my %attrs = %$attrs;
+
+  # take off any column specs, any pagers, record_filter is cdbi, and no point of ordering a count
+  delete @attrs{qw/select as rows offset page order_by record_filter/};
 
-  my $tmp_attrs = { %$attrs };
+  return \%attrs;
+}
+
+sub count {
+  my ($self, $source, $attrs) = @_;
 
-  # take off any pagers, record_filter is cdbi, and no point of ordering a count
-  delete $tmp_attrs->{$_} for (qw/select as rows offset page order_by record_filter/);
+  my $tmp_attrs = $self->_copy_attributes_for_count($source, $attrs);
 
   # overwrite the selector
   $tmp_attrs->{select} = { count => '*' };
@@ -1332,7 +1359,7 @@ sub count_grouped {
   my $sub_attrs = { %$attrs };
 
   # these can not go in the subquery, and there is no point of ordering it
-  delete $sub_attrs->{$_} for qw/prefetch collapse select as order_by/;
+  delete $sub_attrs->{$_} for qw/collapse select as order_by/;
 
   # if we prefetch, we group_by primary keys only as this is what we would get out of the rs via ->next/->all
   # simply deleting group_by suffices, as the code below will re-fill it
@@ -1349,7 +1376,7 @@ sub count_grouped {
   }];
 
   # the subquery replaces this
-  delete $attrs->{$_} for qw/where bind prefetch collapse group_by having having_bind rows offset page pager/;
+  delete $attrs->{$_} for qw/where bind collapse group_by having having_bind rows offset/;
 
   return $self->count ($source, $attrs);
 }