DynGig-Util

 view release on metacpan or  search on metacpan

lib/DynGig/Util/EZDB.pm  view on Meta::CPAN

    my $sth = $this->{sth};

    my %count;
    map { $count{$_}++ } keys %$sth, keys %$table;
    map { delete $sth->{$_} } grep { $count{$_} == 1 } keys %$sth;
    map { $this->_statement( $_ ) } grep { $count{$_} == 1 } keys %$table;

    return $this;
}

=head2 stat()

Stat of database file. Also see stat().
Returns ARRAY reference in scalar context.

=cut
sub stat
{
    my $this = shift @_;
    my @stat = stat $this->{fh};

    return wantarray ? @stat : \@stat;
}

sub _statement
{
    my ( $this, $table ) = @_;
    my @attr = map { $SCHEMA[ $_ << 1 ] } 0 .. @SCHEMA / 2 - 1;
    my $key = join ',', @attr;
    my $val = join ',', map { '?' } @attr; 

    my %op =
    (
        truncate => 'DELETE FROM %s',
        select_all => 'SELECT * FROM %s',
        insert => "INSERT OR REPLACE INTO %s ($key) VALUES ($val)",
    );

    map { $op{ 'delete_'.$_ } = "DELETE FROM %s WHERE $_ = ?" } @attr;

    my $dbh = $this->{dbh};
    my $sth = $this->{sth}{$table} ||= {};
    my $neat = DBI::neat( $table );

    map { $sth->{$_} = $dbh->prepare( sprintf $op{$_}, $neat ) } keys %op;
}

sub _execute
{
    my ( $this, $table, $name ) = splice @_, 0, 3;
    my $handle = $this->{sth};

    return unless $table && $handle->{$table};

    my ( $sth, $result ) = $handle->{$table}{$name};

    while ( $sth )
    {   
        $result = eval { $sth->execute( @_ ) };
        last unless $@;
        croak $@ if $@ !~ /locked/;
    }

    return $result, $sth;
}

=head1 SEE ALSO

DBI

=head1 NOTE

See DynGig::Util

=cut

1;

__END__



( run in 0.579 second using v1.01-cache-2.11-cpan-5a3173703d6 )