Jifty-DBI

 view release on metacpan or  search on metacpan

lib/Jifty/DBI/Collection.pm  view on Meta::CPAN

        case_sensitive   => undef,
        operator         => '=',
        escape           => undef,
        subclause        => undef,
        leftjoin         => undef,
        @_    # get the real argumentlist
    );

    return if $self->derived;

    #If we're performing a left join, we really want the alias to be the
    #left join criterion.

    if (   ( defined $args{'leftjoin'} )
        && ( not defined $args{'alias'} ) )
    {
        $args{'alias'} = $args{'leftjoin'};
    }

    # {{{ if there's no alias set, we need to set it

    unless ( defined $args{'alias'} ) {

        #if the table we're looking at is the same as the main table
        if ( !defined $args{'table'} || $args{'table'} eq $self->table ) {

            # TODO this code assumes no self joins on that table.
            # if someone can name a case where we'd want to do that,
            # I'll change it.

            $args{'alias'} = 'main';
        }

        else {
            $args{'alias'} = $self->new_alias( $args{'table'} );
        }
    }

    # }}}

    # $column_obj is undefined when the table2 argument to the join is a table
    # name and not a collection model class.  In that case, the class key
    # doesn't exist for the join.
    my $class
        = $self->{joins}{ $args{alias} }
        && $self->{joins}{ $args{alias} }{class}
        ? $self->{joins}{ $args{alias} }{class}
        ->new( $self->_new_collection_args )
        : $self;
    my $column_obj = $class->record_class->column( $args{column} );

    $self->new_item->_apply_input_filters(
        column    => $column_obj,
        value_ref => \$args{'value'},
    ) if $column_obj && $column_obj->encode_on_select && $args{operator} !~ /IS/;

    # Ensure that the column has nothing fishy going on.  We can't
    # simply check $column_obj's truth because joins mostly join by
    # table name, not class, and we don't track table_name -> class.
    if ($args{column} =~ /\W/) {
        warn "Possible SQL injection on column '$args{column}' in limit at @{[join(',',(caller)[1,2])]}\n";
        %args = (
            %args,
            column   => 'id',
            operator => '<',
            value    => 0,
        );
    }
    if ($args{operator} !~ /^(=|<|>|!=|<>|<=|>=
                             |(NOT\s*)?LIKE
                             |(NOT\s*)?(STARTS|ENDS)_?WITH
                             |(NOT\s*)?MATCHES
                             |IS(\s*NOT)?
                             |IN)$/ix) {
        warn "Unknown operator '$args{operator}' in limit at  @{[join(',',(caller)[1,2])]}\n";
        %args = (
            %args,
            column   => 'id',
            operator => '<',
            value    => 0,
        );
    }


    # Set this to the name of the column and the alias, unless we've been
    # handed a subclause name
    my $qualified_column
        = $args{'alias'}
        ? $args{'alias'} . "." . $args{'column'}
        : $args{'column'};
    my $clause_id = $args{'subclause'} || $qualified_column;


    # make passing in an object DTRT
    my $value_ref = ref( $args{value} );
    if ($value_ref) {
        if ( ( $value_ref ne 'ARRAY' )
            && $args{value}->isa('Jifty::DBI::Record') )
        {
            my $by = (defined $column_obj and defined $column_obj->by)
                        ? $column_obj->by
                        : 'id';
            $args{value} = $args{value}->$by;
        } elsif ( $value_ref eq 'ARRAY' ) {

            # Don't modify the original reference, it isn't polite
            $args{value} = [ @{ $args{value} } ];
            map {
                my $by = (defined $column_obj and defined $column_obj->by)
                            ? $column_obj->by
                            : 'id';
                $_ = (
                      ( ref $_ && $_->isa('Jifty::DBI::Record') )
                    ? ( $_->$by )
                    : $_
                )
            } @{ $args{value} };
        }
    }

    #since we're changing the search criteria, we need to redo the search
    $self->redo_search();

    #If it's a like, we supply the %s around the search term
    if ( $args{'operator'} =~ /MATCHES/i ) {
        $args{'value'} = "%" . $args{'value'} . "%";
    } elsif ( $args{'operator'} =~ /STARTS_?WITH/i ) {
        $args{'value'} = $args{'value'} . "%";
    } elsif ( $args{'operator'} =~ /ENDS_?WITH/i ) {
        $args{'value'} = "%" . $args{'value'};
    }
    $args{'operator'} =~ s/(?:MATCHES|ENDS_?WITH|STARTS_?WITH)/LIKE/i;

    # Force the value to NULL (non-quoted) if the operator is IS.
    if ($args{'operator'} =~ /^IS(\s*NOT)?$/i) {



( run in 1.286 second using v1.01-cache-2.11-cpan-800906f7e73 )