1. The cdbi construct method is very nifty for refactoring existing SQL queries. The code might then look something like:
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: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 .... }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |