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.
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.
# Add names in a transaction
eval {
my $tx = $db->begin;
$db->query('insert into names values (?)', 'Baerbel');
$db->query('insert into names values (?)', 'Wolfgang');
$tx->commit;
};
say $@ if $@;
disconnect
$db->disconnect;
Disconnect database handle and prevent it from getting cached again.
pid
my $pid = $db->pid;
Return the connection id of the backend server process.
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 blocking statement and return a Mojo::mysql::Results object with the results. 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;
quote
my $escaped = $db->quote($str);
Quote a string literal for use as a literal value in an SQL statement.
quote_id
my $escaped = $db->quote_id($id);
Quote an identifier (table name etc.) for use in an SQL statement.