8 use DBI::Const::GetInfoType;
10 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/};
12 #warn "$dsn $user $pass";
14 plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test'
15 unless ($dsn && $user);
19 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
21 my $dbh = $schema->storage->dbh;
23 $dbh->do("DROP TABLE IF EXISTS artist;");
25 $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));");
27 $dbh->do("DROP TABLE IF EXISTS cd;");
29 $dbh->do("CREATE TABLE cd (cdid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, artist INTEGER, title TEXT, year INTEGER, genreid INTEGER, single_track INTEGER);");
31 $dbh->do("DROP TABLE IF EXISTS producer;");
33 $dbh->do("CREATE TABLE producer (producerid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT);");
35 $dbh->do("DROP TABLE IF EXISTS cd_to_producer;");
37 $dbh->do("CREATE TABLE cd_to_producer (cd INTEGER,producer INTEGER);");
39 $dbh->do("DROP TABLE IF EXISTS owners;");
41 $dbh->do("CREATE TABLE owners (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL);");
43 $dbh->do("DROP TABLE IF EXISTS books;");
45 $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);");
47 #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', '');
49 # This is in Core now, but it's here just to test that it doesn't break
50 $schema->class('Artist')->load_components('PK::Auto');
52 # test primary key handling
53 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
54 ok($new->artistid, "Auto-PK worked");
58 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
60 my $it = $schema->resultset('Artist')->search( {},
63 order_by => 'artistid' }
65 is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists
66 is( $it->next->name, "Artist 2", "iterator->next ok" );
69 is( $it->next, undef, "next past end of resultset ok" );
71 my $test_type_info = {
76 'default_value' => undef,
79 'data_type' => 'VARCHAR',
82 'default_value' => undef,
88 'default_value' => 13,
91 'data_type' => 'CHAR',
94 'default_value' => undef,
98 $schema->populate ('Owners', [
105 $schema->populate ('BooksInLibrary', [
106 [qw/source owner title /],
107 [qw/Library 1 secrets1/],
108 [qw/Eatery 1 secrets2/],
109 [qw/Library 2 secrets3/],
113 # try a distinct + prefetch on tables with identically named columns
114 # (mysql doesn't seem to like subqueries with equally named columns)
118 # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed)
119 my $owners = $schema->resultset ('Owners')->search (
120 { 'books.id' => { '!=', undef }},
121 { prefetch => 'books', distinct => 1 }
123 my $owners2 = $schema->resultset ('Owners')->search ({ id => { -in => $owners->get_column ('me.id')->as_query }});
124 for ($owners, $owners2) {
125 is ($_->all, 2, 'Prefetched grouped search returns correct number of rows');
126 is ($_->count, 2, 'Prefetched grouped search returns correct count');
129 # try a ->belongs_to direction (no select collapse)
130 my $books = $schema->resultset ('BooksInLibrary')->search (
131 { 'owner.name' => 'wiggle' },
132 { prefetch => 'owner', distinct => 1 }
134 my $books2 = $schema->resultset ('BooksInLibrary')->search ({ id => { -in => $books->get_column ('me.id')->as_query }});
135 for ($books, $books2) {
136 is ($_->all, 1, 'Prefetched grouped search returns correct number of rows');
137 is ($_->count, 1, 'Prefetched grouped search returns correct count');
142 my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
143 skip "Cannot determine MySQL server version", 1 if !$mysql_version;
145 my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/;
146 skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2);
150 if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) {
151 $test_type_info->{charfield}->{data_type} = 'VARCHAR';
154 my $type_info = $schema->storage->columns_info_for('artist');
155 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
158 my $cd = $schema->resultset ('CD')->create ({});
159 my $producer = $schema->resultset ('Producer')->create ({});
160 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
163 ## Can we properly deal with the null search problem?
165 ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect
166 ## But I'm not sure if we should do this or not (Ash, 2008/06/03)
169 local $TODO = 'Fix pending in branches/mysql_ansi';
170 my $ansi_schema = DBICTest::Schema->connect ($dsn, $user, $pass);
172 $ansi_schema->resultset('Artist')->create ({ name => 'last created artist' });
174 ok my $artist1_rs = $ansi_schema->resultset('Artist')->search({artistid=>6666})
175 => 'Created an artist resultset of 6666';
177 is $artist1_rs->count, 0
178 => 'Got no returned rows';
180 ok my $artist2_rs = $ansi_schema->resultset('Artist')->search({artistid=>undef})
181 => 'Created an artist resultset of undef';
183 is $artist2_rs->count, 0
186 my $artist = $artist2_rs->single;