Class::DBI takes care of all the basics for you while at the same time helping to keep all your sql stuff in dedicated modules. Once you have your module set up for a table inserts & updates are easy as pie and custom queries aren't much harder.

package My::Table; use base 'My::Class::DBI'; __PACKAGE__->table('blah'); __PACKAGE__->columns( All => qw/ foo bar baz quid quux / ); __PACKAGE__->columns( Essential => qw/ foo bar / ); __PACKAGE__->columns( Primary => 'foo' ); # assuming we have another table called 'other'.. # this creates a method called sql_method_name __PACKAGE__->set_sql( method_name => qq( select b.foo, b.bar, o.summat +, o.summat_else from blah b left join other o using(foo) where b.quid +=?)); sub public_method_name { my ($class,$quid) = @_; my $sth = $class->sql_method_name(); $sth->execute($quid); return @{$sth->fetchall_arrayref({})}; }

and using the module...

# fetch a My::Table object my $record = My::Table->create({ foo => 1, bar => 'bar val', baz => 'baz val', quid => 'quid val', quux => 'quux val' }); # update a value $record->quid('new quid val'); $record->update; # fetch a list of records via our join method my @list = My::Table->public_method_name('new quid val');

I've also recently started using TEMP columns to store values from other tables in the retrieved object via custom join methods so I can still use the oo interface to read the values (can't update in this case) but it's beer o'clock and I don't have an example of that handy.. 8)

cheers,

J


In reply to Re: 'automating' SQL queries, use Class::DBI? by edoc
in thread 'automating' SQL queries, use Class::DBI? by geektron

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.