- NAME
- SYNOPSYS
- DESCRIPTION
- COPYRIGHT
- VERSION

# NAME

GT::SQL::Condition - Creates complex where clauses

# SYNOPSYS

        my $cond = GT::SQL::Condition->new(Column => LIKE => 'foo%');
        print $cond->sql;

        my $cond = GT::SQL::Condition->new(
            Column  => LIKE => 'foo%',
            Column2 => '<'  => 'abc'
        );
        $cond->bool('OR');
        print $cond->sql;

# DESCRIPTION

The condition module is useful for generating complex SQL WHERE clauses. At it's simplest, a condition is composed of three parts: column, condition and value.

Here are some examples.

To find all users with a first name that starts with Alex use:

        my $cond = GT::SQL::Condition->new(FirstName => LIKE => 'Alex%');

To find users with first name like alex, and last name like krohn use:

        my $cond = GT::SQL::Condition->new(
            FirstName => LIKE => 'Alex%',
            LastName  => LIKE => 'Krohn%'
        );

To find users with first name like alex or last name like krohn use:

        my $cond = GT::SQL::Condition->new(
            FirstName => LIKE => 'Alex%',
            LastName  => LIKE => 'Krohn%'
        );
        $cond->bool('OR');

You may also specify this as:

        my $cond = GT::SQL::Condition->new(
            FirstName => LIKE => 'Alex%',
            LastName  => LIKE => 'Krohn%',
            'OR'
        );

Now say we wanted something a bit more complex that would normally involve setting parentheses. We want to find users who have either first name like alex or last name like krohn, and whose employer is Gossamer Threads. We could use:

        my $cond1 = GT::SQL::Condition->new(
            'FirstName', 'LIKE', 'Alex%',
            'LastName', 'LIKE', 'Krohn%'
        );
        $cond1->bool('or');
        my $cond2 = GT::SQL::Condition->new(
            $cond1,
            Employer => '=' => 'Gossamer Threads'
        );

By default, all values are quoted, so you don't need to bother using any quote function. If you don't want something quoted (say you want to use a function for example), then you pass in a reference.

For example, to find users who have a last name that sounds like 'krohn', you could use your SQL engines SOUNDEX function:

        my $cond = GT::SQL::Condition->new(LastName => '=' => \"SOUNDEX('krohn')");

and the right side wouldn't be quoted.

You can also use a condition object to specify a list of multiple values, which will become the SQL 'IN' operator. For example, to match anyone with a first name of Alex, Scott or Jason, you can do:

        my $cond = GT::SQL::Condition->new(FirstName => IN => ['Alex', 'Scott', 'Jason']);

which will turn into:

        FirstName IN ('Alex', 'Scott', 'Jason')

Note that when using multiple values, you can use '=' instead of 'IN'. Empty lists will be treated as an impossible condition (1 = 0). This is primarily useful for list handling list of id numbers.

To match NULL values, you can use `undef` for the value passed to the `add()` method. If specifying '=' as the operator, it will automatically be changed to 'IS':

        $cond->add(MiddleName => '=' => undef);

becomes:

        MiddleName IS NULL

To negate your queries you can use the `not` function.

        my $cond = GT::SQL::Condition->new(a => '=' => 5);
        $cond->not;

would translate into NOT (a = '5'). You can also do this all on one line like:

        print GT::SQL::Condition->new(a => '=' => '5')->not->sql;

This returns the sql right away.

# COPYRIGHT

Copyright (c) 2004 Gossamer Threads Inc. All Rights Reserved. http://www.gossamer-threads.com/

# VERSION

Revision: $Id: Condition.pm,v 1.44 2004/10/12 17:54:30 jagerman Exp $