I try to keep code that calls DBI functions in its own subroutines, and I get the database handle and set environment variables etc from a perl module. I also tell perl what the schema of a db is going to be and make checks to see that everything is legal inside these separate routines (although being lazy I tend to ignore the hashes I've made up which tell me what can be NULL).

So I can get a field or update arbitrary fields with this kind of thing:

my $oldpoints = &getfield("users",$id,"arigatopoints"); $ret = &updatefields($table,$id,\@fields,\@vals);
but it depends on what I'm trying to do. One report I generate is built around parsing a single complex query. But in a web site I tend to do lots of little things like the code above and the bulk of the code is site logic. I put the getfield and updatefields routines in my library module out of the way so I can get at all the SQL at once if I need to, and I can include that module in other programs too.

If you are doing lots of little things it might pay to abstract it out of the center of things and try to see how few subs you can have that are written with SQL. But fact is, I recently sped up a script from 3 minutes to <10 seconds just by doing a complex query instead of little ones interspersed by Perl. SQL engines are fast.


In reply to Re: Leashing DBI by mattr
in thread Leashing DBI by billyak

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.