<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package CPANPLUS::Internals::Source::SQLite::Tie;
use deprecate;

use strict;
use warnings;

use CPANPLUS::Error;
use CPANPLUS::Module;
use CPANPLUS::Module::Fake;
use CPANPLUS::Module::Author::Fake;
use CPANPLUS::Internals::Constants;

use Params::Check               qw[check];
use Module::Load::Conditional   qw[can_load];
use Locale::Maketext::Simple    Class =&gt; 'CPANPLUS', Style =&gt; 'gettext';

use vars qw[@ISA $VERSION];
$VERSION = "0.9135";

require Tie::Hash;
push @ISA, 'Tie::StdHash';


sub TIEHASH {
    my $class = shift;
    my %hash  = @_;

    my $tmpl = {
        dbh     =&gt; { required =&gt; 1 },
        table   =&gt; { required =&gt; 1 },
        key     =&gt; { required =&gt; 1 },
        cb      =&gt; { required =&gt; 1 },
        offset  =&gt; { default  =&gt; 0 },
    };

    my $args = check( $tmpl, \%hash ) or return;
    my $obj  = bless { %$args, store =&gt; {} } , $class;

    return $obj;
}

sub FETCH {
    my $self    = shift;
    my $key     = shift or return;
    my $dbh     = $self-&gt;{dbh};
    my $cb      = $self-&gt;{cb};
    my $table   = $self-&gt;{table};


    ### did we look this one up before?
    if( my $obj = $self-&gt;{store}-&gt;{$key} ) {
        return $obj;
    }

    my $res  = $dbh-&gt;query(
                    "SELECT * from $table where $self-&gt;{key} = ?", $key
                ) or do {
                    error( $dbh-&gt;error );
                    return;
                };

    my $href = $res-&gt;hash;

    ### get rid of the primary key
    delete $href-&gt;{'id'};

    ### no results?
    return unless keys %$href;

    ### expand author if needed
    ### XXX no longer generic :(
    if( $table eq 'module' ) {
        $href-&gt;{author} = $cb-&gt;author_tree( $href-&gt;{author } ) or return;
    }

    my $class = {
        module  =&gt; 'CPANPLUS::Module',
        author  =&gt; 'CPANPLUS::Module::Author',
    }-&gt;{ $table };

    my $obj = $self-&gt;{store}-&gt;{$key} = $class-&gt;new( %$href, _id =&gt; $cb-&gt;_id );

    return $obj;
}

sub STORE {
    my $self = shift;
    my $key  = shift;
    my $val  = shift;

    $self-&gt;{store}-&gt;{$key} = $val;
}

1;

sub FIRSTKEY {
    my $self = shift;
    my $dbh  = $self-&gt;{'dbh'};

    my $res  = $dbh-&gt;query(
                    "select $self-&gt;{key} from $self-&gt;{table} order by $self-&gt;{key} limit 1"
               );

    $self-&gt;{offset} = 0;

    my $key = $res-&gt;flat-&gt;[0];

    return $key;
}

sub NEXTKEY {
    my $self = shift;
    my $dbh  = $self-&gt;{'dbh'};

    my $res  = $dbh-&gt;query(
                    "select $self-&gt;{key} from $self-&gt;{table} ".
                    "order by $self-&gt;{key} limit 1 offset $self-&gt;{offset}"
               );

    $self-&gt;{offset} +=1;

    my $key = $res-&gt;flat-&gt;[0];
    my $val = $self-&gt;FETCH( $key );

    ### use each() semantics
    return wantarray ? ( $key, $val ) : $key;
}

sub EXISTS   { !!$_[0]-&gt;FETCH( $_[1] ) }

sub SCALAR   {
    my $self = shift;
    my $dbh  = $self-&gt;{'dbh'};

    my $res  = $dbh-&gt;query( "select count(*) from $self-&gt;{table}" );

    return $res-&gt;flat;
}

### intentionally left blank
sub DELETE   {  }
sub CLEAR    {  }

</pre></body></html>