| 1 | { | ||
| 2 | |||
| 3 | package DBIx::MysqlCoroAnyEvent; | ||
| 4 | |||
| 5 | our $VERSION = "0.01"; | ||
| 6 | use base 'DBI'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | DBIx::MysqlCoroAnyEvent - DBD::mysql + Coro + AnyEvent | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use DBI; | ||
| 15 | $dbh = DBI->connect("dbi:mysql:dbname=$dbname", $username, $auth, { RootClass =>"DBIx::MysqlCoroAnyEvent", %rest_attr}); | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | } | ||
| 20 | { | ||
| 21 | package DBIx::MysqlCoroAnyEvent::db; | ||
| 22 | use DBD::mysql; | ||
| 23 | use DBI; | ||
| 24 | use base 'DBI::db'; | ||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | sub prepare { | ||
| 29 | my ($dbh, $statement, @attribs) = @_; | ||
| 30 | return undef if !defined $statement; | ||
| 31 | $attribs[0]{async} = 1; | ||
| 32 | $dbh->SUPER::prepare($statement, @attribs); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub do { | ||
| 36 | my ($dbh, $statement, $attr, @params) = @_; | ||
| 37 | my $sth = prepare($dbh, $statement, $attr) or return; | ||
| 38 | $sth->execute(@params) or return; | ||
| 39 | my $rows = $sth->rows; | ||
| 40 | ($rows == 0) ? "0E0" : $rows; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | { | ||
| 45 | package DBIx::MysqlCoroAnyEvent::st; | ||
| 46 | use Coro; | ||
| 47 | use AnyEvent; | ||
| 48 | use Coro::AnyEvent; | ||
| 49 | use base 'DBI::st'; | ||
| 50 | |||
| 51 | sub execute { | ||
| 52 | my ($sth, @vars) = @_; | ||
| 53 | my $res = $sth->SUPER::execute(@vars); | ||
| 54 | my $dbh = $sth->{Database}; | ||
| 55 | Coro::AnyEvent::readable $dbh->mysql_fd while !$sth->mysql_async_ready; | ||
| 56 | $sth->mysql_async_result; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; |