MySQL5/Connection.pm

filter: Statementstmt 55.0% 176 / 320 Branchbran 38.9% 60 / 154 Conditioncond 13.0% 6 / 46 MC/DCmcdc 3.7% 1 / 27 Subroutinesub 55.7% 29 / 52 Podpod 100% 4 / 4 totaltotal 45.7% 276 / 603 CC 103 SCAR 31.2
CC
103
Cov
47%
connect
132.0
_seq_next
72.0
_send_auth
56.0
CRAP 22.7
« MySQL5/PubSub.pm ↑ Summary MySQL5/URL.pm »
1 package Mojo::MySQL5::Connection;
2 8 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Encode qw(_utf8_off _utf8_on);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Digest::SHA qw(sha1);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Mojo::MySQL5::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 has state => 'disconnected';
12  
13 1 ▶has url => sub { Mojo::MySQL5::URL->new('mysql:///') };
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 8 ▶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
08
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 8 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 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 8 };
52  
53 8 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
54 8 REV_CHARSET => { reverse %{CHARSET()} },
55 8 REV_DATATYPE => { map { chr(DATATYPE->{$_}) => $_ } keys %{DATATYPE()} },
56 8 };
57  
58 # state machine
59 # doing => { state => '_op', state => '_op' }
60 8 ▶use constant SEQ => {
Subroutine BEGIN: called CC=1 SCAR=0.0
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 8 };
81  
82  
83 # encode fixed length integer
84 0 ▶sub _encode_int($$) {
Subroutine _encode_int: not called CC=5 SCAR=34.0
85 0 my ($int, $len) = @_;
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 29 ▶sub _encode_lcint($) {
Subroutine _encode_lcint: called CC=5 SCAR=16.1
93 29 my $int = shift;
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 12 ▶sub _encode_lcstr($) {
Subroutine _encode_lcstr: called CC=2 SCAR=6.9
104 12 my $str = shift;
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 109 ▶sub _get_int {
Subroutine _get_int: called CC=7 SCAR=19.5
110 109 my ($self, $len, $chew) = @_;
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 41 ▶sub _chew_lcint {
Subroutine _chew_lcint: called CC=6 SCAR=17.9
125 41 my $self = shift;
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 14 ▶sub _chew_lcstr {
Subroutine _chew_lcstr: called CC=2 SCAR=6.9
137 14 my $self = shift;
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 3 ▶sub _chew_zstr {
Subroutine _chew_zstr: called CC=2 SCAR=7.0
144 3 my $self = shift;
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 8 ▶sub _chew_str {
Subroutine _chew_str: called CC=2 SCAR=7.1
153 8 my ($self, $len) = @_;
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 0 ▶sub _send_auth {
Subroutine _send_auth: not called CC=7 SCAR=40.3
160 0 my $self = shift;
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 0 ▶sub _send_quit {
Subroutine _send_quit: not called CC=1 SCAR=6.9
192 0 my $self = shift;
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 0 ▶sub _send_query {
Subroutine _send_query: not called CC=1 SCAR=6.9
199 0 my $self = shift;
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 0 ▶sub _send_ping {
Subroutine _send_ping: not called CC=1 SCAR=6.9
209 0 my $self = shift;
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 1 ▶sub _recv_error {
Subroutine _recv_error: called CC=2 SCAR=6.9
216 1 my $self = shift;
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 2 ▶sub _recv_ok {
Subroutine _recv_ok: called CC=5 SCAR=16.2
235 2 my $self = shift;
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 1 ▶sub _recv_query_responce {
Subroutine _recv_query_responce: called CC=3 SCAR=11.2
260 1 my $self = shift;
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 2 ▶sub _recv_eof {
Subroutine _recv_eof: called CC=6 SCAR=18.3
274 2 my $self = shift;
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 2 ▶sub _recv_field {
Subroutine _recv_field: called CC=7 SCAR=19.6
306 2 my $self = shift;
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 3 ▶sub _recv_row {
Subroutine _recv_row: called CC=5 SCAR=16.2
341 3 my $self = shift;
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 1 ▶sub _recv_handshake {
Subroutine _recv_handshake: called CC=6 SCAR=18.3
360 1 my $self = shift;
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 1 ▶sub _reset {
Subroutine _reset: called CC=2 SCAR=6.9
400 1 my $self = shift;
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 0 ▶sub _ioloop {
Subroutine _ioloop: not called CC=3 SCAR=24.8
411 0 ▶ $_[1] ? Mojo::IOLoop->singleton : ($_[0]->{ioloop} ||= Mojo::IOLoop->new);
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 0 ▶sub _seq_next_ready {
Subroutine _seq_next_ready: not called CC=2 SCAR=17.9
415 0 my $self = shift;
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 0 ▶sub _seq_next {
Subroutine _seq_next: not called CC=8 SCAR=42.8
421 0 my ($self, $cmd, $writeonly) = @_;
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 0 ▶sub _seq {
Subroutine _seq: not called CC=3 SCAR=24.8
444 0 my ($self, $cmd, $cb) = @_;
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 0 ▶ $self->{stream}->on(read => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
453 0 my ($stream, $bytes) = @_;
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 0 ▶ $self->{stream}->on(error => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
468 0 my ($stream, $err) = @_;
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 0 ▶ $self->{stream}->on(timeout => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
474 0 warn "stream timeout state:", $self->state, "\n" if DEBUG;
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 0 ▶ $self->{stream}->on(close => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
478 0 warn 'stream closed state:', $self->state, "\n" if DEBUG;
479 0 $self->{socket} = undef;
480 0 $self->state('disconnected');
481 0 ▶ $self->{error_message} //= 'disconnected' unless $cmd eq 'disconnect';
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd eq "disconnect" 0 0 0
Condition0%0/2
$self->{"error_message"} //= "disconnected"
l!l
00
MC/DC0%0/1
$self->{"error_message"} //= "disconnected"
$self->{"error_message"}
Truth table0%0/2
A
A= $self->{"error_message"}
Aresult
11
00
482 0 ▶ $cb ? $self->$cb() : $self->_ioloop(0)->stop;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
483 0 });
484  
485 0 $self->{stream}->start;
486 0 $self->_seq_next($cmd, 1);
487 }
488  
489 0 ▶sub _cmd {
Subroutine _cmd: not called CC=5 SCAR=34.0
490 0 my ($self, $cmd, $cb) = @_;
491 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
492 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
493  
494 0 $self->_reset;
495 0 $self->_seq($cmd, $cb);
496 0 ▶ $self->_ioloop(0)->start unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
497 0 ▶ return $self->state eq 'idle' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->state eq 'idle' ? : 0 0 0
498 }
499  
500 0 ▶sub connect {
Subroutine connect: not called CC=11 SCAR=48.8
Pod documented
501 0 my ($self, $cb) = @_;
502  
503 0 $self->state('connecting');
504 0 $self->_reset;
505  
506 0 ▶ if ($self->url->host eq '' or $self->url->host eq 'localhost') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url->host eq '' or $self->url->host eq 'localhost') { } 0 0 0
Condition0%0/3
$self->url->host eq "" or $self->url->host eq "localhost"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->url->host eq "" or $self->url->host eq "localhost"
$self->url->host eq "" $self->url->host eq "localhost"
Truth table0%0/3
A or B
A= $self->url->host eq ""B= $self->url->host eq "localhost"
ABresult
1X1
011
000
507 0 ▶ if (!$self->url->options->{socket}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->url->options->{"socket"}) 0 0 0
508 0 $self->url->options->{socket} = `mysql_config --socket`;
509 0 chomp $self->url->options->{socket};
510 }
511 0 warn "Connecting to UNIX socket '", $self->url->options->{socket}, "'\n" if DEBUG;
512 $self->{socket} = IO::Socket::UNIX->new(
513 Peer => $self->url->options->{socket},
514 0 ▶ Timeout => $self->url->options->{connect_timeout} // 10,
Condition0%0/2
$self->url->options->{"connect_timeout"} // 10
l!l
00
MC/DC0%0/1
$self->url->options->{"connect_timeout"} // 10
$self->url->options->{"connect_timeout"}
Truth table0%0/2
A
A= $self->url->options->{"connect_timeout"}
Aresult
11
00
515 Blocking => 0
516 );
517 0 ▶ return unless $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"socket"} 0 0 0
518 0 $self->state('connected');
519 0 $self->_seq('connect', $cb);
520 }
521 else {
522 0 $self->{client} = Mojo::IOLoop::Client->new;
523 0 ▶ $self->{client}->reactor($self->_ioloop(0)->reactor) unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
524 0 weaken $self;
525  
526 0 ▶ $self->{client}->on(connect => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
527 0 my ($client, $handle) = @_;
528 0 delete $self->{client};
529 0 $self->{socket} = $handle;
530 0 $self->state('connected');
531 0 $self->_seq('connect', $cb);
532 0 });
533 0 ▶ $self->{client}->on(error => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
534 0 my ($client, $err) = @_;
535 0 delete $self->{client};
536 0 $self->state('disconnected');
537 0 $self->emit(error => $err);
538 0 ▶ $cb ? $self->$cb() : $self->_ioloop(0)->stop;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
539 0 });
540  
541 $self->{client}->connect(
542 address => $self->url->host || '127.0.0.1',
543 port => $self->url->port || 3306,
544 0 ▶ timeout => $self->url->options->{connect_timeout} // 10
Condition0%0/6
$self->url->host || '127.0.0.1'
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 || '127.0.0.1'
$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
545 );
546 }
547  
548 0 ▶ $self->_ioloop(0)->start unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
549 }
550  
551 0 ▶sub disconnect { shift->_cmd('disconnect') }
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
552  
553 0 ▶sub ping {
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
554 0 my ($self, $cb) = @_;
555 0 ▶ return $self->state eq 'disconnected' ? 0 : $self->_cmd('ping', $cb);
Branch: 1 branch
BranchTrueFalseTotal
$self->state eq 'disconnected' ? : 0 0 0
556 }
557  
558 0 ▶sub query {
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
559 0 my ($self, $sql, $cb) = @_;
560 0 $self->{sql} = $sql;
561 0 $self->_cmd('query', $cb);
562 }
563  
564 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=13.1
565 1 my $self = shift;
566 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
567 }
568  
569 # Private util functions
570 0 ▶sub _flag_list($$;$) {
Subroutine _flag_list: not called CC=2 SCAR=17.9
571 0 my ($list, $data, $sep) = @_;
572 0 my $i = 0;
573 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
574 }
575  
576 0 ▶sub _flag_set($;@) {
Subroutine _flag_set: not called CC=4 SCAR=30.0
577 0 my ($list, @ops) = @_;
578 0 my ($i, $flags) = (0, 0);
579 0 foreach my $flag (@$list) {
580 0 ▶ do { $flags |= 1 << $i if $_ eq $flag } for @ops;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $flag 0 0 0
581 0 $i++;
582 }
583 0 return $flags;
584 }
585  
586 0 ▶sub _flag_is($$$) {
Subroutine _flag_is: not called CC=3 SCAR=24.8
587 0 my ($list, $data, $flag) = @_;
588 0 my $i = 0;
589 0 foreach (@$list) {
590 0 ▶ return $data & 1 << $i if $flag eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $flag eq $_ 0 0 0
591 0 $i++;
592 }
593 0 return undef;
594 }
595  
596 8 1;
597  
598 =encoding utf8
599  
600 =head1 NAME
601  
602 Mojo::MySQL5::Connection - TCP connection to MySQL Server
603  
604 =head1 SYNOPSIS
605  
606 use Mojo::MySQL5::Conection;
607  
608 my $c = Mojo::MySQL5::Conection->new(
609 url => Mojo::MySQL5->new(
610 'mysql://test:password@127.0.0.1:3306/test?found_rows=1&connect_timeout=2')
611 );
612  
613 Mojo::IOLoop->delay(
614 sub {
615 my $delay = shift;
616 $c->connect($delay->begin);
617 },
618 sub {
619 my ($delay, $c) = @_;
620 $c->query('select * from test_data', $delay->begin);
621 },
622 sub {
623 my ($delay, $c) = @_;
624 }
625 )->wait;
626  
627  
628 =head1 DESCRIPTION
629  
630 L<Mojo::MySQL5::Conection> is Asyncronous Protocol Implementation for connection to MySQL Server
631 managed by L<Mojo::IOLoop>.
632  
633 =head1 EVENTS
634  
635 L<Mojo::MySQL5> inherits all events from L<Mojo::EventEmitter> and can emit the
636 following new ones.
637  
638 =head2 fields
639  
640 $c->on(fields => sub {
641 my ($c, $fields) = @_;
642 ...
643 });
644  
645 Emitted after posting query and fields definition is received.
646  
647 =head2 result
648  
649 $c->on(result => sub {
650 my ($c, $result) = @_;
651 ...
652 });
653  
654 Emited when a result row is received.
655  
656 =head2 end
657  
658 $c->on(end => sub {
659 my $c = shift;
660 ...
661 });
662  
663 Emited when query ended successfully.
664  
665 =head2 error
666  
667 $c->on(error => sub {
668 my ($c, $error) = @_;
669 ...
670 });
671  
672 Emited when Error is received.
673  
674  
675 =head1 ATTRIBUTES
676  
677 L<Mojo::MySQL5::Conection> implements the following attributes.
678  
679 =head2 state
680  
681 my $state = $c->state;
682 $c->state('disconnected');
683  
684 Connection State.
685  
686 Possible States are:
687  
688 =over 2
689  
690 =item disconnected
691  
692 Initial state before connecting to server.
693  
694 Same state after fatal erorr.
695  
696 =item connected
697  
698 Connection to server is established.
699  
700 Waiting for C<Initial Handshake> packet.
701  
702 =item handshake
703  
704 Server responded with C<Initial Handshake>.
705  
706 Next send C<Handshake Response> (authentication) packet.
707  
708 =item auth
709  
710 C<Handshake Response> (authentication) packet sent to server.
711  
712 Next wait for C<OK> or C<Error> packet.
713  
714 =item idle
715  
716 Connection is idle and ready for sending commands.
717  
718 =item query
719  
720 C<COM_QUERY> packet sent to server.
721  
722 Waiting for C<COM_QUERY Response> packet. C<OK> is expected for non-SELECT queries.
723  
724 =item field
725  
726 Waiting for C<Column Definition> packets. C<EOF> is expected for end of column definition.
727  
728 =item result
729  
730 Waiting for C<Text Resultset Row> packets. C<EOF> is expected for end of result rows.
731  
732 =item ping
733  
734 C<COM_PING> packet is sent to server.
735  
736 Waitint for C<OK> packet.
737  
738 =back
739  
740 =head2 url
741  
742 my $url = $c->url;
743 $c->url(Mojo::MySQL5::URL->new('mysql://localhost/test');
744  
745 MySQL Connection URL.
746  
747 Supported Options are:
748  
749 =over 2
750  
751 =item found_rows
752  
753 Enables or disables the flag C<CLIENT_FOUND_ROWS> while connecting to the MySQL server.
754 Without C<found_rows>, if you perform a query like
755
756 UPDATE $table SET id = 1 WHERE id = 1;
757
758 then the MySQL engine will return 0, because no rows have changed.
759 With C<found_rows>, it will return the number of rows that have an id 1.
760  
761 =item multi_statements
762  
763 Enables or disables the flag C<CLIENT_MULTI_STATEMENTS> while connecting to the server.
764 If enabled multiple statements separated by semicolon (;) can be send with single
765 call to L<query>.
766  
767 =item utf8
768  
769 If enabled default character set is to C<utf8_general_ci> while connecting to the server.
770 If disabled C<binary> is the default character set.
771  
772 =item connect_timeout
773  
774 The connect request to the server will timeout if it has not been successful
775 after the given number of seconds.
776  
777 =item query_timeout
778  
779 If enabled, the read or write operation to the server will timeout
780 if it has not been successful after the given number of seconds.
781  
782 =item socket
783  
784 Unix socket that is used for connecting to the server.
785  
786 Determined by calling C<mysql_config --socket> unless specified.
787  
788 Unix socket is used if host part of L</"url"> is C<''> or C<'localhost'>.
789 Use C<'127.0.0.1'> to connect to local machine via TCP.
790  
791 =back
792  
793 =head1 METHODS
794  
795 L<Mojo::MySQL5::Conection> inherits all methods from L<Mojo::EventEmitter> and
796 implements the following new ones.
797  
798 =head2 connect
799  
800 # Blocking
801 $c->connect;
802 # Non-Blocking
803 $c->connect(sub { ... });
804  
805 Connect and authenticate to MySQL Server.
806  
807 =head2 disconnect
808  
809 $c->disconnect;
810  
811 Disconnect gracefully from server.
812  
813 =head2 ping
814
815 say "ok" if $c->ping;
816  
817 Check if connection is alive.
818  
819 =head2 query
820  
821 # Blocking
822 $c->query('select 1 as `one`');
823 # Non-Blocking
824 $c->query('select 1 as `one`', sub { ... });
825  
826 Send SQL query to server.
827 Results are handled by events.
828  
829 =head1 DEBUGGING
830  
831 Debugging is enabled if environment variable MOJO_MYSQL_DEBUG is set.
832  
833 Packet tracing is enabled if MOJO_MYSQL_DEBUG is 2 or greater.
834  
835 =head1 AUTHOR
836  
837 Svetoslav Naydenov, C<harryl@cpan.org>.
838  
839 =head1 COPYRIGHT AND LICENSE
840  
841 Copyright (C) 2015, Svetoslav Naydenov.
842  
843 This program is free software, you can redistribute it and/or modify it under
844 the terms of the Artistic License version 2.0.
845  
846 =head1 SEE ALSO
847  
848 L<http://dev.mysql.com/doc/internals/en/client-server-protocol.html>,
849  
850 L<Mojo::MySQL5>.
851  
852 =cut
« MySQL5/PubSub.pm ↑ Summary MySQL5/URL.pm »