7 use DBI::Const::GetInfoType;
8 use Scalar::Util qw/weaken/;
9 use DBIx::Class::Optional::Dependencies ();
13 use DBIC::SqlMakerTest;
15 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mysql')
16 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mysql');
18 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
20 #warn "$dsn $user $pass";
22 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
23 unless ($dsn && $user);
25 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
27 my $dbh = $schema->storage->dbh;
29 $dbh->do("DROP TABLE IF EXISTS artist;");
31 $dbh->do("CREATE TABLE artist (artistid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), rank INTEGER NOT NULL DEFAULT '13', charfield CHAR(10));");
33 $dbh->do("DROP TABLE IF EXISTS cd;");
35 $dbh->do("CREATE TABLE cd (cdid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, artist INTEGER, title TEXT, year DATE, genreid INTEGER, single_track INTEGER);");
37 $dbh->do("DROP TABLE IF EXISTS producer;");
39 $dbh->do("CREATE TABLE producer (producerid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT);");
41 $dbh->do("DROP TABLE IF EXISTS cd_to_producer;");
43 $dbh->do("CREATE TABLE cd_to_producer (cd INTEGER,producer INTEGER);");
45 $dbh->do("DROP TABLE IF EXISTS owners;");
47 $dbh->do("CREATE TABLE owners (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL);");
49 $dbh->do("DROP TABLE IF EXISTS books;");
51 $dbh->do("CREATE TABLE books (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, source VARCHAR(100) NOT NULL, owner integer NOT NULL, title varchar(100) NOT NULL, price integer);");
53 #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', '');
55 # make sure sqlt_type overrides work (::Storage::DBI::mysql does this)
57 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
59 ok (!$schema->storage->_dbh, 'definitely not connected');
60 is ($schema->storage->sqlt_type, 'MySQL', 'sqlt_type correct pre-connection');
63 # This is in Core now, but it's here just to test that it doesn't break
64 $schema->class('Artist')->load_components('PK::Auto');
66 # test primary key handling
67 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
68 ok($new->artistid, "Auto-PK worked");
72 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
74 my $it = $schema->resultset('Artist')->search( {},
77 order_by => 'artistid' }
79 is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists
80 is( $it->next->name, "Artist 2", "iterator->next ok" );
83 is( $it->next, undef, "next past end of resultset ok" );
85 # Limit with select-lock
87 $schema->txn_do (sub {
89 $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
90 'DBICTest::Schema::Artist',
93 } 'Limited FOR UPDATE select works';
97 $schema->txn_do (sub {
99 $schema->resultset('Artist')->find({artistid => 1}, {for => 'shared'}),
100 'DBICTest::Schema::Artist',
103 } 'LOCK IN SHARE MODE select works';
105 my $test_type_info = {
107 'data_type' => 'INT',
110 'default_value' => undef,
113 'data_type' => 'VARCHAR',
116 'default_value' => undef,
119 'data_type' => 'INT',
122 'default_value' => 13,
125 'data_type' => 'CHAR',
128 'default_value' => undef,
132 $schema->populate ('Owners', [
139 $schema->populate ('BooksInLibrary', [
140 [qw/source owner title /],
141 [qw/Library 1 secrets1/],
142 [qw/Eatery 1 secrets2/],
143 [qw/Library 2 secrets3/],
147 # try a distinct + prefetch on tables with identically named columns
148 # (mysql doesn't seem to like subqueries with equally named columns)
152 # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed)
153 my $owners = $schema->resultset ('Owners')->search (
154 { 'books.id' => { '!=', undef }},
155 { prefetch => 'books', distinct => 1 }
157 my $owners2 = $schema->resultset ('Owners')->search ({ id => { -in => $owners->get_column ('me.id')->as_query }});
158 for ($owners, $owners2) {
159 is ($_->all, 2, 'Prefetched grouped search returns correct number of rows');
160 is ($_->count, 2, 'Prefetched grouped search returns correct count');
163 # try a ->belongs_to direction (no select collapse)
164 my $books = $schema->resultset ('BooksInLibrary')->search (
165 { 'owner.name' => 'wiggle' },
166 { prefetch => 'owner', distinct => 1 }
168 my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }});
169 for ($books, $books2) {
170 is ($_->all, 1, 'Prefetched grouped search returns correct number of rows');
171 is ($_->count, 1, 'Prefetched grouped search returns correct count');
176 my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
177 skip "Cannot determine MySQL server version", 1 if !$mysql_version;
179 my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/;
180 skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2);
184 if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) {
185 $test_type_info->{charfield}->{data_type} = 'VARCHAR';
188 my $type_info = $schema->storage->columns_info_for('artist');
189 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
192 my $cd = $schema->resultset ('CD')->create ({});
193 my $producer = $schema->resultset ('Producer')->create ({});
194 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
197 my $artist = $schema->resultset('Artist')->next;
198 my $cd = $schema->resultset('CD')->next;
199 $cd->set_from_related ('artist', $artist);
202 my $rs = $schema->resultset('CD')->search ({}, { prefetch => 'artist' });
206 is ($cd->artist->name, $artist->name, 'Prefetched artist');
207 }, 'join does not throw (mysql 3 test)';
209 # induce a jointype override, make sure it works even if we don't have mysql3
210 local $schema->storage->sql_maker->{_default_jointype} = 'inner';
214 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
215 artist.artistid, artist.name, artist.rank, artist.charfield
217 INNER JOIN artist artist ON artist.artistid = me.artist
220 'overriden default join type works',
225 # Test support for straight joins
226 my $cdsrc = $schema->source('CD');
227 my $artrel_info = $cdsrc->relationship_info ('artist');
228 $cdsrc->add_relationship(
230 $artrel_info->{class},
231 $artrel_info->{cond},
232 { %{$artrel_info->{attrs}}, join_type => 'straight' },
235 $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
237 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
238 straight_artist.artistid, straight_artist.name, straight_artist.rank, straight_artist.charfield
240 STRAIGHT_JOIN artist straight_artist ON straight_artist.artistid = me.artist
243 'straight joins correctly supported for mysql'
247 ## Can we properly deal with the null search problem?
249 ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect
250 ## But I'm not sure if we should do this or not (Ash, 2008/06/03)
252 # There is now a built-in function to do this, test that everything works
253 # with it (ribasushi, 2009/07/03)
256 my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass, { on_connect_call => 'set_strict_mode' });
258 $ansi_schema->resultset('Artist')->create ({ name => 'last created artist' });
260 ok my $artist1_rs = $ansi_schema->resultset('Artist')->search({artistid=>6666})
261 => 'Created an artist resultset of 6666';
263 is $artist1_rs->count, 0
264 => 'Got no returned rows';
266 ok my $artist2_rs = $ansi_schema->resultset('Artist')->search({artistid=>undef})
267 => 'Created an artist resultset of undef';
269 is $artist2_rs->count, 0
272 my $artist = $artist2_rs->single;
278 # check for proper grouped counts
280 my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass, {
281 on_connect_call => 'set_strict_mode',
284 my $rs = $ansi_schema->resultset('CD');
287 $years->{$_->year|| scalar keys %$years}++ for $rs->all; # NULL != NULL, thus the keys eval
291 $rs->search ({}, { group_by => 'year'})->count,
293 'grouped count correct',
295 }, 'Grouped count does not throw');
298 $ansi_schema->resultset('Owners')->search({}, {
299 join => 'books', group_by => [ 'me.id', 'books.id' ]
301 }, 'count on grouped columns with the same name does not throw');
313 my $rs = $schema->resultset ('CD');
315 for my $y (keys %$cds_per_year) {
316 for my $c (1 .. $cds_per_year->{$y} ) {
317 $rs->create ({ title => "CD $y-$c", artist => 1, year => "$y-01-01" });
321 is ($rs->count, 6, 'CDs created successfully');
323 $rs = $rs->search ({}, {
324 select => [ \ 'YEAR(year)' ], as => ['y'], distinct => 1,
328 [ sort ($rs->get_column ('y')->all) ],
329 [ sort keys %$cds_per_year ],
330 'Years group successfully',
333 $rs->create ({ artist => 1, year => '0-1-1', title => 'Jesus Rap' });
336 [ sort $rs->get_column ('y')->all ],
337 [ 0, sort keys %$cds_per_year ],
338 'Zero-year groups successfully',
341 # convoluted search taken verbatim from list
342 my $restrict_rs = $rs->search({ -and => [
344 year => { '!=', undef }
348 [ $restrict_rs->get_column('y')->all ],
349 [ $rs->get_column ('y')->all ],
350 'Zero year was correctly excluded from resultset',
354 # make sure find hooks determine driver
356 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
357 $schema->resultset("Artist")->find(4);
358 isa_ok($schema->storage->sql_maker, 'DBIx::Class::SQLMaker::MySQL');
361 # make sure the mysql_auto_reconnect buggery is avoided
363 local $ENV{MOD_PERL} = 'boogiewoogie';
364 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
365 ok (! $schema->storage->_get_dbh->{mysql_auto_reconnect}, 'mysql_auto_reconnect unset regardless of ENV' );
367 # Make sure hardcore forking action still works even if mysql_auto_reconnect
368 # is true (test inspired by ether)
370 my $schema_autorecon = DBICTest::Schema->connect($dsn, $user, $pass, { mysql_auto_reconnect => 1 });
371 my $orig_dbh = $schema_autorecon->storage->_get_dbh;
374 ok ($orig_dbh, 'Got weak $dbh ref');
375 ok ($orig_dbh->{mysql_auto_reconnect}, 'mysql_auto_reconnect is properly set if explicitly requested' );
377 my $rs = $schema_autorecon->resultset('Artist');
380 if (! defined $pid ) {
381 die "fork() failed: $!"
385 $schema_autorecon->storage->dbh_do(sub {
386 is ($_[1], $orig_dbh, 'Storage holds correct $dbh in parent');
390 $schema_autorecon->storage->_dbh(undef);
393 local $TODO = "Perl $] is known to leak like a sieve"
394 if DBIx::Class::_ENV_::PEEPEENESS();
396 ok (! defined $orig_dbh, 'Parent $dbh handle is gone');
400 # wait for parent to kill its $dbh
403 #simulate a subtest to not confuse the parent TAP emission
404 Test::More->builder->reset;
405 Test::More->builder->plan('no_plan');
406 Test::More->builder->_indent(' ' x 4);
408 # try to do something dbic-esque
409 $rs->create({ name => "Hardcore Forker $$" });
413 local $TODO = "Perl $] is known to leak like a sieve"
414 if DBIx::Class::_ENV_::PEEPEENESS();
416 ok (! defined $orig_dbh, 'DBIC operation triggered reconnect - old $dbh is gone');
423 ok(!$?, 'Child subtests passed');
425 ok ($rs->find({ name => "Hardcore Forker $pid" }), 'Expected row created');