NAME
Mojo::Pg - Mojolicious ♥ PostgreSQL
SYNOPSIS
use Mojo::Pg;
# Create a table
my $pg = Mojo::Pg->new('dbi:Pg:dbname=test', 'postgres');
$pg->db->do('create table names (name varchar(255))');
# Insert a few rows
my $db = $pg->db;
$db->query('insert into names values (?)', 'Sara');
$db->query('insert into names values (?)', 'Daniel');
# Select all rows
say for $db->query('select * from names')
->hashes->map(sub { $_->{name} })->each;
# Select all rows non-blocking
Mojo::IOLoop->delay(
sub {
my $delay = shift;
$db->query('select * from names' => $delay->begin);
},
sub {
my ($delay, $err, $results) = @_;
say for $results->hashes->map(sub { $_->{name} })->each;
}
)->wait;
DESCRIPTION
Mojo::Pg is a tiny wrapper around DBD::Pg that makes PostgreSQL a lot of fun to use with the Mojolicious real-time web framework.
All cached database handles will be reset automatically if a new process has been forked, this allows multiple processes to share the same Mojo::Pg object safely.
Note that this whole distribution is EXPERIMENTAL and will change without warning!
ATTRIBUTES
Mojo::Pg implements the following attributes.
dsn
my $dsn = $pg->dsn;
$pg = $pg->dsn('dbi:Pg:dbname=foo');
Data Source Name, defaults to dbi:Pg:dbname=test
.
max_connections
my $max = $pg->max_connections;
$pg = $pg->max_connections(3);
Maximum number of idle database handles to cache for future use, defaults to 5
.
options
my $options = $pg->options;
$pg = $pg->options({AutoCommit => 1});
Options for database handles, defaults to activating AutoCommit
as well as RaiseError
and deactivating PrintError
.
password
my $password = $pg->password;
$pg = $pg->password('s3cret');
Database password, defaults to an empty string.
username
my $username = $pg->username;
$pg = $pg->username('sri');
Database username, defaults to an empty string.
METHODS
Mojo::Pg inherits all methods from Mojo::Base and implements the following new ones.
db
my $db = $pg->db;
Get Mojo::Pg::Database object for a cached or newly created database handle.
new
my $pg = Mojo::Pg->new;
my $pg = Mojo::Pg->new(
'dbi:Pg:dbname=foo', 'sri', 's3cret', {AutoCommit => 1});
Construct a new Mojo::Pg object.
AUTHOR
Sebastian Riedel, sri@cpan.org
.
COPYRIGHT AND LICENSE
Copyright (C) 2014, Sebastian Riedel.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
SEE ALSO
https://github.com/kraih/mojo-pg, Mojolicious::Guides, http://mojolicio.us.