Доброго всем
¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
Mojo::Pg::Che - mix of parent Mojo::Pg and DBI.pm
See Mojo::Pg
Version 0.850
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); # or my $pg = Mojo::Pg->new('pg://postgres@/test'); # 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 async sth my $sth = $pg->prepare_cached('select ...', {Async => 1,},); # Non-blocking query for async sth $pg->query($sth, undef, @bind, sub {my ($db, $err, $result) = @_; ...}); Mojo::IOLoop->start unless Mojo::IOLoop->is_running; # Result non-blocking query for async sth my $ref_cb = $pg->query($sth, {Async => 1,}, @bind,); Mojo::IOLoop->start unless Mojo::IOLoop->is_running; # Mojo::Pg::Results style my res = $$ref_cb->()->hash; # same DBI style my $res = $$ref_cb->()->fetchrow_hashref; # Mojo::Pg style my $now = $pg->db->query('select now() as now')->hash->{now}; $pg->db->query('select pg_sleep(?::int), now() as now', undef, 2, $cb); # 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');
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 $db = $pg->db; $db->begin; $db->do('insert into foo (name) values (?)', 'bazzzz'); $db->rollback; $db->begin; $db->query('insert into foo (name) values (?)', 'barrr'); $db->commit;
Depends on $attr->{Async} and callback:
1. $attr->{Async} set to 1. None $cb pass. Callback will create inside methods ->query() ->select...() and will returns ref on that callback. You need start Mojo::IOLoop:
->query()
->select...()
# async sth my $sth = $pg->prepare('select ...', {Async => 1,},); # Result non-blocking query for async sth my $res_cb = $pg->query($sth, {Async => 1,}, @bind,); Mojo::IOLoop->start unless Mojo::IOLoop->is_running; # Mojo::Pg::Results style my res = $$res_cb->()->hash;
2. $attr->{Async} not set. $cb defined. 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. Results pass to $cb. You need start Mojo::IOLoop.
All methods from parent module Mojo::Pg are inherits and implements the following new ones.
DBI-style of new object instance. See DBI#connect
Overriden method of Mojo::Pg#db. Because can first input param - DBI database handler (when prepared statement used).
Prepare and return DBI statement handler for query string.
Prepare and return DBI cached statement handler for query string.
Like Mojo::Pg::Database#query but input params - Mojo::Pg::Che#Params-for-quering-methods
Blocking query without attr Async or callback.
Non-blocking query with attr Async or callback.
Same method query.
query
DBI style quering. See DBI#selectrow_array. Blocking | non-blocking. Input params - Mojo::Pg::Che#Params-for-quering-methods.
DBI style quering. See DBI#selectrow_arrayref. Blocking | non-blocking. Input params - Mojo::Pg::Che#Params-for-quering-methods.
DBI style quering. See DBI#selectrow_hashref. Blocking | non-blocking. Input params - Mojo::Pg::Che#Params-for-quering-methods.
DBI style quering. See DBI#selectall_arrayref. Blocking | non-blocking. Input params - Mojo::Pg::Che#Params-for-quering-methods.
DBI style quering. See DBI#selectall_hashref. Blocking | non-blocking. Input params - Mojo::Pg::Che#Params-for-quering-methods.
DBI style quering. See DBI#selectcol_arrayref. Blocking | non-blocking. Input params - Mojo::Pg::Che#Params-for-quering-methods.
DBI style quering. See DBI#do. Blocking | non-blocking. Input params - Mojo::Pg::Che#Params-for-quering-methods.
Start transaction and return new Mojo::Pg::Che::Database object which attr {tx} is a Mojo::Pg::Transaction object. Sinonyms are: ->tx and ->begin_work.
{tx}
->tx
->begin_work
The methods query, select..., do has next ordered input params:
select...
do
String query | statement handler object
Hashref attrs (optional)
Array of bind values (optional)
Last param - callback/coderef for non-blocking (optional)
Mojo::Pg
DBI
Михаил Че (Mikhail Che), <mche[-at-]cpan.org>
<mche[-at-]cpan.org>
Please report any bugs or feature requests at https://github.com/mche/Mojo-Pg-Che/issues. Pull requests also welcome.
Copyright 2016 Mikhail Che.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Mojo::Pg::Che, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::Pg::Che
CPAN shell
perl -MCPAN -e shell install Mojo::Pg::Che
For more information on module installation, please visit the detailed CPAN module installation guide.