Mojo::Pg::Database(3) Database

SYNOPSIS


use Mojo::Pg::Database;
my $db = Mojo::Pg::Database->new(pg => $pg, dbh => $dbh);
$db->query('select * from foo')
->hashes->map(sub { $_->{bar} })->join("\n")->say;

DESCRIPTION

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

EVENTS

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

close

  $db->on(close => sub {
    my $db = shift;
    ...
  });

Emitted when the database connection gets closed while waiting for notifications.

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($dbh);

DBD::Pg database handle used for all queries.

  # Use DBI utility methods
  my $quoted = $db->dbh->quote_identifier('foo.bar');

pg

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

Mojo::Pg object this database belongs to.

METHODS

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

begin

  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.

  # 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 $@;

disconnect

  $db->disconnect;

Disconnect ``dbh'' and prevent it from getting reused.

dollar_only

  $db = $db->dollar_only;

Activate "pg_placeholder_dollaronly" for next ``query'' call and allow "?" to be used as an operator.

  # Check for a key in a JSON document
  $db->dollar_only->query('select * from foo where bar ? $1', 'baz')
    ->expand->hashes->map(sub { $_->{bar}{baz} })->join("\n")->say;

is_listening

  my $bool = $db->is_listening;

Check if ``dbh'' is listening for notifications.

listen

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

Subscribe to a channel and receive ``notification'' events when the Mojo::IOLoop event loop is running.

notify

  $db = $db->notify('foo');
  $db = $db->notify(foo => 'bar');

Notify a channel.

pid

  my $pid = $db->pid;

Return the process 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);
  my $results = $db->query('select ?::json as foo', {json => {bar => 'baz'}});

Execute a blocking SQL <http://www.postgresql.org/docs/current/static/sql.html> statement and return a Mojo::Pg::Results object with the results. The DBD::Pg statement handle will be automatically reused when it is not active anymore, to increase the performance of future queries. You can also append a callback to perform operations non-blocking.

  $db->query('insert into foo values (?, ?, ?)' => @values => sub {
    my ($db, $err, $results) = @_;
    ...
  });
  Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

Hash reference arguments containing a value named "json", will be encoded to JSON text with ``to_json'' in Mojo::JSON. To accomplish the reverse, you can use the method ``expand'' in Mojo::Pg::Results, which automatically decodes all fields of the types "json" and "jsonb" with ``from_json'' in Mojo::JSON to Perl values.

  # "I ♥ Mojolicious!"
  $db->query('select ?::jsonb as foo', {json => {bar => 'I ♥ Mojolicious!'}})
    ->expand->hash->{foo}{bar};

tables

  my $tables = $db->tables;

Return table and view names for this database, that are visible to the current user and not internal, as an array reference.

  # Names of all tables
  say for @{$db->tables};

unlisten

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

Unsubscribe from a channel, "*" can be used to unsubscribe from all channels.