NAME

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

SYNOPSIS

use Mojo::MySQL5::Conection;

my $c = Mojo::MySQL5::Conection->new(
  url => 'mysql://test:password@127.0.0.1:3306/test',
  options => { 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.

url

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

MySQL Connection URL.

options

my $options = $c->options;
$c->options({ connect_timeout => 5, query_timeout => 30, utf => 1 });

Options for Connection.

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.

Default is 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.

Default is 0.

utf8

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

Default is 1.

connect_timeout

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

Default is 10.

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.

Default is 0 (disabled).

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

Mojo::MySQL5, Mojolicious::Guides, http://mojolicio.us.