Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Apache::DBI and DBI subclasses

by Hrunting (Pilgrim)
on Dec 09, 2001 at 07:01 UTC ( [id://130499]=perlquestion: print w/replies, xml ) Need Help??

Hrunting has asked for the wisdom of the Perl Monks concerning the following question:

I've posted this to a couple of mailing lists and haven't received any answers, so maybe the folks at perlmonks can give some insight.

According to the subclassing posts that are out there, one of the things that you're supposed to do when subclassing DBI is to call __PACKAGE__->init_rootclass(). That works fine.

However, when you use Apache::DBI, Apache::DBI will be used to make the connection, but an Apache::DBI::db connection won't be returned to you; a connection from your class will. If the init_rootclass() is removed from the subclass, all works as expected, but of course, you lose any adjustments you may have made withing subclass::db and subclass::st.

Does anyone know of a workaround to this? It seems a shame to lose the persistent functionality of Apache::DBI if you want to use subclassed DBI::db and ::st objects. Conversely, it seems stupid to have to reinvent the Apache::DBI wheel when you have subclasses.

From what I've read in Apache::DBI's code, I suspect that a partial rewrite of Apache::DBI would be needed to support subclassed DBI objects, but I wanted to hear at least *some* opinions on the matter, first.

Replies are listed 'Best First'.
Re (tilly) 1: Apache::DBI and DBI subclasses
by tilly (Archbishop) on Dec 09, 2001 at 08:26 UTC
    I don't have the modules in question in front of me. However my gut feeling is that the right way to go about this is to subclass Apache::DBI. There may be some gotchas to be aware of when you try that. But conceptually it makes more sense to make things work that way, and it is probably possible to succeed in that without any rewriting of Apache::DBI.
      Well, the big 'gotcha' with subclasses Apache::DBI is that you can only use it that you need to be running under mod_perl to do it, but I suppose you could do some sort of environment check right before you decide which modules to include and which classes to subclass.
        Well, whether this is doable depends on what you wanted to do. But Perl does have multiple inheritance. You can choose to inherit directly from Apache::DBI and then directly from DBI. So you are a subclass of both, and if Apache::DBI isn't there things still have a chance to work. I say chance because if Apache::DBI and DBI do something differently enough, you might have to work around the difference.

        Another approach is to reconsider doing any subclassing. What you want to do is preserve the interface, right? Well you can have an object that has an attribute which is the database handle it proxies through with an AUTOLOAD method. You can see an example of how to do that at Re (tilly) 1: Reverse Inheritance Irritance. And now you can wrap either one of them without having to do a lot of work...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://130499]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-24 04:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found