DB-Object

 view release on metacpan or  search on metacpan

lib/DB/Object/Query/Elements.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::Elements - Query Elements Manipulation Class

=head1 SYNOPSIS

    use DB::Object::Query::Elements;
    my $elems = DB::Object::Query::Elements->new( debug => 4 ) || 
        die( DB::Object::Query::Elements->error );
    $elems->push( $new_element );
    $elems->push({
        field => $some_field_name,
        value => $some_field_value,
        type  => $sql_type,
        format => $insert_formatting,
        # Could also be $1, $2, ?1, ?2, or other variants supported by driver
        placeholder => '?',
    });
    $elems->merge( $other_elements_object );
    # Clause class inherits from Elements class
    $elems->merge( $some_clause_object );

=head1 VERSION

    v0.2.0

=head1 DESCRIPTION

This class represent a query manipulation class designed to access, store and retrieve L<query elements|DB::Object::Query::Element>

Elements are stored in an internal array object accessible with L</elements>, and all the other methods are used to access or manipulate those elements data.

=head1 CONSTRUCTOR

=head2 new

Takes an hash or hash reference of key-value pairs matching any of the methods below.

Returns a newly instantiated object upon success, or sets an L<error|Module::Generic/error> and return C<undef> or an empty list, depending on the caller's context.

=head1 METHODS

=head2 attach

This takes a query object, and attach it to our current object.

In turn, it will also attach it to all the elements, this object holds with the L<elements|/elements> method.

This returns the current object upon success, or upon error, it sets an L<error object|DB::Object::Exception>, and returns C<undef> in scalar context, or an empty list in list context.



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