--- AnyEvent-DBI/DBI.pm 2008/06/06 15:44:34 1.2 +++ AnyEvent-DBI/DBI.pm 2008/07/21 02:34:40 1.7 @@ -6,6 +6,23 @@ use AnyEvent::DBI; + my $cv = AnyEvent->condvar; + + my $dbh = new AnyEvent::DBI "DBI:SQLite:dbname=test.db", "", ""; + + $dbh->exec ("select * from test where num=?", 10, sub { + my ($rows, $rv) = @_; + + print "@$_\n" + for @$rows; + + $cv->broadcast; + }); + + # asynchronously do sth. else here + + $cv->wait; + =head1 DESCRIPTION This module is an L user, you need to make sure that you use and @@ -16,6 +33,10 @@ It means that you can run DBI requests in parallel to other tasks. +The overhead for very simple statements ("select 0") is somewhere +around 120% to 200% (dual/single core CPU) compared to an explicit +prepare_cached/execute/fetchrow_arrayref/finish combination. + =cut package AnyEvent::DBI; @@ -33,7 +54,7 @@ use AnyEvent (); use AnyEvent::Util (); -our $VERSION = '1.0'; +our $VERSION = '1.1'; # this is the forked server code @@ -52,10 +73,10 @@ my $sth = $DBH->prepare_cached ($st, undef, 1); - $sth->execute (@args) + my $rv = $sth->execute (@args) or die $sth->errstr; - [$sth->fetchall_arrayref] + [1, $sth->{NUM_OF_FIELDS} ? $sth->fetchall_arrayref : undef, { rv => $rv }] } sub serve { @@ -92,7 +113,15 @@ } }; - kill 9, $$; # no other way on the broken windows platform + if (AnyEvent::WIN32) { + kill 9, $$; # no other way on the broken windows platform + # and the above doesn't even work on windows, it seems the only + # way to is to leak memory and kill 9 from the parent. yay. + } + + require POSIX; + POSIX::_exit (0); + # and the above kills the parent process on windows } =head2 METHODS @@ -186,6 +215,13 @@ } }); + $self->{ww_cb} = sub { + my $len = syswrite $client, $wself->{wbuf} + or return delete $wself->{ww}; + + substr $wself->{wbuf}, 0, $len, ""; + }; + my $pid = fork; if ($pid) { @@ -233,21 +269,22 @@ my $len = syswrite $self->{fh}, $self->{wbuf}; substr $self->{wbuf}, 0, $len, ""; - #TODO, ww_cb # still any left? then install a write watcher $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $self->{ww_cb}) if length $self->{wbuf}; } } -=item $dbh->exec ("statement", @args, $cb->($rows, %extra)) +=item $dbh->exec ("statement", @args, $cb->($rows, $rv, ...)) Executes the given SQL statement with placeholders replaced by C<@args>. The statement will be prepared and cached on the server side, so using placeholders is compulsory. The callback will be called with the result of C as -first argument and possibly a hash reference with additional information. +first argument (or C if the statement wasn't a select statement) +and the return value of C as second argument. Additional +arguments might get passed as well. If an error occurs and the C callback returns, then no arguments will be passed and C<$@> contains the error message. @@ -269,8 +306,8 @@ =head1 AUTHOR - Marc Lehmann - http://home.schmorp.de/ + Marc Lehmann + http://home.schmorp.de/ =cut