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 #'dbi:mysql:host=localhost;database=dbic_test', 'dbic_test', '');
41 # This is in Core now, but it's here just to test that it doesn't break
42 $schema->class('Artist')->load_components('PK::Auto');
44 # test primary key handling
45 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
46 ok($new->artistid, "Auto-PK worked");
50 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
52 my $it = $schema->resultset('Artist')->search( {},
55 order_by => 'artistid' }
57 is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists
58 is( $it->next->name, "Artist 2", "iterator->next ok" );
61 is( $it->next, undef, "next past end of resultset ok" );
63 my $test_type_info = {
68 'default_value' => undef,
71 'data_type' => 'VARCHAR',
74 'default_value' => undef,
80 'default_value' => 13,
83 'data_type' => 'CHAR',
86 'default_value' => undef,
91 my $mysql_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} );
92 skip "Cannot determine MySQL server version", 1 if !$mysql_version;
94 my ($v1, $v2, $v3) = $mysql_version =~ /^(\d+)\.(\d+)(?:\.(\d+))?/;
95 skip "Cannot determine MySQL server version", 1 if !$v1 || !defined($v2);
99 if( ($v1 < 5) || ($v1 == 5 && $v2 == 0 && $v3 <= 3) ) {
100 $test_type_info->{charfield}->{data_type} = 'VARCHAR';
103 my $type_info = $schema->storage->columns_info_for('artist');
104 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
107 ## Can we properly deal with the null search problem?
109 ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect
110 ## But I'm not sure if we should do this or not (Ash, 2008/06/03)
114 ok my $artist1_rs = $schema->resultset('Artist')->search({artistid=>6666})
115 => 'Created an artist resultset of 6666';
117 is $artist1_rs->count, 0
118 => 'Got no returned rows';
120 ok my $artist2_rs = $schema->resultset('Artist')->search({artistid=>undef})
121 => 'Created an artist resultset of undef';
124 local $TODO = "need to fix the row count =1 when select * from table where pk IS NULL problem";
125 is $artist2_rs->count, 0
129 my $artist = $artist2_rs->single;
135 my $cd = $schema->resultset ('CD')->create ({});
137 my $producer = $schema->resultset ('Producer')->create ({});
139 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
143 #$dbh->do("DROP TABLE artist") if $dbh;