… | |
… | |
9 | my $cv = AnyEvent->condvar; |
9 | my $cv = AnyEvent->condvar; |
10 | |
10 | |
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 ($rows) = @_; |
14 | my ($rows, $rv) = @_; |
15 | |
15 | |
16 | print "@$_\n" |
16 | print "@$_\n" |
17 | for @$rows; |
17 | for @$rows; |
18 | |
18 | |
19 | $cv->broadcast; |
19 | $cv->broadcast; |
… | |
… | |
52 | use DBI (); |
52 | use DBI (); |
53 | |
53 | |
54 | use AnyEvent (); |
54 | use AnyEvent (); |
55 | use AnyEvent::Util (); |
55 | use AnyEvent::Util (); |
56 | |
56 | |
57 | our $VERSION = '1.0'; |
57 | our $VERSION = '1.1'; |
58 | |
58 | |
59 | # this is the forked server code |
59 | # this is the forked server code |
60 | |
60 | |
61 | our $DBH; |
61 | our $DBH; |
62 | |
62 | |
… | |
… | |
71 | sub req_exec { |
71 | sub req_exec { |
72 | my (undef, $st, @args) = @{+shift}; |
72 | my (undef, $st, @args) = @{+shift}; |
73 | |
73 | |
74 | my $sth = $DBH->prepare_cached ($st, undef, 1); |
74 | my $sth = $DBH->prepare_cached ($st, undef, 1); |
75 | |
75 | |
76 | $sth->execute (@args) |
76 | my $rv = $sth->execute (@args) |
77 | or die $sth->errstr; |
77 | or die $sth->errstr; |
78 | |
78 | |
79 | [$sth->fetchall_arrayref] |
79 | [1, $sth->{NUM_OF_FIELDS} ? $sth->fetchall_arrayref : undef, { rv => $rv }] |
80 | } |
80 | } |
81 | |
81 | |
82 | sub serve { |
82 | sub serve { |
83 | my ($fh) = @_; |
83 | my ($fh) = @_; |
84 | |
84 | |
… | |
… | |
111 | } |
111 | } |
112 | } |
112 | } |
113 | } |
113 | } |
114 | }; |
114 | }; |
115 | |
115 | |
|
|
116 | if (AnyEvent::WIN32) { |
116 | 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 |
117 | } |
125 | } |
118 | |
126 | |
119 | =head2 METHODS |
127 | =head2 METHODS |
120 | |
128 | |
121 | =over 4 |
129 | =over 4 |
… | |
… | |
265 | $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $self->{ww_cb}) |
273 | $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $self->{ww_cb}) |
266 | if length $self->{wbuf}; |
274 | if length $self->{wbuf}; |
267 | } |
275 | } |
268 | } |
276 | } |
269 | |
277 | |
270 | =item $dbh->exec ("statement", @args, $cb->($rows, %extra)) |
278 | =item $dbh->exec ("statement", @args, $cb->($rows, $rv, ...)) |
271 | |
279 | |
272 | Executes the given SQL statement with placeholders replaced by |
280 | Executes the given SQL statement with placeholders replaced by |
273 | C<@args>. The statement will be prepared and cached on the server side, so |
281 | C<@args>. The statement will be prepared and cached on the server side, so |
274 | using placeholders is compulsory. |
282 | using placeholders is compulsory. |
275 | |
283 | |
276 | The callback will be called with the result of C<fetchall_arrayref> as |
284 | The callback will be called with the result of C<fetchall_arrayref> as |
277 | first argument and possibly a hash reference with additional information. |
285 | first argument (or C<undef> if the statement wasn't a select statement) |
|
|
286 | and the return value of C<execute> as second argument. Additional |
|
|
287 | arguments might get passed as well. |
278 | |
288 | |
279 | If an error occurs and the C<on_error> callback returns, then no arguments |
289 | If an error occurs and the C<on_error> callback returns, then no arguments |
280 | will be passed and C<$@> contains the error message. |
290 | will be passed and C<$@> contains the error message. |
281 | |
291 | |
282 | =cut |
292 | =cut |