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

I have some perfectly functional code which uses Class::DBI, but I find it a bother to maintain because fields are added to the database with some frequency.

I decided to try Class::DBI::Loader but have run in to a problem with transactions. When I was using Class::DBI, I simply defined a routine do_transaction() as suggested in the documentation, and this was inherited by all my table classes. However, with Class::DBI::Loader, the table classes are automatically created and don't inherit my code.

I solved this by creating a second package in the file, which contained the do_transaction() routine, and describing it as an additional_base_class. This seems to work, but I'm not sure if it was the best way to handle the problem, so I hope that someone with experience using Class::DBI::Loader could tell me how this is usually addressed. The documentation for Class::DBI::Loader is a bit on the skimpy side.

Thank you for your advice.

Replies are listed 'Best First'.
Re: Class::DBI::Loader and transactions
by jasonk (Parson) on Apr 26, 2005 at 15:26 UTC

    In addition to additional_base_classes, as recommended earlier, you can also just declare packages and subs after building the loader. Here is a trimmed-down example of my database.pl script, this is pre-loaded by mod_perl so that my website (which is HTML::Mason based) will have access to all the database objects it needs. In this snippet of code the classes JSK::Tag and JSK::Software are generated by Class::DBI::Loader from the tag and software tables in the database.


    We're not surrounded, we're in a target-rich environment!
Re: Class::DBI::Loader and transactions
by davidrw (Prior) on Apr 26, 2005 at 14:22 UTC
    Reading your problem in the first 2 paragraphs, the first thing that came to mind was the base class solution you described in the third.. I would also recommend asking these folks on the cdbi mailing list.
Re: Class::DBI::Loader and transactions
by perrin (Chancellor) on Apr 26, 2005 at 14:50 UTC
    I've never used Loader, but I do think that is the purpose of additional_base_class.