DB-Object

 view release on metacpan or  search on metacpan

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

    my $self       = CORE::shift( @_ );
    my $serialiser = CORE::shift( @_ ) // '';
    my $class      = CORE::ref( $self );

    # We keep a strict allow-list to avoid accidentally freezing DBI handles or other
    # process-local state.
    my @props = @{$self->{_fields}};

    my $hash = {};
    foreach my $prop ( @props )
    {
        if( CORE::exists( $self->{ $prop } ) &&
            defined( $self->{ $prop } ) &&
            CORE::ref( $self->{ $prop } ) ne 'CODE' )
        {
            $hash->{ $prop } = $self->{ $prop };
        }
    }

    # Return an array reference rather than a list so this works with Sereal and CBOR.
    # Before Sereal version 4.023, Sereal did not support multiple values returned.
    if( $serialiser eq 'Sereal' )
    {
        require Sereal::Encoder;
        require version;

        if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) )
        {
            CORE::return( [$class, $hash] );
        }
    }

    # But Storable wants a list with the first element being the serialised element
    CORE::return( $class, $hash );
}

sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }

sub STORABLE_thaw { return( shift->THAW( @_ ) ); }

sub THAW
{
    # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
    # 'CBOR' as the second value.
    my( $self, undef, @args ) = @_;
    my $ref   = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
    my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
    my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
    my $new;
    # Storable pattern requires to modify the object it created rather than returning a new one
    if( CORE::ref( $self ) )
    {
        foreach( CORE::keys( %$hash ) )
        {
            $self->{ $_ } = CORE::delete( $hash->{ $_ } );
        }
        $new = $self;
    }
    else
    {
        $new = CORE::bless( $hash => $class );
    }
    CORE::return( $new );
}

1;
# NOTE: POD
__END__

=encoding utf-8

=head1 NAME

DB::Object::Query::Clause - SQL Query Clause Object

=head1 SYNOPSIS

    my $clause = DB::Object::Query::Clause->new({
        value => "$field != '$user'",
        generic => "$field != ?",
        type => 'where',
        # or possibly:
        # bind => 
        # {
        #     values => $values_array_ref,
        #     types => $types_array_ref
        # }
    })
    # A DB::Object::Query object
    $clause->query_object( $q );
    $clause->bind->values( $res );
    $clause->bind->types( '' );
    $clause->fields( $field ) if( Scalar::Util::blessed( $field ) && $field->isa( 'DB::Object::Fields::Field' ) );

Merging multiple clauses

    $clause = DB::Object::Query::Clause->new->merge( $dbh->AND( @clauses ) );
    $clause->bind->values( @values ) if( $bind );
    $clause->bind->types( @types ) if( $bind );

Get the clause stringified

    my $sql = "SELECT * FROM my_table WHERE $clause";

This could become something like:

    SELECT * FROM my_table WHERE username != 'joe' AND username != 'bob'

However if binding values is activated, this would rather become:

    SELECT * FROM my_table WHERE username != ? AND username != ?

And the associated values would be automatically bound to the query upon execution

=head1 VERSION

v1.1.1

=head1 DESCRIPTION

The purpose of this module is to contain various attributes of a SQL clause so that it can be accessed and manipulated flexibly.



( run in 1.729 second using v1.01-cache-2.11-cpan-f56aa216473 )