Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Class::DBI -- does it get any easier?

by geektron (Curate)
on Nov 12, 2004 at 07:46 UTC ( [id://407290]=perlmeditation: print w/replies, xml ) Need Help??

I just fell in love with Class::DBI. i don't know why i've put off learning it for so long.

as of now, the 'learning curve' has been about 20 minutes of reading the documentation and writing *very* small (and still 100% functional) 'test modules.'

i think i just felt my productivity double, and for those who haven't started using Class::DBI, i strongly suggest looking into it.

Replies are listed 'Best First'.
Re: Class::DBI -- does it get any easier?
by csuhockey3 (Curate) on Nov 12, 2004 at 08:28 UTC
    Class::DBI certainly makes your life easier. Have you looked at this site? My favorite new(ish) one is DBI::Shell. It has a little ways to go, but I like it so far -- check it out.
Re: Class::DBI -- does it get any easier?
by davorg (Chancellor) on Nov 12, 2004 at 09:31 UTC

      While I can recommend Class::DBI::Loader, I can only discourage people from using Class::DBI::Loader::Relationship, which I consider underdocumented and over-hyped. It is/was nice and convincing for Simons live demonstration of how to create a web frontend for a database in under 20 lines of code, but using english as a configuration language for your relationships is really error prone. The error messages or diagnostics (not) output by cdbi:l:r don't help either, and unless you have taken a long look at the source code, it's not easy to modify the examples to suit your actual situation. A more formal syntax would help here and remove the heavy dependency on a good and judicous choice of words made by the program author while still preserving the wanted clarity.

      The module relies on the very simple-minded yet totally undocumented assumption that all phrases will be of the form single-noun verb (a|many) noun - once you glean this from the source code, you can start using it, but the time is better invested in some less magic method of setting things up.

      Adding to the situation is, that the module is not maintained anymore, and if I stepped up as maintainer of it, I would mainly depreciate the use of the module and replace it by something with a more formal syntax:

      brewery 1:n beer # "a brewery produces many beers" beer m:n(handpump) pub # "a pub has beers on handpumps"

        I find the module useful, tho' I recognise many of the shortcomings that you mention. I've asked to take over maintenance of the module, and if I get it I'll certainly be interested in any suggestions for improvement.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

      Regarding Class::DBI::Loader, I've briefly looked at it before and thought it looked very handy (especially in introducing Class::DBI for a large number of existing tables). But does this work if you need custom logic in the module in addition to the basic column/relationship definitions? Can you still define My::TableName as a package, add the logic there, and have perl see it automagically? It's still unclear after reading the docs and code.
        as i understand it ( and i'm not an expert on it by any means ), there are easy ways to override behaviours if needed, some initialization hooks, etc. if all else were to fail, nothing stops one from going back to 'basic' SQL for some things.

        Yes. I often have packages that look something like this:

        use Class::DBI::Loader; my $loader = Class::DBI::Loader->new( dsn => 'dbi:mysql:dbname', user => 'user', password => 'p455w0rd', namespace => 'MyApp', ); # MyObject is one of the tables in the database package MyApp::MyObject; # define various methods on that class 1;
        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Class::DBI -- does it get any easier?
by johnnywang (Priest) on Nov 12, 2004 at 08:49 UTC
    I agree, I've also only recently started trying Class:DBI, wonderful stuff. I ran into a few problems and asked a few questions here in the past week. In general, I think for most projects, one should not need to write SQL anymore. In java, I use Hibernate, I was thrilled when I got my first example to work, and throw away pretty much all the db layer I developed before. Class::DBI is even easier due to the dynamic nature of perl, you don't even need to write those getter/setters. Of course, if performance is an issue or your query becomes too complicated, go back to DBI.
Re: Class::DBI -- does it get any easier?
by blokhead (Monsignor) on Nov 12, 2004 at 17:54 UTC
    Does it get easier? You may want to check out Class::Tables. It's not a C::DBI add-on, but a separate object-relational package which has about the same interface. It does what I think Class::DBI::Loader should have done: figure out *everything* from the database schema -- not just column and table names, but all the key relationships. The schema becomes its own documentation and you don't have to re-explain how it works to your persistence class. Of course, the tradeoff is the uber-flexibility provided by C::DBI. But I've found it sufficient for most of my simple DB apps.

    blokhead

      well, i'll take a look at Class::Tables as well, but now that the project for which i was going to use Class::DBI has been cancelled/ suspended ... i'll have to find another excuse to use it.
Re: Class::DBI -- does it get any easier?
by stvn (Monsignor) on Nov 12, 2004 at 15:26 UTC
    i think i just felt my productivity double, and for those who haven't started using Class::DBI, i strongly suggest looking into it.

    Productivity gains aside, before i start using it, it would be nice if it was a bit more thoroughly tested. The test coverage seems to be a very low 24% (see here for details). It is on the list of the Phalanx 100, maybe someone who uses it could pick it up for a week or so and improve the tests a bit. If enough people do that it would surely be up to 90-100% in no time.

    -stvn
      Coverage numbers are not a perfect indicator. Class::DBI has one of the largest test suites around, and Tony always asks people to submit tests with their patches. It is a well-tested module.
        Coverage numbers are not a perfect indicator.

        No they are not, and it seems from looking closer at some of the source that a good amount of Class::DBI seems to be auto-generated (all that __PACKAGE__->mk_something stuff). Which makes it very difficult to check the test's code coverage. But that code is not even included by Devel::Cover in the coverage report anyway (see here). However, a good portion of the code that is checked by Devel::Cover is not tested.

        I will agree that Class::DBI is surely better tested than the Devel::Cover numbers give it credit for, but that is not to say that it couldn't use a little more.

        -stvn
Re: Class::DBI -- does it get any easier?
by castaway (Parson) on Nov 14, 2004 at 22:55 UTC
    Having just switched from Class::DBI to Tie::DBI for one app in which CDBI was giving me errors, and nobody seemed inclined to help me figure out why, I'm going to answer 'yes' to this question. At least for easy reading of and updating of data, theres no need for all the setup rigamarole with this module, just point it at the table, tell it the primary key (it needs a single one), and it works. To insert new rows however, you need to grab the dbh and use DBI. Nothing's perfect ,)

    C.

      this is an old thread, but would that Tie::DBI error have anything to do w/ :
      Can't locate Encode/ConfigLocal.pm in @INC

      i decided cpan::Tie::DBI looked much easier than Class::DBI for a new project, but cpan::Tie::DBI keeps kicking that error at me, even though i can find no such module on CPAN.

Re: Class::DBI -- does it get any easier?
by Anonymous Monk on Nov 12, 2004 at 18:33 UTC
    Does it get easier? Ruby's ActiveRecord :)

    But no, I know what you mean. Automagical stuff like that is cool.

      Tangram is similar. However, things like that end up having limitations that become problems in real-world apps. With Class::DBI, there is literally no limit on what SQL you can use with it. That's the reason I use it.
        I can see it would be a problem when making larger apps and apps that use existing tables, definitely. But for building new apps, it is unbelievable useful. I have one real-world app running on our intranet right now, but it's pretty light use and doesn't have that many tables to it. The "Tables" link above seems fairly similar to ActiveRecord, so I'll have to check it out as well -- though I will say I'm more interested in the "::DBI" aspect of it all than the class thing. I'm not a firm believer of sticking inheritance models in databases.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found