[Dbix-class] constructing a query where fields are compared to each other

Karen Etheridge perl at froods.org
Wed Oct 16 23:20:57 GMT 2013


I'm trying to figure out how to construct a WHERE clause [1] like:

... WHERE field_x - field_y = 1

Is this possible?  The best guess I have is:

    { '1' => [ '-' => { -ident => 'field_x' },
                      { -ident => 'field_y' }  ]  }

...but this expands to:
    ( 1 = ? OR 1 = me.field_x OR 1 = me.field_y )   with the bind variable being '-'.

The only operators that are covered in the SQL::Abstract docs are >, <, =
and !=, so are arithmetic operations even possible?

Since this is a pretty gross query regardless, I am leaning towards simply
adding a new column to this table, which is the value of field_x - field_y,
so I can query on that directly.


[1] I'm actually wanting to do this in a join relationship, so my real code
looks like the below - but I hope I've simplified adequately...

__PACKAGE__->has_many(cats_dogs => 'MyApp::Schema::Dogs' =>
    sub {
        my $args = shift;
        return +{
            # foreign.field_x - foreign.field_y = 1
            '1' => [ '-' => { -ident => "$args->{foreign_alias}.field_x" },
                            { -ident => "$args->{foreign_alias}.field_y" }
        }
    },
);


thanks,
-ether at cpan.org



More information about the DBIx-Class mailing list