9 use DBIC::SqlMakerTest;
10 use DBIx::Class::Optional::Dependencies ();
14 plan skip_all => 'Set $ENV{DBICTEST_DBD_ANYDATA} = 1 to run this test'
15 unless ($ENV{DBICTEST_DBD_ANYDATA});
18 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_ss_anydata')
19 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_ss_anydata');
21 # just in case they try to use these conflicting versions
24 'Incompatible versions of DBD::AnyData and DBI'
25 if ($DBD::AnyData::VERSION <= 0.110 && $DBI::VERSION >= 1.623);
27 my ($dsn, $opts) = ('dbi:AnyData:', {});
29 my $schema = DBICTest::Schema->connect($dsn, '', '', $opts);
30 is ($schema->storage->sqlt_type, 'AnyData', 'sqlt_type correct pre-connection');
31 isa_ok($schema->storage->sql_maker, 'DBIx::Class::SQLMaker::SQLStatement');
34 my $dbh = $schema->storage->dbh;
35 my @cmds = split /\s*\;\s*/, scalar file(qw/t lib dbdfile.sql/)->slurp;
36 $dbh->do($_) for @cmds;
38 ### S:S doesn't have any sort of AUTOINCREMENT support, so IDs will have to be generated by hand ###
40 # test primary key handling
41 my $new = $schema->resultset('Artist')->create({
45 ok($new->artistid, "Create worked");
49 $schema->resultset('Artist')->create({
54 my $it = $schema->resultset('Artist')->search( {}, {
57 order_by => 'artistid'
59 is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists
60 is( $it->next->name, "Artist 2", "iterator->next ok" );
63 is( $it->next, undef, "next past end of resultset ok" );
65 # Limit with select-lock (which is silently thrown away)
68 $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
69 'DBICTest::Schema::Artist',
71 } 'Limited FOR UPDATE select works';
73 # shared-lock (which is silently thrown away)
76 $schema->resultset('Artist')->find({artistid => 1}, {for => 'shared'}),
77 'DBICTest::Schema::Artist',
79 } 'LOCK IN SHARE MODE select works';
81 # (No sizes with DBD::AnyData and all is_nullable)
82 my $test_type_info = {
84 'data_type' => 'VARCHAR',
89 'data_type' => 'VARCHAR',
94 'data_type' => 'VARCHAR',
99 'data_type' => 'VARCHAR',
105 $ENV{DBIC_DBDFILE_TXN_NOWARN} = 1;
107 $schema->populate ('Owners', [
114 $schema->populate ('BooksInLibrary', [
115 [qw/id source owner title /],
116 [qw/1 Library 1 secrets1/],
117 [qw/2 Eatery 1 secrets2/],
118 [qw/3 Library 2 secrets3/],
122 # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed)
123 my $owners = $schema->resultset('Owners')->search(
124 { 'books.id' => { '!=', undef }},
125 { prefetch => 'books', cache => 1 }
127 is($owners->all, 2, 'Prefetched grouped search returns correct number of rows');
129 # only works here because of the full cache
130 # S:S would croak on a subselect otherwise
131 is($owners->count, 2, 'Prefetched grouped search returns correct count');
133 # try a ->belongs_to direction (no select collapse)
134 my $books = $schema->resultset('BooksInLibrary')->search (
135 { 'owner.name' => 'wiggle' },
136 { prefetch => 'owner', distinct => 1 }
140 local $TODO = 'populate does not subtract the non-Library INSERTs here...';
141 is($owners->all, 1, 'Prefetched grouped search returns correct number of rows');
142 is($owners->count, 1, 'Prefetched grouped search returns correct count');
146 my $type_info = $schema->storage->columns_info_for('artist');
147 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
149 my $cd = $schema->resultset('CD')->create({ cdid => 1 });
150 my $producer = $schema->resultset('Producer')->create({ producerid => 1 });
151 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
154 my $artist = $schema->resultset('Artist')->next;
155 my $cd = $schema->resultset('CD')->next;
156 $cd->set_from_related('artist', $artist);
159 my $rs = $schema->resultset('CD')->search ({}, { prefetch => 'artist' });
163 is ($cd->artist->name, $artist->name, 'Prefetched artist');
164 }, 'join does not throw';
166 local $schema->storage->sql_maker->{_default_jointype} = 'inner';
171 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
172 artist.artistid, artist.name, artist.rank, artist.charfield
174 INNER JOIN artist artist ON artist.artistid = me.artist
177 'overriden default join type works',
182 # Test support for straight joins
183 my $cdsrc = $schema->source('CD');
184 my $artrel_info = $cdsrc->relationship_info ('artist');
185 $cdsrc->add_relationship(
187 $artrel_info->{class},
188 $artrel_info->{cond},
189 { %{$artrel_info->{attrs}}, join_type => 'straight' },
192 $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
195 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
196 straight_artist.artistid, straight_artist.name, straight_artist.rank, straight_artist.charfield
198 STRAIGHT JOIN artist straight_artist ON straight_artist.artistid = me.artist
201 'straight joins correctly supported'
205 # Can we properly deal with the null search problem?
207 $schema->resultset('Artist')->create({ artistid => 2222, name => 'last created artist' });
209 ok my $artist1_rs = $schema->resultset('Artist')->search({artistid=>6666})
210 => 'Created an artist resultset of 6666';
212 is $artist1_rs->count, 0
213 => 'Got no returned rows';
215 ok my $artist2_rs = $schema->resultset('Artist')->search({artistid=>undef})
216 => 'Created an artist resultset of undef';
218 is $artist2_rs->count, 0
221 my $artist = $artist2_rs->single;
234 # kill the scalar ref here
235 $schema->source('CD')->name('cd');
237 my $rs = $schema->resultset('CD');
240 foreach my $y (keys %$cds_per_year) {
241 foreach my $c (1 .. $cds_per_year->{$y} ) {
242 $rs->create({ cdid => $cdid++, title => "CD $y-$c", artist => 1, year => "$y-01-01" });
246 is ($rs->count, 6, 'CDs created successfully');