NAME

Mojo::MySQL5::Connection - TCP connection to MySQL Server

SYNOPSIS

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;

DESCRIPTION

Mojo::MySQL5::Conection is Asyncronous Protocol Implementation for connection to MySQL Server managed by Mojo::IOLoop.

EVENTS

Mojo::MySQL5 inherits all events from Mojo::EventEmitter and can emit the following new ones.

fields

$c->on(fields => sub {
  my ($c, $fields) = @_;
  ...
});

Emitted after posting query and fields definition is received.

result

$c->on(result => sub {
  my ($c, $result) = @_;
  ...
});

Emited when a result row is received.

end

$c->on(end => sub {
  my $c = shift;
  ...
});

Emited when query ended successfully.

error

$c->on(error => sub {
  my ($c, $error) = @_;
  ...
});

Emited when Error is received.

ATTRIBUTES

Mojo::MySQL5::Conection implements the following attributes.

state

my $state = $c->state;
$c->state('disconnected');

Connection State.

Possible States are:

disconnected

Initial state before connecting to server.

Same state after fatal erorr.

connected

Connection to server is established.

Waiting for Initial Handshake packet.

handshake

Server responded with Initial Handshake.

Next send Handshake Response (authentication) packet.

auth

Handshake Response (authentication) packet sent to server.

Next wait for OK or Error packet.

idle

Connection is idle and ready for sending commands.

query

COM_QUERY packet sent to server.

Waiting for COM_QUERY Response packet. OK is expected for non-SELECT queries.

field

Waiting for Column Definition packets. EOF is expected for end of column definition.

result

Waiting for Text Resultset Row packets. EOF is expected for end of result rows.

ping

COM_PING packet is sent to server.

Waitint for OK packet.

url

my $url = $c->url;
$c->url(Mojo::MySQL5::URL->new('mysql://localhost/test');

MySQL Connection URL.

Supported Options are:

found_rows

Enables or disables the flag CLIENT_FOUND_ROWS while connecting to the MySQL server. Without found_rows, if you perform a query like

 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.

multi_statements

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.

utf8

If enabled default character set is to utf8_general_ci while connecting to the server. If disabled binary is the default character set.

connect_timeout

The connect request to the server will timeout if it has not been successful after the given number of seconds.

query_timeout

If enabled, the read or write operation to the server will timeout if it has not been successful after the given number of seconds.

socket

Unix socket that is used for connecting to the server.

Determined by calling mysql_config --socket unless specified.

Unix socket is used if host part of "url" is '' or 'localhost'. Use '127.0.0.1' to connect to local machine via TCP.

METHODS

Mojo::MySQL5::Conection inherits all methods from Mojo::EventEmitter and implements the following new ones.

connect

# Blocking
$c->connect;
# Non-Blocking
$c->connect(sub { ... });

Connect and authenticate to MySQL Server.

disconnect

$c->disconnect;

Disconnect gracefully from server.

ping

say "ok" if $c->ping;

Check if connection is alive.

query

# 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

Debugging is enabled if environment variable MOJO_MYSQL_DEBUG is set.

Packet tracing is enabled if MOJO_MYSQL_DEBUG is 2 or greater.

AUTHOR

Svetoslav Naydenov, harryl@cpan.org.

COPYRIGHT AND LICENSE

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.

SEE ALSO

http://dev.mysql.com/doc/internals/en/client-server-protocol.html,

Mojo::MySQL5.