Mojo::Pg::Database - Database
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;
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(close => sub { my $db = shift; ... });
Emitted when the database connection gets closed while waiting for notifications.
$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($dbh);
DBD::Pg database handle used for all queries.
# Use DBI utility methods my $quoted = $db->dbh->quote_identifier('foo.bar');
my $pg = $db->pg; $db = $db->pg(Mojo::Pg->new);
Mojo::Pg object this database belongs to.
my $class = $db->results_class; $db = $db->results_class('MyApp::Results');
Class to be used by "query", defaults to Mojo::Pg::Results. Note that this class needs to have already been loaded before "query" is called.
Mojo::Pg::Database inherits all methods from Mojo::EventEmitter and implements the following new ones.
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->insert('frameworks', {name => 'Catalyst'}); $db->insert('frameworks', {name => 'Mojolicious'}); $tx->commit; }; say $@ if $@;
my $results = $db->delete($table, \%where, \%options);
Generate a DELETE statement with "abstract" in Mojo::Pg (usually an SQL::Abstract object) and execute it with "query". You can also append a callback to perform operations non-blocking.
DELETE
$db->delete(some_table => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Use all the same argument variations you would pass to the delete method of SQL::Abstract.
delete
# "delete from some_table" $db->delete('some_table'); # "delete from some_table where foo = 'bar'" $db->delete('some_table', {foo => 'bar'}); # "delete from some_table where foo like '%test%'" $db->delete('some_table', {foo => {-like => '%test%'}}); # "delete from some_table where foo = 'bar' returning id" $db->delete('some_table', {foo => 'bar'}, {returning => 'id'});
$db->disconnect;
Disconnect "dbh" and prevent it from getting reused.
$db = $db->dollar_only;
Activate pg_placeholder_dollaronly for next "query" call and allow ? to be used as an operator.
pg_placeholder_dollaronly
?
# 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;
my $results = $db->insert($table, \@values || \%fieldvals, \%options);
Generate an INSERT statement with "abstract" in Mojo::Pg (usually an SQL::Abstract object) and execute it with "query". You can also append a callback to perform operations non-blocking.
INSERT
$db->insert(some_table => {foo => 'bar'} => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Use all the same argument variations you would pass to the insert method of SQL::Abstract.
insert
# "insert into some_table (foo, baz) values ('bar', 'yada')" $db->insert('some_table', {foo => 'bar', baz => 'yada'}); # "insert into some_table (foo) values ({1,2,3})" $db->insert('some_table', {foo => [1, 2, 3]}); # "insert into some_table (foo) values ('bar') returning id" $db->insert('some_table', {foo => 'bar'}, {returning => 'id'}); # "insert into some_table (foo) values ('bar') returning id, foo" $db->insert('some_table', {foo => 'bar'}, {returning => ['id', 'foo']});
my $bool = $db->is_listening;
Check if "dbh" is listening for notifications.
$db = $db->listen('foo');
Subscribe to a channel and receive "notification" events when the Mojo::IOLoop event loop is running.
$db = $db->notify('foo'); $db = $db->notify(foo => 'bar');
Notify a channel.
my $pid = $db->pid;
Return the process id of the backend server process.
my $bool = $db->ping;
Check database connection.
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 statement and return a results object based on "results_class" (which is usually Mojo::Pg::Results) with the query 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.
json
jsonb
# "I ♥ Mojolicious!" $db->query('select ?::jsonb as foo', {json => {bar => 'I ♥ Mojolicious!'}}) ->expand->hash->{foo}{bar};
Hash reference arguments containing values named type and value, can be used to bind specific DBD::Pg data types to placeholders.
type
value
# Insert binary data use DBD::Pg ':pg_types'; $db->query('insert into bar values (?)', {type => PG_BYTEA, value => $bytes});
my $results = $db->select($source, $fields, $where, $order);
Generate a SELECT statement with "abstract" in Mojo::Pg (usually an SQL::Abstract object) and execute it with "query". You can also append a callback to perform operations non-blocking.
SELECT
$db->select(some_table => ['foo'] => {bar => 'yada'} => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Use all the same argument variations you would pass to the select method of SQL::Abstract.
select
# "select * from some_table" $db->select('some_table'); # "select id, foo from some_table" $db->select('some_table', ['id', 'foo']); # "select * from some_table where foo = 'bar'" $db->select('some_table', undef, {foo => 'bar'}); # "select * from some_table where foo = 'bar' order by id desc" $db->select('some_table', undef, {foo => 'bar'}, {-desc => 'id'}); # "select * from some_table where foo like '%test%'" $db->select('some_table', undef, {foo => {-like => '%test%'}});
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};
$db = $db->unlisten('foo'); $db = $db->unlisten('*');
Unsubscribe from a channel, * can be used to unsubscribe from all channels.
*
my $results = $db->update($table, \%fieldvals, \%where, \%options);
Generate an UPDATE statement with "abstract" in Mojo::Pg (usually an SQL::Abstract object) and execute it with "query". You can also append a callback to perform operations non-blocking.
UPDATE
$db->update(some_table => {foo => 'baz'} => {foo => 'bar'} => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Use all the same argument variations you would pass to the update method of SQL::Abstract.
update
# "update some_table set foo = 'bar' where id = 23" $db->update('some_table', {foo => 'bar'}, {id => 23}); # "update some_table set foo = {1,2,3} where id = 23" $db->update('some_table', {foo => [1, 2, 3]}, {id => 23}); # "update some_table set foo = 'bar' where foo like '%test%'" $db->update('some_table', {foo => 'bar'}, {foo => {-like => '%test%'}}); # "update some_table set foo = 'bar' where id = 23 returning id" $db->update('some_table', {foo => 'bar'}, {id => 23}, {returning => 'id'});
Mojo::Pg, Mojolicious::Guides, http://mojolicious.org.
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.