DB-Object

 view release on metacpan or  search on metacpan

lib/DB/Object/Query.pm  view on Meta::CPAN

    if( @_ )
    {
        my @params = @_;
        # This will change the belonging of the object $self to the class DB::Object::Prepare so method
        # such as select, insert, update, delete know there are some conditionning clause to be added
        my $table      = $tbl_o->name;
        my $db         = $tbl_o->database;
        my $multi_db   = $tbl_o->prefix_database;
        my $prefix     = $tbl_o->prefix;
        my $fields_ref = $tbl_o->fields;
        my $fields     = CORE::join( '|', keys( %$fields_ref ) );
        my $fields_type = $tbl_o->types;

        my $process_where_condition;
        $process_where_condition = sub
        {
            # my @parameters = @_;
            # my $data = shift( @_ ) if( @_ % 2 && !( scalar( @_ ) == 1 && $self->_is_object( $_[0] ) ) );
            my $data;
            unless( scalar( @_ ) == 1 && $self->_is_object( $_[0] ) )
            {
                $data = shift( @_ ) if( ( @_ % 2 ) && !$self->_is_object( $_[0] ) );
            }
            # Implicit AND operator by default to join multiple fields
            my $agg_op = 'AND';
            my @arg = ();
            if( $self->_is_a( $_[0], 'DB::Object::Operator' ) )
            {
                return( $self->error( "I was expecting an operator object, but got \"", $_[0], "\" instead." ) ) if( !$_[0]->isa( 'DB::Object::Operator' ) );
                $agg_op = $_[0]->operator || return( $self->error( "Unknown operator for \"", $_[0], "\"." ) );
                # We filter out any unknown field
                ( @arg ) = grep( !$self->_is_a( $_ => 'DB::Object::Fields::Unknown' ), $_[0]->value );
            }
            else
            {
                @arg = @_;
            }
            $data      = \@arg if( @arg );
            my $str    = '';
            my @binded = ();
            my @types  = ();
            my $clause;
            # A simple scalar
            if( ref( $data ) eq 'SCALAR' )
            {
                $str = $$data;
            }
            elsif( ref( $data ) )
            {
                my @list = ();
                my( $field, $value );
                while( @arg )
                {
                    if( $self->_is_object( $arg[0] ) && $arg[0]->isa( 'DB::Object::Operator' ) )
                    {
                        my $op_object = shift( @arg );
                        $clause = $process_where_condition->( $op_object );
                        push( @list, $clause );
                        next;
                    }
                    # This is an already formulated clause
                    elsif( $self->_is_object( $arg[0] ) && $arg[0]->isa( 'DB::Object::Query::Clause' ) )
                    {
                        push( @list, shift( @arg ) );
                        next;
                    }
                    # An expression
                    elsif( $self->_is_a( $arg[0] => 'DB::Object::Expression' ) )
                    {
                        push( @list, shift( @arg ) );
                        next;
                    }
                    elsif( $self->_is_object( $arg[0] ) && $arg[0]->isa( 'DB::Object::Fields::Overloaded' ) )
                    {
                        my $f = shift( @arg );
                        my $cl = $self->new_clause(
                            value => $f,
                            type => 'where',
                        );
                        # $cl->bind->types->push( '' ) if( $f->binded );
                        if( $f->placeholder )
                        {
                            my $const = $f->field->datatype->constant;
                            my $offset = $f->index;
#                             $cl->push({
#                                 field   => $f->field,
#                                 # Use the offset expressly provided (e.g. $1 (PostgreSQL) or ?1 (SQLite), or the last position in the array
#                                 ( defined( $offset ) ? ( index => $offset ) : () ),
#                                 ( $const ? ( type => $const ) : () ),
#                             }) || return( $self->pass_error( $cl->error ) );
                            # Even better than instantiating a new DB::Object::Element object, we just use as-is the DB::Object::Fields::Overloaded object, which inherits from DB::Object::Element :)
                            $cl->push( $f );

#                             if( $const )
#                             {
#                                 $cl->bind->types->push( $const );
#                             }
#                             else
#                             {
#                                 $cl->bind->types->push( '' );
#                             }
                        }
                        push( @list, $cl );

                        # If this field value assignment is followed (as a pair) by just a regular field, this is likely a typo.
                        # Catching some typical typo errors for the benefit of the coder (from experience)
                        if( scalar( @arg ) && 
                            $self->_is_a( $arg[0], 'DB::Object::Fields::Field' ) )
                        {
                            warn( "Warning only: found a (proper) field value assignment ($f) followed by a field object '$arg[0]' (never mind the surrounding quotes) (", overload::StrVal( $arg[0] ), "). Did you forget to assign a value such as \$tbl...
                        }
                        next;
                    }
                    # Ignore it
                    elsif( $self->_is_a( $arg[0] => 'DB::Object::Fields::Unknown' ) )
                    {
                        # warn( "Found an unknown field object DB::Object::Fields::Unknown '", $arg[0]->field, "' of table '", $arg[0]->table, "' in WHERE or HAVING clause: ", $arg[0]->error ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
                        # shift( @arg );
                        # next;
                        return( $self->error( "Found an unknown field object DB::Object::Fields::Unknown '", $arg[0]->field, "' of table '", $arg[0]->table, "' in WHERE or HAVING clause: ", $arg[0]->error ) );
                    }



( run in 0.637 second using v1.01-cache-2.11-cpan-39bf76dae61 )