AnyEvent-DBI-MySQL
view release on metacpan or search on metacpan
lib/AnyEvent/DBI/MySQL.pm view on Meta::CPAN
croak q{callback required};
}
return $sth->SUPER::execute(@args);
}
package AnyEvent::DBI::MySQL::st::ready;
use base qw( DBI::st );
sub execute { return '0E0' };
1; # Magic true value required at end of module
__END__
=encoding utf8
=head1 NAME
AnyEvent::DBI::MySQL - Asynchronous MySQL queries
=head1 VERSION
This document describes AnyEvent::DBI::MySQL version v2.1.0
=head1 SYNOPSIS
use AnyEvent::DBI::MySQL;
# get cached but not in use $dbh
$dbh = AnyEvent::DBI::MySQL->connect(â¦);
# async
$dbh->do(â¦, sub { my ($rv, $dbh) = @_; ⦠});
$sth = $dbh->prepare(â¦);
$sth->execute(â¦, sub { my ($rv, $sth) = @_; ⦠});
$dbh->selectall_arrayref(â¦, sub { my ($ary_ref) = @_; ⦠});
$dbh->selectall_hashref(â¦, sub { my ($hash_ref) = @_; ⦠});
$dbh->selectcol_arrayref(â¦, sub { my ($ary_ref) = @_; ⦠});
$dbh->selectrow_array(â¦, sub { my (@row_ary) = @_; ⦠});
$dbh->selectrow_arrayref(â¦, sub { my ($ary_ref) = @_; ⦠});
$dbh->selectrow_hashref(â¦, sub { my ($hash_ref) = @_; ⦠});
# sync
$rv = $dbh->do('â¦');
$dbh->do('â¦', {async=>0}, sub { my ($rv, $dbh) = @_; ⦠});
=head1 DESCRIPTION
This module is an L<AnyEvent> user, you need to make sure that you use and
run a supported event loop.
This module implements asynchronous MySQL queries using
L<DBD::mysql/"ASYNCHRONOUS QUERIES"> feature. Unlike L<AnyEvent::DBI> it
doesn't spawn any processes.
You shouldn't use C<< {RaiseError=>1} >> with this module and should check
returned values in your callback to detect errors. This is because with
C<< {RaiseError=>1} >> exception will be thrown B<instead> of calling your
callback function, which isn't what you want in most cases.
=head1 INTERFACE
The API is trivial: use it just like usual DBI, but instead of expecting
return value from functions which may block add one extra parameter: callback.
That callback will be executed with usual returned value of used method in
params (only exception is extra $dbh/$sth param in do() and execute() for
convenience).
=head2 SYNCHRONOUS QUERIES
In most cases to make usual synchronous query it's enough to don't provide
callback - use standard DBI params and it will work just like usual DBI.
Only exception is prepare()/execute() pair: you should use
C<< {async=>0} >> attribute for prepare() to have synchronous execute().
For convenience, you can quickly turn asynchronous query to synchronous by
adding C<< {async=>0} >> attribute - you don't have to rewrite code to
remove callback function. In this case your callback will be called
immediately after executing this synchronous query.
=head2 SUPPORTED DBI METHODS
=head3 connect
$dbh = AnyEvent::DBI::MySQL->connect(...);
L<DBD::mysql> support only single asynchronous query per MySQL connection.
To make it easier to overcome this limitation provided connect()
constructor work using DBI->connect_cached() under the hood, but it reuse
only inactive $dbh - i.e. one which you didn't use anymore. So, connect()
guarantee to not return $dbh which is already in use in your code.
For example, in FastCGI or Mojolicious app you can safely use connect() to
get own $dbh per each incoming connection; after you send response and
close this connection that $dbh should automatically go out of scope and
become inactive (you can force this by C<$dbh=undef;>); after that this
$dbh may be returned by connect() when handling next incoming request.
As result you should automatically get a pool of connected $dbh which size
should match peak amount of simultaneously handled CGI requests.
You can flush that $dbh cache as documented by L<DBI> at any time.
NOTE: To implement this caching behavior this module catch DESTROY() for
$dbh and instead of destroying it (and calling $dbh->disconnect()) make it
available for next connect() call in cache. So, if you need to call
$dbh->disconnect() - do it manually and don't expect it to happens
automatically on $dbh DESTROY(), like it work in DBI.
Also, usual limitations for cached connections apply as documented by
L<DBI> (read: don't change $dbh configuration).
=head3 do
$dbh->do(..., sub {
my ($rv, $dbh) = @_;
...
});
=head3 execute
( run in 1.936 second using v1.01-cache-2.11-cpan-f56aa216473 )