AnyEvent-DBI
view release on metacpan or search on metacpan
$dbh = new AnyEvent::DBI ...
fork_template => $template;
timeout => seconds
If you supply a timeout parameter (fractional values are
supported), then a timer is started any time the DBI handle
expects a response from the server. This includes connection
setup as well as requests made to the backend. The timeout spans
the duration from the moment the first data is written (or
queued to be written) until all expected responses are returned,
but is postponed for "timeout" seconds each time more data is
returned from the server. If the timer ever goes off then a
fatal error is generated. If you have an "on_error" handler
installed, then it will be called, otherwise your program will
die().
When altering your databases with timeouts it is wise to use
transactions. If you quit due to timeout while performing
insert, update or schema-altering commands you can end up not
knowing if the action was submitted to the database,
complicating recovery.
Timeout errors are always fatal.
Any additional key-value pairs will be rolled into a hash reference
and passed as the final argument to the "DBI->connect (...)" call.
For example, to suppress errors on STDERR and send them instead to
an AnyEvent::Handle you could do:
$dbh = new AnyEvent::DBI
"DBI:mysql:test;mysql_read_default_file=/root/.my.cnf", "", "",
PrintError => 0,
on_error => sub {
$log_handle->push_write ("DBI Error: $@ at $_[1]:$_[2]\n");
};
$dbh->on_error ($cb->($dbh, $filename, $line, $fatal))
Sets (or clears, with "undef") the "on_error" handler.
$dbh->timeout ($seconds)
Sets (or clears, with "undef") the database timeout. Useful to
extend the timeout when you are about to make a really long query.
$dbh->attr ($attr_name[, $attr_value], $cb->($dbh, $new_value))
An accessor for the database handle attributes, such as
"AutoCommit", "RaiseError", "PrintError" and so on. If you provide
an $attr_value (which might be "undef"), then the given attribute
will be set to that value.
The callback will be passed the database handle and the attribute's
value if successful.
If an error occurs and the "on_error" callback returns, then only
$dbh will be passed and $@ contains the error message.
$dbh->exec ("statement", @args, $cb->($dbh, \@rows, $rv))
Executes the given SQL statement with placeholders replaced by
@args. The statement will be prepared and cached on the server side,
so using placeholders is extremely important.
The callback will be called with a weakened AnyEvent::DBI object as
the first argument and the result of "fetchall_arrayref" as (or
"undef" if the statement wasn't a select statement) as the second
argument.
Third argument is the return value from the "DBI->execute" method
call.
If an error occurs and the "on_error" callback returns, then only
$dbh will be passed and $@ contains the error message.
$dbh->stattr ($attr_name, $cb->($dbh, $value))
An accessor for the statement attributes of the most recently
executed statement, such as "NAME" or "TYPE".
The callback will be passed the database handle and the attribute's
value if successful.
If an error occurs and the "on_error" callback returns, then only
$dbh will be passed and $@ contains the error message.
$dbh->begin_work ($cb->($dbh[, $rc]))
$dbh->commit ($cb->($dbh[, $rc]))
$dbh->rollback ($cb->($dbh[, $rc]))
The begin_work, commit, and rollback methods expose the equivalent
transaction control method of the DBI driver. On success, $rc is
true.
If an error occurs and the "on_error" callback returns, then only
$dbh will be passed and $@ contains the error message.
$dbh->func ('string_which_yields_args_when_evaled', $func_name,
$cb->($dbh, $rc, $dbi_err, $dbi_errstr))
This gives access to database driver private methods. Because they
are not standard you cannot always depend on the value of $rc or
$dbi_err. Check the documentation for your specific driver/function
combination to see what it returns.
Note that the first argument will be eval'ed to produce the argument
list to the func() method. This must be done because the
serialization protocol between the AnyEvent::DBI server process and
your program does not support the passage of closures.
Here's an example to extend the query language in SQLite so it
supports an intstr() function:
$cv = AnyEvent->condvar;
$dbh->func (
q{
instr => 2, sub {
my ($string, $search) = @_;
return index $string, $search;
},
},
create_function => sub {
return $cv->send ($@)
unless $#_;
$cv->send (undef, @_[1,2,3]);
}
);
( run in 2.200 seconds using v1.01-cache-2.11-cpan-5a3173703d6 )