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

Hi!

I am trying to convert an old pure SQL application to using DBIx::class/SQL::abstract. I am running into some problems for two types of cases.

The application is basically a single query where a number of tables are joined multiple times (selecting a number of protein domain rows, as it were) and a lot of conditions are applied to the resulting join.

In some cases, there are SQL WHERE conditions on the form

... table1.variable - table2.variable < (integer value) ... (all joined together with AND)

In DBIx::class, I suppose what I want is a huge hash holding all of these which will eventually go into a resultset->search () call.

I suppose it should look something like

%searchConditions = { 'table1.variable' => {'<', '(integer value) - table2.variable'} };


but can I just embed arithmetics like that into the second part of the condition and expect it to be properly processed?

My second question concerns how to represent multiple AND conditions involving the same table field.

Suppose I have the SQL

... table1.variable - table2.variable < (integer value 1) AND table1.variable - table3.variable < (integer value 2)...


I feel it should translate to something like

%searchConditions = { 'table1.variable' => {'<', '(integer value 1) - table2.variable'}, 'table1.variable' => {'<', '(integer value 2) - table3.variable'} };


since things should go in a hash to join them by AND logic, but this does not seem like a legal hash since there are keys appearing more than once. Should it be something like:

%searchConditions = { 'table1.variable' => {'-and' => [{'<', '(integer value 1) - table2.var +iable'}, {'<', '(integer value 2) - table3.variable'}]} };


I apologize beforehand if this is illegible, silly or full of code suggestions that are syntactically wrong. However, I am days or weeks away from actually being able to test the code, so if possible, I thought I should ask people who actually know the syntax, to see if I could get it right from the beginning...

Grateful for help,

Yours sincerely,

Kristoffer Forslund

Replies are listed 'Best First'.
Re: Arithmetic SQL operation in DBIx::class?
by Herkum (Parson) on May 07, 2009 at 14:13 UTC

    SQL::Abstract is decent for simple stuff. When things get complicated... You are basically shooting yourself in the foot. It is hard to keep track of complicated conditionals and evaluations.

    I would recommend you create a View in your database which preps the data for you or write Perl code which pulls the data from the database and then reformats it to the result that you desire.

Re: Arithmetic SQL operation in DBIx::class?
by Your Mother (Archbishop) on May 07, 2009 at 15:31 UTC

    Herkum's advice about creating views might be just the ticket. You could also probably create custom result sets in DBIC to do the things you want and break out the complexity into constituents. See the masterclass slides for a nice tour.

    I'm a die-hard DBIC user but if you're doing biology stuff you might consider hitting Rose::DB instead. It's a wonderful ORM and it's faster than DBIC which might be important in protein analysis kinds of stuff.