MySQL5/Connection.pm

filter: Statementstmt 56.9% 176 / 309 Branchbran 41.1% 60 / 146 Conditioncond 15.3% 6 / 39 MC/DCmcdc 4.3% 1 / 23 Subroutinesub 55.7% 29 / 52 Podpod 100% 4 / 4 totaltotal 48.1% 276 / 573 SCAR 71.8
CC
96
Cov
49%
_seq_next
72.0
_send_auth
56.0
connect
42.0
CRAP 1319.4
↑ Summary MySQL5/Database.pm »
1 package Mojo::MySQL5::Connection;
2 1 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Encode qw(_utf8_off _utf8_on);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Digest::SHA qw(sha1);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::MySQL5::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 has state => 'disconnected';
12  
13 1 ▶has url => sub { Mojo::MySQL5::URL->new('mysql:///') };
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 1 ▶use constant DEBUG => $ENV{MOJO_MYSQL_DEBUG} // 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"MOJO_MYSQL_DEBUG"} // 0
l!l
01
MC/DC0%0/1
$ENV{"MOJO_MYSQL_DEBUG"} // 0
$ENV{"MOJO_MYSQL_DEBUG"}
Truth table50%1/2
A
A= $ENV{"MOJO_MYSQL_DEBUG"}
Aresult
11
00
16  
17 use constant {
18 1 CLIENT_CAPABILITY => [ qw(
19 LONG_PASSWORD FOUND_ROWS LONG_FLAG CONNECT_WITH_DB
20 NO_SCHEMA COMPRESS ODBC LOCAL_FILES
21 IGNORE_SPACE PROTOCOL_41 INTERACTIVE SSL
22 IGNORE_SIGPIPE TRANSACTIONS RESERVED SECURE_CONNECTION
23 MULTI_STATEMENTS MULTI_RESULTS PS_MULTI_RESULTS PLUGIN_AUTH
24 CONNECT_ATTRS PLUGIN_AUTH_LENENC_CLIENT_DATA CAN_HANDLE_EXPIRED_PASSWORDS SESSION_TRACK
25 DEPRECATE_EOF) ],
26  
27 SERVER_STATUS => [ qw(
28 STATUS_IN_TRANS STATUS_AUTOCOMMIT RESERVED MORE_RESULTS_EXISTS
29 STATUS_NO_GOOD_INDEX_USED STATUS_NO_INDEX_USED STATUS_CURSOR_EXISTS STATUS_LAST_ROW_SENT
30 STATUS_DB_DROPPED STATUS_NO_BACKSLASH_ESCAPES STATUS_METADATA_CHANGED QUERY_WAS_SLOW
31 PS_OUT_PARAMS STATUS_IN_TRANS_READONLY SESSION_STATE_CHANGED) ],
32  
33 FIELD_FLAG => [ qw(
34 NOT_NULL PRI_KEY UNIQUE_KEY MULTIPLE_KEY
35 BLOB UNSIGNED ZEROFILL BINARY
36 ENUM AUTO_INCREMENT TIMESTAMP SET) ],
37  
38 CHARSET => {
39 UTF8 => 33, BINARY => 63, ASCII => 65 },
40  
41 DATATYPE => {
42 DECIMAL => 0x00, TINY => 0x01, SHORT => 0x02, LONG => 0x03,
43 FLOAT => 0x04, DOUBLE => 0x05,
44 NULL => 0x06, TIMESTAMP => 0x07,
45 LONGLONG => 0x08, INT24 => 0x09,
46 DATE => 0x0a, TIME => 0x0b, DATETIME => 0x0c, YEAR => 0x0d, NEWDATE => 0x0e,
47 VARCHAR => 0x0f, BIT => 0x10,
48 NEWDECIMAL => 0xf6, ENUM => 0xf7, SET => 0xf8,
49 TINY_BLOB => 0xf9, MEDIUM_BLOB => 0xfa, LONG_BLOB => 0xfb, BLOB => 0xfc,
50 VAR_STRING => 0xfd, STRING => 0xfe, GEOMETRY => 0xff },
51 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 use constant {
54 1 REV_CHARSET => { reverse %{CHARSET()} },
55 1 REV_DATATYPE => { map { chr(DATATYPE->{$_}) => $_ } keys %{DATATYPE()} },
56 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # state machine
59 # doing => { state => '_op', state => '_op' }
60 1 use constant SEQ => {
61 connect => {
62 connected => '_recv_handshake',
63 handshake => '_send_auth',
64 auth => '_recv_ok',
65 },
66 query => {
67 idle => '_send_query',
68 query => '_recv_query_responce',
69 field => '_recv_field',
70 result => '_recv_row',
71 },
72 ping => {
73 idle => '_send_ping',
74 ping => '_recv_ok',
75 },
76 disconnect => {
77 idle => '_send_quit',
78 quit => '_recv_ok'
79 }
80 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82  
83 # encode fixed length integer
84 sub _encode_int($$) {
85 0 ▶ my ($int, $len) = @_;
Subroutine _encode_int: not called CC=5 SCAR=34.0
86 0 ▶ return substr pack('V', $int), 0, $len if $len >= 1 and $len <= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $len >= 1 and $len <= 4 0 0 0
Condition0%0/3
$len >= 1 and $len <= 4
!ll&&!rl&&r
000
MC/DC0%0/2
$len >= 1 and $len <= 4
$len >= 1 $len <= 4
Truth table0%0/3
A and B
A= $len >= 1B= $len <= 4
ABresult
0X0
100
111
87 0 ▶ return substr pack('VV', int $int % 2 ** 32, int $int / 2 ** 32), 0, $len if $len == 6 or $len = 8;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 6 or $len = 8 0 0 0
Condition0%0/2
$len == 6 or $len = 8
l!l
00
MC/DC0%0/1
$len == 6 or $len = 8
$len == 6
Truth table0%0/2
A
A= $len == 6
Aresult
11
00
88 0 return undef;
89 }
90  
91 # encode length coded integer
92 sub _encode_lcint($) {
93 29 ▶ my $int = shift;
Subroutine _encode_lcint: called CC=5 SCAR=16.1
94 return
95 29 ▶ !defined $int ? pack 'C', 251 :
Branch: 4 branches
BranchTrueFalseTotal
!defined($int) ? : 3 26 29
$int <= 250 ? : 9 17 26
$int <= 65535 ? : 6 11 17
$int <= 16777215 ? : 5 6 11
96 $int <= 250 ? pack 'C', $int :
97 $int <= 0xffff ? pack 'Cv', 252, $int :
98 $int <= 0xffffff ? substr pack('CV', 253, $int), 0, 4 :
99 pack 'CVV', 254, int $int % 2 ** 32, int $int / 2 ** 32;
100 }
101  
102 # encode length coded string
103 sub _encode_lcstr($) {
104 12 ▶ my $str = shift;
Subroutine _encode_lcstr: called CC=2 SCAR=6.9
105 12 ▶ return defined $str ? _encode_lcint(length $str) . $str : _encode_lcint($str);
Branch: 1 branch
BranchTrueFalseTotal
defined $str ? : 10 2 12
106 }
107  
108 # get fixed length integer
109 sub _get_int {
110 109 ▶ my ($self, $len, $chew) = @_;
Subroutine _get_int: called CC=7 SCAR=19.5
111 109 ▶ my $data = $chew ? substr $self->{incoming}, 0, $len, '' : substr $self->{incoming}, 0, $len;
Branch: 1 branch
BranchTrueFalseTotal
$chew ? : 81 28 109
112 109 ▶ return unpack 'C', $data if $len == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 1 78 31 109
113 31 ▶ return unpack 'V', $data . "\0\0" if $len >= 2 and $len <= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $len >= 2 and $len <= 4 25 6 31
Condition66%2/3
$len >= 2 and $len <= 4
!ll&&!rl&&r
0625
MC/DC50%1/2
$len >= 2 and $len <= 4
$len >= 2 $len <= 4
Truth table66%2/3
A and B
A= $len >= 2B= $len <= 4
ABresult
0X0
100
111
114 6 ▶ return undef unless $len == 8;
Branch: 1 branch
BranchTrueFalseTotal
unless $len == 8 0 6 6
115 6 my $lo = unpack ('V', substr $data, 0, 4);
116 6 my $hi = unpack('V', substr $data, 4, 4);
117 6 ▶ return $hi ?
Branch: 1 branch
BranchTrueFalseTotal
$hi ? : 2 4 6
118 int $lo + int $hi * 2 ** 32 : $lo;
119 }
120  
121 81 ▶sub _chew_int { shift->_get_int(shift, 1) }
Subroutine _chew_int: called CC=1 SCAR=0.0
122  
123 # get length coded integer
124 sub _chew_lcint {
125 41 ▶ my $self = shift;
Subroutine _chew_lcint: called CC=6 SCAR=17.9
126 41 my $first = $self->_chew_int(1);
127 return
128 41 ▶ $first < 251 ? $first :
Branch: 5 branches
BranchTrueFalseTotal
$first < 251 ? : 21 20 41
$first == 251 ? : 3 17 20
$first == 252 ? : 6 11 17
$first == 253 ? : 5 6 11
$first == 254 ? : 6 0 6
129 $first == 251 ? undef :
130 $first == 252 ? $self->_chew_int(2) :
131 $first == 253 ? $self->_chew_int(3) :
132 $first == 254 ? $self->_chew_int(8) : undef;
133 }
134  
135 # get length coded string
136 sub _chew_lcstr {
137 14 ▶ my $self = shift;
Subroutine _chew_lcstr: called CC=2 SCAR=6.9
138 14 my $len = $self->_chew_lcint;
139 14 ▶ return defined $len ? substr $self->{incoming}, 0, $len, '' : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $len ? : 13 1 14
140 }
141  
142 # get zero ending string
143 sub _chew_zstr {
144 3 ▶ my $self = shift;
Subroutine _chew_zstr: called CC=2 SCAR=7.0
145 3 my $str = unpack 'Z*', $self->{incoming};
146 3 ▶ return undef unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 3 3
147 3 substr $self->{incoming}, 0, length($str) + 1, '';
148 3 return $str;
149 }
150  
151 # get fixed length string
152 sub _chew_str {
153 8 ▶ my ($self, $len) = @_;
Subroutine _chew_str: called CC=2 SCAR=7.1
154 8 ▶ die "_chew_str($len) error" if $len > length $self->{incoming};
Branch: 1 branch
BranchTrueFalseTotal
if $len > length $self->{"incoming"} 0 8 8
155 8 return substr $self->{incoming}, 0, $len, '';
156 }
157  
158  
159 sub _send_auth {
160 0 ▶ my $self = shift;
Subroutine _send_auth: not called CC=7 SCAR=40.3
161  
162 0 my ($username, $password, $database, $crypt) =
163 ($self->url->username, $self->url->password, $self->url->database, '');
164  
165 0 my @flags = qw(LONG_PASSWORD LONG_FLAG PROTOCOL_41 TRANSACTIONS SECURE_CONNECTION MULTI_RESULTS);
166 0 ▶ push @flags, 'CONNECT_WITH_DB' if $database;
Branch: 1 branch
BranchTrueFalseTotal
if $database 0 0 0
167 0 ▶ push @flags, 'MULTI_STATEMENTS' if $self->url->options->{multi_statements};
Branch: 1 branch
BranchTrueFalseTotal
if $self->url->options->{"multi_statements"} 0 0 0
168 0 ▶ push @flags, 'FOUND_ROWS' if $self->url->options->{found_rows};
Branch: 1 branch
BranchTrueFalseTotal
if $self->url->options->{"found_rows"} 0 0 0
169 0 my $flags = _flag_set(CLIENT_CAPABILITY, @flags);
170  
171 0 warn '>>> AUTH ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n",
172 ' user:', $username, ' database:', $database,
173 ' flags:', _flag_list(CLIENT_CAPABILITY, $flags),
174 '(', sprintf('%08X', $flags), ')', "\n" if DEBUG > 1;
175  
176 0 _utf8_off $username; _utf8_off $password; _utf8_off $database;
177  
178 0 ▶ if ($password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($password) 0 0 0
179 0 my $crypt1 = sha1($password);
180 0 my $crypt2 = sha1($self->{auth_plugin_data} . sha1 $crypt1);
181 0 $crypt = $crypt1 ^ $crypt2;
182 }
183  
184 0 $self->state('auth');
185 0 delete $self->{auth_plugin_data};
186 return pack 'VVCx23Z*a*Z*',
187 $flags, 131072, ($self->url->options->{utf8} // 1) ? CHARSET->{UTF8} : CHARSET->{BINARY},
188 0 ▶ $username, _encode_lcstr($crypt), $database, 'mysql_native_password';
Branch: 1 branch
BranchTrueFalseTotal
$self->url->options->{'utf8'} // 1 ? : 0 0 0
Condition0%0/2
$self->url->options->{"utf8"} // 1
l!l
00
MC/DC0%0/1
$self->url->options->{"utf8"} // 1
$self->url->options->{"utf8"}
Truth table0%0/2
A
A= $self->url->options->{"utf8"}
Aresult
11
00
189 }
190  
191 sub _send_quit {
192 0 ▶ my $self = shift;
Subroutine _send_quit: not called CC=1 SCAR=6.9
193 0 warn '>>> QUIT ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n" if DEBUG > 1;
194 0 $self->state('quit');
195 0 return pack 'C', 1;
196 }
197  
198 sub _send_query {
199 0 ▶ my $self = shift;
Subroutine _send_query: not called CC=1 SCAR=6.9
200 0 my $sql = $self->{sql};
201 0 warn '>>> QUERY ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n",
202 " sql:$sql\n" if DEBUG > 1;
203 0 _utf8_off $sql;
204 0 $self->state('query');
205 0 return pack('C', 3) . $sql;
206 }
207  
208 sub _send_ping {
209 0 ▶ my $self = shift;
Subroutine _send_ping: not called CC=1 SCAR=6.9
210 0 warn '>>> PING ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n" if DEBUG > 1;
211 0 $self->state('ping');
212 0 return pack 'C', 14;
213 }
214  
215 sub _recv_error {
216 1 ▶ my $self = shift;
Subroutine _recv_error: called CC=2 SCAR=6.9
217 1 my $first = $self->_chew_int(1);
218 1 ▶ die "_recv_error() wrong packet $first" unless $first == 255;
Branch: 1 branch
BranchTrueFalseTotal
unless $first == 255 0 1 1
219  
220 1 $self->{error_code} = $self->_chew_int(2);
221 1 $self->_chew_str(1);
222 1 $self->{sql_state} = $self->_chew_str(5);
223 1 $self->{error_message} = $self->_chew_zstr;
224  
225 warn '<<< ERROR ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n",
226 ' error:', $self->{error_code},
227 ' state:', $self->{sql_state},
228 1 ' message:', $self->{error_message}, "\n" if DEBUG > 1;
229  
230 1 $self->state('idle');
231 1 $self->emit(error => $self->{error_message});
232 }
233  
234 sub _recv_ok {
235 2 ▶ my $self = shift;
Subroutine _recv_ok: called CC=5 SCAR=16.2
236 2 my $first = $self->_get_int(1);
237 2 ▶ return $self->_recv_error if $first == 255;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 255 1 1 2
238 1 ▶ die "_recv_ok() wrong packet $first" unless $first == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $first == 0 0 1 1
239  
240 1 $self->_chew_int(1);
241 1 $self->{affected_rows} = $self->_chew_lcint;
242 1 $self->{last_insert_id} = $self->_chew_lcint;
243 1 $self->{status_flags} = $self->_chew_int(2);
244 1 $self->{warnings_count} = $self->_chew_int(2);
245 1 $self->{field_count} = 0;
246  
247 warn '<<< OK ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n",
248 ' affected:', $self->{affected_rows},
249 ' last_insert_id:', $self->{last_insert_id},
250 ' status:', _flag_list(SERVER_STATUS, $self->{status_flags}),
251 '(', sprintf('%04X', $self->{status_flags}), ')',
252 1 ' warnings:', $self->{warnings_count}, "\n" if DEBUG > 1;
253  
254 1 ▶ $self->emit('connect') if $self->state eq 'auth';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "auth" 0 1 1
255 1 ▶ $self->emit('end') if $self->state eq 'query';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "query" 0 1 1
256 1 $self->state('idle');
257 }
258  
259 sub _recv_query_responce {
260 1 ▶ my $self = shift;
Subroutine _recv_query_responce: called CC=3 SCAR=11.2
261 1 my $first = $self->_get_int(1);
262 1 ▶ return $self->_recv_error if $first == 255;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 255 0 1 1
263 1 ▶ return $self->_recv_ok if $first == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 0 0 1 1
264  
265 1 $self->{field_count} = $self->_chew_lcint;
266  
267 warn '<<< QUERY_RESPONSE ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n",
268 1 ' fields:', $self->{field_count}, "\n" if DEBUG > 1;
269  
270 1 $self->state('field');
271 }
272  
273 sub _recv_eof {
274 2 ▶ my $self = shift;
Subroutine _recv_eof: called CC=6 SCAR=18.3
275 2 my $first = $self->_get_int(1);
276 2 ▶ return $self->_recv_error if $first == 255;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 255 0 2 2
277 2 ▶ die "_recv_eof() wrong packet $first" unless $first == 254;
Branch: 1 branch
BranchTrueFalseTotal
unless $first == 254 0 2 2
278  
279 2 $self->_chew_int(1);
280 2 $self->{warnings_count} = $self->_chew_int(2);
281 2 $self->{status_flags} = $self->_chew_int(2);
282  
283 warn '<<< EOF ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n",
284 ' warnings:', $self->{warnings_count},
285 ' status:', _flag_list(SERVER_STATUS, $self->{status_flags}),
286 2 '(', sprintf('%04X', $self->{status_flags}), ')', "\n" if DEBUG > 1;
287  
288 2 ▶ if ($self->state eq 'field') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->state eq 'field') { } 1 1 2
elsif ($self->state eq 'result') { } 1 0 1
289 1 $self->emit(fields => $self->{column_info});
290 1 $self->state('result');
291 }
292 elsif ($self->state eq 'result') {
293 1 $self->{column_info} = [];
294 1 ▶ if ($self->{status_flags} & 0x0008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'status_flags'} & 8) { } 0 1 1
295 # MORE_RESULTS
296 0 $self->state('query');
297 }
298 else {
299 1 $self->emit(end => undef);
300 1 $self->state('idle');
301 }
302 }
303 }
304  
305 sub _recv_field {
306 2 ▶ my $self = shift;
Subroutine _recv_field: called CC=7 SCAR=19.6
307 2 my $first = $self->_get_int(1);
308 2 ▶ return $self->_recv_error if $first == 255;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 255 0 2 2
309 2 ▶ return $self->_recv_eof if $first == 254;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 254 1 1 2
310 1 ▶ die "_recv_field() wrong packet $first" if $first > 250;
Branch: 1 branch
BranchTrueFalseTotal
if $first > 250 0 1 1
311  
312 1 my $field = {};
313 1 $field->{catalog} = $self->_chew_lcstr;
314 1 $field->{schema} = $self->_chew_lcstr;
315 1 $field->{table} = $self->_chew_lcstr;
316 1 $field->{org_table} = $self->_chew_lcstr;
317 1 $field->{name} = $self->_chew_lcstr;
318 1 $field->{org_name} = $self->_chew_lcstr;
319 1 $self->_chew_lcint;
320 1 $field->{character_set} = $self->_chew_int(2);
321 1 $field->{column_length} = $self->_chew_int(4);
322 1 $field->{column_type} = $self->_chew_int(1);
323 1 $field->{flags} = $self->_chew_int(2);
324 1 $field->{decimals} = $self->_chew_int(1);
325 1 $self->_chew_str(2);
326  
327 1 do { _utf8_on $field->{$_} for qw(catalog schema table org_table name org_name) }
328 1 ▶ if ($self->url->options->{utf8} // 1);
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url->options->{"utf8"} // 1) 1 0 1
Condition50%1/2
$self->url->options->{"utf8"} // 1
l!l
01
MC/DC0%0/1
$self->url->options->{"utf8"} // 1
$self->url->options->{"utf8"}
Truth table50%1/2
A
A= $self->url->options->{"utf8"}
Aresult
11
00
329  
330 1 push @{$self->{column_info}}, $field;
331  
332 warn '<<< FIELD ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n",
333 ' name:', $field->{name},
334 ' type:', REV_DATATYPE->{chr $field->{column_type}}, '(', $field->{column_type}, ')',
335 ' length:', $field->{column_length},
336 ' charset:', REV_CHARSET->{$field->{character_set}} // 'UNKNOWN', '(', $field->{character_set}, ')',
337 1 ' flags:', _flag_list(FIELD_FLAG, $field->{flags}), '(', $field->{flags}, ')', , "\n" if DEBUG > 1;
338 }
339  
340 sub _recv_row {
341 3 ▶ my $self = shift;
Subroutine _recv_row: called CC=5 SCAR=16.2
342 3 my $first = $self->_get_int(1);
343 3 ▶ return $self->_recv_error if $first == 255;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 255 0 3 3
344 3 ▶ return $self->_recv_eof if $first == 254;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 254 1 2 3
345  
346 2 my @row;
347 2 for (0 .. $self->{field_count} - 1) {
348 2 $row[$_] = $self->_chew_lcstr;
349 _utf8_on $row[$_]
350 2 ▶ if $self->{column_info}->[$_]->{character_set} == CHARSET->{UTF8};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"column_info"}[$_]{"character_set"} == {"ASCII" => 65,"BINARY" => 63,"UTF8" => 33}->{"UTF8"} 2 0 2
351 }
352  
353 warn '<<< ROW ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n",
354 2 join(', ', map { defined $_ ? "'" . $_ . "'" : 'null' } @row), "\n" if DEBUG > 1;
355  
356 2 $self->emit(result => \@row);
357 }
358  
359 sub _recv_handshake {
360 1 ▶ my $self = shift;
Subroutine _recv_handshake: called CC=6 SCAR=18.3
361 1 my $first = $self->_get_int(1);
362 1 ▶ return $self->_recv_error if $first == 255;
Branch: 1 branch
BranchTrueFalseTotal
if $first == 255 0 1 1
363  
364 1 $self->{protocol_version} = $self->_chew_int(1);
365 1 $self->{server_version} = $self->_chew_zstr;
366 1 $self->{connection_id} = $self->_chew_int(4);
367 1 $self->{auth_plugin_data} = $self->_chew_str(8);
368 1 $self->_chew_str(1);
369 1 $self->{capability_flags} = $self->_chew_int(2);
370 1 $self->{character_set} = $self->_chew_int(1);
371 1 $self->{status_flags} = $self->_chew_int(2);
372 1 $self->{capability_flags} |= $self->_chew_int(2) << 16;
373 1 my $auth_len = $self->_chew_int(1);
374 1 $self->_chew_str(10);
375 1 $self->{auth_plugin_data} .= $self->_chew_str(12);
376 1 $self->_chew_str(1);
377 1 my $auth_plugin_name = $self->_chew_zstr;
378  
379 warn '<<< HANDSHAKE ', $self->{connection_id}, ' #', $self->{seq}, ' state:', $self->state, "\n",
380 ' protocol:', $self->{protocol_version},
381 ' version:', $self->{server_version},
382 ' connection:', $self->{connection_id},
383 ' status:', _flag_list(SERVER_STATUS, $self->{status_flags}),
384 '(', sprintf('%04X', $self->{status_flags}), ')',
385 ' capabilities:', _flag_list(CLIENT_CAPABILITY, $self->{capability_flags}),
386 1 '(', sprintf('%08X', $self->{capability_flags}), ')',
387 ' auth:', $auth_plugin_name, "\n" if DEBUG > 1;
388  
389 die '_recv_handshake() invalid protocol version ' . $self->{protocol_version}
390 1 ▶ unless $self->{protocol_version} == 10;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"protocol_version"} == 10 0 1 1
391 1 ▶ die '_recv_handshake() unsupported auth method ' . $auth_plugin_name
Branch: 1 branch
BranchTrueFalseTotal
unless $auth_plugin_name eq "mysql_native_password" 0 1 1
392 unless $auth_plugin_name eq 'mysql_native_password';
393 die '_recv_handshake() invalid auth data '
394 1 ▶ unless $auth_len == 21 and length($self->{auth_plugin_data}) == 20;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth_len == 21 and length $self->{"auth_plugin_data"} == 20 0 1 1
Condition33%1/3
$auth_len == 21 and length $self->{"auth_plugin_data"} == 20
!ll&&!rl&&r
001
MC/DC0%0/2
$auth_len == 21 and length $self->{"auth_plugin_data"} == 20
$auth_len == 21 length $self->{"auth_plugin_data"} == 20
Truth table33%1/3
A and B
A= $auth_len == 21B= length $self->{"auth_plugin_data"} == 20
ABresult
0X0
100
111
395  
396 1 $self->state('handshake');
397 }
398  
399 sub _reset {
400 1 ▶ my $self = shift;
Subroutine _reset: called CC=2 SCAR=6.9
401  
402 1 undef $self->{$_} for qw(error_code sql_state error_message
403 affected_rows last_insert_id status_flags warnings_count field_count);
404  
405 1 $self->{column_info} = [];
406 1 $self->{seq} = 0;
407 1 $self->{incoming} = '';
408 }
409  
410 sub _ioloop {
411 0 ▶ $_[1] ? Mojo::IOLoop->singleton : ($_[0]->{ioloop} ||= Mojo::IOLoop->new);
Subroutine _ioloop: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
Condition0%0/3
$_[0]{"ioloop"} ||= "Mojo::IOLoop"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"ioloop"} ||= "Mojo::IOLoop"->new
$_[0]{"ioloop"} "Mojo::IOLoop"->new
Truth table0%0/3
A ||= B
A= $_[0]{"ioloop"}B= "Mojo::IOLoop"->new
ABresult
1X1
011
000
412 }
413  
414 sub _seq_next_ready {
415 0 ▶ my $self = shift;
Subroutine _seq_next_ready: not called CC=2 SCAR=17.9
416 0 ▶ return 0 if length $self->{incoming} < 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"incoming"} < 4 0 0 0
417 0 return length($self->{incoming}) - 4 >= $self->_get_int(3);
418 }
419  
420 sub _seq_next {
421 0 ▶ my ($self, $cmd, $writeonly) = @_;
Subroutine _seq_next: not called CC=8 SCAR=42.8
422 0 my $next = SEQ->{$cmd}{$self->state};
423 0 warn 'stream state:', $self->state, ' doing:', $cmd, ' next:', ($next // ''), "\n" if DEBUG > 2;
424 0 ▶ return unless $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 0 0 0
425 0 ▶ if (substr($next, 0, 6) eq '_send_') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($next, 0, 6) eq '_send_') { } 0 0 0
elsif (substr($next, 0, 6) eq '_recv_') { } 0 0 0
426 0 my $packet = $self->$next();
427 0 $self->{stream}->write(_encode_int(length $packet, 3) . _encode_int($self->{seq}, 1) . $packet);
428 }
429 elsif (substr($next, 0, 6) eq '_recv_') {
430 0 ▶ return if $writeonly;
Branch: 1 branch
BranchTrueFalseTotal
if $writeonly 0 0 0
431 0 my ($len, $seq) = ($self->_chew_int(3), $self->_chew_int(1));
432 die "_next_packet() packet out of order $seq " . $self->{seq}
433 0 ▶ if $self->{seq} and $seq != (($self->{seq} + 1) & 0xff);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seq"} and $seq != ($self->{"seq"} + 1 & 255) 0 0 0
Condition0%0/3
$self->{"seq"} and $seq != ($self->{"seq"} + 1 & 255)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"seq"} and $seq != ($self->{"seq"} + 1 & 255)
$self->{"seq"} $seq != ($self->{"seq"} + 1 & 255)
Truth table0%0/3
A and B
A= $self->{"seq"}B= $seq != ($self->{"seq"} + 1 & 255)
ABresult
0X0
100
111
434 0 ▶ die "_next_packet() not ready" if $len > length($self->{incoming});
Branch: 1 branch
BranchTrueFalseTotal
if $len > length $self->{"incoming"} 0 0 0
435 0 $self->{seq}++;
436 0 $self->$next();
437 }
438 else {
439 0 $self->$next();
440 }
441 }
442  
443 sub _seq {
444 0 ▶ my ($self, $cmd, $cb) = @_;
Subroutine _seq: not called CC=3 SCAR=24.8
445  
446 0 $self->{stream} = Mojo::IOLoop::Stream->new($self->{socket});
447 0 ▶ $self->{stream}->reactor($self->_ioloop(0)->reactor) unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
448 $self->{stream}->timeout($self->url->options->{query_timeout})
449 0 ▶ if $self->url->options->{query_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->url->options->{"query_timeout"} 0 0 0
450 0 weaken $self;
451  
452 $self->{stream}->on(read => sub {
453 0 ▶ my ($stream, $bytes) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
454 0 $self->{incoming} .= $bytes;
455  
456 0 ▶ $self->_seq_next($cmd, 0) while $self->_seq_next_ready;
Branch: 1 branch
BranchTrueFalseTotal
$self->_seq_next_ready and do { $self->_seq_next($cmd, 0) } 0 0 0
457  
458 0 ▶ if ($self->state eq 'idle') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'idle') { } 0 0 0
459 0 $stream->steal_handle;
460 0 delete $self->{stream};
461 0 ▶ $cb ? $self->$cb() : $self->_ioloop(0)->stop;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
462 }
463 else {
464 0 $self->_seq_next($cmd, 1);
465 }
466 0 });
467 $self->{stream}->on(error => sub {
468 0 ▶ my ($stream, $err) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
469 0 warn "stream error: $err state:", $self->state, "\n" if DEBUG;
470 0 ▶ $self->{error_message} //= $err;
Condition0%0/3
$self->{"error_message"} //= $err
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"error_message"} //= $err
$self->{"error_message"} $err
Truth table0%0/3
A //= B
A= $self->{"error_message"}B= $err
ABresult
1X1
011
000
471 0 $self->emit(error => $err);
472 0 });
473 $self->{stream}->on(timeout => sub {
474 0 ▶ warn "stream timeout state:", $self->state, "\n" if DEBUG;
Subroutine __ANON__: not called CC=2 SCAR=17.9
475 0 ▶ $self->{error_message} //= 'timeout';
Condition0%0/2
$self->{"error_message"} //= "timeout"
l!l
00
MC/DC0%0/1
$self->{"error_message"} //= "timeout"
$self->{"error_message"}
Truth table0%0/2
A
A= $self->{"error_message"}
Aresult
11
00
476 0 });
477 $self->{stream}->on(close => sub {
478 0 ▶ $self->{socket} = undef;
Subroutine __ANON__: not called CC=2 SCAR=17.9
479 0 $self->state('disconnected');
480 0 ▶ $cb ? $self->$cb() : $self->_ioloop(0)->stop;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
481 0 });
482  
483 0 $self->{stream}->start;
484 0 $self->_seq_next($cmd, 1);
485 }
486  
487 sub _cmd {
488 0 ▶ my ($self, $cmd, $cb) = @_;
Subroutine _cmd: not called CC=5 SCAR=34.0
489 0 ▶ die 'invalid cmd:' . $cmd unless exists SEQ->{$cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless exists {"connect" => {"auth" => "_recv_ok","connected" => "_recv_handshake","handshake" => "_send_auth"},"disconnect" => {"idle" => "_send_quit","quit" => "_recv_ok"},"ping" => {"idle" => "_send_ping","ping" => "_recv_ok"},"query" => {"field" => "_recv_field","idle" => "_send_query","query" => "_recv_query_responce","result" => "_recv_row"}}->{$cmd} 0 0 0
490 0 ▶ die 'invalid state:' . $self->state . ' doing:'. $cmd unless exists SEQ->{$cmd}{$self->state};
Branch: 1 branch
BranchTrueFalseTotal
unless exists {"connect" => {"auth" => "_recv_ok","connected" => "_recv_handshake","handshake" => "_send_auth"},"disconnect" => {"idle" => "_send_quit","quit" => "_recv_ok"},"ping" => {"idle" => "_send_ping","ping" => "_recv_ok"},"query" => {"field" => "_recv_field","idle" => "_send_query","query" => "_recv_query_responce","result" => "_recv_row"}}->{$cmd}{$self->state} 0 0 0
491  
492 0 $self->_reset;
493 0 $self->_seq($cmd, $cb);
494 0 ▶ $self->_ioloop(0)->start unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
495 0 ▶ return $self->state eq 'idle' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->state eq 'idle' ? : 0 0 0
496 }
497  
498 sub connect {
499 0 ▶ my ($self, $cb) = @_;
Subroutine connect: not called CC=6 SCAR=37.4
Pod documented
500  
501 0 $self->state('connecting');
502 0 $self->_reset;
503  
504 0 $self->{client} = Mojo::IOLoop::Client->new;
505 0 ▶ $self->{client}->reactor($self->_ioloop(0)->reactor) unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
506 0 weaken $self;
507  
508 $self->{client}->on(connect => sub {
509 0 ▶ my ($client, $handle) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
510 0 delete $self->{client};
511 0 $self->{socket} = $handle;
512 0 $self->state('connected');
513 0 $self->_seq('connect', $cb);
514 0 });
515 $self->{client}->on(error => sub {
516 0 ▶ my ($client, $err) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
517 0 delete $self->{client};
518 0 $self->state('disconnected');
519 0 $self->emit(error => $err);
520 0 ▶ $cb ? $self->$cb() : $self->_ioloop(0)->stop;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
521 0 });
522  
523 $self->{client}->connect(
524 address => $self->url->host || 'localhost',
525 port => $self->url->port || 3306,
526 0 ▶ timeout => $self->url->options->{connect_timeout} // 10
Condition0%0/6
$self->url->host || 'localhost'
l!l
00
$self->url->port || 3306
l!l
00
$self->url->options->{"connect_timeout"} // 10
l!l
00
MC/DC0%0/1
$self->url->host || 'localhost'
$self->url->host
MC/DC0%0/1
$self->url->port || 3306
$self->url->port
MC/DC0%0/1
$self->url->options->{"connect_timeout"} // 10
$self->url->options->{"connect_timeout"}
Truth table0%0/2
A
A= $self->url->host
Aresult
11
00
Truth table0%0/2
A
A= $self->url->port
Aresult
11
00
Truth table0%0/2
A
A= $self->url->options->{"connect_timeout"}
Aresult
11
00
527 );
528  
529 0 ▶ $self->_ioloop(0)->start unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
530 }
531  
532 0 ▶sub disconnect { shift->_cmd('disconnect') }
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
533  
534 sub ping {
535 0 ▶ my ($self, $cb) = @_;
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
536 0 ▶ return $self->state eq 'disconnected' ? 0 : $self->_cmd('ping', $cb);
Branch: 1 branch
BranchTrueFalseTotal
$self->state eq 'disconnected' ? : 0 0 0
537 }
538  
539 sub query {
540 0 ▶ my ($self, $sql, $cb) = @_;
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
541 0 $self->{sql} = $sql;
542 0 $self->_cmd('query', $cb);
543 }
544  
545 sub DESTROY {
546 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=3 SCAR=13.1
547 1 ▶ $self->disconnect if $self->state eq 'idle' and $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "idle" and $self->{"socket"} 0 1 1
Condition33%1/3
$self->state eq "idle" and $self->{"socket"}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->state eq "idle" and $self->{"socket"}
$self->state eq "idle" $self->{"socket"}
Truth table33%1/3
A and B
A= $self->state eq "idle"B= $self->{"socket"}
ABresult
0X0
100
111
548 }
549  
550 # Private util functions
551 sub _flag_list($$;$) {
552 0 ▶ my ($list, $data, $sep) = @_;
Subroutine _flag_list: not called CC=2 SCAR=17.9
553 0 my $i = 0;
554 0 ▶ return join $sep || '|', grep { $data & 1 << $i++ } @$list;
Condition0%0/2
$sep || '|'
l!l
00
MC/DC0%0/1
$sep || '|'
$sep
Truth table0%0/2
A
A= $sep
Aresult
11
00
555 }
556  
557 sub _flag_set($;@) {
558 0 ▶ my ($list, @ops) = @_;
Subroutine _flag_set: not called CC=4 SCAR=30.0
559 0 my ($i, $flags) = (0, 0);
560 0 foreach my $flag (@$list) {
561 0 ▶ do { $flags |= 1 << $i if $_ eq $flag } for @ops;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $flag 0 0 0
562 0 $i++;
563 }
564 0 return $flags;
565 }
566  
567 sub _flag_is($$$) {
568 0 ▶ my ($list, $data, $flag) = @_;
Subroutine _flag_is: not called CC=3 SCAR=24.8
569 0 my $i = 0;
570 0 foreach (@$list) {
571 0 ▶ return $data & 1 << $i if $flag eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $flag eq $_ 0 0 0
572 0 $i++;
573 }
574 0 return undef;
575 }
576  
577 1 1;
578  
579 =encoding utf8
580  
581 =head1 NAME
582  
583 Mojo::MySQL5::Connection - TCP connection to MySQL Server
584  
585 =head1 SYNOPSIS
586  
587 use Mojo::MySQL5::Conection;
588  
589 my $c = Mojo::MySQL5::Conection->new(
590 url => Mojo::MySQL5->new(
591 'mysql://test:password@127.0.0.1:3306/test?found_rows=1&connect_timeout=2')
592 );
593  
594 Mojo::IOLoop->delay(
595 sub {
596 my $delay = shift;
597 $c->connect($delay->begin);
598 },
599 sub {
600 my ($delay, $c) = @_;
601 $c->query('select * from test_data', $delay->begin);
602 },
603 sub {
604 my ($delay, $c) = @_;
605 }
606 )->wait;
607  
608  
609 =head1 DESCRIPTION
610  
611 L<Mojo::MySQL5::Conection> is Asyncronous Protocol Implementation for connection to MySQL Server
612 managed by L<Mojo::IOLoop>.
613  
614 =head1 EVENTS
615  
616 L<Mojo::MySQL5> inherits all events from L<Mojo::EventEmitter> and can emit the
617 following new ones.
618  
619 =head2 fields
620  
621 $c->on(fields => sub {
622 my ($c, $fields) = @_;
623 ...
624 });
625  
626 Emitted after posting query and fields definition is received.
627  
628 =head2 result
629  
630 $c->on(result => sub {
631 my ($c, $result) = @_;
632 ...
633 });
634  
635 Emited when a result row is received.
636  
637 =head2 end
638  
639 $c->on(end => sub {
640 my $c = shift;
641 ...
642 });
643  
644 Emited when query ended successfully.
645  
646 =head2 error
647  
648 $c->on(error => sub {
649 my ($c, $error) = @_;
650 ...
651 });
652  
653 Emited when Error is received.
654  
655  
656 =head1 ATTRIBUTES
657  
658 L<Mojo::MySQL5::Conection> implements the following attributes.
659  
660 =head2 state
661  
662 my $state = $c->state;
663 $c->state('disconnected');
664  
665 Connection State.
666  
667 Possible States are:
668  
669 =over 2
670  
671 =item disconnected
672  
673 Initial state before connecting to server.
674  
675 Same state after fatal erorr.
676  
677 =item connected
678  
679 Connection to server is established.
680  
681 Next wait for C<Initial Handshake> packet.
682  
683 =item handshake
684  
685 Server responded with C<Initial Handshake>.
686  
687 Next send C<Handshake Response> (authentication) packet.
688  
689 =item auth
690  
691 C<Handshake Response> (authentication) packet sent to server.
692  
693 Next wait for C<OK> or C<Error> packet.
694  
695 =item idle
696  
697 Connection is idle and ready for sending commands.
698  
699 =item query
700  
701 C<COM_QUERY> packet sent to server.
702  
703 Waiting for C<COM_QUERY Response> packet. C<OK> is expected for non-SELECT queries.
704  
705 =item field
706  
707 Waiting for C<Column Definition> packets. C<EOF> is expected for end of column definition.
708  
709 =item result
710  
711 Waiting for C<Text Resultset Row> packets. C<EOF> is expected for end of result rows.
712  
713 =item ping
714  
715 C<COM_PING> packet is sent to server.
716  
717 Waitint for C<OK> packet.
718  
719 =back
720  
721 =head2 url
722  
723 my $url = $c->url;
724 $c->url(Mojo::MySQL5::URL->new('mysql://localhost/test');
725  
726 MySQL Connection URL.
727  
728 Supported Options are:
729  
730 =over 2
731  
732 =item found_rows
733  
734 Enables or disables the flag C<CLIENT_FOUND_ROWS> while connecting to the MySQL server.
735 Without C<found_rows>, if you perform a query like
736
737 UPDATE $table SET id = 1 WHERE id = 1;
738
739 then the MySQL engine will return 0, because no rows have changed.
740 With C<found_rows>, it will return the number of rows that have an id 1.
741  
742 =item multi_statements
743  
744 Enables or disables the flag C<CLIENT_MULTI_STATEMENTS> while connecting to the server.
745 If enabled multiple statements separated by semicolon (;) can be send with single
746 call to L<query>.
747  
748 =item utf8
749  
750 If enabled default character set is to C<utf8_general_ci> while connecting to the server.
751 If disabled C<binary> is the default character set.
752  
753 =item connect_timeout
754  
755 The connect request to the server will timeout if it has not been successful
756 after the given number of seconds.
757  
758 =item query_timeout
759  
760 If enabled, the read or write operation to the server will timeout
761 if it has not been successful after the given number of seconds.
762  
763 =back
764  
765 =head1 METHODS
766  
767 L<Mojo::MySQL5::Conection> inherits all methods from L<Mojo::EventEmitter> and
768 implements the following new ones.
769  
770 =head2 connect
771  
772 # Blocking
773 $c->connect;
774 # Non-Blocking
775 $c->connect(sub { ... });
776  
777 Connect and authenticate to MySQL Server.
778  
779 =head2 disconnect
780  
781 $c->disconnect;
782  
783 Disconnect gracefully from server.
784  
785 =head2 ping
786
787 say "ok" if $c->ping;
788  
789 Check if connection is alive.
790  
791 =head2 query
792  
793 # Blocking
794 $c->query('select 1 as `one`');
795 # Non-Blocking
796 $c->query('select 1 as `one`', sub { ... });
797  
798 Send SQL query to server.
799 Results are handled by events.
800  
801 =head1 DEBUGGING
802  
803 Debugging is enabled if environment variable MOJO_MYSQL_DEBUG is set.
804  
805 Packet tracing is enabled if MOJO_MYSQL_DEBUG is 2 or greater.
806  
807 =head1 AUTHOR
808  
809 Svetoslav Naydenov, C<harryl@cpan.org>.
810  
811 =head1 COPYRIGHT AND LICENSE
812  
813 Copyright (C) 2015, Svetoslav Naydenov.
814  
815 This program is free software, you can redistribute it and/or modify it under
816 the terms of the Artistic License version 2.0.
817  
818 =head1 SEE ALSO
819  
820 L<http://dev.mysql.com/doc/internals/en/client-server-protocol.html>,
821  
822 L<Mojo::MySQL5>.
823  
824 =cut
↑ Summary MySQL5/Database.pm »