7 eval "use DBD::SQLite";
9 ? ( skip_all => 'needs DBD::SQLite for testing' )
13 # test the abstract join => SQL generator
14 my $sa = new DBIC::SQL::Abstract;
17 { child => 'person' },
18 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
19 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
21 my $match = 'person child JOIN person father ON ( father.person_id = '
22 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
23 . '= child.mother_id )'
25 is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
28 { mother => 'person' },
29 [ [ { child => 'person' },
30 [ { father => 'person' },
31 { 'father.person_id' => 'child.father_id' }
34 { 'mother.person_id' => 'child.mother_id' }
37 $match = 'person mother JOIN (person child JOIN person father ON ('
38 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
41 is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
44 { child => 'person' },
45 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
46 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
48 $match = 'person child INNER JOIN person father ON ( father.person_id = '
49 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
50 . '= child.mother_id )'
53 is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
55 my $rs = $schema->resultset("CD")->search(
56 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
57 { from => [ { 'me' => 'cd' },
59 { artist => 'artist' },
60 { 'me.artist' => 'artist.artistid' }
64 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
66 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
68 $rs = $schema->resultset("CD")->search(
69 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
70 { join => 'artist' });
72 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
74 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
76 $rs = $schema->resultset("CD")->search(
77 { 'artist.name' => 'We Are Goth',
78 'liner_notes.notes' => 'Kill Yourself!' },
79 { join => [ qw/artist liner_notes/ ] });
81 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
83 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
85 # when using join attribute, make sure slice()ing all objects has same count as all()
86 $rs = $schema->resultset("CD")->search(
88 { join => [qw/artist/], order_by => 'artist.name' }
90 cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
92 $rs = $schema->resultset("Artist")->search(
93 { 'liner_notes.notes' => 'Kill Yourself!' },
94 { join => { 'cds' => 'liner_notes' } });
96 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
98 is($rs->first->name, 'We Are Goth', 'Correct record returned');
100 $rs = $schema->resultset("CD")->search(
101 { 'artist.name' => 'Caterwauler McCrae' },
102 { prefetch => [ qw/artist liner_notes/ ],
103 order_by => 'me.cdid' });
105 cmp_ok($rs->count, '==', 3, 'Correct number of records returned');
107 # start test for prefetch SELECT count
108 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
109 DBI->trace(1, 't/var/dbic.trace');
113 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
115 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
117 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
119 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
121 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
126 my $trace = IO::File->new('t/var/dbic.trace', '<')
127 or die "Unable to read trace file";
129 $selects++ if /SELECT/;
132 unlink 't/var/dbic.trace';
133 is($selects, 1, 'prefetch ran only 1 select statement');
135 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
136 { order_by => 'artistid DESC', join => 'cds' });
138 is($artist->name, 'Random Boy Band', "Join search by object ok");
140 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
141 { join => 'liner_notes' });
143 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
145 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
147 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
148 { join => { 'cds' => 'tags' } });
150 cmp_ok( @artists, '==', 2, "two-join search ok" );