Mojo::Pg::Database - Database
use Mojo::Pg::Database; my $db = Mojo::Pg::Database->new(pg => $pg, dbh => $dbh); $db->query('select * from foo')->hashes->pluck('bar')->join("\n")->say;
Mojo::Pg::Database is a container for DBD::Pg database handles used by Mojo::Pg.
Mojo::Pg::Database inherits all events from Mojo::EventEmitter and can emit the following new ones.
$db->on(notification => sub { my ($db, $name, $pid, $payload) = @_; ... });
Emitted when a notification has been received.
Mojo::Pg::Database implements the following attributes.
my $dbh = $db->dbh; $db = $db->dbh(DBI->new);
DBD::Pg database handle used for all queries.
my $pg = $db->pg; $db = $db->pg(Mojo::Pg->new);
Mojo::Pg object this database belongs to.
my $max = $db->max_statements; $db = $db->max_statements(5);
Maximum number of statement handles to cache for future queries, defaults to 10.
10
Mojo::Pg::Database inherits all methods from Mojo::EventEmitter and implements the following new ones.
my $num = $db->backlog;
Number of waiting non-blocking queries.
my $tx = $db->begin;
Begin transaction and return Mojo::Pg::Transaction object, which will automatically roll back the transaction unless "commit" in Mojo::Pg::Transaction has 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;
$db->disconnect;
Disconnect "dbh" and prevent it from getting cached again.
$db = $db->do('create table foo (bar varchar(255))');
Execute a statement and discard its result.
my $bool = $db->is_listening;
Check if "dbh" is listening for notifications.
$db = $db->listen('foo');
Start listening for notifications when the Mojo::IOLoop event loop is running.
$db = $db->notify('foo'); $db = $db->notify('foo', 'bar');
Send notification with optional payload.
my $bool = $db->ping;
Check database connection.
my $results = $db->query('select * from foo'); my $results = $db->query('insert into foo values (?, ?, ?)', @values);
Execute a blocking statement and return a Mojo::Pg::Results object with the results. The DBD::Pg 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('insert into foo values (?, ?, ?)' => @values => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
$db = $db->unlisten('foo'); $db = $db->unlisten('*');
Stop listening for notifications.
Mojo::Pg, Mojolicious::Guides, http://mojolicio.us.
To install Mojo::Pg, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::Pg
CPAN shell
perl -MCPAN -e shell install Mojo::Pg
For more information on module installation, please visit the detailed CPAN module installation guide.