… | |
… | |
3 | AnyEvent::DBI - asynchronous DBI access |
3 | AnyEvent::DBI - asynchronous DBI access |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent::DBI; |
7 | use AnyEvent::DBI; |
|
|
8 | |
|
|
9 | my $cv = AnyEvent->condvar; |
|
|
10 | |
|
|
11 | my $dbh = new AnyEvent::DBI "DBI:SQLite:dbname=test.db", "", ""; |
|
|
12 | |
|
|
13 | $dbh->exec ("select * from test where num=?", 10, sub { |
|
|
14 | my ($rows, $rv) = @_; |
|
|
15 | |
|
|
16 | print "@$_\n" |
|
|
17 | for @$rows; |
|
|
18 | |
|
|
19 | $cv->broadcast; |
|
|
20 | }); |
|
|
21 | |
|
|
22 | # asynchronously do sth. else here |
|
|
23 | |
|
|
24 | $cv->wait; |
8 | |
25 | |
9 | =head1 DESCRIPTION |
26 | =head1 DESCRIPTION |
10 | |
27 | |
11 | This module is an L<AnyEvent> user, you need to make sure that you use and |
28 | This module is an L<AnyEvent> user, you need to make sure that you use and |
12 | run a supported event loop. |
29 | run a supported event loop. |
13 | |
30 | |
14 | This module implements asynchronous DBI access my forking or executing |
31 | This module implements asynchronous DBI access by forking or executing |
15 | separate "DBI-Server" processes and sending them requests. |
32 | separate "DBI-Server" processes and sending them requests. |
16 | |
33 | |
17 | It means that you can run DBI requests in parallel to other tasks. |
34 | It means that you can run DBI requests in parallel to other tasks. |
18 | |
35 | |
19 | The overhead for very simple statements ("select 0") is somewhere |
36 | The overhead for very simple statements ("select 0") is somewhere |
20 | around 120% to 200% (single/dual core CPU) compared to an explicit |
37 | around 120% to 200% (dual/single core CPU) compared to an explicit |
21 | prepare_cached/execute/fetchrow_arrayref/finish combination. |
38 | prepare_cached/execute/fetchrow_arrayref/finish combination. |
22 | |
39 | |
23 | =cut |
40 | =cut |
24 | |
41 | |
25 | package AnyEvent::DBI; |
42 | package AnyEvent::DBI; |
… | |
… | |
35 | use DBI (); |
52 | use DBI (); |
36 | |
53 | |
37 | use AnyEvent (); |
54 | use AnyEvent (); |
38 | use AnyEvent::Util (); |
55 | use AnyEvent::Util (); |
39 | |
56 | |
40 | our $VERSION = '1.0'; |
57 | our $VERSION = '1.1'; |
41 | |
58 | |
42 | # this is the forked server code |
59 | # this is the forked server code |
43 | |
60 | |
44 | our $DBH; |
61 | our $DBH; |
45 | |
62 | |
… | |
… | |
54 | sub req_exec { |
71 | sub req_exec { |
55 | my (undef, $st, @args) = @{+shift}; |
72 | my (undef, $st, @args) = @{+shift}; |
56 | |
73 | |
57 | my $sth = $DBH->prepare_cached ($st, undef, 1); |
74 | my $sth = $DBH->prepare_cached ($st, undef, 1); |
58 | |
75 | |
59 | $sth->execute (@args) |
76 | my $rv = $sth->execute (@args) |
60 | or die $sth->errstr; |
77 | or die $sth->errstr; |
61 | |
78 | |
62 | [$sth->fetchall_arrayref] |
79 | [1, $sth->{NUM_OF_FIELDS} ? $sth->fetchall_arrayref : undef, { rv => $rv }] |
63 | } |
80 | } |
64 | |
81 | |
65 | sub serve { |
82 | sub serve { |
66 | my ($fh) = @_; |
83 | my ($fh) = @_; |
67 | |
84 | |
… | |
… | |
94 | } |
111 | } |
95 | } |
112 | } |
96 | } |
113 | } |
97 | }; |
114 | }; |
98 | |
115 | |
|
|
116 | if (AnyEvent::WIN32) { |
99 | kill 9, $$; # no other way on the broken windows platform |
117 | kill 9, $$; # no other way on the broken windows platform |
|
|
118 | # and the above doesn't even work on windows, it seems the only |
|
|
119 | # way to is to leak memory and kill 9 from the parent. yay. |
|
|
120 | } |
|
|
121 | |
|
|
122 | require POSIX; |
|
|
123 | POSIX::_exit (0); |
|
|
124 | # and the above kills the parent process on windows |
100 | } |
125 | } |
101 | |
126 | |
102 | =head2 METHODS |
127 | =head2 METHODS |
103 | |
128 | |
104 | =over 4 |
129 | =over 4 |
… | |
… | |
225 | delete $self->{ww}; |
250 | delete $self->{ww}; |
226 | delete $self->{fh}; |
251 | delete $self->{fh}; |
227 | |
252 | |
228 | $@ = $error; |
253 | $@ = $error; |
229 | |
254 | |
|
|
255 | if ($self->{on_error}) { |
230 | $self->{on_error}($self, $filename, $line, $fatal) |
256 | $self->{on_error}($self, $filename, $line, $fatal); |
231 | if $self->{on_error}; |
257 | return unless $fatal; |
|
|
258 | } |
232 | |
259 | |
233 | die "$error at $filename, line $line\n"; |
260 | die "$error at $filename, line $line\n"; |
234 | } |
261 | } |
235 | |
262 | |
236 | sub _req { |
263 | sub _req { |
… | |
… | |
248 | $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $self->{ww_cb}) |
275 | $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $self->{ww_cb}) |
249 | if length $self->{wbuf}; |
276 | if length $self->{wbuf}; |
250 | } |
277 | } |
251 | } |
278 | } |
252 | |
279 | |
253 | =item $dbh->exec ("statement", @args, $cb->($rows, %extra)) |
280 | =item $dbh->exec ("statement", @args, $cb->($rows, $rv, ...)) |
254 | |
281 | |
255 | Executes the given SQL statement with placeholders replaced by |
282 | Executes the given SQL statement with placeholders replaced by |
256 | C<@args>. The statement will be prepared and cached on the server side, so |
283 | C<@args>. The statement will be prepared and cached on the server side, so |
257 | using placeholders is compulsory. |
284 | using placeholders is compulsory. |
258 | |
285 | |
259 | The callback will be called with the result of C<fetchall_arrayref> as |
286 | The callback will be called with the result of C<fetchall_arrayref> as |
260 | first argument and possibly a hash reference with additional information. |
287 | first argument (or C<undef> if the statement wasn't a select statement) |
|
|
288 | and the return value of C<execute> as second argument. Additional |
|
|
289 | arguments might get passed as well. |
261 | |
290 | |
262 | If an error occurs and the C<on_error> callback returns, then no arguments |
291 | If an error occurs and the C<on_error> callback returns, then no arguments |
263 | will be passed and C<$@> contains the error message. |
292 | will be passed and C<$@> contains the error message. |
264 | |
293 | |
265 | =cut |
294 | =cut |