AnyEvent-DBD-Pg
view release on metacpan or search on metacpan
lib/AnyEvent/DBD/Pg.pm view on Meta::CPAN
if ($self->{db}->{pg_async_status} == 1 ) {
warn "Can't dequeue, while processing query ($self->{current}[0])";
return;
}
#warn "Run dequeue with status=$self->{db}->{pg_async_status}";
return $self->{current} = undef unless @{ $self->{queue} };
my $next = shift @{ $self->{queue} };
my $at = shift @$next;
$self->{queuetime} += time - $at;
my $method = shift @$next;
local $self->{queuing} = 0;
$self->$method(@$next);
}
our $AUTOLOAD;
sub AUTOLOAD {
my ($method) = $AUTOLOAD =~ /([^:]+)$/;
my $self = shift;
die sprintf qq{Can't locate autoloaded object method "%s" (%s) via package "%s" at %s line %s.\n}, $method, $AUTOLOAD, ref $self, (caller)[1,2]
unless exists $METHOD{$method};
my $fetchmethod = $METHOD{$method};
defined $fetchmethod or croak "Method $method not implemented yet";
ref (my $cb = pop) eq 'CODE' or croak "need callback";
if ($self->{db}->{pg_async_status} == 1 or $self->{current} ) {
if ( @{ $self->{queue} } >= $self->{queue_size} - 1 ) {
my $c = 1;
my $counter = ++$self->{querynum};
local $@ = "Query $_[0] run out of queue size $self->{queue_size}";
printf STDERR "\e[036;1mQ$counter\e[0m. [\e[03${c};1m%0.4fs\e[0m] < \e[03${c};1m%s\e[0m > ".("\e[031;1mQuery run out of queue size\e[0m")."\n", 0 , $_[0];
return $cb->();
} else {
warn "Query $_[0] pushed to queue\n" if $self->{debug} > 1;
push @{ $self->{queue} }, [time(), $method, @_,$cb];
return;
}
}
my $query = shift;
my $args = shift || {};
$args->{pg_async} = PG_ASYNC;
my $counter = ++$self->{querynum};
warn "prepare call <$query>( @_ ), async status = ".$self->{db}->{pg_async_status} if $self->{debug} > 2;
$self->{current} = [$query,@_];
$self->{current_start} = time();
weaken $self;
$self or return;
my ($st,$w,$t,$check);
my @watchers;
push @watchers, sub {
$self and $st or warn("no self"), @watchers = (), return 1;
#warn "check status=$self->{db}->{pg_async_status}\n";
if($self->{db}->{pg_async_status} and $st->pg_ready()) {
undef $w;
local $@;
my $res = $st->pg_result;
my $run = time - $self->{current_start};
$self->{querytime} += $run;
my ($diag,$DIE);
if ($self->{debug}) {
$diag = $self->{current}[0];
my @bind = @{ $self->{current} };
shift @bind;
$diag =~ s{\?}{ "'".shift(@bind)."'" }sge;
} else {
$diag = $self->{current}[0];
}
if (!$res) {
$DIE = $self->{db}->errstr;
}
local $self->{qd} = $diag;
if ($self->{debug}) {
my $c = $run < 0.01 ? '2' : $run < 0.1 ? '3' : '1';
my $x = $DIE ? '1' : '6';
printf STDERR "\e[036;1mQ$counter\e[0m. [\e[03${c};1m%0.4fs\e[0m] < \e[03${x};1m%s\e[0m > ".($DIE ? "\e[031;1m$DIE\e[0m" : '')."\n", $run , $diag;
}
local $self->{queuing} = @{ $self->{queue} };
if ($res) {
if (ref $fetchmethod) {
$cb->($res, $st->$fetchmethod($args));
} else {
$cb->($res, $st->$fetchmethod);
# my @res = $st->$fetchmethod;
# undef $st;
# $self->_dequeue();
# $cb->($res, @res);
}
undef $st;
undef $self->{current};
$self->_dequeue();
@watchers = ();
} else {
local $@ = $DIE;
#warn "st failed: $@";
$st->finish;
$cb->();
undef $st;
undef $self->{current};
@watchers = ();
$self->_dequeue();
}
return 1;
}
return 0;
#undef $w;
};
$st = $self->{db}->prepare($query,$args)
and $st->execute(@_)
or return do{
undef $st;
@watchers = ();
local $@ = $self->{db}->errstr;
warn;
$cb->();
$self->_dequeue;
};
# At all we don't need timers for the work, but if we have some bugs, it will help us to find them
push @watchers, AE::timer 1,1, $watchers[0];
push @watchers, AE::io $self->{fh}, 0, $watchers[0];
$watchers[0]() and return;
return;
( run in 0.666 second using v1.01-cache-2.11-cpan-2398b32b56e )