Coro-Mysql

 view release on metacpan or  search on metacpan

Mysql.pm  view on Meta::CPAN

use Coro::AnyEvent (); # not necessary with newer Coro versions

# we need this extra indirection, as Coro doesn't support
# calling SLF-like functions via call_sv.

sub readable { &Coro::Handle::FH::readable }
sub writable { &Coro::Handle::FH::writable }

BEGIN {
   our $VERSION = '2.1';

   require XSLoader;
   XSLoader::load Coro::Mysql::, $VERSION;
}

=item $DBH = Coro::Mysql::unblock $DBH

This function takes a DBI database handles and "patches" it
so it becomes compatible to Coro threads.

After that, it returns the patched handle - you should always use the
newly returned database handle.

It is safe to call this function on any database handle (or just about any
value), but it will only do anything to L<DBD::mysql> handles, others are
returned unchanged. That means it is harmless when applied to database
handles of other databases.

It is also safe to pass C<undef>, so code like this is works as expected:

   my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock
      or die $DBI::errstr;

=cut

sub unblock {
   my ($DBH) = @_;

   if ($DBH) {
      my $mariadb = $DBH->{Driver}{Name} eq "MariaDB";
      if ($mariadb or $DBH->{Driver}{Name} eq "mysql") {
         my $sock   = $mariadb ? $DBH->{mariadb_sock} : $DBH->{sock};
         my $sockfd = $mariadb ? $DBH->mariadb_sockfd : $DBH->{sockfd};
         my $cvers  = $mariadb ? $DBH->{mariadb_clientversion} : $DBH->{mysql_clientversion};

         open my $fh, "+>&$sockfd"
            or croak "Coro::Mysql unable to dup mariadb/mysql fd";

         if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) {
            require Coro::Handle;
            $fh = Coro::Handle::unblock ($fh);
         }

         _patch $sock, $sockfd, $cvers, $fh, tied *$$fh;
      }
   }

   $DBH
}

=item $bool = Coro::Mysql::is_unblocked $DBH

Returns true iff the database handle was successfully patched for
non-blocking operations.

=cut

sub is_unblocked {
   my ($DBH) = @_;

   if ($DBH) {
      my $mariadb = $DBH->{Driver}{Name} eq "MariaDB";
      if ($mariadb or $DBH->{Driver}{Name} eq "mysql") {
         my $sock = $mariadb ? $DBH->{mariadb_sock} : $DBH->{sock};
         return _is_patched $sock
      }
   }

   0
}

=item $bool = Coro::Mysql::have_ev

Returns true if this Coro::Mysql installation is compiled with special
support for L<EV> or not.

Even if compiled in, it will only be used if L<EV> is actually the
AnyEvent event backend.

=cut

1;

=back

=head1 USAGE EXAMPLE

This example uses L<PApp::SQL> and L<Coro::on_enter> to implement a
function C<with_db>, that connects to a database, uses C<unblock> on the
resulting handle and then makes sure that C<$PApp::SQL::DBH> is set to the
(per-thread) database handle when the given thread is running (it does not
restore any previous value of $PApp::SQL::DBH, however):

   use Coro;
   use Coro::Mysql;
   use PApp::SQL;

   sub with_db($$$&) {
      my ($database, $user, $pass, $cb) = @_;

      my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock
         or die $DBI::errstr;

      Coro::on_enter { $PApp::SQL::DBH = $dbh };

      $cb->();
   }

This function makes it possible to easily use L<PApp::SQL> with
L<Coro::Mysql>, without worrying about database handles.

   # now start 10 threads doing stuff
   async {

      with_db "DBI:mysql:test", "", "", sub {
         sql_exec "update table set col = 5 where id = 7";

         my $st = sql_exec \my ($id, $name),



( run in 2.240 seconds using v1.01-cache-2.11-cpan-5a3173703d6 )