Che.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« Che/Results.pm ↑ Summary
1 package Mojo::Pg::Che;
2  
3 1 ▶use Mojo::Base 'Mojo::EventEmitter';#'Mojo::Pg';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use Mojo::Pg;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use DBI;
6 use Carp qw(croak);
7 use Mojo::Pg::Che::Database;
8 use Scalar::Util 'blessed';
9  
10 our $VERSION = '0.857';
11  
12 has pg => sub { Mojo::Pg->new };#, weak => 1;
13 has database_class => 'Mojo::Pg::Che::Database';
14 has dsn => 'dbi:Pg:';
15 has max_connections => 5;
16 has [qw(password username)] => '';
17 has [qw(parent search_path)];
18 has options => sub {
19 {
20 AutoCommit => 1,
21 AutoInactiveDestroy => 1,
22 PrintError => 0,
23 RaiseError => 1,
24 ShowErrorStatement => 1,
25 pg_enable_utf8 => 1,
26 };
27 };
28  
29 has debug => $ENV{DEBUG_Mojo_Pg_Che} || 0;
30 my $PKG = __PACKAGE__;
31  
32 # as Mojo::Pg
33 sub new {
34 my $class = shift;
35 my $from_string = @_ == 1;
36 my $pg = $from_string && Mojo::Pg->new->from_string(shift);
37  
38 my $self = $class->SUPER::new(@_);
39 $self->pg($pg->parent || $pg)
40 if $pg;
41  
42 map { $self->$_($self->pg->$_); }
43 qw(dsn username password search_path)#options
44 if $from_string;
45  
46 $self->dsn('dbi:Pg:'.$self->dsn)
47 unless !$self->dsn || $self->dsn =~ /^dbi:Pg:/;
48  
49 map { $self->pg->$_($self->$_); }
50 qw(dsn username password options search_path max_connections);#database_class pubsub
51
52 $self->pg->attr(debug => $self->debug);
53
54 return $self;
55 }
56  
57 #as DBI
58 sub connect {
59 my $self = ref $_[0] ? shift : shift->SUPER::new;
60 map { my $has = shift; $has && $self->$_($has)} qw(dsn username password);
61  
62 if (ref $_[0]) {
63 my $arg = shift;
64 my $options = $self->options;
65 @$options{ keys %$arg } = values %$arg;
66 }
67 if (@_) {
68 my $attrs = {@_};
69 map $self->$_($attrs->{$_}), keys %$attrs;
70 }
71
72 $self->dsn('dbi:Pg:'.$self->dsn)
73 unless !$self->dsn || $self->dsn =~ /^dbi:Pg:/;
74
75 my $pg = $self->pg->parent || $self->pg;
76
77 map $pg->$_($self->$_),
78 qw(dsn username password options search_path max_connections);#database_class pubsub
79
80 $self->debug
81 && say STDERR sprintf("[$PKG->connect] prepare connection data for [%s]", $self->dsn, );
82
83 $pg->attr(debug => $self->debug);
84 return $self;
85 }
86  
87 sub db {
88 my ($self, $dbh) = (shift, shift);
89  
90 my $pg = $self->pg->parent || $self->pg;
91
92 # Fork-safety if $dbh
93 undef $dbh
94 unless ($pg->{pid} //= $$) eq $$;
95  
96 $dbh ||= $pg->_dequeue;
97  
98 return $self->database_class->new(dbh => $dbh, pg => $pg, debug=>$self->debug);
99 }
100  
101 sub prepare { shift->db->prepare(@_); }
102 sub prepare_cached { shift->db->prepare_cached(@_); }
103  
104 # если уже sth и он не в асинхроне - взять в запрос его
105 # или просто у него взять строку запроса для нового dbh
106 sub _db_st {
107 my ($self, $st) = @_;
108 return ($self->db($st->{Database}), $st)
109 if ref($st) && $st->{pg_async_status} != 1;
110 return ($self->db, ref($st) ? $st->{Statement} : $st);
111 }
112  
113 sub query { my ($db, $st) = shift->_db_st(shift); $db->select($st, @_); }
114 sub select { my ($db, $st) = shift->_db_st(shift); $db->select($st, @_); }
115 sub selectrow_array { my ($db, $st) = shift->_db_st(shift); $db->selectrow_array($st, @_); }
116 sub selectrow_arrayref { my ($db, $st) = shift->_db_st(shift); $db->selectrow_arrayref($st, @_); }
117 sub selectrow_hashref { my ($db, $st) = shift->_db_st(shift); $db->selectrow_hashref($st, @_); }
118 sub selectall_arrayref { my ($db, $st) = shift->_db_st(shift); $db->selectall_arrayref($st, @_); }
119 sub selectall_hashref { my ($db, $st) = shift->_db_st(shift); $db->selectall_hashref($st, @_); }
120 sub selectcol_arrayref { my ($db, $st) = shift->_db_st(shift); $db->selectcol_arrayref($st, @_); }
121 sub do { my ($db, $st) = shift->_db_st(shift); $db->do($st, @_); }
122  
123 #~ sub begin_work {croak 'Use $pg->db->tx | $pg->db->begin';}
124 sub tx {shift->begin}
125 sub begin_work {shift->begin}
126 sub begin {
127 my $self = shift;
128 my $db = $self->db;
129 $db->begin;
130 return $db;
131 }
132  
133 sub commit {croak 'Instead use: $tx = $pg->begin; $tx->do(...); $tx->commit;';}
134 sub rollback {croak 'Instead use: $tx = $pg->begin; $tx->do(...); $tx->rollback;';}
135  
136 sub dequeue { my $pg = $_[0]->pg->parent || $_[0]->pg; $pg->_dequeue; }
137 sub enqueue { my $pg = $_[0]->pg->parent || $_[0]->pg; $pg->_enqueue; }
138  
139 { # Patches
140 no warnings 'redefine';
141 # Patch Mojo::Pg::_dequeue
142 sub Mojo::Pg::_dequeue {
143 my $self = shift;
144
145 # Fork-safety
146 delete @$self{qw(pid queue)} unless ($self->{pid} //= $$) eq $$;
147
148 my $queue = $self->{queue} ||= [];
149 for my $i (0..$#$queue) {
150
151 my $dbh = $queue->[$i];
152  
153 next
154 if $dbh->{pg_async_status} && $dbh->{pg_async_status} > 0;
155
156 splice(@$queue, $i, 1); #~ delete $queue->[$i]
157  
158 next
159 unless blessed($dbh) && $dbh->ping; # не понятно почему может $dbh не blessed
160  
161 $self->debug
162 && say STDERR sprintf("[$PKG->_dequeue] [$dbh][pg_pid %s] does dequeued, pool count:[%s]", $dbh->{pg_pid}, scalar @$queue);
163
164 return $dbh;
165 }
166
167 my $dbh = DBI->connect(map { $self->$_ } qw(dsn username password options));
168 $self->debug
169 && say STDERR sprintf("[$PKG->_dequeue] new DBI connection [$dbh][pg_pid %s]", $dbh->{pg_pid});
170
171 # Search path
172 if (my $path = $self->search_path) {
173 my $search_path = join ', ', map { $dbh->quote_identifier($_) } @$path;
174 $dbh->do("set search_path to $search_path");
175 }
176  
177 $self->emit(connection => $dbh);
178  
179 return $dbh;
180 }
181 # Patch Mojo::Pg::_enqueue
182 sub Mojo::Pg::_enqueue {
183 my ($self, $dbh) = @_;
184 # Fork-safety
185 delete @$self{qw(pid queue)}
186 and return
187 unless ($self->{pid} //= $$) eq $$;
188
189 my $queue = $self->{queue} ||= [];
190
191 if ($dbh->{Active} && $dbh->ping && @$queue < $self->max_connections) {#($dbh->{pg_async_status} && $dbh->{pg_async_status} > 0) ||
192 unshift @$queue, $dbh;
193 # push @$queue, $dbh; # /home/guest/Mojo-Pg-Che/t/09-base-database.t line 108
194 $self->debug
195 && say STDERR sprintf("[$PKG->_enqueue] [$dbh][pg_pid %s] does enqueued, pool count:[%s], pg_async_status=[%s]", $dbh->{pg_pid}, scalar @$queue, $dbh->{pg_async_status});
196 return;
197 }
198
199 $self->debug
200 && say STDERR sprintf("[$PKG->_enqueue] [$dbh][pg_pid %s] does not enqueued, pool count:[%s]", $dbh->{pg_pid}, scalar @$queue);
201 }
202  
203 }# end no warnings 'redefine';
204  
205  
206 1;
207  
208 __END__
« Che/Results.pm ↑ Summary