MySQL5/Database.pm

filter: Statementstmt 19.3% 23 / 119 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 26.9% 7 / 26 Podpod 100% 9 / 9 totaltotal 16.7% 39 / 233 CC 35 SCAR 45.2
CC
35
Cov
12%
query
306.0
DESTROY
12.0
quote
12.0
CRAP 91.8
↑ Summary MySQL5/PubSub.pm »
1 package Mojo::MySQL5::Database;
2 7 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 7 ▶use Mojo::MySQL5::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Mojo::MySQL5::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Mojo::MySQL5::Transaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Encode '_utf8_off';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 has ['mysql', 'connection'];
12  
13 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
14 0 my $self = shift;
15 0 ▶ return unless my $c = $self->connection;
Branch: 1 branch
BranchTrueFalseTotal
unless my $c = $self->connection 0 0 0
16 0 ▶ return unless my $mysql = $self->mysql;
Branch: 1 branch
BranchTrueFalseTotal
unless my $mysql = $self->mysql 0 0 0
17 0 $mysql->_enqueue($c);
18 }
19  
20 0 ▶sub backlog { scalar @{shift->{waiting} || []} }
Subroutine backlog: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{'waiting'} || []
l!l
00
MC/DC0%0/1
+(shift())->{'waiting'} || []
+(shift())->{'waiting'}
Truth table0%0/2
A
A= +(shift())->{'waiting'}
Aresult
11
00
21  
22 0 ▶sub begin {
Subroutine begin: not called CC=2 SCAR=17.9
Pod documented
23 0 my $self = shift;
24 0 ▶ croak 'Already in a transaction' if ($self->connection->{status_flags} & 0x0001);
Branch: 1 branch
BranchTrueFalseTotal
if $self->connection->{"status_flags"} & 1 0 0 0
25 0 $self->query('START TRANSACTION');
26 0 $self->query('SET autocommit=0');
27 0 my $tx = Mojo::MySQL5::Transaction->new(db => $self);
28 0 weaken $tx->{db};
29 0 return $tx;
30 }
31  
32 0 ▶sub connect {
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
33 0 my $self = shift;
34 0 my $c = Mojo::MySQL5::Connection->new(url => $self->mysql->url->clone);
35 0 ▶ $c->on(error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 0 my ($c, $err) = @_;
37 0 warn 'Unable to connect to "', $self->mysql->url, '" ', $err, "\n";
38 0 });
39 0 $c->connect();
40 0 $c->unsubscribe('error');
41 0 return $self->connection($c);
42 }
43  
44 0 ▶sub disconnect { shift->connection->disconnect }
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
45  
46 0 ▶sub pid { shift->connection->{connection_id} }
Subroutine pid: not called CC=1 SCAR=6.9
Pod documented
47  
48 0 ▶sub ping { shift->connection->ping }
Subroutine ping: not called CC=1 SCAR=6.9
Pod documented
49  
50 0 ▶sub query {
Subroutine query: not called CC=17 SCAR=57.2
Pod documented
51 0 my $self = shift;
52 0 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 0 0
53 0 my $sql = shift;
54 0 my $expand_sql = '';
55  
56 0 _utf8_off $sql;
57  
58 0 ▶ while (length($sql) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $sql > 0) 0 0 0
59 0 my $token;
60 0 ▶ if ($sql =~ /^(\s+)/s # whitespace
Branch: 2 branches
BranchTrueFalseTotal
if ($sql =~ /^(\s+)/su or $sql =~ /^(\w+)/u) { } 0 0 0
elsif ($sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu or $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/pu or $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/pu) { } 0 0 0
Condition0%0/3
$sql =~ /^(\s+)/su or $sql =~ /^(\w+)/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$sql =~ /^(\s+)/su or $sql =~ /^(\w+)/u
$sql =~ /^(\s+)/su $sql =~ /^(\w+)/u
Truth table0%0/3
A or B
A= $sql =~ /^(\s+)/suB= $sql =~ /^(\w+)/u
ABresult
1X1
011
000
61 or $sql =~ /^(\w+)/) { # general name
62 0 $token = $1;
63 }
64 ▶ elsif ($sql =~ /^--.*(?:\n|\z)/p # double-dash comment
Condition0%0/15
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu or $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/pu or $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/pu
l!l&&r!l&&!r
000
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu or $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/pu
l!l&&r!l&&!r
000
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu
l!l&&r!l&&!r
000
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu
l!l&&r!l&&!r
000
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu
l!l&&r!l&&!r
000
MC/DC0%0/6
$sql =~ /^--.*(?:\n|\z)/pu or $sql =~ /^\#.*(?:\n|\z)/pu or $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu or $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/pu or $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/pu
$sql =~ /^--.*(?:\n|\z)/pu $sql =~ /^\#.*(?:\n|\z)/pu $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]pu $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/pu $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/pu $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/pu
Truth table0%0/7
A or B or C or D or E or F
A= $sql =~ /^--.*(?:\n|\z)/puB= $sql =~ /^\#.*(?:\n|\z)/puC= $sql =~ m[^/\*(?:[^\*]|\*[^/])*(?:\*/|\*\z|\z)]puD= $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/puE= $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/puF= $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/pu
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
65 or $sql =~ /^\#.*(?:\n|\z)/p # hash comment
66 or $sql =~ /^\/\*(?:[^\*]|\*[^\/])*(?:\*\/|\*\z|\z)/p # C-style comment
67 or $sql =~ /^'(?:[^'\\]*|\\(?:.|\n)|'')*(?:'|\z)/p # single-quoted literal text
68 or $sql =~ /^"(?:[^"\\]*|\\(?:.|\n)|"")*(?:"|\z)/p # double-quoted literal text
69 or $sql =~ /^`(?:[^`]*|``)*(?:`|\z)/p) { # schema-quoted literal text
70 0 $token = ${^MATCH};
71 }
72 else {
73 0 $token = substr($sql, 0, 1);
74 }
75 0 ▶ $expand_sql .= $token eq '?' ? $self->quote(shift) : $token;
Branch: 1 branch
BranchTrueFalseTotal
$token eq '?' ? : 0 0 0
76 0 substr($sql, 0, length($token), '');
77 }
78  
79 0 ▶ croak 'async query in flight' if $self->backlog and !$cb;
Branch: 1 branch
BranchTrueFalseTotal
if $self->backlog and not $cb 0 0 0
Condition0%0/3
$self->backlog and not $cb
!ll&&!rl&&r
000
MC/DC0%0/2
$self->backlog and not $cb
$self->backlog not $cb
Truth table0%0/3
A and not(B)
A= $self->backlogB= not $cb
ABresult
0X0
100
111
80 0 ▶ $self->_subscribe unless $self->backlog;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->backlog 0 0 0
81  
82 0 push @{$self->{waiting}}, { cb => $cb, sql => $expand_sql, count => 0, started => 0,
83 results => Mojo::MySQL5::Results->new };
84  
85 # Blocking
86 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
87 0 $self->connection->query($expand_sql);
88 0 $self->_unsubscribe;
89 0 my $current = shift @{$self->{waiting}};
90 0 ▶ croak $self->connection->{error_message} if $self->connection->{error_code};
Branch: 1 branch
BranchTrueFalseTotal
if $self->connection->{"error_code"} 0 0 0
91 0 return $current->{results};
92 }
93  
94 # Non-blocking
95 0 $self->_next;
96 }
97  
98 0 ▶sub quote {
Subroutine quote: not called CC=3 SCAR=24.8
Pod documented
99 0 my ($self, $string) = @_;
100 0 ▶ return 'NULL' unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
101  
102 0 for ($string) {
103 0 s/\\/\\\\/g;
104 0 s/\0/\\0/g;
105 0 s/\n/\\n/g;
106 0 s/\r/\\r/g;
107 0 s/'/\\'/g;
108 # s/"/\\"/g;
109 0 s/\x1a/\\Z/g;
110 }
111  
112 0 return "'$string'";
113 }
114  
115 0 ▶sub quote_id {
Subroutine quote_id: not called CC=2 SCAR=17.9
Pod documented
116 0 my ($self, $id) = @_;
117 0 ▶ return 'NULL' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
118 0 $id =~ s/`/``/g;
119 0 return "`$id`";
120 }
121  
122  
123 0 ▶sub _next {
Subroutine _next: not called CC=3 SCAR=24.8
124 0 my $self = shift;
125  
126 0 ▶ return unless my $next = $self->{waiting}[0];
Branch: 1 branch
BranchTrueFalseTotal
unless my $next = $self->{"waiting"}[0] 0 0 0
127 0 ▶ return if $next->{started}++;
Branch: 1 branch
BranchTrueFalseTotal
if $next->{"started"}++ 0 0 0
128  
129 0 ▶ $self->connection->query($next->{sql}, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
130 0 my $c = shift;
131 0 my $current = shift @{$self->{waiting}};
132 0 my $error = $c->{error_message};
133  
134 0 ▶ $self->backlog ? $self->_next : $self->_unsubscribe;
Branch: 1 branch
BranchTrueFalseTotal
$self->backlog ? : 0 0 0
135  
136 0 my $cb = $current->{cb};
137 0 $self->$cb($error, $current->{results});
138 0 });
139  
140 }
141  
142 0 ▶sub _subscribe {
Subroutine _subscribe: not called CC=1 SCAR=6.9
143 0 my $self = shift;
144  
145 0 ▶ $self->connection->on(fields => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
146 0 my ($c, $fields) = @_;
147 0 ▶ return unless my $res = $self->{waiting}->[0]->{results};
Branch: 1 branch
BranchTrueFalseTotal
unless my $res = $self->{"waiting"}[0]{"results"} 0 0 0
148 0 push @{ $res->{_columns} }, $fields;
149 0 $self->{waiting}->[0]->{count}++;
150 0 });
151  
152 0 ▶ $self->connection->on(result => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
153 0 my ($c, $row) = @_;
154 0 ▶ return unless my $res = $self->{waiting}->[0]->{results};
Branch: 1 branch
BranchTrueFalseTotal
unless my $res = $self->{"waiting"}[0]{"results"} 0 0 0
155 0 ▶ push @{ $res->{_results}->[$self->{waiting}->[0]->{count} - 1] //= [] }, $row;
Condition0%0/2
$res->{"_results"}[$self->{"waiting"}[0]{"count"} - 1] //= []
l!l
00
MC/DC0%0/1
$res->{"_results"}[$self->{"waiting"}[0]{"count"} - 1] //= []
$res->{"_results"}[$self->{"waiting"}[0]{"count"} - 1]
Truth table0%0/2
A
A= $res->{"_results"}[$self->{"waiting"}[0]{"count"} - 1]
Aresult
11
00
156 0 });
157  
158 0 ▶ $self->connection->on(end => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
159 0 my $c = shift;
160 0 ▶ return unless my $res = $self->{waiting}->[0]->{results};
Branch: 1 branch
BranchTrueFalseTotal
unless my $res = $self->{"waiting"}[0]{"results"} 0 0 0
161 0 $res->{$_} = $c->{$_} for qw(affected_rows last_insert_id warnings_count);
162 0 });
163  
164 0 ▶ $self->connection->on(error => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
165 0 my $c = shift;
166 0 ▶ return unless my $res = $self->{waiting}->[0]->{results};
Branch: 1 branch
BranchTrueFalseTotal
unless my $res = $self->{"waiting"}[0]{"results"} 0 0 0
167 0 $res->{$_} = $c->{$_} for qw(error_code sql_state error_message);
168 0 });
169 }
170  
171 0 ▶sub _unsubscribe {
Subroutine _unsubscribe: not called CC=2 SCAR=17.9
172 0 my $self = shift;
173 0 $self->connection->unsubscribe($_) for qw(fields result end error);
174 }
175  
176 7 1;
177  
178 =encoding utf8
179  
180 =head1 NAME
181  
182 Mojo::MySQL5::Database - Database
183  
184 =head1 SYNOPSIS
185  
186 use Mojo::MySQL5::Database;
187  
188 my $db = Mojo::MySQL5::Database->new(
189 mysql => $mysql,
190 connection => Mojo::MySQL5::Connection->new);
191  
192 =head1 DESCRIPTION
193  
194 L<Mojo::MySQL5::Database> is a container for L<Mojo::MySQL5::Connection> handles used by L<Mojo::MySQL5>.
195  
196 =head1 ATTRIBUTES
197  
198 L<Mojo::MySQL5::Database> implements the following attributes.
199  
200 =head2 connection
201  
202 my $c = $db->connection;
203 $db = $db->connection(Mojo::MySQL5::Connection->new);
204  
205 Database connection used for all queries.
206  
207 =head2 mysql
208  
209 L<Mojo::MySQL5> object this database belongs to.
210  
211 =head1 METHODS
212  
213 L<Mojo::MySQL5::Database> inherits all methods from L<Mojo::EventEmitter> and
214 implements the following ones.
215  
216 =head2 backlog
217  
218 my $num = $db->backlog;
219  
220 Number of waiting non-blocking queries.
221  
222 =head2 begin
223  
224 my $tx = $db->begin;
225  
226 Begin transaction and return L<Mojo::MySQL5::Transaction> object, which will
227 automatically roll back the transaction unless
228 L<Mojo::MySQL5::Transaction/"commit"> bas been called before it is destroyed.
229  
230 # Insert rows in a transaction
231 eval {
232 my $tx = $db->begin;
233 $db->query('insert into frameworks values (?)', 'Catalyst');
234 $db->query('insert into frameworks values (?)', 'Mojolicious');
235 $tx->commit;
236 };
237 say $@ if $@;
238  
239 =head2 connect
240  
241 $db->connect;
242  
243 Connect to MySQL server.
244  
245 =head2 disconnect
246  
247 $db->disconnect;
248  
249 Disconnect database connection and prevent it from getting cached again.
250  
251 =head2 pid
252  
253 my $pid = $db->pid;
254  
255 Return the connection id of the backend server process.
256  
257 =head2 ping
258  
259 my $bool = $db->ping;
260  
261 Check database connection.
262  
263 =head2 query
264  
265 my $results = $db->query('select * from foo');
266 my $results = $db->query('insert into foo values (?, ?, ?)', @values);
267  
268 Execute a blocking statement and return a L<Mojo::MySQL5::Results> object with the
269 results. You can also append a callback to perform operation non-blocking.
270  
271 $db->query('select * from foo' => sub {
272 my ($db, $err, $results) = @_;
273 ...
274 });
275 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
276  
277 =head2 quote
278
279 my $escaped = $db->quote($str);
280
281 Quote string value for passing to SQL query.
282
283 =head2 quote_id
284
285 my $escaped = $db->quote_id($id);
286
287 Quote identifier for passing to SQL query.
288  
289 =head1 SEE ALSO
290  
291 L<Mojo::MySQL5>.
292  
293 =cut
↑ Summary MySQL5/PubSub.pm »