Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'm considering using Class::DBI for a large project. I've already started the project, but I've been successful in retrofitting Class::DBI into most of my pages, however I run into a snag in stuations where I attempt to order my select statement by a column on a another table (i.e. my Tasks table will have a reference to the owner of a project, and I want to sort by that person's last name.

I've tried the standard Class::DBI->search as well Class::DBI::AbstractSearch, but neither seems to deal with searches on joined tables. I know i could create custom SQL code wth Class::DBI->set_sql, but not without repeating program logic in two places.

The code below shows what I did in the original DBI code, building up a select statement depending on the arguments I receive, can this kind of code be abstracted with Class:DBI or a related module?

my $statement =q{ SELECT as tid, long_desc, employees.fname as first, employees.lname as last, DATE_FORMAT(start_date,"%b %d %y") as +fdate, status.descr as status_d FROM employees,tasks,status WHERE owner = AND status = }; if (exists $ARGS{status} && $ARGS{status} >0){ $statement .= " AND status = $ARGS{status}"; }elsif (exists $ARGS{status} && $ARGS{status} eq "active"){ $statement .= " AND (status=1 OR status=2)"; } if (exists $ARGS{owner} && $ARGS{owner} > 0){ $statement .= " AND owner = $ARGS{owner}"; } if ($ARGS{order} eq "osd"){ $statement .= q{ ORDER BY employees.lname, status.descr,start_da +te }; }elsif ($ARGS{order} eq "ods"){ $statement .= q{ ORDER BY employees.lname, start_date, +scr }; }elsif ($ARGS{order} eq "sdo"){ $statement .= q{ ORDER BY status.descr, start_date,employees.lna +me }; }else{ $statement .=q{ ORDER BY start_date,employees.lname,status.desc +r }; } my $sth = $dbh->prepare($statement) or die $dbh->errstr; $sth->execute;

In reply to Problem with table joins with Class::DBI by thunders

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and not a whimper to be heard...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (10)
    As of 2018-06-22 17:40 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (124 votes). Check out past polls.