[Dbix-class] 08100: branch roundup, owners please respond.

Matt S Trout dbix-class at trout.me.uk
Sun Apr 6 19:58:49 BST 2008


On Mon, Mar 31, 2008 at 06:17:26PM -0700, John Napiorkowski wrote:
> Okay, so I think this is actually much easier than I
> first thought.  Basically since I know that each
> storage specific subclass inherits from
> DBIx::Class::Storage::DBI I just put a stublike method
> there which performs the connection and redispatches
> to the subclass on an as needed basis.
> 
> So for example, if a storage specific subclass needs
> to define bind parameters, such as how the Pg Storage
> subclasses needs to set bytea bind parameters to
> support using bytea for long/blob storage, it
> overrides the method "bind_attribute_by_data_type"
> from DBIx::Class::Storage::DBI like so:
> 
> sub bind_attribute_by_data_type {
>   my ($self,$data_type) = @_;
> 
>   my $bind_attributes = {
>     bytea => { pg_type => DBD::Pg::PG_BYTEA },
>   };
>  
>   if( defined $bind_attributes->{$data_type} ) {
>     return $bind_attributes->{$data_type};
>   }
>   else {
>     return;
>   }
> }
> 
> and in the parent class I have a version which, if
> executed, performs the connection and then sends it
> off to the storage specific subclass:
> 
> sub bind_attribute_by_data_type {
>   my ($self, $data_type, $storage) = @_;
>   if($storage) {
>     $storage->ensure_connected;
>     return
> $storage->bind_attribute_by_data_type($data_type)
>   }
>   return;
> }

You need to trap the case where it isn't overrided in the subclass. So
something like

our $NO_SUBCLASS;
return if $NO_SUBCLASS;
$self->ensure_connected;
local $NO_SUBCLASS = 1;
$self->bind_...

or similar. Or maybe

sub bind_... {
  $self->ensure_connected;
  $self->_bind_... # note the _

which is probably simpler and harder to get wrong.

The other thing to consider is: Can we not just do ensure_connected on the
way in to anything that's going to use the dbh (insert, select etc.) so
specific methods don't have to worry about it at all?

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the DBIx-Class mailing list