Mojolicious-Plugin-QuickPg
view release on metacpan or search on metacpan
lib/Mojolicious/Plugin/QuickPg.pm view on Meta::CPAN
}
# A wrapper around DBI's quote_identifier which first splits on ".", so that
# e.g. database.table gets quoted as `database`.`table`, not `database.table`
sub _quote_identifier {
my ($self, $identifier) = @_;
return join '.', map {
$self->pg->db->dbh->quote_identifier($_)
} split /\./, $identifier;
}
1;
__END__
=encoding UTF-8
=head1 NAME
Mojolicious::Plugin::QuickPg - Mojolicious Plugin that provided quick access methods for Mojo::Pg
=head1 SYNOPSIS
# Mojolicious::Lite
plugin 'QuickPg' => {dsn => 'postgresql://sri:123456@localhost/testdb'};
# Mojolicious (not Lite)
# in startup
$self->plugin('Mojolicious::Plugin::QuickPg' =>
{ dsn => 'postgresql://sri:123456@localhost/testdb',
debug => 1 } );
# in controller
# quick select
# returns array of hashes [{},{}]
my ($all_table) = $c->qselect('table_name');
# returns hash {}
my $one_row = $c->qselect('table_name');
# example with offset and limits
my ($array_ref) = $c->qselect('models', {},{
limit => 10,
offset => 0 }
);
# quick count
$c->qcount('table_name', {name => {like => 'Mos%'} } );
# quick insert
# returns value of primary key (like as last_insert_id on MySQL)
my $id = $c->qinsert('models', { name => 'Moscow',
foto => 'https://www.flickr.com/search/?text=Moscow' } );
# or you can do like this
my $params = $c->req->json;
# Do not forget to validate $params before it:
my $id = $c->insert('models', $params);
# quick update
# returns numbers of updated rows
$c->qupdate('models', {id => $id}, { name => 'New York',
foto => 'https://www.flickr.com/search/?text=New%20York'
} );
# quick delete
# returns numbers of deleted rows
$c->qdelete('models', { id => $id });
# catch the errors on insert/delete methods
$c->qerror; # returns $@ value
# custom requests - returns Mojo::Pg::Results object
my $result = $s->qcustom('SELECT a.id, b.name
FROM table1 a, table2 b
WHERE a.id = b.id AND b.name = ?', $name);
my $arrays = $result->hashes->to_array;
=head1 DESCRIPTION
L<Mojolicous::Plugin::QuickPg> is a plugin for Mojolicious apps thas provide simple access to L<Mojo::Pg>.
The most part of the code for plugin is taken from L<Dancer::Plugin::Database::Core::Handle> (under Artistic License)
=head1 HELPERS
L<Mojolicious::Plugin::QuickPg> contains next helpers: I<qselect>, I<qinsert>, I<qupdate>, I<qdelete>, I<qcustom>, I<qerror>,
I<qcount>.
=head2 C<qselect>
my $one_row = $c->qselect('table_name', {id => 1},
{ order_by => {desc => 'id'}, limit => 10, offset => 5, columns => qw[id name]});
For more examples see /examples/*
=head2 C<qinsert>
For examples see /examples/*
=head2 C<qupdate>
For examples see /examples/*
=head2 C<qdelete>
For examples see /examples/*
=head2 C<qcustom>
For examples see /examples/*
( run in 1.362 second using v1.01-cache-2.11-cpan-39bf76dae61 )