[Dbix-class] as_query() and DBIx::Class::Cursor::Cached aren't friends

Peter Rabbitson rabbit+dbic at rabbit.us
Mon Jun 8 09:39:55 GMT 2009


Rob Kinyon wrote:
> On Sun, Jun 7, 2009 at 20:36, Peter Rabbitson <rabbit+dbic at rabbit.us> wrote:
>> The fix should be quite simple, i.e. move as_query out of the Cursor
>> class. But first I'd like someone to comment on why it ended up there
>> in the first place.
> 
> The only thing that has a query is the cursor. Everything else has the
> building blocks of a query, but hasn't been materialized as such. A
> ResultSet (which is where I originally thought to put it) can
> materialize as all sorts of queries, so it can't live there.
> 

But this makes no sense whatsoever. A cursor is just a cursor, it knows
nothing more than what the resultset object already does. Consider:

=== lib/DBIx/Class/ResultSet.pm
==================================================================
--- lib/DBIx/Class/ResultSet.pm	(revision 6543)
+++ lib/DBIx/Class/ResultSet.pm	(local)
@@ -1925,7 +1925,10 @@

 =cut

-sub as_query { return shift->cursor->as_query(@_) }
+sub as_query {
+  my $self = shift;
+  return $self->result_source->storage->as_query($self);
+}

 =head2 find_or_new

=== lib/DBIx/Class/Storage/DBI/Cursor.pm
==================================================================
--- lib/DBIx/Class/Storage/DBI/Cursor.pm	(revision 6543)
+++ lib/DBIx/Class/Storage/DBI/Cursor.pm	(local)
@@ -49,32 +49,6 @@
   return bless ($new, $class);
 }

-=head2 as_query
-
-=over 4
-
-=item Arguments: none
-
-=item Return Value: \[ $sql, @bind ]
-
-=back
-
-Returns the SQL statement and bind vars associated with the invocant.
-
-=cut
-
-sub as_query {
-  my $self = shift;
-
-  my $storage = $self->{storage};
-  my $sql_maker = $storage->sql_maker;
-  local $sql_maker->{for};
-
-  my @args = $storage->_select_args(@{$self->{args}});
-  my ($sql, $bind)  = $storage->_prep_for_execute(@args[0 .. 2], [@args[4 .. $#args]]);
-  return \[ "($sql)", @{ $bind || [] }];
-}
-
 =head2 next

 =over 4
=== lib/DBIx/Class/Storage/DBI.pm
==================================================================
--- lib/DBIx/Class/Storage/DBI.pm	(revision 6543)
+++ lib/DBIx/Class/Storage/DBI.pm	(local)
@@ -910,6 +910,40 @@
   return ($sql, \@bind);
 }

+=head2 as_query
+
+=over 4
+
+=item Arguments: none
+
+=item Return Value: \[ $sql, @bind ]
+
+=back
+
+Returns the SQL statement and bind vars that would result from the given
+ResultSet (does not actually run a query)
+
+=cut
+
+sub as_query {
+  my ($self, $rs) = @_;
+
+  my $sql_maker = $self->sql_maker;
+  local $sql_maker->{for};
+
+  my $attr = $rs->_resolved_attrs;
+
+  # my ($op, $bind, $ident, $bind_attrs, $select, $cond, $order, $rows, $offset) = $self->_select_args(...);
+  my @args = $self->_select_args($attr->{from}, $attr->{select}, $attr->{where}, $attr);
+
+  # my ($sql, $bind) = $self->_prep_for_execute($op, $bind, $ident, [ $select, $cond, $order, $rows, $offset ]);
+  my ($sql, $bind) = $self->_prep_for_execute(
+    @args[0 .. 2],
+    [ @args[4 .. $#args] ],
+  );
+  return \[ "($sql)", @{ $bind || [] }];
+}
+
 sub _fix_bind_params {
     my ($self, @bind) = @_;


I am not committing this until I get more input, but it seems rather
straightforward to me.




More information about the DBIx-Class mailing list