Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Class::DBI

by stonecolddevin (Vicar)
on Apr 26, 2005 at 15:56 UTC ( #451644=modulereview: print w/ replies, xml ) Need Help??

Item Description: A Simple Database Abstraction

Review Synopsis:

Class::DBI is quite a simple module really, mainly you inherit it's methods and go off about your business.
SQL is no longer a problem in the way of causing minor issues with your code, so you can retrieve, create, update or do any other database modifications necessary with the greatest of ease.
The only requirements are to set up your primary index columns, and the columns you need to use for the selects in a module that inherit's from Class::DBI, and then you have total access to Class::DBI's magic.
(example of setup)

## Main class package My::DBI; use base 'Class::DBI'; my $dsn = "dbi:mysql:***"; my $user = "****"; my $passwd = "****"; __PACKAGE__->set_db('Main', $dsn, $user, $passwd); ## Second class package My::Tables; use base 'My::DBI'; __PACKAGE__->columns( Primary => qw[id] ); __PACKAGE__->columns( All => qw [ id author title date content ip ] ); __PACKAGE__->table('entries');

Other useful methods:
add_constructor
Allows you to construct an SQL query snippet and call it through your object.
Music::CD->add_constructor(new_music => 'year > ?'); my @recent = Music::CD->new_music(2000);

retrieve_from_sql
Allows you to consruct your own SQL query like so:
(NOTE: You inlining the entire WHERE clause)
my @cds = Music::CD->retrieve_from_sql(qq{ artist = 'Ozzy Osbourne' AND title like "%Crazy" AND year <= 1986 ORDER BY year LIMIT 2,3 });

Class::DBI::AbstractSearch A search class provided by Class::DBI that allows you to write arbitrarily complex searches using perl data structures, rather than SQL.
my @music = Music::CD->search_where( artist => [ 'Ozzy', 'Kelly' ], status => { '!=', 'outdated' }, );


These are just a few of the features of Class::DBI that I have found quite useful. Check the module out for yourself, it cut my development time in at least half.

Comment on Class::DBI
Select or Download Code
Re: Class::DBI
by b10m (Vicar) on Apr 26, 2005 at 21:29 UTC

    I probably like Class::DBI as much as you (and probably everyone who worked with it), but wouldn't it be more helpful to come up with your own examples for this review? Examples that differ from the POD already available?

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: Class::DBI
by sir.shz (Novice) on Apr 26, 2005 at 22:29 UTC
    Class::DBI is quite a simple module really
    I think it's actually a quite complex module, it's perl's Object-to-Relational mapping module. Other language's OR libraries are discussed in books, for example, there are at least five books dedicated to Hibernate, which is java's OR. I'm always wondering why nobody writes a book about Class::DBI, it would help to raise perl to "enterprise" status, after all, OR sounds fancy and well educated.
Re: Class::DBI
by mugwumpjism (Hermit) on May 24, 2005 at 01:44 UTC

    Of course, if you actually want to abstract away your SQL rather than just inlining it, then you can use Tangram for that;

    my $r_cd = $storage->remote("Music::CD"); my @cds = $storage->select ( filter => ( ( $r_cd->{artist} eq "Ozzy Osbourne" ) & ( $r_cd->{title}->like("%Crazy") ) & ( $r_cd->{year} <= 1986 ) ), order => $r_cd->{year}, limit => [2, 3] );

    This has great advantages in that query filters can be passed around as objects and hence built dynamically, including arbitrary joins.

    See the Music Store demo in the Tangram distribution for more examples, including many queries not easily written with Class::DBI.

    $h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";

Back to Reviews

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: modulereview [id://451644]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-08-22 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (148 votes), past polls