NAME

Mojolicious::Plugin::QuickPg - Mojolicious Plugin that provided quick access methods for Mojo::Pg

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');

# 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' } );

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

DESCRIPTION

Mojolicous::Plugin::QuickPg is a plugin for Mojolicious apps thas provide simple access to Mojo::Pg. The most part of the code for plugin is taken from Dancer::Plugin::Database::Core::Handle (under Artistic License)

HELPERS

Mojolicious::Plugin::QuickPg contains next helpers: qselect, qinsert, qupdate, qdelete, qcustom, qerror.

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/*

CONFIG

Mojolicious::Plugin::QuickPg configuration support two keys.

  • dsn

    $self->plugin('Mojolicious::Plugin::QuickPg' =>
                               {dsn => 'postgresql://sri:123456@localhost/testdb'} );

    Set connection string

  • debug

    # Lite
    
    plugin 'QuickPg' => {dsn => 'postgresql://sri:123456@localhost/testdb', debug => 1};
    
    # Adults App :)
    
    $self->plugin('Mojolicious::Plugin::QuickPg' =>
                                      { dsn   => 'postgresql://sri:123456@localhost/testdb',
                                        debug => 1 } );
                                                     

    This key switches on|off printing on console SQL requests.

SEE ALSO

Mojo::Pg Mojolicious Mojolicious::Guides http://mojolicious.org.

AUTHOR

Pavel Kuptsov <pkuptsov@gmail.com>

THANKS

Alberto Simões <ambs@perl-hackers.net> Sebastian Riedel <sri@cpan.org>

BUGS

Please report any bugs or feature requests to bug-mojolicious-plugin-quickpg at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.htMail?Queue=Mojolicious-Plugin-QuickPg. We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.

COPYRIGHT & LICENSE

Copyright (C) 2016 by Pavel Kuptsov.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.