NAME

MojoX::Mysql - Mojolicious ♥ Mysql

SYNOPSIS

use MojoX::Mysql;
use Mojo::Util qw(dumper);

my %config = (
    user=>'root',
    password=>undef,
    server=>[
        {dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', type=>'master'},
        {dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', type=>'slave'},
        {dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>1, type=>'master'},
        {dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>1, type=>'slave'},
        {dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>2, type=>'master'},
        {dsn=>'database=test;host=localhost;port=3306;mysql_connect_timeout=5;', id=>2, type=>'slave'},
    ]
);

my $mysql = MojoX::Mysql->new(%config);

DESCRIPTION

MojoX::Mysql is a tiny wrapper around DBD::mysql that makes Mysql a lot of fun to use with the Mojolicious real-time web framework.

ATTRIBUTES

id

$mysql->id(1); # choice id server

slave

$mysql->slave(1); # query only slave server

async

$mysql->async(1); # query async mode

METHODS

db

$mysql->db;

Return MojoX::Mysql::DB object.

do

my ($insertid,$counter) = $mysql->do('INSERT INTO `names` (`id`,`name`) VALUES(1,?)', 'Lilu Kazerogova');

do (choice server)

my ($insertid,$counter) = $mysql->id(1)->do('INSERT INTO `names` (`id`,`name`) VALUES(1,?)', 'Lilu Kazerogova');

query

my $collection_object = $mysql->query('SELECT * FROM `names` WHERE id = ?', 1);

# or

my ($collection,$counter,$sth,$dbh) = $mysql->query('SELECT * FROM `names` WHERE id = ?', 1);

# or callback

$mysql->query('SELECT `text` FROM `test` WHERE `id` = ? LIMIT 1', $insertid, sub {
    my ($self,$data) = @_;
    say dumper $data;
});

Return Mojo::Collection object.

query (choice server)

my $collection_object = $mysql->id(1)->query('SELECT * FROM `names` WHERE id = ?', 1);

# or

my ($collection,$counter,$sth,$dbh) = $mysql->id(1)->query('SELECT * FROM `names` WHERE id = ?', 1);

query (async)

my ($sth1,$dbh1) = $mysql->id(1)->async(1)->query('SELECT SLEEP(?) as `sleep`', 1); # Automatically new connection
my ($sth2,$dbh2) = $mysql->id(1)->async(1)->query('SELECT SLEEP(?) as `sleep`', 1); # Automatically new connection

my $collection_object1 = $mysql->result->async($sth1,$dbh1); # Automatically executed methods finish, commit, disconnect
my $collection_object2 = $mysql->result->async($sth2,$dbh2); # Automatically executed methods finish, commit, disconnect

# Performed concurrently (1 seconds)

Return Mojo::Collection object.

query (slave server)

my $collection_object = $mysql->id(1)->slave(1)->query('SELECT * FROM `names` WHERE id = ?', 1);

# or

my ($collection,$counter,$sth,$dbh) = $mysql->id(1)->slave(1)->query('SELECT * FROM `names` WHERE id = ?', 1);

commit, rollback, disconnect

$mysql->db->commit;
$mysql->db->rollback;
$mysql->db->disconnect;

quote

$mysql->util->quote("test'test");

id

$mysql->util->id;

Return id servers in Mojo::Collection object.

Mojolicious Plugin

SEE ALSO Mojolicious::Plugin::Mysql

AUTHOR

Kostya Ten, kostya@cpan.org.

COPYRIGHT AND LICENSE

Copyright (C) 2014, Kostya Ten.

This program is free software, you can redistribute it and/or modify it under the terms of the Apache License version 2.0.