Mojo/mysql.pm

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