9 use DBIC::SqlMakerTest;
10 use DBIx::Class::Optional::Dependencies ();
15 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_ss_dbm')
16 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_ss_dbm');
18 my $db_dir = dir(qw/t var/, "ss_dbm-$$");
19 $db_dir->mkpath unless -d $db_dir;
21 my ($dsn, $opts) = ('dbi:DBM:', {
23 f_lockfile => '.lock',
25 dbm_type => 'BerkeleyDB',
26 dbm_mldbm => 'Storable',
27 dbm_store_metadata => 1,
30 my $schema = DBICTest::Schema->connect($dsn, '', '', $opts);
31 is ($schema->storage->sqlt_type, 'DBM', 'sqlt_type correct pre-connection');
32 isa_ok($schema->storage->sql_maker, 'DBIx::Class::SQLMaker::SQLStatement');
35 my $dbh = $schema->storage->dbh;
36 my @cmds = split /\s*\;\s*/, scalar file(qw/t lib test_deploy DBICTest-Schema-1.x-SQL-Statement.sql/)->slurp;
37 $dbh->do($_) for @cmds;
39 ### S:S doesn't have any sort of AUTOINCREMENT support, so IDs will have to be generated by hand ###
41 # test primary key handling
42 my $new = $schema->resultset('Artist')->create({
46 ok($new->artistid, "Create worked");
50 $schema->resultset('Artist')->create({
55 my $it = $schema->resultset('Artist')->search( {}, {
58 order_by => 'artistid'
60 is( $it->count, 3, "LIMIT count ok" ); # ask for 3 rows out of 7 artists
61 is( $it->next->name, "Artist 2", "iterator->next ok" );
64 is( $it->next, undef, "next past end of resultset ok" );
66 # Limit with select-lock (which is silently thrown away)
69 $schema->resultset('Artist')->find({artistid => 1}, {for => 'update', rows => 1}),
70 'DBICTest::Schema::Artist',
72 } 'Limited FOR UPDATE select works';
74 # shared-lock (which is silently thrown away)
77 $schema->resultset('Artist')->find({artistid => 1}, {for => 'shared'}),
78 'DBICTest::Schema::Artist',
80 } 'LOCK IN SHARE MODE select works';
82 # (No nullables with DBD::DBM)
83 my $test_type_info = {
85 'data_type' => 'VARCHAR',
90 'data_type' => 'VARCHAR',
95 'data_type' => 'VARCHAR',
100 'data_type' => 'VARCHAR',
106 $schema->populate ('Owners', [
113 $schema->populate ('BooksInLibrary', [
114 [qw/id source owner title /],
115 [qw/1 Library 1 secrets1/],
116 [qw/2 Eatery 1 secrets2/],
117 [qw/3 Library 2 secrets3/],
121 # try a ->has_many direction (due to a 'multi' accessor the select/group_by group is collapsed)
122 my $owners = $schema->resultset('Owners')->search(
123 { 'books.id' => { '!=', undef }},
124 { prefetch => 'books', cache => 1 }
126 is($owners->all, 2, 'Prefetched grouped search returns correct number of rows');
128 # only works here because of the full cache
129 # S:S would croak on a subselect otherwise
130 is($owners->count, 2, 'Prefetched grouped search returns correct count');
132 # try a ->belongs_to direction (no select collapse)
133 my $books = $schema->resultset('BooksInLibrary')->search (
134 { 'owner.name' => 'wiggle' },
135 { prefetch => 'owner', distinct => 1 }
139 local $TODO = 'populate does not subtract the non-Library INSERTs here...';
140 is($owners->all, 1, 'Prefetched grouped search returns correct number of rows');
141 is($owners->count, 1, 'Prefetched grouped search returns correct count');
145 my $type_info = $schema->storage->columns_info_for('artist');
146 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
148 my $cd = $schema->resultset('CD')->create({ cdid => 1 });
149 my $producer = $schema->resultset('Producer')->create({ producerid => 1 });
150 lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die';
153 my $artist = $schema->resultset('Artist')->next;
154 my $cd = $schema->resultset('CD')->next;
155 $cd->set_from_related('artist', $artist);
158 my $rs = $schema->resultset('CD')->search ({}, { prefetch => 'artist' });
162 is ($cd->artist->name, $artist->name, 'Prefetched artist');
163 }, 'join does not throw';
165 local $schema->storage->sql_maker->{_default_jointype} = 'inner';
170 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
171 artist.artistid, artist.name, artist.rank, artist.charfield
173 INNER JOIN artist artist ON artist.artistid = me.artist
176 'overriden default join type works',
181 # Test support for straight joins
182 my $cdsrc = $schema->source('CD');
183 my $artrel_info = $cdsrc->relationship_info ('artist');
184 $cdsrc->add_relationship(
186 $artrel_info->{class},
187 $artrel_info->{cond},
188 { %{$artrel_info->{attrs}}, join_type => 'straight' },
191 $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query,
194 me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
195 straight_artist.artistid, straight_artist.name, straight_artist.rank, straight_artist.charfield
197 STRAIGHT JOIN artist straight_artist ON straight_artist.artistid = me.artist
200 'straight joins correctly supported'
204 # Can we properly deal with the null search problem?
206 $schema->resultset('Artist')->create({ artistid => 2222, name => 'last created artist' });
208 ok my $artist1_rs = $schema->resultset('Artist')->search({artistid=>6666})
209 => 'Created an artist resultset of 6666';
211 is $artist1_rs->count, 0
212 => 'Got no returned rows';
214 ok my $artist2_rs = $schema->resultset('Artist')->search({artistid=>undef})
215 => 'Created an artist resultset of undef';
217 is $artist2_rs->count, 0
220 my $artist = $artist2_rs->single;
233 # kill the scalar ref here
234 $schema->source('CD')->name('cd');
236 my $rs = $schema->resultset('CD');
239 foreach my $y (keys %$cds_per_year) {
240 foreach my $c (1 .. $cds_per_year->{$y} ) {
241 $rs->create({ cdid => $cdid++, title => "CD $y-$c", artist => 1, year => "$y-01-01" });
245 is ($rs->count, 6, 'CDs created successfully');