NAME

Mojo::Pg::Database - Database

SYNOPSIS

use Mojo::Pg::Database;

my $db = Mojo::Pg::Database->new(pg => $pg, dbh => $dbh);

DESCRIPTION

Mojo::Pg::Database is a container for database handles used by Mojo::Pg.

EVENTS

Mojo::Pg::Database inherits all events from Mojo::EventEmitter and can emit the following new ones.

notification

$db->on(notification => sub {
  my ($db, $name, $pid, $payload) = @_;
  ...
});

Emitted when a notification has been received.

ATTRIBUTES

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

dbh

my $dbh = $db->dbh;
$db     = $db->dbh(DBI->new);

Database handle used for all queries.

pg

my $pg = $db->pg;
$db    = $db->pg(Mojo::Pg->new);

Mojo::Pg 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::Pg::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

$db = $db->begin;

Begin transaction.

commit

$db->commit;

Commit transaction.

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.

is_listening

my $bool = $db->is_listening;

Check if database handle is listening of notifications.

listen

$db = $db->listen('foo');

Start listening for notifications when the Mojo::IOLoop event loop is running.

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::Pg::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;

rollback

$db->rollback;

Rollback transaction.

unlisten

$db = $db->unlisten('foo');
$db = $db->unlisten('*');

Stop listening for notifications.

SEE ALSO

Mojo::Pg, Mojolicious::Guides, http://mojolicio.us.