forked from jhthorsen/mojo-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.pm
235 lines (160 loc) · 6.11 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
package Mojo::mysql;
use Mojo::Base -base;
use Carp 'croak';
use DBI;
use Mojo::mysql::Database;
use Mojo::URL;
has dsn => 'dbi:mysql:dbname=test';
has max_connections => 5;
has options => sub { {AutoCommit => 1, PrintError => 0, RaiseError => 1} };
has [qw(password username)] => '';
our $VERSION = '0.03';
sub db {
my $self = shift;
# Fork safety
delete @$self{qw(pid queue)} unless ($self->{pid} //= $$) eq $$;
return Mojo::mysql::Database->new(dbh => $self->_dequeue, 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 { @_ > 1 ? shift->SUPER::new->from_string(@_) : shift->SUPER::new }
sub _dequeue {
my $self = shift;
my $dbh;
while ($dbh = shift @{$self->{queue} || []}) { return $dbh if $dbh->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;
$dbh;
}
sub _enqueue {
my ($self, $dbh) = @_;
push @{$self->{queue}}, $dbh if $dbh->{Active};
shift @{$self->{queue}} while @{$self->{queue}} > $self->max_connections;
}
1;
=encoding utf8
=head1 NAME
Mojo::mysql - Mojolicious and Async MySQL
=head1 SYNOPSIS
use Mojo::mysql;
# Create a table
my $mysql = Mojo::mysql->new('mysql://username@/test');
$mysql->db->do('create table if not exists names (name varchar(255))');
# Insert a few rows
my $db = $mysql->db;
$db->query('insert into names values (?)', 'Sara');
$db->query('insert into names values (?)', 'Daniel');
# Select all rows blocking
say for $db->query('select * from names')
->hashes->map(sub { $_->{name} })->each;
# Select all rows non-blocking
Mojo::IOLoop->delay(
sub {
my $delay = shift;
$db->query('select * from names' => $delay->begin);
},
sub {
my ($delay, $err, $results) = @_;
say for $results->hashes->map(sub { $_->{name} })->each;
}
)->wait;
=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 statement handles are cached automatically, so they can be reused
transparently to increase performance. 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.
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.
Note that this whole distribution is EXPERIMENTAL and will change without
warning!
=head1 ATTRIBUTES
L<Mojo::mysql> implements the following attributes.
=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 options
my $options = $mysql->options;
$mysql = $mysql->options({AutoCommit => 1});
Options for database handles, defaults to activating C<AutoCommit> as well as
C<RaiseError> and deactivating C<PrintError>.
=head2 password
my $password = $mysql->password;
$mysql = $mysql->password('s3cret');
Database password, defaults to an empty string.
=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::Base> 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('mysql://user@/test');
Construct a new L<Mojo::mysql> object and parse connection string with
L</"from_string"> if necessary.
=head1 AUTHOR
Jan Henning Thorsen, C<[email protected]>.
This code is mostly a rip-off from Sebastian Riedel's L<Mojo::Pg>.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2014, 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<Mojo::Pg>, L<https://github.com/jhthorsen/mojo-mysql>,
L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut