… | |
… | |
11 | my $dbh = new AnyEvent::DBI "DBI:SQLite:dbname=test.db", "", ""; |
11 | my $dbh = new AnyEvent::DBI "DBI:SQLite:dbname=test.db", "", ""; |
12 | |
12 | |
13 | $dbh->exec ("select * from test where num=?", 10, sub { |
13 | $dbh->exec ("select * from test where num=?", 10, sub { |
14 | my ($dbh, $rows, $rv) = @_; |
14 | my ($dbh, $rows, $rv) = @_; |
15 | |
15 | |
16 | $rows or die "failure: $@"; |
16 | $#_ or die "failure: $@"; |
17 | |
17 | |
18 | print "@$_\n" |
18 | print "@$_\n" |
19 | for @$rows; |
19 | for @$rows; |
20 | |
20 | |
21 | $cv->broadcast; |
21 | $cv->broadcast; |
… | |
… | |
72 | |
72 | |
73 | use Errno (); |
73 | use Errno (); |
74 | use Fcntl (); |
74 | use Fcntl (); |
75 | use POSIX (); |
75 | use POSIX (); |
76 | |
76 | |
77 | our $VERSION = '1.19'; |
77 | our $VERSION = '2.0'; |
78 | |
78 | |
79 | our $FD_MAX = eval { POSIX::sysconf (&POSIX::_SC_OPEN_MAX) - 1 } || 1023; |
79 | our $FD_MAX = eval { POSIX::sysconf (&POSIX::_SC_OPEN_MAX) - 1 } || 1023; |
80 | |
80 | |
81 | # this is the forked server code, could/should be bundled as it's own file |
81 | # this is the forked server code, could/should be bundled as it's own file |
82 | |
82 | |
… | |
… | |
124 | |
124 | |
125 | sub req_func { |
125 | sub req_func { |
126 | my (undef, $arg_string, $function) = @{+shift}; |
126 | my (undef, $arg_string, $function) = @{+shift}; |
127 | my @args = eval $arg_string; |
127 | my @args = eval $arg_string; |
128 | |
128 | |
129 | die "Bad func () arg string: $@" |
129 | die "error evaling \$dbh->func() arg_string: $@" |
130 | if $@; |
130 | if $@; |
131 | |
131 | |
132 | my $rv = $DBH->func (@args, $function); |
132 | my $rc = $DBH->func (@args, $function); |
133 | return [$rv, $DBH->err]; |
133 | return [1, $rc, $DBI::err, $DBI::errstr]; |
134 | } |
134 | } |
135 | |
135 | |
136 | sub serve_fh($$) { |
136 | sub serve_fh($$) { |
137 | my ($fh, $version) = @_; |
137 | my ($fh, $version) = @_; |
138 | |
138 | |
… | |
… | |
234 | |
234 | |
235 | =item exec_server => 1 |
235 | =item exec_server => 1 |
236 | |
236 | |
237 | If you supply an C<exec_server> argument, then the DBI server process will |
237 | If you supply an C<exec_server> argument, then the DBI server process will |
238 | fork and exec another perl interpreter (using C<$^X>) with just the |
238 | fork and exec another perl interpreter (using C<$^X>) with just the |
239 | AnyEvent::DBI proxy running. This will provide the cleanest possible porxy |
239 | AnyEvent::DBI proxy running. This will provide the cleanest possible proxy |
240 | for your database server. |
240 | for your database server. |
241 | |
241 | |
242 | If you do not supply the C<exec_server> argument (or supply it with a |
242 | If you do not supply the C<exec_server> argument (or supply it with a |
243 | false value) then the traditional method of starting the server by forking |
243 | false value) then the traditional method of starting the server by forking |
244 | the current process is used. The forked interpreter will try to clean |
244 | the current process is used. The forked interpreter will try to clean |
… | |
… | |
571 | if successful. |
571 | if successful. |
572 | |
572 | |
573 | If an error occurs and the C<on_error> callback returns, then only C<$dbh> |
573 | If an error occurs and the C<on_error> callback returns, then only C<$dbh> |
574 | will be passed and C<$@> contains the error message. |
574 | will be passed and C<$@> contains the error message. |
575 | |
575 | |
576 | =item $dbh->begin_work ($cb->($dbh[, $success])) |
576 | =item $dbh->begin_work ($cb->($dbh[, $rc])) |
577 | |
577 | |
578 | =item $dbh->commit ($cb->($dbh[, $success])) |
578 | =item $dbh->commit ($cb->($dbh[, $rc])) |
579 | |
579 | |
580 | =item $dbh->rollback ($cb->($dbh[, $success])) |
580 | =item $dbh->rollback ($cb->($dbh[, $rc])) |
581 | |
581 | |
582 | The begin_work, commit, and rollback methods expose the equivalent |
582 | The begin_work, commit, and rollback methods expose the equivalent |
583 | transaction control method of the DBI driver. On success, C<$success> |
583 | transaction control method of the DBI driver. On success, C<$rc> is true. |
584 | is true. |
|
|
585 | |
584 | |
586 | If an error occurs and the C<on_error> callback returns, then only C<$dbh> |
585 | If an error occurs and the C<on_error> callback returns, then only C<$dbh> |
587 | will be passed and C<$@> contains the error message. |
586 | will be passed and C<$@> contains the error message. |
588 | |
587 | |
589 | =item $dbh->func ('string_which_yields_args_when_evaled', $func_name, $cb->($dbh, $result, $handle_error)) |
588 | =item $dbh->func ('string_which_yields_args_when_evaled', $func_name, $cb->($dbh, $rc, $dbi_err, $dbi_errstr)) |
590 | |
589 | |
591 | This gives access to database driver private methods. Because they |
590 | This gives access to database driver private methods. Because they |
592 | are not standard you cannot always depend on the value of C<$result> |
591 | are not standard you cannot always depend on the value of C<$rc> or |
593 | or C<$handle_error>. Check the documentation for your specific |
592 | C<$dbi_err>. Check the documentation for your specific driver/function |
594 | driver/function combination to see what it returns. |
593 | combination to see what it returns. |
595 | |
594 | |
596 | Note that the first argument will be eval'ed to produce the argument list to |
595 | Note that the first argument will be eval'ed to produce the argument list to |
597 | the func() method. This must be done because the serialization protocol |
596 | the func() method. This must be done because the serialization protocol |
598 | between the AnyEvent::DBI server process and your program does not support the |
597 | between the AnyEvent::DBI server process and your program does not support the |
599 | passage of closures. |
598 | passage of closures. |
… | |
… | |
608 | my ($string, $search) = @_; |
607 | my ($string, $search) = @_; |
609 | return index $string, $search; |
608 | return index $string, $search; |
610 | }, |
609 | }, |
611 | }, |
610 | }, |
612 | create_function => sub { |
611 | create_function => sub { |
613 | return $cv->send($@) |
612 | return $cv->send ($@) |
614 | unless $_[0]; |
613 | unless $#_; |
615 | $cv->send (undef, @_[1,2]); |
614 | $cv->send (undef, @_[1,2,3]); |
616 | } |
615 | } |
617 | ); |
616 | ); |
618 | |
617 | |
619 | my ($err,$result,$handle_err) = $cv->recv; |
618 | my ($err,$rc,$errcode,$errstr) = $cv->recv; |
620 | |
619 | |
|
|
620 | die $err if defined $err; |
621 | die "EVAL failed: $err" |
621 | die "EVAL failed: $errstr" |
622 | if $err; |
622 | if $errcode; |
623 | |
623 | |
624 | # otherwise, we can ignore $result and $handle_err for this particular func |
624 | # otherwise, we can ignore $rc and $errcode for this particular func |
625 | |
625 | |
626 | =cut |
626 | =cut |
627 | |
627 | |
628 | for my $cmd_name (qw(exec attr begin_work commit rollback func)) { |
628 | for my $cmd_name (qw(exec attr begin_work commit rollback func)) { |
629 | eval 'sub ' . $cmd_name . '{ |
629 | eval 'sub ' . $cmd_name . '{ |