NAME

Mojo::MySQL5::Database - Database

SYNOPSIS

use Mojo::MySQL5::Database;

my $db = Mojo::MySQL5::Database->new(
  mysql => $mysql,
  connection => Mojo::MySQL5::Connection->new);

DESCRIPTION

Mojo::MySQL5::Database is a container for Mojo::MySQL5::Connection handles used by Mojo::MySQL5.

ATTRIBUTES

Mojo::MySQL5::Database implements the following attributes.

connection

my $c = $db->connection;
$db   = $db->connection(Mojo::MySQL5::Connection->new);

Database connection used for all queries.

mysql

Mojo::MySQL5 object this database belongs to.

METHODS

Mojo::MySQL5::Database inherits all methods from Mojo::EventEmitter and implements the following ones.

backlog

my $num = $db->backlog;

Number of waiting non-blocking queries.

begin

my $tx = $db->begin;

Begin transaction and return Mojo::MySQL5::Transaction object, which will automatically roll back the transaction unless "commit" in Mojo::MySQL5::Transaction bas been called before it is destroyed.

# Insert rows in a transaction
eval {
  my $tx = $db->begin;
  $db->query('insert into frameworks values (?)', 'Catalyst');
  $db->query('insert into frameworks values (?)', 'Mojolicious');
  $tx->commit;
};
say $@ if $@;

connect

$db->connect;

Connect to MySQL server.

disconnect

$db->disconnect;

Disconnect database connection 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::MySQL5::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 string value for passing to SQL query.

quote_id

 my $escaped = $db->quote_id($id);

Quote identifier for passing to SQL query.

SEE ALSO

Mojo::MySQL5.