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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.