Mojo::MySQL5::Connection - TCP connection to MySQL Server
use Mojo::MySQL5::Conection; my $c = Mojo::MySQL5::Conection->new( url => Mojo::MySQL5->new( 'mysql://test:password@127.0.0.1:3306/test?found_rows=1&connect_timeout=2') ); Mojo::IOLoop->delay( sub { my $delay = shift; $c->connect($delay->begin); }, sub { my ($delay, $c) = @_; $c->query('select * from test_data', $delay->begin); }, sub { my ($delay, $c) = @_; } )->wait;
Mojo::MySQL5::Conection is Asyncronous Protocol Implementation for connection to MySQL Server managed by Mojo::IOLoop.
Mojo::MySQL5 inherits all events from Mojo::EventEmitter and can emit the following new ones.
$c->on(fields => sub { my ($c, $fields) = @_; ... });
Emitted after posting query and fields definition is received.
$c->on(result => sub { my ($c, $result) = @_; ... });
Emited when a result row is received.
$c->on(end => sub { my $c = shift; ... });
Emited when query ended successfully.
$c->on(error => sub { my ($c, $error) = @_; ... });
Emited when Error is received.
Mojo::MySQL5::Conection implements the following attributes.
my $state = $c->state; $c->state('disconnected');
Connection State.
Possible States are:
Initial state before connecting to server.
Same state after fatal erorr.
Connection to server is established.
Waiting for Initial Handshake packet.
Initial Handshake
Server responded with Initial Handshake.
Next send Handshake Response (authentication) packet.
Handshake Response
Handshake Response (authentication) packet sent to server.
Next wait for OK or Error packet.
OK
Error
Connection is idle and ready for sending commands.
COM_QUERY packet sent to server.
COM_QUERY
Waiting for COM_QUERY Response packet. OK is expected for non-SELECT queries.
COM_QUERY Response
Waiting for Column Definition packets. EOF is expected for end of column definition.
Column Definition
EOF
Waiting for Text Resultset Row packets. EOF is expected for end of result rows.
Text Resultset Row
COM_PING packet is sent to server.
COM_PING
Waitint for OK packet.
my $url = $c->url; $c->url(Mojo::MySQL5::URL->new('mysql://localhost/test');
MySQL Connection URL.
Supported Options are:
Enables or disables the flag CLIENT_FOUND_ROWS while connecting to the MySQL server. Without found_rows, if you perform a query like
CLIENT_FOUND_ROWS
found_rows
UPDATE $table SET id = 1 WHERE id = 1;
then the MySQL engine will return 0, because no rows have changed. With found_rows, it will return the number of rows that have an id 1.
Enables or disables the flag CLIENT_MULTI_STATEMENTS while connecting to the server. If enabled multiple statements separated by semicolon (;) can be send with single call to query.
CLIENT_MULTI_STATEMENTS
If enabled default character set is to utf8_general_ci while connecting to the server. If disabled binary is the default character set.
utf8_general_ci
binary
The connect request to the server will timeout if it has not been successful after the given number of seconds.
If enabled, the read or write operation to the server will timeout if it has not been successful after the given number of seconds.
Unix socket that is used for connecting to the server.
Determined by calling mysql_config --socket unless specified.
mysql_config --socket
Unix socket is used if host part of "url" is '' or 'localhost'. Use '127.0.0.1' to connect to local machine via TCP.
''
'localhost'
'127.0.0.1'
Mojo::MySQL5::Conection inherits all methods from Mojo::EventEmitter and implements the following new ones.
# Blocking $c->connect; # Non-Blocking $c->connect(sub { ... });
Connect and authenticate to MySQL Server.
$c->disconnect;
Disconnect gracefully from server.
say "ok" if $c->ping;
Check if connection is alive.
# Blocking $c->query('select 1 as `one`'); # Non-Blocking $c->query('select 1 as `one`', sub { ... });
Send SQL query to server. Results are handled by events.
Debugging is enabled if environment variable MOJO_MYSQL_DEBUG is set.
Packet tracing is enabled if MOJO_MYSQL_DEBUG is 2 or greater.
Svetoslav Naydenov, harryl@cpan.org.
harryl@cpan.org
Copyright (C) 2015, Svetoslav Naydenov.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
http://dev.mysql.com/doc/internals/en/client-server-protocol.html,
Mojo::MySQL5.
To install Mojo::MySQL5, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::MySQL5
CPAN shell
perl -MCPAN -e shell install Mojo::MySQL5
For more information on module installation, please visit the detailed CPAN module installation guide.