ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-DBI/DBI.pm
(Generate patch)

Comparing AnyEvent-DBI/DBI.pm (file contents):
Revision 1.11 by root, Sun Jun 28 14:59:51 2009 UTC vs.
Revision 1.13 by root, Sat Oct 23 21:47:13 2010 UTC

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
73use Errno (); 73use Errno ();
74use Fcntl (); 74use Fcntl ();
75use POSIX (); 75use POSIX ();
76 76
77our $VERSION = '1.19'; 77our $VERSION = '2.0';
78 78
79our $FD_MAX = eval { POSIX::sysconf (&POSIX::_SC_OPEN_MAX) - 1 } || 1023; 79our $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
125sub req_func { 125sub 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
136sub serve_fh($$) { 136sub 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
237If you supply an C<exec_server> argument, then the DBI server process will 237If you supply an C<exec_server> argument, then the DBI server process will
238fork and exec another perl interpreter (using C<$^X>) with just the 238fork and exec another perl interpreter (using C<$^X>) with just the
239AnyEvent::DBI proxy running. This will provide the cleanest possible porxy 239AnyEvent::DBI proxy running. This will provide the cleanest possible proxy
240for your database server. 240for your database server.
241 241
242If you do not supply the C<exec_server> argument (or supply it with a 242If you do not supply the C<exec_server> argument (or supply it with a
243false value) then the traditional method of starting the server by forking 243false value) then the traditional method of starting the server by forking
244the current process is used. The forked interpreter will try to clean 244the current process is used. The forked interpreter will try to clean
571if successful. 571if successful.
572 572
573If an error occurs and the C<on_error> callback returns, then only C<$dbh> 573If an error occurs and the C<on_error> callback returns, then only C<$dbh>
574will be passed and C<$@> contains the error message. 574will 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
582The begin_work, commit, and rollback methods expose the equivalent 582The begin_work, commit, and rollback methods expose the equivalent
583transaction control method of the DBI driver. On success, C<$success> 583transaction control method of the DBI driver. On success, C<$rc> is true.
584is true.
585 584
586If an error occurs and the C<on_error> callback returns, then only C<$dbh> 585If an error occurs and the C<on_error> callback returns, then only C<$dbh>
587will be passed and C<$@> contains the error message. 586will 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
591This gives access to database driver private methods. Because they 590This gives access to database driver private methods. Because they
592are not standard you cannot always depend on the value of C<$result> 591are not standard you cannot always depend on the value of C<$rc> or
593or C<$handle_error>. Check the documentation for your specific 592C<$dbi_err>. Check the documentation for your specific driver/function
594driver/function combination to see what it returns. 593combination to see what it returns.
595 594
596Note that the first argument will be eval'ed to produce the argument list to 595Note that the first argument will be eval'ed to produce the argument list to
597the func() method. This must be done because the serialization protocol 596the func() method. This must be done because the serialization protocol
598between the AnyEvent::DBI server process and your program does not support the 597between the AnyEvent::DBI server process and your program does not support the
599passage of closures. 598passage 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
628for my $cmd_name (qw(exec attr begin_work commit rollback func)) { 628for my $cmd_name (qw(exec attr begin_work commit rollback func)) {
629 eval 'sub ' . $cmd_name . '{ 629 eval 'sub ' . $cmd_name . '{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines