At a guess all the left joins are there just to pull in related data for use by the application. If so, these relationships can be setup via Class::DBI, greatly simplifying this query:

1. The cdbi construct method is very nifty for refactoring existing SQL queries. The code might then look something like:

my $query = 'select event.* from event where sid = 1 order by +timestamp desc, event.sid desc, event.cid desc'; my $sth = MyClass::db_Main->prepare( $query ); $sth->execute; my @events; # # Fetch and construct each object # while (my $row = $sth->fetchrow_hashref()) { push(@events, MyClass::Event->construct($row)); } # # Process these objects # foreach my $event (@events) { my $signature = $event->signature; print "Signature Name=".$signature->sig_name."\n"; ## whatever .... }
2. You may want to use the setup_table method from Class::DBI::Xx. (eg Class::DBI::Pg for postgres) to load column definitions and primary keys from the database. This will ensure that your your columns definitions match the database and SQL wildcards (select event.*) can safely be used in conjuction with Class::DBI::construct:

package MyClass; use strict; use warnings; use DBI; use base qw(Class::DBI::Pg); { __PACKAGE__->connection("dbi:Pg:dbname=mydb", 'some-user', 'some-pa +ssword', { RaiseError => 1, Auto +Commit => 1 }); } ######################################################## package MyClass::Event; use base qw/MyClass/; _PACKAGE__->set_up_table('event'); __PACKAGE__->has_a(signature => 'MyClass::Signature'); ######################################################## package MyClass::Signature; use base qw/MyClass/; _PACKAGE__->set_up_table('signature'); __PACKAGE__->has_many(events => 'MyClass::Event' => signature); # ....

3. By "Paging Support" Do you mean display only the nth screen-full of data? If so, the SQL offset and limit clauses are your friends:

my $page_size = 24; my $page_num = 3; my $offset = ($page_num - 1) * $page_size; my $query = "select * from event where sid = 1 order by timestamp desc +, event.sid desc, event.cid desc OFFSET $offset LIMIT $page_size";

In reply to Re: Class::DBI and Query with JOIN'S + Paging support by snoopy
in thread Class::DBI and Query with JOIN'S + Paging support by Anonymous Monk

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.