AnyEvent-DBI-MySQL

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

{
   "abstract" : "Asynchronous MySQL queries",
   "author" : [
      "Alex Efros <powerman@cpan.org>"
   ],
   "dynamic_config" : 0,
   "generated_by" : "Dist::Zilla version 5.047, Dist::Milla version v1.0.16, CPAN::Meta::Converter version 2.150005",
   "license" : [
      "mit"
   ],
   "meta-spec" : {
      "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",

META.yml  view on Meta::CPAN

---
abstract: 'Asynchronous MySQL queries'
author:
  - 'Alex Efros <powerman@cpan.org>'
build_requires:
  Test::Database: '0'
  Test::Exception: '0'
  Test::More: '0'
  Time::HiRes: '0'
configure_requires:
  Module::Build::Tiny: '0.034'
dynamic_config: 0

README  view on Meta::CPAN

NAME

    AnyEvent::DBI::MySQL - Asynchronous MySQL queries

VERSION

    This document describes AnyEvent::DBI::MySQL version v2.1.0

SYNOPSIS

        use AnyEvent::DBI::MySQL;
    
        # get cached but not in use $dbh

README  view on Meta::CPAN

    
        # sync
        $rv = $dbh->do('…');
        $dbh->do('…', {async=>0}, sub { my ($rv, $dbh) = @_; … });

DESCRIPTION

    This module is an AnyEvent user, you need to make sure that you use and
    run a supported event loop.

    This module implements asynchronous MySQL queries using "ASYNCHRONOUS
    QUERIES" in DBD::mysql feature. Unlike AnyEvent::DBI it doesn't spawn
    any processes.

    You shouldn't use {RaiseError=>1} with this module and should check
    returned values in your callback to detect errors. This is because with
    {RaiseError=>1} exception will be thrown instead of calling your
    callback function, which isn't what you want in most cases.

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).

 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
    {async=>0} attribute for prepare() to have synchronous execute().

    For convenience, you can quickly turn asynchronous query to synchronous
    by adding {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.

 SUPPORTED DBI METHODS

  connect

        $dbh = AnyEvent::DBI::MySQL->connect(...);

    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 $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

lib/AnyEvent/DBI/MySQL.pm  view on Meta::CPAN

    return;
}

sub do { ## no critic(ProhibitBuiltinHomonyms)
    my ($dbh, @args) = @_;
    local $SIG{__WARN__} = sub { (my $msg=shift)=~s/ at .*//ms; carp $msg };
    my $ref = ref $args[-1];
    if ($ref eq 'CODE' || $ref eq 'AnyEvent::CondVar') {
        my $data = $DATA[ $dbh->{$PRIVATE} ];
        if ($data->{cb}) {
            croak q{can't make more than one asynchronous query simultaneously};
        }
        $data->{cb} = pop @args;
        $data->{h} = $dbh;
        weaken($data->{h});
        $args[1] //= {};
        $args[1]->{async} //= 1;
        if (!$args[1]->{async}) {
            my $cb = delete $data->{cb};
            my $h  = delete $data->{h};
            $cb->( $dbh->SUPER::do(@args), $h );

lib/AnyEvent/DBI/MySQL.pm  view on Meta::CPAN

use Carp;
use Scalar::Util qw( weaken );

sub execute {
    my ($sth, @args) = @_;
    local $SIG{__WARN__} = sub { (my $msg=shift)=~s/ at .*//ms; carp $msg };
    my $data = $DATA[ $sth->{$PRIVATE} ];
    my $ref = ref $args[-1];
    if ($ref eq 'CODE' || $ref eq 'AnyEvent::CondVar') {
        if ($data->{cb}) {
            croak q{can't make more than one asynchronous query simultaneously};
        }
        $data->{cb} = pop @args;
        $data->{h} = $sth;
        if (!$sth->{$PRIVATE_async}) {
            my $cb = delete $data->{cb};
            my $h  = delete $data->{h};
            $cb->( $sth->SUPER::execute(@args), $h );
            return;
        }
        $sth->SUPER::execute(@args);

lib/AnyEvent/DBI/MySQL.pm  view on Meta::CPAN

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;

lib/AnyEvent/DBI/MySQL.pm  view on Meta::CPAN

    # 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



( run in 0.363 second using v1.01-cache-2.11-cpan-0d8aa00de5b )