set_sql with __TABLE__ and multi-level inheritance

[prev] [thread] [next] [Date index for 2005/03/27]

From: Thomas L. Shinnick
Subject: set_sql with __TABLE__ and multi-level inheritance
Date: 02:22 on 27 Mar 2005
I'm worried I'm being too clever and will trip myself up.  I'm trying to push as much common code into my common base class.  I want to move some set_sql() calls into the base class, make references in the SQL to '__TABLE__', and wonder whether the correct definitions of '__TABLE__' will be inserted into the generated SQL.

Example class inheritance structure:

  package MyCDBI;
  use base 'Class::DBI';
      __PACKAGE__->set_sql( deleteAllRecords => <<'' );
  DELETE FROM __TABLE__

  package MyUsers;
  use base 'MyCDBI';
      __PACKAGE__->table( 'TUsers' );

  package MyGroups;
  use base 'MyCDBI';
      __PACKAGE__->table( 'TGroups' );

  package main;

      MyUsers->sql_deleteAllRecords()->execute();
 
      MyGroups->sql_deleteAllRecords()->execute();

It seems to me that the _actual_ creation of the sql_deleteAllRecords method is delayed until first called, and that the value of $class used inside _mk_sql_closure therefore reflects the subclass referenced (e.g. 'MyUsers', 'MyGroups') and not the base class MyCDBI.  

But if the set_sql() call was from MyCDBI, then won't there be only one such named method, MyCBDI::sql_deleteAllRecords(), and then the __TABLE__ value is permanently set by the first use of the method?

But trying to test all this, it seems to work just fine!?!
  DBD::mysql::st execute failed: 
    You have an error in your SQL syntax near 'FOOBAR FROM TUsers' at line 1 
    [for Statement "FOOBAR FROM TUsers"] 
    at /usr/lib/perl5/site_perl/5.6.1/DBIx/ContextualFetch.pm line 51.
  DBD::mysql::st execute failed: 
    You have an error in your SQL syntax near 'FOOBAR FROM TGroups' at line 1
   [for Statement "FOOBAR FROM TGroups"] 
    at /usr/lib/perl5/site_perl/5.6.1/DBIx/ContextualFetch.pm line 51.

Yours truly,
confused


set_sql with __TABLE__ and multi-level inheritance
Thomas L. Shinnick 02:22 on 27 Mar 2005

Re: set_sql with __TABLE__ and multi-level inheritance
Thomas L Shinnick 22:23 on 27 Mar 2005

Generated at 14:42 on 11 Apr 2005 by mariachi v0.52