horano311 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

In short I'm looking for a perl module/framework(?) I can leverage to assist me in building potential queries, to a given database.

Basically I just designed and populated a db, with the requirements being the users want to be able to query with about any combination of criteria.

Done..at the db level. So they'll have a web form were they can do all kinds of and | or on criteria, over given date range | time range. There's only about 6 pieces of information, and then there will be all the possible date/time combinations for it.

I'd like to avoid as much as the tedium of if they clicked this and this then do join on this, but if they click then it's select this that etc.

I feel my database design is pretty good and well normalized. I'm hoping there's something that can ease the rest. Something that would give me a skeleton web form would be nice too :)

Something for perl but ruby on rails ish?????
Thank you!

Replies are listed 'Best First'.
Re: Module to ease query building
by jasonk (Parson) on Feb 20, 2007 at 19:58 UTC

    DBIx::Class makes this relatively easy, as you can pass additional parameters to a resultset and get back a resultset that will produce a query that matches all of the things you've searched for so far, so you could do something like this:

    my $rs = $schema->resultset( 'Users' ); if ( my $year = $cgi->param( 'birth_year' ) ) { $rs = $rs->search( birth_year => $year ); } if ( my $fname = $cgi->param( 'first_name' ) ) { $rs = $rs->search( first_name => $fname ); } while ( my $user = $rs->next ) { print "User ".$user->name." matched!\n"; }

    We're not surrounded, we're in a target-rich environment!
Re: Module to ease query building
by kyle (Abbot) on Feb 20, 2007 at 20:16 UTC

    You may be able to use SQL::Abstract for that. You may wind up with something like:

    my %where_clause = (); if ( $cgi->param( 'field1' ) ) { # obviously, some validation should be here $where_clause{ 'field1' } = $cgi->param( 'field1' ); } # etc. my $sql = SQL::Abstract->new(); my ( $stmt, @bind ) = $sql->select( 'table', [ qw( field list ) ], \%where_clause ); my $sth = $dbh->prepare( $stmt ); $sth->execute( @bind );
Re: Module to ease query building
by perrin (Chancellor) on Feb 20, 2007 at 20:09 UTC