views:

53

answers:

5

How do I generate the WHERE clause for this query using SQL::Abstract:

SELECT COUNT(*) FROM table WHERE id = 111 AND NOT FIND_IN_SET(type, '1,2,3,4') AND status = 'pending';

What's the proper way to include conditions like WHERE FIND_IN_SET(type, '1,2,3,4')?

+1  A: 

The documentation of SQL::Abstract explains and gives various examples on how to make it emit literal SQL and how to use database functions. Didn't any of that work when you tried it?

rafl
+1  A: 

FIND_IN_SET isn't standard SQL, so SQL::Abstract doesn't have support for it. You can however put any literal SQL into an SQL::Abstract query. I expect your solution lies down that route.

davorg
+1  A: 

This code generates the WHERE clause:

my $sql = SQL::Abstract->new;    

my %where = (
    id => 111,
    -nest => \"NOT FIND_IN_SET(type, '1,2,3,4')",
    status => 'pending',
);

my ($stmt, @bind) = $sql->where(\%where, \@order);
planetp
+3  A: 

See the not_bool unary operator option:

use SQL::Abstract;

my $sql = SQL::Abstract->new;

my $where = {
    id => 111,
    status => 'pending',
    -not_bool => "FIND_IN_SET(type, '1,2,3,4')",
};

my ($query, @bind) = $sql->select( 
    'table',
    'count(*)',
    $where,
);

This is how $query looks:

SELECT count(*) FROM table WHERE ( ( (NOT FIND_IN_SET(type, '1,2,3,4')) 
AND id = ? AND status = ? ) )

/I3az/

draegtun
A: 

give a try to dalmp http://code.google.com/p/dalmp/

tareco