/
Database.pm
295 lines (195 loc) · 6.1 KB
/
Database.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package Mojo::Pg::Database;
use Mojo::Base 'Mojo::EventEmitter';
use DBD::Pg ':async';
use IO::Handle;
use Mojo::IOLoop;
use Mojo::Pg::Results;
has [qw(dbh pg)];
has max_statements => 10;
sub DESTROY {
my $self = shift;
if ((my $dbh = $self->dbh) && (my $pg = $self->pg)) { $pg->_enqueue($dbh) }
}
sub backlog { scalar @{shift->{waiting} || []} }
sub begin { shift->_dbh(begin_work => @_) }
sub commit { shift->dbh->commit }
sub disconnect {
my $self = shift;
$self->_unwatch;
delete $self->{queue};
$self->dbh->disconnect;
}
sub do { shift->_dbh(do => @_) }
sub is_listening { !!keys %{shift->{listen} || {}} }
sub listen {
my ($self, $name) = @_;
my $dbh = $self->dbh;
local $dbh->{AutoCommit} = 1;
$dbh->do('listen ' . $dbh->quote_identifier($name))
unless $self->{listen}{$name}++;
$self->_watch;
return $self;
}
sub ping { shift->dbh->ping }
sub query {
my ($self, $query) = (shift, shift);
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
# Blocking
unless ($cb) {
my $sth = $self->_dequeue(0, $query);
$sth->execute(@_);
return Mojo::Pg::Results->new(db => $self, sth => $sth);
}
# Non-blocking
push @{$self->{waiting}}, {args => [@_], cb => $cb, query => $query};
$self->$_ for qw(_next _watch);
}
sub rollback { shift->dbh->rollback }
sub unlisten {
my ($self, $name) = @_;
my $dbh = $self->dbh;
local $dbh->{AutoCommit} = 1;
$dbh->do('unlisten' . $dbh->quote_identifier($name));
$name eq '*' ? delete($self->{listen}) : delete($self->{listen}{$name});
$self->_unwatch unless $self->backlog || $self->is_listening;
return $self;
}
sub _dbh {
my ($self, $method) = (shift, shift);
$self->dbh->$method(@_);
return $self;
}
sub _dequeue {
my ($self, $async, $query) = @_;
my $queue = $self->{queue} ||= [];
for (my $i = 0; $i <= $#$queue; $i++) {
my $sth = $queue->[$i];
return splice @$queue, $i, 1
if !(!$sth->{pg_async} ^ !$async) && $sth->{Statement} eq $query;
}
return $self->dbh->prepare($query, $async ? {pg_async => PG_ASYNC} : ());
}
sub _enqueue {
my ($self, $sth) = @_;
push @{$self->{queue}}, $sth;
shift @{$self->{queue}} while @{$self->{queue}} > $self->max_statements;
}
sub _next {
my $self = shift;
return unless my $next = $self->{waiting}[0];
return if $next->{sth};
my $sth = $next->{sth} = $self->_dequeue(1, $next->{query});
$sth->execute(@{$next->{args}});
}
sub _unwatch {
my $self = shift;
return unless delete $self->{watching};
Mojo::IOLoop->singleton->reactor->remove($self->{handle});
}
sub _watch {
my $self = shift;
return if $self->{watching} || $self->{watching}++;
my $dbh = $self->dbh;
$self->{handle} ||= IO::Handle->new_from_fd($dbh->{pg_socket}, 'r');
Mojo::IOLoop->singleton->reactor->io(
$self->{handle} => sub {
my $reactor = shift;
# Notifications
while (my $notify = $dbh->pg_notifies) {
$self->emit(notification => @$notify);
}
return unless (my $waiting = $self->{waiting}) && $dbh->pg_ready;
my ($sth, $cb) = @{shift @$waiting}{qw(sth cb)};
# Do not raise exceptions inside the event loop
my $result = do { local $dbh->{RaiseError} = 0; $dbh->pg_result };
my $err = defined $result ? undef : $dbh->errstr;
$self->$cb($err, Mojo::Pg::Results->new(db => $self, sth => $sth));
$self->_next;
$self->_unwatch unless $self->backlog || $self->is_listening;
}
)->watch($self->{handle}, 1, 0);
}
1;
=encoding utf8
=head1 NAME
Mojo::Pg::Database - Database
=head1 SYNOPSIS
use Mojo::Pg::Database;
my $db = Mojo::Pg::Database->new(pg => $pg, dbh => $dbh);
=head1 DESCRIPTION
L<Mojo::Pg::Database> is a container for L<DBD::Pg> database handles used by
L<Mojo::Pg>.
=head1 EVENTS
L<Mojo::Pg::Database> inherits all events from L<Mojo::EventEmitter> and can
emit the following new ones.
=head2 notification
$db->on(notification => sub {
my ($db, $name, $pid, $payload) = @_;
...
});
Emitted when a notification has been received.
=head1 ATTRIBUTES
L<Mojo::Pg::Database> implements the following attributes.
=head2 dbh
my $dbh = $db->dbh;
$db = $db->dbh(DBI->new);
L<DBD::Pg> database handle used for all queries.
=head2 pg
my $pg = $db->pg;
$db = $db->pg(Mojo::Pg->new);
L<Mojo::Pg> object this database belongs to.
=head2 max_statements
my $max = $db->max_statements;
$db = $db->max_statements(5);
Maximum number of statement handles to cache for future queries, defaults to
C<10>.
=head1 METHODS
L<Mojo::Pg::Database> inherits all methods from L<Mojo::EventEmitter> and
implements the following new ones.
=head2 backlog
my $num = $db->backlog;
Number of waiting non-blocking queries.
=head2 begin
$db = $db->begin;
Begin transaction.
=head2 commit
$db->commit;
Commit transaction.
=head2 disconnect
$db->disconnect;
Disconnect L</"dbh"> and prevent it from getting cached again.
=head2 do
$db = $db->do('create table foo (bar varchar(255))');
Execute a statement and discard its result.
=head2 is_listening
my $bool = $db->is_listening;
Check if L</"dbh"> is listening for notifications.
=head2 listen
$db = $db->listen('foo');
Start listening for notifications when the L<Mojo::IOLoop> event loop is
running.
=head2 ping
my $bool = $db->ping;
Check database connection.
=head2 query
my $results = $db->query('select * from foo');
my $results = $db->query('insert into foo values (?, ?, ?)', @values);
Execute a blocking statement and return a L<Mojo::Pg::Results> object with the
results. The L<DBD::Pg> statement handle will be automatically cached again
when that object is destroyed, so future queries can reuse it to increase
performance. You can also append a callback to perform operation non-blocking.
$db->query('insert into foo values (?, ?, ?)' => @values => sub {
my ($db, $err, $results) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 rollback
$db->rollback;
Rollback transaction.
=head2 unlisten
$db = $db->unlisten('foo');
$db = $db->unlisten('*');
Stop listening for notifications.
=head1 SEE ALSO
L<Mojo::Pg>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut