8 use DBIC::SqlMakerTest;
10 my $schema = DBICTest->init_schema();
12 my $orig_debug = $schema->storage->debug;
17 eval "use DBD::SQLite";
19 ? ( skip_all => 'needs DBD::SQLite for testing' )
23 # figure out if we've got a version of sqlite that is older than 3.2.6, in
24 # which case COUNT(DISTINCT()) doesn't work
25 my $is_broken_sqlite = 0;
26 my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
27 split /\./, $schema->storage->dbh->get_info(18);
28 if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
29 ( ($sqlite_major_ver < 3) ||
30 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
31 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
32 $is_broken_sqlite = 1;
35 # test the abstract join => SQL generator
36 my $sa = new DBIC::SQL::Abstract;
39 { child => 'person' },
40 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
41 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
43 my $match = 'person child JOIN person father ON ( father.person_id = '
44 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
45 . '= child.mother_id )'
48 $sa->_recurse_from(@j), [],
54 { mother => 'person' },
55 [ [ { child => 'person' },
56 [ { father => 'person' },
57 { 'father.person_id' => 'child.father_id' }
60 { 'mother.person_id' => 'child.mother_id' }
63 $match = 'person mother JOIN (person child JOIN person father ON ('
64 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
68 $sa->_recurse_from(@j2), [],
75 { child => 'person' },
76 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
77 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
79 $match = 'person child INNER JOIN person father ON ( father.person_id = '
80 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
81 . '= child.mother_id )'
85 $sa->_recurse_from(@j3), [],
87 'join 3 (inner join) ok'
91 { mother => 'person' },
92 [ [ { child => 'person', -join_type => 'left' },
93 [ { father => 'person', -join_type => 'right' },
94 { 'father.person_id' => 'child.father_id' }
97 { 'mother.person_id' => 'child.mother_id' }
100 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
101 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
102 . 'child.mother_id )'
105 $sa->_recurse_from(@j4), [],
107 'join 4 (nested joins + join types) ok'
111 { child => 'person' },
112 [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
113 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
115 $match = 'person child JOIN person father ON ( father.person_id != '
116 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
117 . '= child.mother_id )'
120 $sa->_recurse_from(@j5), [],
122 'join 5 (SCALAR reference for ON statement) ok'
126 { child => 'person' },
127 [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ],
128 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
130 $match = qr/^HASH reference arguments are not supported in JOINS - try using "\.\.\." instead/;
131 eval { $sa->_recurse_from(@j6) };
132 like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' );
134 my $rs = $schema->resultset("CD")->search(
135 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
136 { from => [ { 'me' => 'cd' },
138 { artist => 'artist' },
139 { 'me.artist' => 'artist.artistid' }
143 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
145 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
147 $rs = $schema->resultset("CD")->search(
148 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
149 { join => 'artist' });
151 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
153 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
155 $rs = $schema->resultset("CD")->search(
156 { 'artist.name' => 'We Are Goth',
157 'liner_notes.notes' => 'Kill Yourself!' },
158 { join => [ qw/artist liner_notes/ ] });
160 cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
162 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
164 # when using join attribute, make sure slice()ing all objects has same count as all()
165 $rs = $schema->resultset("CD")->search(
167 { join => [qw/artist/], order_by => 'artist.name' }
169 cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
171 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
172 'Slicing beyond end of rs returns a zero count');
174 $rs = $schema->resultset("Artist")->search(
175 { 'liner_notes.notes' => 'Kill Yourself!' },
176 { join => { 'cds' => 'liner_notes' } });
178 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
180 is($rs->first->name, 'We Are Goth', 'Correct record returned');
182 # test for warnings on delete of joined resultset
183 $rs = $schema->resultset("CD")->search(
184 { 'artist.name' => 'Caterwauler McCrae' },
185 { join => [qw/artist/]}
187 my $tst_delete_warning;
189 local $SIG{__WARN__} = sub { $tst_delete_warning = shift };
193 ok( ($@ || $tst_delete_warning), 'fail/warning on attempt to delete a join-ed resultset');
195 # test for warnings on update of joined resultset
196 $rs = $schema->resultset("CD")->search(
197 { 'artist.name' => 'Random Boy Band' },
198 { join => [qw/artist/]}
200 my $tst_update_warning;
202 local $SIG{__WARN__} = sub { $tst_update_warning = shift };
203 $rs->update({ 'artist' => 1 });
206 ok( ($@ || $tst_update_warning), 'fail/warning on attempt to update a join-ed resultset');