NAME
Mojo::mysql::Database - Database
SYNOPSIS
use Mojo::mysql::Database;
my $db = Mojo::mysql::Database->new(mysql => $mysql, dbh => $dbh);
DESCRIPTION
Mojo::mysql::Database is a container for database handles used by Mojo::MySQL.
ATTRIBUTES
Mojo::mysql::Database implements the following attributes.
dbh
my $dbh = $db->dbh;
$db = $db->dbh(DBI->new);
Database handle used for all queries.
mysql
my $mysql = $db->mysql;
$db = $db->mysql(Mojo::mysql->new);
Mojo::mysql object this database belongs to.
max_statements
my $max = $db->max_statements;
$db = $db->max_statements(5);
Maximum number of statement handles to cache for future queries, defaults to 10
.
METHODS
Mojo::mysql::Database inherits all methods from Mojo::EventEmitter and implements the following new ones.
backlog
my $num = $db->backlog;
Number of waiting non-blocking queries.
begin
my $tx = $db->begin;
Begin transaction and return Mojo::mysql::Transaction object, which will automatically roll back the transaction unless "commit" in Mojo::mysql::Transaction bas been called before it is destroyed.
my $tx = $db->begin;
$db->query('insert into names values (?)', 'Baerbel');
$db->query('insert into names values (?)', 'Wolfgangl');
$tx->commit;
disconnect
$db->disconnect;
Disconnect database handle and prevent it from getting cached again.
do
$db = $db->do('create table foo (bar varchar(255))');
Execute a statement and discard its result.
ping
my $bool = $db->ping;
Check database connection.
query
my $results = $db->query('select * from foo');
my $results = $db->query('insert into foo values (?, ?, ?)', @values);
Execute a statement and return a Mojo::mysql::Results object with the results. The statement handle will be automatically cached again when that object is destroyed, so future queries can reuse it to increase performance. You can also append a callback to perform operation non-blocking.
$db->query('select * from foo' => sub {
my ($db, $err, $results) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;