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 INTEGER, 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 # This is in Core now, but it's here just to test that it doesn't break
49 $schema->class('Artist')->load_components('PK::Auto');
51 # test primary key handling
52 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
53 ok($new->artistid, "Auto-PK worked");
57 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
59 my $it = $schema->resultset('Artist')->search( {},
62 order_by => 'artistid' }
64 is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists
65 is( $it->next->name, "Artist 2", "iterator->next ok" );
68 is( $it->next, undef, "next past end of resultset ok" );
70 my $test_type_info = {
75 'default_value' => undef,
78 'data_type' => 'VARCHAR',
81 'default_value' => undef,
87 'default_value' => 13,
90 'data_type' => 'CHAR',
93 'default_value' => undef,
97 $schema->populate ('Owners', [
104 $schema->populate ('BooksInLibrary', [
105 [qw/source owner title /],
106 [qw/Library 1 secrets1/],
107 [qw/Eatery 1 secrets2/],
108 [qw/Library 2 secrets3/],
112 # try a distinct + prefetch on tables with identically named columns
113 # (mysql doesn't seem to like subqueries with equally named columns)
117 # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed)
118 my $owners = $schema->resultset ('Owners')->search (
119 { 'books.id' => { '!=', undef }},
120 { prefetch => 'books', distinct => 1 }
122 my $owners2 = $schema->resultset ('Owners')->search ({ id => { -in => $owners->get_column ('me.id')->as_query }});
123 for ($owners, $owners2) {
124 is ($_->all, 2, 'Prefetched grouped search returns correct number of rows');
125 is ($_->count, 2, 'Prefetched grouped search returns correct count');
128 # try a ->belongs_to direction (no select collapse)
129 my $books = $schema->resultset ('BooksInLibrary')->search (
130 { 'owner.name' => 'wiggle' },
131 { prefetch => 'owner', distinct => 1 }
133 my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }});
134 for ($books, $books2) {
135 is ($_->all, 1, 'Prefetched grouped search returns correct number of rows');
136 is ($_->count, 1, 'Prefetched grouped search returns correct count');
141 my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
142 skip "Cannot determine MySQL server version", 1 if !$mysql_version;
144 my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/;
145 skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2);
149 if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) {
150 $test_type_info->{charfield}->{data_type} = 'VARCHAR';
153 my $type_info = $schema->storage->columns_info_for('artist');
154 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
159 my $cd = $schema->resultset ('CD')->create ({});
160 my $producer = $schema->resultset ('Producer')->create ({});
161 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
164 my $artist = $schema->resultset('Artist')->next;
165 my $cd = $schema->resultset('CD')->next;
166 $cd->set_from_related ('artist', $artist);
169 my $rs = $schema->resultset('CD')->search ({}, { prefetch => 'artist' });
173 is ($cd->artist->name, $artist->name, 'Prefetched artist');
174 }, 'join does not throw (mysql 3 test)';
176 # induce a jointype override, make sure it works even if we don't have mysql3
177 local $schema->storage->sql_maker->{_default_jointype} = 'inner';
181 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
182 artist.artistid, artist.name, artist.rank, artist.charfield
184 INNER JOIN artist artist ON artist.artistid = me.artist
187 'overriden default join type works',
191 ## Can we properly deal with the null search problem?
193 ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect
194 ## But I'm not sure if we should do this or not (Ash, 2008/06/03)
196 # There is now a built-in function to do this, test that everything works
197 # with it (ribasushi, 2009/07/03)
200 my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass, { on_connect_call => 'set_strict_mode' });
202 $ansi_schema->resultset('Artist')->create ({ name => 'last created artist' });
204 ok my $artist1_rs = $ansi_schema->resultset('Artist')->search({artistid=>6666})
205 => 'Created an artist resultset of 6666';
207 is $artist1_rs->count, 0
208 => 'Got no returned rows';
210 ok my $artist2_rs = $ansi_schema->resultset('Artist')->search({artistid=>undef})
211 => 'Created an artist resultset of undef';
213 is $artist2_rs->count, 0
216 my $artist = $artist2_rs->single;