Доброго всем

Mojo::Pg::Che

¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !

NAME

Mojo::Pg::Che - mix of parent Mojo::Pg and DBI.pm

VERSION

Version 0.02

SYNOPSIS

use Mojo::Pg::Che;

my $pg = Mojo::Pg::Che->connect("dbname=test;", "postgres", 'pg-pwd', \%attrs);
# or
my $pg = Mojo::Pg::Che->new
  ->dsn("DBI:Pg:dbname=test;")
  ->username("postgres")
  ->password('pg--pw')
  ->options(\%attrs);

# Bloking query
my $result = $pg->query('select ...', undef, @bind);

# Non-blocking query
my $result = $pg->query('select ...', {Async => 1, ...}, @bind);

# Cached query
my $result = $pg->query('select ...', {Cached => 1, ...}, @bind);

# prepare sth
my $sth = $pg->prepare('select ...');

# cached sth
my $sth = $pg->prepare_cached('select ...');

# Non-blocking query sth
my $result = $pg->query($sth, undef, @bind, sub {my ($db, $err, $result) = @_; ...});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

# Result non-blocking query sth
my $result = $pg->query($sth, {Async => 1,}, @bind,);

# Mojo::Pg style
my $now = $pg->db->query('select now() as now')->hash->{now};

# DBI style
my $now = $pg->selectrow_hashref('select now() as now')->{now};
my $now = $pg->db->selectrow_hashref('select now() as now')->{now};

my $now = $pg->selectrow_array('select now() as now');

Transaction syntax

eval {
  my $tx = $pg->begin;
  $tx->query('insert into foo (name) values (?)', 'bar');
  $tx->do('insert into foo (name) values (?)', 'baz');
  $tx->commit;
};
die $@ if $@;

my $tx = $pg->begin;
$tx->do('insert into foo (name) values (?)', 'bazzzz');
$tx->rollback;
$tx->begin;
$tx->query('insert into foo (name) values (?)', 'barrr');
$tx->commit;

Non-blocking query cases

Depends on $attr->{Async} and callback:

1. $attr->{Async} set to 1. None $cb. Callback will create and Mojo::IOLoop will auto start. Method ->query() will return result object. Methods <-select...()>> will return there perl structures.

2. $attr->{Async} not set. $cb defined. All ->query() and ->select...() methods will return reactor object and results pass to $cb. You need start Mojo::IOLoop:

my @results;
my $cb = sub {
  my ($db, $err, $results) = @_;
  die $err if $err;
  push @results, $results;
};
$pg->query('select ?::date as d, pg_sleep(?::int)', undef, ("2016-06-$_", 1), $cb)
  for 17..23;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
like($_->hash->{d}, qr/2016-06-\d+/, 'correct async query')
  for @results;

3. $attr->{Async} set to 1. $cb defined. Mojo::IOLoop will auto start. Results pass to $cb.

METHODS

new

Parent method of Mojo::Pg#new

connect

DBI-style of new object instance. See DBI#connect

query

Is same as Mojo::Pg::Database#query.

Blocking query without attr pg_async.

Non-blocking query with attr pg_async.

AUTHOR

Михаил Че (Mikhail Che), <mche[-at-]cpan.org>

BUGS / CONTRIBUTING

Please report any bugs or feature requests at https://github.com/mche/Mojo-Pg-Che/issues. Pull requests also welcome.

COPYRIGHT

Copyright 2016 Mikhail Che.

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