Skip to content

Instantly share code, notes, and snippets.

@jef-sure
Last active November 11, 2017 19:40
Show Gist options
  • Select an option

  • Save jef-sure/b6faaff5257397ca91fba2502b6a8474 to your computer and use it in GitHub Desktop.

Select an option

Save jef-sure/b6faaff5257397ca91fba2502b6a8474 to your computer and use it in GitHub Desktop.
use FindBin qw($Bin);
use lib "$Bin/../lib";
use lib "$Bin/../t";
use Test::More;
use DBI;
use Time::HiRes 'time';
use Test::mysqld;
use feature 'say';
use DBIx::MysqlAsync;
SKIP: {
my $mysqld = Test::mysqld->new(
my_cnf => {
'skip-networking' => '', # no TCP socket
}
) or skip "no MySQL found";
sub db_connect {
DBI->connect(
$mysqld->dsn(),
undef, undef,
{ AutoCommit => 1,
RootClass => 'DBIx::MysqlAsync'
}
);
}
# my $cv = AE::cv;
my $start_time = time;
ok(my $dbh = db_connect(), 'connected');
$dbh->do('SELECT SLEEP(2)');
while (!$dbh->mysql_async_ready) {
say 'not ready yet!';
sleep 1;
}
my $rows = $dbh->mysql_async_result;
print "total run time: " . (time - $start_time) . " sec\n";
}
done_testing();
{
package DBIx::MysqlAsync;
our $VERSION = "0.01";
}
{
package DBIx::MysqlAsync::db;
use DBD::mysql;
use DBI;
use base qw'DBI::db';
use strict;
use warnings;
sub prepare {
my ($dbh, $statement, $attrs) = @_;
return undef if !defined $statement;
$attrs //={};
$attrs->{async} = 1;
print "must be async\n";
DBD::mysql::db::prepare($dbh, $statement, $attrs);
}
sub do {
my ($dbh, $statement, $attr, @params) = @_;
my $sth = prepare($dbh, $statement, $attr) or return ;
$sth->execute(@params) or return ;
my $rows = $sth->rows;
($rows == 0) ? "0E0" : $rows;
}
}
{
package DBIx::MysqlAsync::st;
use base 'DBI::st';
sub execute {
goto &DBD::mysql::st::execute;
}
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment