Mojo/mysql/Database.pm

filter: Statementstmt 23.0% 33 / 143 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 26.4% 9 / 34 Podpod 100% 10 / 10 totaltotal 19.1% 52 / 272 CC 41 SCAR 33.0
CC
41
Cov
15%
__ANON__
110.0
query
56.0
_bind_params
42.0
CRAP 27.2
« Mojo/mysql/PubSub.pm ↑ Summary Mojo/mysql/Results.pm »
1 package Mojo::mysql::Database;
2 18 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Mojo::JSON 'to_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Mojo::mysql::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Mojo::mysql::Transaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use Mojo::Util 'monkey_patch';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 18 has [qw(dbh mysql)];
14 18 has results_class => 'Mojo::mysql::Results';
15  
16 18 for my $name (qw(delete insert select update)) {
17 monkey_patch __PACKAGE__, $name, sub {
18 0 ▶ my $self = shift;
Subroutine delete: not called CC=2 SCAR=17.9
Subroutine insert: not called CC=2 SCAR=17.9
19 0 ▶ my @cb = ref $_[-1] eq 'CODE' ? pop : ();
Branch: 2 branches
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 0 0
ref $_[-1] eq 'CODE' ? : 0 0 0
20 0 return $self->query($self->mysql->abstract->$name(@_), @cb);
21 72 };
22 monkey_patch __PACKAGE__, "${name}_p", sub {
23 0 ▶ my $self = shift;
Subroutine update_p: not called CC=1 SCAR=6.9
Subroutine delete_p: not called CC=1 SCAR=6.9
24 0 return $self->query_p($self->mysql->abstract->$name(@_));
25 72 };
26 }
27  
28 sub DESTROY {
29 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=3 SCAR=24.8
30 0 $self->_cleanup_sth;
31 0 ▶ return unless (my $mysql = $self->mysql) and (my $dbh = $self->dbh);
Branch: 1 branch
BranchTrueFalseTotal
unless my $mysql = $self->mysql and my $dbh = $self->dbh 0 0 0
Condition0%0/3
my $mysql = $self->mysql and my $dbh = $self->dbh
!ll&&!rl&&r
000
MC/DC0%0/2
my $mysql = $self->mysql and my $dbh = $self->dbh
my $mysql = $self->mysql my $dbh = $self->dbh
Truth table0%0/3
A and B
A= my $mysql = $self->mysqlB= my $dbh = $self->dbh
ABresult
0X0
100
111
32 0 $mysql->_enqueue($dbh, $self->{handle});
33 }
34  
35 0 ▶sub backlog { scalar @{shift->{waiting} || []} }
Subroutine backlog: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"waiting"} or [] 0 0 0
36  
37 sub begin {
38 0 ▶ my $self = shift;
Subroutine begin: not called CC=1 SCAR=6.9
Pod documented
39 0 my $tx = Mojo::mysql::Transaction->new(db => $self);
40 0 weaken $tx->{db};
41 0 return $tx;
42 }
43  
44 sub disconnect {
45 0 ▶ my $self = shift;
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
46 0 $self->_cleanup_sth;
47 0 $self->_unwatch;
48 0 $self->dbh->disconnect;
49 }
50  
51 0 ▶sub pid { shift->_dbh_attr('mysql_thread_id') }
Subroutine pid: not called CC=1 SCAR=6.9
Pod documented
52  
53 0 ▶sub ping { shift->dbh->ping }
Subroutine ping: not called CC=1 SCAR=6.9
Pod documented
54  
55 sub query {
56 0 ▶ my ($self, $query) = (shift, shift);
Subroutine query: not called CC=7 SCAR=40.3
Pod documented
57 0 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 0 0
58  
59 # Blocking
60 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
61 0 ▶ Carp::confess('Cannot perform blocking query, while waiting for async response') if $self->backlog;
Branch: 1 branch
BranchTrueFalseTotal
if $self->backlog 0 0 0
62 0 my $sth = $self->dbh->prepare($query);
63 0 ▶ local $sth->{HandleError} = sub { $_[0] = Carp::shortmess($_[0]); 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 _bind_params($sth, @_);
65 0 my $rv = $sth->execute;
66 0 my $res = $self->results_class->new(db => $self, is_blocking => 1, sth => $sth);
67 0 ▶ $res->{affected_rows} = defined $rv && $rv >= 0 ? 0 + $rv : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $rv && $rv >= 0 ? : 0 0 0
Condition0%0/3
defined $rv and $rv >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rv and $rv >= 0
defined $rv $rv >= 0
Truth table0%0/3
A and B
A= defined $rvB= $rv >= 0
ABresult
0X0
100
111
68 0 return $res;
69 }
70  
71 # Non-blocking
72 0 push @{$self->{waiting}}, {args => [@_], err => Carp::shortmess('__MSG__'), cb => $cb, query => $query};
73 0 $self->$_ for qw(_next _watch);
74 0 return $self;
75 }
76  
77 sub query_p {
78 0 ▶ my $self = shift;
Subroutine query_p: not called CC=1 SCAR=6.9
Pod documented
79 0 my $promise = Mojo::Promise->new;
80 0 ▶ $self->query(@_ => sub { $_[1] ? $promise->reject($_[1]) : $promise->resolve($_[2]) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
81 0 return $promise;
82 }
83  
84 0 ▶sub quote { shift->dbh->quote(shift) }
Subroutine quote: not called CC=1 SCAR=6.9
Pod documented
85  
86 0 ▶sub quote_id { shift->dbh->quote_identifier(shift) }
Subroutine quote_id: not called CC=1 SCAR=6.9
Pod documented
87  
88 sub tables {
89 0 ▶ shift->query('show tables')->arrays->reduce(sub { push @$a, $b->[0]; $a }, []);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine tables: not called CC=1 SCAR=6.9
Pod documented
90 }
91  
92 sub _bind_params {
93 0 ▶ my $sth = shift;
Subroutine _bind_params: not called CC=6 SCAR=37.4
94 0 for my $i (0 .. $#_) {
95 0 my $param = $_[$i];
96 0 my %attrs;
97 0 ▶ if (ref $param eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param eq "HASH") 0 0 0
98 0 ▶ if (exists $param->{json}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $param->{'json'}) { } 0 0 0
elsif (exists $param->{'type'} and exists $param->{'value'}) { } 0 0 0
99 0 $param = to_json $param->{json};
100 }
101 ▶ elsif (exists $param->{type} && exists $param->{value}) {
Condition0%0/3
exists $param->{"type"} and exists $param->{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $param->{"type"} and exists $param->{"value"}
exists $param->{"type"} exists $param->{"value"}
Truth table0%0/3
A and B
A= exists $param->{"type"}B= exists $param->{"value"}
ABresult
0X0
100
111
102 0 ($param, $attrs{TYPE}) = @$param{qw(value type)};
103 }
104 }
105  
106 0 $sth->bind_param($i + 1, $param, \%attrs);
107 }
108 0 return $sth;
109 }
110  
111 sub _cleanup_sth {
112 0 ▶ my $self = shift;
Subroutine _cleanup_sth: not called CC=3 SCAR=24.8
113 0 delete $self->{done_sth};
114 0 ▶ $_->{cb}($self, 'Premature connection close', undef) for @{delete $self->{waiting} || []};
Branch: 1 branch
BranchTrueFalseTotal
delete $self->{"waiting"} or [] 0 0 0
115 }
116  
117 sub _dbh_attr {
118 0 ▶ my $self = shift;
Subroutine _dbh_attr: not called CC=3 SCAR=24.8
119 0 ▶ my $dbh = ref $self ? $self->dbh : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
120 0 my $name = shift;
121 0 $name =~ s!^mysql!{lc $dbh->{Driver}{Name}}!e;
122 0 ▶ return $dbh->{$name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
123 0 return $dbh->{$name};
124 }
125  
126 sub _next {
127 0 ▶ my $self = shift;
Subroutine _next: not called CC=4 SCAR=30.0
128  
129 0 ▶ return unless my $next = $self->{waiting}[0];
Branch: 1 branch
BranchTrueFalseTotal
unless my $next = $self->{"waiting"}[0] 0 0 0
130 0 ▶ return if $next->{sth};
Branch: 1 branch
BranchTrueFalseTotal
if $next->{"sth"} 0 0 0
131  
132 0 my $dbh = $self->dbh;
133 0 ▶ my $flag = lc $dbh->{Driver}{Name} eq 'mariadb' ? 'mariadb_async' : 'async';
Branch: 1 branch
BranchTrueFalseTotal
lc $dbh->{'Driver'}{'Name'} eq 'mariadb' ? : 0 0 0
134 0 my $sth = $next->{sth} = $self->dbh->prepare($next->{query}, {$flag => 1});
135 0 _bind_params($sth, @{$next->{args}});
136 0 $sth->execute;
137 }
138  
139 sub _unwatch {
140 0 ▶ Mojo::IOLoop->singleton->reactor->remove(delete $_[0]->{handle}) if $_[0]->{handle};
Subroutine _unwatch: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"handle"} 0 0 0
141 }
142  
143 sub _watch {
144 0 ▶ my $self = shift;
Subroutine _watch: not called CC=4 SCAR=30.0
145 0 ▶ return if $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 0 0 0
146  
147 0 my $dbh = $self->dbh;
148 0 my $driver = lc $dbh->{Driver}{Name};
149 0 my $ready_method = "${driver}_async_ready";
150 0 my $result_method = "${driver}_async_result";
151 0 ▶ my $fd = $driver eq 'mariadb' ? $dbh->mariadb_sockfd : $dbh->mysql_fd;
Branch: 1 branch
BranchTrueFalseTotal
$driver eq 'mariadb' ? : 0 0 0
152 0 ▶ open $self->{handle}, '<&', $fd or die "Could not dup $driver fd: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"handle"}, "<&", $fd or die "Could not dup $driver fd: $!" 0 0 0
153 Mojo::IOLoop->singleton->reactor->io(
154 $self->{handle} => sub {
155 0 ▶ return unless my $waiting = $self->{waiting};
Subroutine __ANON__: not called CC=10 SCAR=47.0
Branch: 1 branch
BranchTrueFalseTotal
unless my $waiting = $self->{"waiting"} 0 0 0
156 0 ▶ return unless @$waiting and $waiting->[0]{sth} and $waiting->[0]{sth}->$ready_method;
Branch: 1 branch
BranchTrueFalseTotal
unless @$waiting and $waiting->[0]{"sth"} and $waiting->[0]{"sth"}->$ready_method 0 0 0
Condition0%0/6
@$waiting and $waiting->[0]{"sth"} and $waiting->[0]{"sth"}->$ready_method
!ll&&!rl&&r
000
@$waiting and $waiting->[0]{"sth"}
!ll&&!rl&&r
000
MC/DC0%0/3
@$waiting and $waiting->[0]{"sth"} and $waiting->[0]{"sth"}->$ready_method
@$waiting $waiting->[0]{"sth"} $waiting->[0]{"sth"}->$ready_method
Truth table0%0/4
A and B and C
A= @$waitingB= $waiting->[0]{"sth"}C= $waiting->[0]{"sth"}->$ready_method
ABCresult
0XX0
10X0
1100
1111
157 0 my ($cb, $err, $sth) = @{shift @$waiting}{qw(cb err sth)};
158  
159 # Do not raise exceptions inside the event loop
160 0 my $rv = do { local $sth->{RaiseError} = 0; $sth->$result_method };
161 0 my $res = $self->results_class->new(db => $self, sth => $sth);
162  
163 0 ▶ $err = undef if defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv 0 0 0
164 0 ▶ $err =~ s!\b__MSG__\b!{$dbh->errstr}!e if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 0 0
165 0 ▶ $res->{affected_rows} = defined $rv && $rv >= 0 ? 0 + $rv : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $rv && $rv >= 0 ? : 0 0 0
Condition0%0/3
defined $rv and $rv >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rv and $rv >= 0
defined $rv $rv >= 0
Truth table0%0/3
A and B
A= defined $rvB= $rv >= 0
ABresult
0X0
100
111
166  
167 0 $self->$cb($err, $res);
168 0 $self->_next;
169 0 ▶ $self->_unwatch unless $self->backlog;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->backlog 0 0 0
170 }
171 0 )->watch($self->{handle}, 1, 0);
172 }
173  
174 18 1;
175  
176 =encoding utf8
177  
178 =head1 NAME
179  
180 Mojo::mysql::Database - Database
181  
182 =head1 SYNOPSIS
183  
184 use Mojo::mysql::Database;
185  
186 my $db = Mojo::mysql::Database->new(mysql => $mysql, dbh => $dbh);
187  
188 =head1 DESCRIPTION
189  
190 L<Mojo::mysql::Database> is a container for database handles used by L<Mojo::mysql>.
191  
192 =head1 ATTRIBUTES
193  
194 L<Mojo::mysql::Database> implements the following attributes.
195  
196 =head2 dbh
197  
198 my $dbh = $db->dbh;
199 $db = $db->dbh(DBI->new);
200  
201 Database handle used for all queries.
202  
203 =head2 mysql
204  
205 my $mysql = $db->mysql;
206 $db = $db->mysql(Mojo::mysql->new);
207  
208 L<Mojo::mysql> object this database belongs to.
209  
210 =head2 results_class
211  
212 $class = $db->results_class;
213 $db = $db->results_class("MyApp::Results");
214  
215 Class to be used by L</"query">, defaults to L<Mojo::mysql::Results>. Note that
216 this class needs to have already been loaded before L</"query"> is called.
217  
218 =head1 METHODS
219  
220 L<Mojo::mysql::Database> inherits all methods from L<Mojo::EventEmitter> and
221 implements the following new ones.
222  
223 =head2 backlog
224  
225 my $num = $db->backlog;
226  
227 Number of waiting non-blocking queries.
228  
229 =head2 begin
230  
231 my $tx = $db->begin;
232  
233 Begin transaction and return L<Mojo::mysql::Transaction> object, which will
234 automatically roll back the transaction unless
235 L<Mojo::mysql::Transaction/"commit"> bas been called before it is destroyed.
236  
237 # Add names in a transaction
238 eval {
239 my $tx = $db->begin;
240 $db->query('insert into names values (?)', 'Baerbel');
241 $db->query('insert into names values (?)', 'Wolfgang');
242 $tx->commit;
243 };
244 say $@ if $@;
245  
246 =head2 delete
247  
248 my $results = $db->delete($table, \%where);
249  
250 Generate a C<DELETE> statement with L<Mojo::mysql/"abstract"> (usually an
251 L<SQL::Abstract> object) and execute it with L</"query">. You can also append a
252 callback to perform operations non-blocking.
253  
254 $db->delete(some_table => sub {
255 my ($db, $err, $results) = @_;
256 ...
257 });
258 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
259  
260 =head2 delete_p
261  
262 my $promise = $db->delete_p($table, \%where, \%options);
263  
264 Same as L</"delete">, but performs all operations non-blocking and returns a
265 L<Mojo::Promise> object instead of accepting a callback.
266  
267 $db->delete_p('some_table')->then(sub {
268 my $results = shift;
269 ...
270 })->catch(sub {
271 my $err = shift;
272 ...
273 })->wait;
274  
275 =head2 disconnect
276  
277 $db->disconnect;
278  
279 Disconnect database handle and prevent it from getting cached again.
280  
281 =head2 insert
282  
283 my $results = $db->insert($table, \@values || \%fieldvals, \%options);
284  
285 Generate an C<INSERT> statement with L<Mojo::mysql/"abstract"> (usually an
286 L<SQL::Abstract> object) and execute it with L</"query">. You can also append a
287 callback to perform operations non-blocking.
288  
289 $db->insert(some_table => {foo => 'bar'} => sub {
290 my ($db, $err, $results) = @_;
291 ...
292 });
293 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
294  
295 =head2 insert_p
296  
297 my $promise = $db->insert_p($table, \@values || \%fieldvals, \%options);
298  
299 Same as L</"insert">, but performs all operations non-blocking and returns a
300 L<Mojo::Promise> object instead of accepting a callback.
301  
302 $db->insert_p(some_table => {foo => 'bar'})->then(sub {
303 my $results = shift;
304 ...
305 })->catch(sub {
306 my $err = shift;
307 ...
308 })->wait;
309  
310 =head2 pid
311  
312 my $pid = $db->pid;
313  
314 Return the connection id of the backend server process.
315  
316 =head2 ping
317  
318 my $bool = $db->ping;
319  
320 Check database connection.
321  
322 =head2 query
323  
324 my $results = $db->query('select * from foo');
325 my $results = $db->query('insert into foo values (?, ?, ?)', @values);
326 my $results = $db->query('insert into foo values (?)', {json => {bar => 'baz'}});
327 my $results = $db->query('insert into foo values (?)', {type => SQL_INTEGER, value => 42});
328  
329 Execute a blocking statement and return a L<Mojo::mysql::Results> object with the
330 results. You can also append a callback to perform operation non-blocking.
331  
332 $db->query('select * from foo' => sub {
333 my ($db, $err, $results) = @_;
334 ...
335 });
336 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
337  
338 Hash reference arguments containing a value named C<json>, will be encoded to
339 JSON text with L<Mojo::JSON/"to_json">. To accomplish the reverse, you can use
340 the method L<Mojo::mysql::Results/"expand">, which automatically decodes data back
341 to Perl data structures.
342  
343 $db->query('insert into foo values (x) values (?)', {json => {bar => 'baz'}});
344 $db->query('select * from foo')->expand->hash->{x}{bar}; # baz
345  
346 Hash reference arguments containing values named C<type> and C<value> can be
347 used to bind specific L<DBI> data types (see L<DBI/"DBI Constants">) to
348 placeholders. This is needed to pass binary data in parameters; see
349 L<DBD::mysql/"mysql_enable_utf8"> for more information.
350  
351 # Insert binary data
352 use DBI ':sql_types';
353 $db->query('insert into bar values (?)', {type => SQL_BLOB, value => $bytes});
354  
355 =head2 query_p
356  
357 my $promise = $db->query_p('select * from foo');
358  
359 Same as L</"query">, but performs all operations non-blocking and returns a
360 L<Mojo::Promise> object instead of accepting a callback.
361  
362 $db->query_p('insert into foo values (?, ?, ?)' => @values)->then(sub {
363 my $results = shift;
364 ...
365 })->catch(sub {
366 my $err = shift;
367 ...
368 })->wait;
369  
370 =head2 quote
371  
372 my $escaped = $db->quote($str);
373  
374 Quote a string literal for use as a literal value in an SQL statement.
375  
376 =head2 quote_id
377  
378 my $escaped = $db->quote_id($id);
379  
380 Quote an identifier (table name etc.) for use in an SQL statement.
381  
382 =head2 select
383  
384 my $results = $db->select($source, $fields, $where, $order);
385  
386 Generate a C<SELECT> statement with L<Mojo::mysql/"abstract"> (usually an
387 L<SQL::Abstract> object) and execute it with L</"query">. You can also append a
388 callback to perform operations non-blocking.
389  
390 $db->select(some_table => ['foo'] => sub {
391 my ($db, $err, $results) = @_;
392 ...
393 });
394 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
395  
396 =head2 select_p
397  
398 my $promise = $db->select_p($source, $fields, $where, $order);
399  
400 Same as L</"select">, but performs all operations non-blocking and returns a
401 L<Mojo::Promise> object instead of accepting a callback.
402  
403 $db->select_p(some_table => ['foo'] => {bar => 'yada'})->then(sub {
404 my $results = shift;
405 ...
406 })->catch(sub {
407 my $err = shift;
408 ...
409 })->wait;
410  
411 =head2 update
412  
413 my $results = $db->update($table, \%fieldvals, \%where);
414  
415 Generate an C<UPDATE> statement with L<Mojo::mysql/"abstract"> (usually an
416 L<SQL::Abstract> object) and execute it with L</"query">. You can also append a
417 callback to perform operations non-blocking.
418  
419 $db->update(some_table => {foo => 'baz'} => {foo => 'bar'} => sub {
420 my ($db, $err, $results) = @_;
421 ...
422 });
423 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
424  
425 =head2 update_p
426  
427 my $promise = $db->update_p($table, \%fieldvals, \%where, \%options);
428  
429 Same as L</"update">, but performs all operations non-blocking and returns a
430 L<Mojo::Promise> object instead of accepting a callback.
431  
432 $db->update_p(some_table => {foo => 'baz'} => {foo => 'bar'})->then(sub {
433 my $results = shift;
434 ...
435 })->catch(sub {
436 my $err = shift;
437 ...
438 })->wait;
439  
440 =head2 tables
441  
442 my $tables = $db->tables;
443  
444 Return an array reference with table names for this database.
445  
446 =head1 SEE ALSO
447  
448 L<Mojo::mysql>.
449  
450 =cut
« Mojo/mysql/PubSub.pm ↑ Summary Mojo/mysql/Results.pm »