MojoX::Mysql - Mojolicious ♥ Mysql
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);
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.
$mysql->id(1); # choice id server
$mysql->slave(1); # query only slave server
$mysql->async(1); # query async mode
$mysql->db;
Return MojoX::Mysql::DB object.
my ($insertid,$counter) = $mysql->do('INSERT INTO `names` (`id`,`name`) VALUES(1,?)', 'Lilu Kazerogova');
my ($insertid,$counter) = $mysql->id(1)->do('INSERT INTO `names` (`id`,`name`) VALUES(1,?)', 'Lilu Kazerogova');
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.
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);
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)
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);
$mysql->db->commit; $mysql->db->rollback; $mysql->db->disconnect;
$mysql->util->quote("test'test");
$mysql->util->id;
Return id servers in Mojo::Collection object.
SEE ALSO Mojolicious::Plugin::Mysql
Kostya Ten, kostya@cpan.org.
kostya@cpan.org
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.
To install MojoX::Mysql, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MojoX::Mysql
CPAN shell
perl -MCPAN -e shell install MojoX::Mysql
For more information on module installation, please visit the detailed CPAN module installation guide.