| 1 | package Mojo::MySQL5; | ||
| 2 | 6 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 6 | ▶ | use Carp 'croak'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 6 | ▶ | use Mojo::MySQL5::Migrations; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Mojo::MySQL5::URL; | ||
| 7 | use Mojo::MySQL5::Database; | ||
| 8 | use Scalar::Util 'weaken'; | ||
| 9 | |||
| 10 | has max_connections => 5; | ||
| 11 | has migrations => sub { | ||
| 12 | my $migrations = Mojo::MySQL5::Migrations->new(mysql => shift); | ||
| 13 | weaken $migrations->{mysql}; | ||
| 14 | return $migrations; | ||
| 15 | }; | ||
| 16 | has url => sub { Mojo::MySQL5::URL->new('mysql:///test') }; | ||
| 17 | |||
| 18 | our $VERSION = '0.07'; | ||
| 19 | |||
| 20 | sub db { | ||
| 21 | my $self = shift; | ||
| 22 | |||
| 23 | # Fork safety | ||
| 24 | delete @$self{qw(pid queue)} unless ($self->{pid} //= $$) eq $$; | ||
| 25 | |||
| 26 | my $c = $self->_dequeue; | ||
| 27 | my $db = Mojo::MySQL5::Database->new(connection => $c, mysql => $self); | ||
| 28 | |||
| 29 | if (!$c) { | ||
| 30 | $db->connect; | ||
| 31 | croak 'connect failed' unless $db->connection->state eq 'idle'; | ||
| 32 | $self->emit(connection => $db); | ||
| 33 | } | ||
| 34 | return $db; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub from_string { | ||
| 38 | my ($self, $str) = @_; | ||
| 39 | my $url = Mojo::MySQL5::URL->new($str); | ||
| 40 | croak qq{Invalid MySQL connection string "$str"} | ||
| 41 | unless $url->protocol eq 'mysql' or $url->protocol eq 'mysql5'; | ||
| 42 | |||
| 43 | $url->options->{utf8} = 1 unless exists $url->options->{utf8}; | ||
| 44 | $url->options->{found_rows} = 1 unless exists $url->options->{found_rows}; | ||
| 45 | |||
| 46 | return $self->url($url); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub new { @_ == 2 ? shift->SUPER::new->from_string(@_) : shift->SUPER::new(@_) } | ||
| 50 | |||
| 51 | sub _dequeue { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | while (my $c = shift @{$self->{queue} || []}) { return $c if $c->ping } | ||
| 55 | return undef; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _enqueue { | ||
| 59 | my ($self, $c) = @_; | ||
| 60 | my $queue = $self->{queue} ||= []; | ||
| 61 | push @$queue, $c; | ||
| 62 | shift @{$self->{queue}} while @{$self->{queue}} > $self->max_connections; | ||
| 63 | } | ||
| 64 | |||
| 65 | # deprecated attributes | ||
| 66 | |||
| 67 | sub password { | ||
| 68 | my $self = shift; | ||
| 69 | return $self->url->password unless @_; | ||
| 70 | $self->url->password(@_); | ||
| 71 | return $self; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub username { | ||
| 75 | my $self = shift; | ||
| 76 | return $self->url->username unless @_; | ||
| 77 | $self->url->username(@_); | ||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub options { | ||
| 82 | my $self = shift; | ||
| 83 | return $self->url->options unless @_; | ||
| 84 | $self->url->options(@_); | ||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | =encoding utf8 | ||
| 91 | |||
| 92 | =head1 NAME | ||
| 93 | |||
| 94 | Mojo::MySQL5 - Pure-Perl non-blocking I/O MySQL Connector | ||
| 95 | |||
| 96 | =head1 SYNOPSIS | ||
| 97 | |||
| 98 | use Mojo::MySQL5; | ||
| 99 | |||
| 100 | # Create a table | ||
| 101 | my $mysql = Mojo::MySQL5->new('mysql://username@/test'); | ||
| 102 | $mysql->db->query( | ||
| 103 | 'create table names (id integer auto_increment primary key, name text)'); | ||
| 104 | |||
| 105 | # Insert a few rows | ||
| 106 | my $db = $mysql->db; | ||
| 107 | $db->query('insert into names (name) values (?)', 'Sara'); | ||
| 108 | $db->query('insert into names (name) values (?)', 'Stefan'); | ||
| 109 | |||
| 110 | # Insert more rows in a transaction | ||
| 111 | { | ||
| 112 | my $tx = $db->begin; | ||
| 113 | $db->query('insert into names (name) values (?)', 'Baerbel'); | ||
| 114 | $db->query('insert into names (name) values (?)', 'Wolfgang'); | ||
| 115 | $tx->commit; | ||
| 116 | }; | ||
| 117 | |||
| 118 | # Insert another row and return the generated id | ||
| 119 | say $db->query('insert into names (name) values (?)', 'Daniel') | ||
| 120 | ->last_insert_id; | ||
| 121 | |||
| 122 | # Select one row at a time | ||
| 123 | my $results = $db->query('select * from names'); | ||
| 124 | while (my $next = $results->hash) { | ||
| 125 | say $next->{name}; | ||
| 126 | } | ||
| 127 | |||
| 128 | # Select all rows blocking | ||
| 129 | $db->query('select * from names') | ||
| 130 | ->hashes->map(sub { $_->{name} })->join("\n")->say; | ||
| 131 | |||
| 132 | # Select all rows non-blocking | ||
| 133 | Mojo::IOLoop->delay( | ||
| 134 | sub { | ||
| 135 | my $delay = shift; | ||
| 136 | $db->query('select * from names' => $delay->begin); | ||
| 137 | }, | ||
| 138 | sub { | ||
| 139 | my ($delay, $err, $results) = @_; | ||
| 140 | $results->hashes->map(sub { $_->{name} })->join("\n")->say; | ||
| 141 | } | ||
| 142 | )->wait; | ||
| 143 | |||
| 144 | =head1 DESCRIPTION | ||
| 145 | |||
| 146 | L<Mojo::MySQL5> makes L<MySQL|http://www.mysql.org> a lot of fun to use with the | ||
| 147 | L<Mojolicious|http://mojolicio.us> real-time web framework. | ||
| 148 | |||
| 149 | Database handles are cached automatically, so they can be reused transparently | ||
| 150 | to increase performance. And you can handle connection timeouts gracefully by | ||
| 151 | holding on to them only for short amounts of time. | ||
| 152 | |||
| 153 | use Mojolicious::Lite; | ||
| 154 | use Mojo::MySQL5; | ||
| 155 | |||
| 156 | helper mysql => | ||
| 157 | sub { state $mysql = Mojo::MySQL5->new('mysql://sri:s3cret@localhost/db') }; | ||
| 158 | |||
| 159 | get '/' => sub { | ||
| 160 | my $c = shift; | ||
| 161 | my $db = $c->mysql->db; | ||
| 162 | $c->render(json => $db->query('select now() as time')->hash); | ||
| 163 | }; | ||
| 164 | |||
| 165 | app->start; | ||
| 166 | |||
| 167 | Every database connection can only handle one active query at a time, this | ||
| 168 | includes asynchronous ones. So if you start more than one, they will be put on | ||
| 169 | a waiting list and performed sequentially. To perform multiple queries | ||
| 170 | concurrently, you have to use multiple connections. | ||
| 171 | |||
| 172 | # Performed sequentially (10 seconds) | ||
| 173 | my $db = $mysql->db; | ||
| 174 | $db->query('select sleep(5)' => sub {...}); | ||
| 175 | $db->query('select sleep(5)' => sub {...}); | ||
| 176 | |||
| 177 | # Performed concurrently (5 seconds) | ||
| 178 | $mysql->db->query('select sleep(5)' => sub {...}); | ||
| 179 | $mysql->db->query('select sleep(5)' => sub {...}); | ||
| 180 | |||
| 181 | All cached database handles will be reset automatically if a new process has | ||
| 182 | been forked, this allows multiple processes to share the same L<Mojo::MySQL5> | ||
| 183 | object safely. | ||
| 184 | |||
| 185 | |||
| 186 | Note that this whole distribution is EXPERIMENTAL and will change without | ||
| 187 | warning! | ||
| 188 | |||
| 189 | =head1 EVENTS | ||
| 190 | |||
| 191 | L<Mojo::MySQL5> inherits all events from L<Mojo::EventEmitter> and can emit the | ||
| 192 | following new ones. | ||
| 193 | |||
| 194 | =head2 connection | ||
| 195 | |||
| 196 | $mysql->on(connection => sub { | ||
| 197 | my ($mysql, $db) = @_; | ||
| 198 | ... | ||
| 199 | }); | ||
| 200 | |||
| 201 | Emitted when a new database connection has been established. | ||
| 202 | |||
| 203 | =head1 ATTRIBUTES | ||
| 204 | |||
| 205 | L<Mojo::MySQL5> implements the following attributes. | ||
| 206 | |||
| 207 | =head2 max_connections | ||
| 208 | |||
| 209 | my $max = $mysql->max_connections; | ||
| 210 | $mysql = $mysql->max_connections(3); | ||
| 211 | |||
| 212 | Maximum number of idle database handles to cache for future use, defaults to | ||
| 213 | C<5>. | ||
| 214 | |||
| 215 | =head2 migrations | ||
| 216 | |||
| 217 | my $migrations = $mysql->migrations; | ||
| 218 | $mysql = $mysql->migrations(Mojo::MySQL5::Migrations->new); | ||
| 219 | |||
| 220 | L<Mojo::MySQL5::Migrations> object you can use to change your database schema more | ||
| 221 | easily. | ||
| 222 | |||
| 223 | # Load migrations from file and migrate to latest version | ||
| 224 | $mysql->migrations->from_file('/home/sri/migrations.sql')->migrate; | ||
| 225 | |||
| 226 | MySQL does not support nested transactions and DDL transactions. | ||
| 227 | DDL statements cause implicit C<COMMIT>. C<ROLLBACK> will be called if | ||
| 228 | any step of migration script fails, but only DML statements after the | ||
| 229 | last implicit or explicit C<COMMIT> can be reverted. | ||
| 230 | Not all MySQL storage engines (like C<MYISAM>) support transactions. | ||
| 231 | |||
| 232 | This means database will most likely be left in unknown state if migration script fails. | ||
| 233 | Use this feature with caution and remember to always backup your database. | ||
| 234 | |||
| 235 | =head2 url | ||
| 236 | |||
| 237 | my $url = $mysql->url; | ||
| 238 | $url = $mysql->url( | ||
| 239 | Mojo::MySQL5::URL->new('mysql://user@host/test?connect_timeout=0')); | ||
| 240 | |||
| 241 | Connection L<URL|Mojo::MySQL5::URL>. | ||
| 242 | |||
| 243 | =head2 options | ||
| 244 | |||
| 245 | Use L<url|/"url">->options. | ||
| 246 | |||
| 247 | See L<Mojo::MySQL5::Connection> for list of supported options. | ||
| 248 | |||
| 249 | =head2 password | ||
| 250 | |||
| 251 | Use L<url|/"url">->password. | ||
| 252 | |||
| 253 | =head2 username | ||
| 254 | |||
| 255 | Use L<url|/"url">->username. | ||
| 256 | |||
| 257 | |||
| 258 | =head1 METHODS | ||
| 259 | |||
| 260 | L<Mojo::MySQL5> inherits all methods from L<Mojo::EventEmitter> and implements the | ||
| 261 | following new ones. | ||
| 262 | |||
| 263 | =head2 db | ||
| 264 | |||
| 265 | my $db = $mysql->db; | ||
| 266 | |||
| 267 | Get L<Mojo::MySQL5::Database> object for a cached or newly created database | ||
| 268 | handle. The database handle will be automatically cached again when that | ||
| 269 | object is destroyed, so you can handle connection timeouts gracefully by | ||
| 270 | holding on to it only for short amounts of time. | ||
| 271 | |||
| 272 | # Add up all the money | ||
| 273 | say $mysql->db->query('select * from accounts') | ||
| 274 | ->hashes->reduce(sub { $a->{money} + $b->{money} }); | ||
| 275 | |||
| 276 | =head2 from_string | ||
| 277 | |||
| 278 | $mysql = $mysql->from_string('mysql://user@/test'); | ||
| 279 | |||
| 280 | Parse configuration from connection string. | ||
| 281 | |||
| 282 | # Just a database | ||
| 283 | $mysql->from_string('mysql:///db1'); | ||
| 284 | |||
| 285 | # Username and database | ||
| 286 | $mysql->from_string('mysql://batman@/db2'); | ||
| 287 | |||
| 288 | # Username, password, host and database | ||
| 289 | $mysql->from_string('mysql://batman:s3cret@localhost/db3'); | ||
| 290 | |||
| 291 | # Username, domain socket and database | ||
| 292 | $mysql->from_string('mysql://batman@%2ftmp%2fmysql.sock/db4'); | ||
| 293 | |||
| 294 | # Username, database and additional options | ||
| 295 | $mysql->from_string('mysql://batman@/db5?PrintError=1'); | ||
| 296 | |||
| 297 | =head2 new | ||
| 298 | |||
| 299 | my $mysql = Mojo::MySQL5->new; | ||
| 300 | my $mysql = Mojo::MySQL5->new('mysql://user:s3cret@host:port/database'); | ||
| 301 | my $mysql = Mojo::MySQL5->new( | ||
| 302 | url => Mojo::MySQL5::URL->new( | ||
| 303 | host => 'localhost', | ||
| 304 | port => 3306, | ||
| 305 | username => 'user', | ||
| 306 | password => 's3cret', | ||
| 307 | options => { utf8 => 1, found_rows => 1 } | ||
| 308 | ) | ||
| 309 | ); | ||
| 310 | |||
| 311 | Construct a new L<Mojo::MySQL5> object and parse connection string with | ||
| 312 | L</"from_string"> if necessary. | ||
| 313 | |||
| 314 | =head1 REFERENCE | ||
| 315 | |||
| 316 | This is the class hierarchy of the L<Mojo::MySQL5> distribution. | ||
| 317 | |||
| 318 | =over 2 | ||
| 319 | |||
| 320 | =item * L<Mojo::MySQL5> | ||
| 321 | |||
| 322 | =item * L<Mojo::MySQL5::Connection> | ||
| 323 | |||
| 324 | =item * L<Mojo::MySQL5::Database> | ||
| 325 | |||
| 326 | =item * L<Mojo::MySQL5::Migrations> | ||
| 327 | |||
| 328 | =item * L<Mojo::MySQL5::Results> | ||
| 329 | |||
| 330 | =item * L<Mojo::MySQL5::Transaction> | ||
| 331 | |||
| 332 | =item * L<Mojo::MySQL5::URL> | ||
| 333 | |||
| 334 | =back | ||
| 335 | |||
| 336 | =head1 AUTHOR | ||
| 337 | |||
| 338 | Jan Henning Thorsen, C<jhthorsen@cpan.org>. | ||
| 339 | |||
| 340 | Svetoslav Naydenov, C<harryl@cpan.org>. | ||
| 341 | |||
| 342 | A lot of code in this module is taken from Sebastian Riedel's L<Mojo::Pg>. | ||
| 343 | |||
| 344 | =head1 COPYRIGHT AND LICENSE | ||
| 345 | |||
| 346 | Copyright (C) 2015, Svetoslav Naydenov. | ||
| 347 | |||
| 348 | This program is free software, you can redistribute it and/or modify it under | ||
| 349 | the terms of the Artistic License version 2.0. | ||
| 350 | |||
| 351 | =head1 SEE ALSO | ||
| 352 | |||
| 353 | L<https://github.com/harryl/mojo-mysql5>, | ||
| 354 | |||
| 355 | L<Mojo::Pg> Async Connector for PostgreSQL using L<DBD::Pg>, L<https://github.com/kraih/mojo-pg>, | ||
| 356 | |||
| 357 | L<Mojo::mysql> Async Connector for MySQL using L<DBD::mysql>, L<https://github.com/jhthorsen/mojo-mysql>, | ||
| 358 | |||
| 359 | L<Mojolicious::Guides>, L<http://mojolicio.us>. | ||
| 360 | |||
| 361 | =cut | ||