8 use DBI::Const::GetInfoType;
9 use DBIC::SqlMakerTest;
11 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
13 #warn "$dsn $user $pass";
15 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
16 unless ($dsn && $user);
18 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
20 my $dbh = $schema->storage->dbh;
22 $dbh->do("DROP TABLE IF EXISTS artist;");
24 $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));");
26 $dbh->do("DROP TABLE IF EXISTS cd;");
28 $dbh->do("CREATE TABLE cd (cdid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, artist INTEGER, title TEXT, year DATE, genreid INTEGER, single_track INTEGER);");
30 $dbh->do("DROP TABLE IF EXISTS producer;");
32 $dbh->do("CREATE TABLE producer (producerid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT);");
34 $dbh->do("DROP TABLE IF EXISTS cd_to_producer;");
36 $dbh->do("CREATE TABLE cd_to_producer (cd INTEGER,producer INTEGER);");
38 $dbh->do("DROP TABLE IF EXISTS owners;");
40 $dbh->do("CREATE TABLE owners (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL);");
42 $dbh->do("DROP TABLE IF EXISTS books;");
44 $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);");
46 #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', '');
48 # make sure sqlt_type overrides work (::Storage::DBI::mysql does this)
50 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
52 ok (!$schema->storage->_dbh, 'definitely not connected');
53 is ($schema->storage->sqlt_type, 'MySQL', 'sqlt_type correct pre-connection');
56 # This is in Core now, but it's here just to test that it doesn't break
57 $schema->class('Artist')->load_components('PK::Auto');
59 # test primary key handling
60 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
61 ok($new->artistid, "Auto-PK worked");
65 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
67 my $it = $schema->resultset('Artist')->search( {},
70 order_by => 'artistid' }
72 is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists
73 is( $it->next->name, "Artist 2", "iterator->next ok" );
76 is( $it->next, undef, "next past end of resultset ok" );
78 # Limit with select-lock
80 $schema->txn_do (sub {
82 $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
83 'DBICTest::Schema::Artist',
86 } 'Limited FOR UPDATE select works';
88 my $test_type_info = {
93 'default_value' => undef,
96 'data_type' => 'VARCHAR',
99 'default_value' => undef,
102 'data_type' => 'INT',
105 'default_value' => 13,
108 'data_type' => 'CHAR',
111 'default_value' => undef,
115 $schema->populate ('Owners', [
122 $schema->populate ('BooksInLibrary', [
123 [qw/source owner title /],
124 [qw/Library 1 secrets1/],
125 [qw/Eatery 1 secrets2/],
126 [qw/Library 2 secrets3/],
130 # try a distinct + prefetch on tables with identically named columns
131 # (mysql doesn't seem to like subqueries with equally named columns)
135 # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed)
136 my $owners = $schema->resultset ('Owners')->search (
137 { 'books.id' => { '!=', undef }},
138 { prefetch => 'books', distinct => 1 }
140 my $owners2 = $schema->resultset ('Owners')->search ({ id => { -in => $owners->get_column ('me.id')->as_query }});
141 for ($owners, $owners2) {
142 is ($_->all, 2, 'Prefetched grouped search returns correct number of rows');
143 is ($_->count, 2, 'Prefetched grouped search returns correct count');
146 # try a ->belongs_to direction (no select collapse)
147 my $books = $schema->resultset ('BooksInLibrary')->search (
148 { 'owner.name' => 'wiggle' },
149 { prefetch => 'owner', distinct => 1 }
151 my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }});
152 for ($books, $books2) {
153 is ($_->all, 1, 'Prefetched grouped search returns correct number of rows');
154 is ($_->count, 1, 'Prefetched grouped search returns correct count');
159 my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
160 skip "Cannot determine MySQL server version", 1 if !$mysql_version;
162 my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/;
163 skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2);
167 if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) {
168 $test_type_info->{charfield}->{data_type} = 'VARCHAR';
171 my $type_info = $schema->storage->columns_info_for('artist');
172 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
175 my $cd = $schema->resultset ('CD')->create ({});
176 my $producer = $schema->resultset ('Producer')->create ({});
177 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
180 my $artist = $schema->resultset('Artist')->next;
181 my $cd = $schema->resultset('CD')->next;
182 $cd->set_from_related ('artist', $artist);
185 my $rs = $schema->resultset('CD')->search ({}, { prefetch => 'artist' });
189 is ($cd->artist->name, $artist->name, 'Prefetched artist');
190 }, 'join does not throw (mysql 3 test)';
192 # induce a jointype override, make sure it works even if we don't have mysql3
193 local $schema->storage->sql_maker->{_default_jointype} = 'inner';
197 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
198 artist.artistid, artist.name, artist.rank, artist.charfield
200 INNER JOIN artist artist ON artist.artistid = me.artist
203 'overriden default join type works',
208 # Test support for straight joins
209 my $cdsrc = $schema->source('CD');
210 my $artrel_info = $cdsrc->relationship_info ('artist');
211 $cdsrc->add_relationship(
213 $artrel_info->{class},
214 $artrel_info->{cond},
215 { %{$artrel_info->{attrs}}, join_type => 'straight' },
218 $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
220 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
221 straight_artist.artistid, straight_artist.name, straight_artist.rank, straight_artist.charfield
223 STRAIGHT_JOIN artist straight_artist ON straight_artist.artistid = me.artist
226 'straight joins correctly supported for mysql'
230 ## Can we properly deal with the null search problem?
232 ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect
233 ## But I'm not sure if we should do this or not (Ash, 2008/06/03)
235 # There is now a built-in function to do this, test that everything works
236 # with it (ribasushi, 2009/07/03)
239 my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass, { on_connect_call => 'set_strict_mode' });
241 $ansi_schema->resultset('Artist')->create ({ name => 'last created artist' });
243 ok my $artist1_rs = $ansi_schema->resultset('Artist')->search({artistid=>6666})
244 => 'Created an artist resultset of 6666';
246 is $artist1_rs->count, 0
247 => 'Got no returned rows';
249 ok my $artist2_rs = $ansi_schema->resultset('Artist')->search({artistid=>undef})
250 => 'Created an artist resultset of undef';
252 is $artist2_rs->count, 0
255 my $artist = $artist2_rs->single;
261 # check for proper grouped counts
263 my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass, {
264 on_connect_call => 'set_strict_mode',
267 my $rs = $ansi_schema->resultset('CD');
270 $years->{$_->year|| scalar keys %$years}++ for $rs->all; # NULL != NULL, thus the keys eval
274 $rs->search ({}, { group_by => 'year'})->count,
276 'grouped count correct',
278 }, 'Grouped count does not throw');
281 $ansi_schema->resultset('Owners')->search({}, {
282 join => 'books', group_by => [ 'me.id', 'books.id' ]
284 }, 'count on grouped columns with the same name does not throw');
296 my $rs = $schema->resultset ('CD');
298 for my $y (keys %$cds_per_year) {
299 for my $c (1 .. $cds_per_year->{$y} ) {
300 $rs->create ({ title => "CD $y-$c", artist => 1, year => "$y-01-01" });
304 is ($rs->count, 6, 'CDs created successfully');
306 $rs = $rs->search ({}, {
307 select => [ \ 'YEAR(year)' ], as => ['y'], distinct => 1,
311 [ sort ($rs->get_column ('y')->all) ],
312 [ sort keys %$cds_per_year ],
313 'Years group successfully',
316 $rs->create ({ artist => 1, year => '0-1-1', title => 'Jesus Rap' });
319 [ sort $rs->get_column ('y')->all ],
320 [ 0, sort keys %$cds_per_year ],
321 'Zero-year groups successfully',
324 # convoluted search taken verbatim from list
325 my $restrict_rs = $rs->search({ -and => [
327 year => { '!=', undef }
331 [ $restrict_rs->get_column('y')->all ],
332 [ $rs->get_column ('y')->all ],
333 'Zero year was correctly excluded from resultset',
337 ## If find() is the first query after connect()
338 ## DBI::Storage::sql_maker() will be called before
339 ## _determine_driver() and so the ::SQLHacks class for MySQL
342 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
343 $schema2->resultset("Artist")->find(4);
344 isa_ok($schema2->storage->sql_maker, 'DBIx::Class::SQLMaker::MySQL');