forked from jhthorsen/mojo-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.pm
518 lines (364 loc) · 14.9 KB
/
mysql.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package Mojo::mysql;
use Mojo::Base 'Mojo::EventEmitter';
use Carp 'croak';
use DBI;
use Mojo::mysql::Database;
use Mojo::mysql::Migrations;
use Mojo::URL;
use Scalar::Util 'weaken';
use SQL::Abstract;
our $VERSION = '1.05';
has abstract => sub { SQL::Abstract->new(quote_char => chr(96), name_sep => '.') };
has auto_migrate => 0;
has database_class => 'Mojo::mysql::Database';
has dsn => 'dbi:mysql:dbname=test';
has max_connections => 5;
has migrations => sub {
my $migrations = Mojo::mysql::Migrations->new(mysql => shift);
weaken $migrations->{mysql};
return $migrations;
};
has options => sub {
{mysql_enable_utf8 => 1, AutoCommit => 1, AutoInactiveDestroy => 1, PrintError => 0, RaiseError => 1};
};
has [qw(password username)] => '';
has pubsub => sub {
require Mojo::mysql::PubSub;
my $pubsub = Mojo::mysql::PubSub->new(mysql => shift);
warn "Use of Mojo::mysql::PubSub is highly EXPERIMENTAL and should be considered an experiment"
unless $ENV{MOJO_PUBSUB_EXPERIMENTAL};
weaken $pubsub->{mysql};
return $pubsub;
};
sub db {
my $self = shift;
# Fork safety
delete @$self{qw(pid queue)} unless ($self->{pid} //= $$) eq $$;
my ($dbh, $handle) = @{$self->_dequeue};
return $self->database_class->new(dbh => $dbh, handle => $handle, mysql => $self);
}
sub from_string {
my ($self, $str) = @_;
# Protocol
return $self unless $str;
my $url = Mojo::URL->new($str);
croak qq{Invalid MySQL connection string "$str"} unless $url->protocol eq 'mysql';
# Database
my $dsn = 'dbi:mysql:dbname=' . $url->path->parts->[0];
# Host and port
if (my $host = $url->host) { $dsn .= ";host=$host" }
if (my $port = $url->port) { $dsn .= ";port=$port" }
# Username and password
if (($url->userinfo // '') =~ /^([^:]+)(?::([^:]+))?$/) {
$self->username($1);
$self->password($2) if defined $2;
}
# Options
my $hash = $url->query->to_hash;
@{$self->options}{keys %$hash} = values %$hash;
return $self->dsn($dsn);
}
sub new { @_ == 2 ? shift->SUPER::new->from_string(@_) : shift->SUPER::new(@_) }
sub strict_mode {
my $self = ref $_[0] ? shift : shift->new(@_);
$self->{strict_mode} = $_[0] ? 1 : @_ ? 0 : 1;
warn "[Mojo::mysql] strict_mode($self->{strict_mode})\n" if $ENV{DBI_TRACE};
delete @$self{qw(pid queue)};
return $self;
}
sub _dequeue {
my $self = shift;
my $dbh;
while (my $c = shift @{$self->{queue} || []}) { return $c if $c->[0]->ping }
$dbh = DBI->connect(map { $self->$_ } qw(dsn username password options));
# <mst> batman's probably going to have more "fun" than you have ...
# especially once he discovers that DBD::mysql randomly reconnects under
# you, silently, but only if certain env vars are set
# hint: force-set mysql_auto_reconnect or whatever it's called to 0
$dbh->{mysql_auto_reconnect} = 0;
# Maintain Commits with Mojo::mysql::Transaction
$dbh->{AutoCommit} = 1;
$self->_set_strict_mode($dbh) if $self->{strict_mode};
$self->migrations->migrate if $self->auto_migrate and !$self->{migrated}++;
$self->emit(connection => $dbh);
[$dbh];
}
sub _enqueue {
my ($self, $dbh, $handle) = @_;
my $queue = $self->{queue} ||= [];
push @$queue, [$dbh, $handle] if $dbh->{Active};
shift @{$self->{queue}} while @{$self->{queue}} > $self->max_connections;
}
sub _set_strict_mode {
$_[1]->do(q[SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)]);
$_[1]->do(q[SET SQL_AUTO_IS_NULL = 0]);
}
1;
=encoding utf8
=head1 NAME
Mojo::mysql - Mojolicious and Async MySQL
=head1 SYNOPSIS
use Mojo::mysql;
# Create a table
my $mysql = Mojo::mysql->strict_mode('mysql://username@/test');
$mysql->db->query(
'create table names (id integer auto_increment primary key, name text)');
# Insert a few rows
my $db = $mysql->db;
$db->query('insert into names (name) values (?)', 'Sara');
$db->query('insert into names (name) values (?)', 'Stefan');
# Insert more rows in a transaction
eval {
my $tx = $db->begin;
$db->query('insert into names (name) values (?)', 'Baerbel');
$db->query('insert into names (name) values (?)', 'Wolfgang');
$tx->commit;
};
say $@ if $@;
# Insert another row and return the generated id
say $db->query('insert into names (name) values (?)', 'Daniel')
->last_insert_id;
# Use SQL::Abstract to generate queries for you
$db->insert('names', {name => 'Isabel'});
say $db->select('names', undef, {name => 'Isabel'})->hash->{id};
$db->update('names', {name => 'Bel'}, {name => 'Isabel'});
$db->delete('names', {name => 'Bel'});
# Select one row at a time
my $results = $db->query('select * from names');
while (my $next = $results->hash) {
say $next->{name};
}
# Select all rows blocking
$db->query('select * from names')
->hashes->map(sub { $_->{name} })->join("\n")->say;
# Select all rows non-blocking
Mojo::IOLoop->delay(
sub {
my $delay = shift;
$db->query('select * from names' => $delay->begin);
},
sub {
my ($delay, $err, $results) = @_;
$results->hashes->map(sub { $_->{name} })->join("\n")->say;
}
)->wait;
# Concurrent non-blocking queries (synchronized with promises)
my $now = $db->query_p('select now() as now');
my $names = $db->query_p('select * from names');
Mojo::Promise->all($now, $names)->then(sub {
my ($now, $names) = @_;
say $now->[0]->hash->{now};
say $_->{name} for $names->[0]->hashes->each;
})->catch(sub {
my $err = shift;
warn "Something went wrong: $err";
})->wait;
# Send and receive notifications non-blocking
$mysql->pubsub->listen(foo => sub {
my ($pubsub, $payload) = @_;
say "foo: $payload";
$pubsub->notify(bar => $payload);
});
$mysql->pubsub->listen(bar => sub {
my ($pubsub, $payload) = @_;
say "bar: $payload";
});
$mysql->pubsub->notify(foo => 'MySQL rocks!');
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head1 DESCRIPTION
L<Mojo::mysql> is a tiny wrapper around L<DBD::mysql> that makes
L<MySQL|http://www.mysql.org> a lot of fun to use with the
L<Mojolicious|http://mojolicio.us> real-time web framework.
Database and handles are cached automatically, so they can be reused
transparently to increase performance. And you can handle connection timeouts
gracefully by holding on to them only for short amounts of time.
use Mojolicious::Lite;
use Mojo::mysql;
helper mysql =>
sub { state $mysql = Mojo::mysql->strict_mode('mysql://sri:s3cret@localhost/db') };
get '/' => sub {
my $c = shift;
my $db = $c->mysql->db;
$c->render(json => $db->query('select now() as time')->hash);
};
app->start;
While all I/O operations are performed blocking, you can wait for long running
queries asynchronously, allowing the L<Mojo::IOLoop> event loop to perform
other tasks in the meantime. Since database connections usually have a very low
latency, this often results in very good performance.
Every database connection can only handle one active query at a time, this
includes asynchronous ones. So if you start more than one, they will be put on
a waiting list and performed sequentially. To perform multiple queries
concurrently, you have to use multiple connections.
# Performed sequentially (10 seconds)
my $db = $mysql->db;
$db->query('select sleep(5)' => sub {...});
$db->query('select sleep(5)' => sub {...});
# Performed concurrently (5 seconds)
$mysql->db->query('select sleep(5)' => sub {...});
$mysql->db->query('select sleep(5)' => sub {...});
All cached database handles will be reset automatically if a new process has
been forked, this allows multiple processes to share the same L<Mojo::mysql>
object safely.
=head1 EVENTS
L<Mojo::mysql> inherits all events from L<Mojo::EventEmitter> and can emit the
following new ones.
=head2 connection
$mysql->on(connection => sub {
my ($mysql, $dbh) = @_;
...
});
Emitted when a new database connection has been established.
=head1 ATTRIBUTES
L<Mojo::mysql> implements the following attributes.
=head2 abstract
$abstract = $mysql->abstract;
$mysql = $mysql->abstract(SQL::Abstract->new);
L<SQL::Abstract> object used to generate CRUD queries for L<Mojo::mysql::Database>.
# Generate statements and bind values
my ($stmt, @bind) = $mysql->abstract->select('names');
=head2 auto_migrate
my $bool = $mysql->auto_migrate;
$mysql = $mysql->auto_migrate($bool);
Automatically migrate to the latest database schema with L</"migrations">, as
soon as the first database connection has been established.
Defaults to false.
=head2 database_class
$class = $mysql->database_class;
$mysql = $mysql->database_class("MyApp::Database");
Class to be used by L</"db">, defaults to L<Mojo::mysql::Database>. Note that this
class needs to have already been loaded before L</"db"> is called.
=head2 dsn
my $dsn = $mysql->dsn;
$mysql = $mysql->dsn('dbi:mysql:dbname=foo');
Data Source Name, defaults to C<dbi:mysql:dbname=test>.
=head2 max_connections
my $max = $mysql->max_connections;
$mysql = $mysql->max_connections(3);
Maximum number of idle database handles to cache for future use, defaults to
C<5>.
=head2 migrations
my $migrations = $mysql->migrations;
$mysql = $mysql->migrations(Mojo::mysql::Migrations->new);
L<Mojo::mysql::Migrations> object you can use to change your database schema more
easily.
# Load migrations from file and migrate to latest version
$mysql->migrations->from_file('/Users/sri/migrations.sql')->migrate;
MySQL does not support nested transactions and DDL transactions.
DDL statements cause implicit C<COMMIT>. C<ROLLBACK> will be called if
any step of migration script fails, but only DML statements after the
last implicit or explicit C<COMMIT> can be reverted.
Not all MySQL storage engines (like C<MYISAM>) support transactions.
This means database will most likely be left in unknown state if migration script fails.
Use this feature with caution and remember to always backup your database.
=head2 options
my $options = $mysql->options;
$mysql = $mysql->options({mysql_use_result => 1});
Options for database handles, defaults to activating C<mysql_enable_utf8>, C<AutoCommit>,
C<AutoInactiveDestroy> as well as C<RaiseError> and deactivating C<PrintError>.
Note that C<AutoCommit> and C<RaiseError> are considered mandatory, so
deactivating them would be very dangerous.
C<mysql_auto_reconnect> is never enabled, L<Mojo::mysql> takes care of dead connections.
C<AutoCommit> cannot not be disabled, use $db->L<begin|Mojo::mysql::Database/"begin"> to manage transactions.
C<RaiseError> is enabled for blocking and disabled in event loop for non-blocking queries.
Note about C<mysql_enable_utf8>:
The mysql_enable_utf8 sets the utf8 charset which only supports up to 3-byte
UTF-8 encodings. mysql_enable_utf8mb4 (as of DBD::mysql 4.032) properly
supports encoding unicode characters to up to 4 bytes, such as 𠜎. It means the
connection charset will be utf8mb4 (supported back to at least mysql 5.5) and
these unicode characters will be supported, but no other changes.
See also L<https://github.com/jhthorsen/mojo-mysql/pull/32>
=head2 password
my $password = $mysql->password;
$mysql = $mysql->password('s3cret');
Database password, defaults to an empty string.
=head2 pubsub
my $pubsub = $mysql->pubsub;
$mysql = $mysql->pubsub(Mojo::mysql::PubSub->new);
L<Mojo::mysql::PubSub> object you can use to send and receive notifications very
efficiently, by sharing a single database connection with many consumers.
# Subscribe to a channel
$mysql->pubsub->listen(news => sub {
my ($pubsub, $payload) = @_;
say "Received: $payload";
});
# Notify a channel
$mysql->pubsub->notify(news => 'MySQL rocks!');
Note that L<Mojo::mysql::PubSub> should be considered an experiment!
=head2 username
my $username = $mysql->username;
$mysql = $mysql->username('batman');
Database username, defaults to an empty string.
=head1 METHODS
L<Mojo::mysql> inherits all methods from L<Mojo::EventEmitter> and implements the
following new ones.
=head2 db
my $db = $mysql->db;
Get L<Mojo::mysql::Database> object for a cached or newly created database
handle. The database handle will be automatically cached again when that
object is destroyed, so you can handle connection timeouts gracefully by
holding on to it only for short amounts of time.
=head2 from_string
$mysql = $mysql->from_string('mysql://user@/test');
Parse configuration from connection string.
# Just a database
$mysql->from_string('mysql:///db1');
# Username and database
$mysql->from_string('mysql://batman@/db2');
# Username, password, host and database
$mysql->from_string('mysql://batman:s3cret@localhost/db3');
# Username, domain socket and database
$mysql->from_string('mysql://batman@%2ftmp%2fmysql.sock/db4');
# Username, database and additional options
$mysql->from_string('mysql://batman@/db5?PrintError=1&RaiseError=0');
=head2 new
my $mysql = Mojo::mysql->new;
my $mysql = Mojo::mysql->new(%attrs);
my $mysql = Mojo::mysql->new(\%attrs);
my $mysql = Mojo::mysql->new('mysql://user@/test');
Construct a new L<Mojo::mysql> object either from L</ATTRIBUTES> and or parse
connection string with L</"from_string"> if necessary.
=head2 strict_mode
my $mysql = Mojo::mysql->strict_mode('mysql://user@/test');
my $mysql = $mysql->strict_mode($boolean);
This method can act as both a constructor and a method. When called as a
constructor, it will be the same as:
my $mysql = Mojo::mysql->new('mysql://user@/test')->strict_mode(1);
Enabling strict mode will execute the following statement when a new connection
is created:
SET SQL_MODE = CONCAT('ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY,', @@sql_mode)
SET SQL_AUTO_IS_NULL = 0
The idea is to set up a connection that makes it harder for MySQL to allow
"invalid" data to be inserted.
This method will not be removed, but the internal commands is subject to
change.
=head1 DEBUGGING
You can set the C<DBI_TRACE> environment variable to get some advanced
diagnostics information printed to C<STDERR> by L<DBI>.
DBI_TRACE=1
=head1 REFERENCE
This is the class hierarchy of the L<Mojo::mysql> distribution.
=over 2
=item * L<Mojo::mysql>
=item * L<Mojo::mysql::Database>
=item * L<Mojo::mysql::Migrations>
=item * L<Mojo::mysql::PubSub>
=item * L<Mojo::mysql::Results>
=item * L<Mojo::mysql::Transaction>
=back
=head1 AUTHOR
Curt Hochwender - C<[email protected]>.
Dan Book - C<[email protected]>
Jan Henning Thorsen - C<[email protected]>.
Mike Magowan
This code is mostly a rip-off from Sebastian Riedel's L<Mojo::Pg>.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2014-2015, Jan Henning Thorsen.
This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.
=head1 SEE ALSO
L<https://github.com/jhthorsen/mojo-mysql>,
L<Mojo::Pg> Async Connector for PostgreSQL using L<DBD::Pg>, L<https://github.com/kraih/mojo-pg>,
L<Mojo::MySQL5> Pure-Perl non-blocking I/O MySQL Connector, L<https://github.com/harry-bix/mojo-mysql5>,
L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut