Possible to set locale in IC::DateTime extra => {} config
[dbsrgits/DBIx-Class.git] / t / 76joins.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
cb3e35d2 7use Data::Dumper;
949172b0 8use DBIC::SqlMakerTest;
70350518 9
a47e1233 10my $schema = DBICTest->init_schema();
0567538f 11
4d91ad3f 12my $orig_debug = $schema->storage->debug;
13
0567538f 14use IO::File;
15
16BEGIN {
17 eval "use DBD::SQLite";
18 plan $@
19 ? ( skip_all => 'needs DBD::SQLite for testing' )
96e7f9ec 20 : ( tests => 16 );
0567538f 21}
22
58ff4acf 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
25my $is_broken_sqlite = 0;
26my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
27 split /\./, $schema->storage->dbh->get_info(18);
28if( $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;
33}
485f6e10 34
0567538f 35# test the abstract join => SQL generator
36my $sa = new DBIC::SQL::Abstract;
37
38my @j = (
39 { child => 'person' },
40 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
41 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
42);
43my $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 )'
46 ;
9b459129 47is_same_sql_bind(
48 $sa->_recurse_from(@j), [],
49 $match, [],
50 'join 1 ok'
51);
0567538f 52
53my @j2 = (
54 { mother => 'person' },
55 [ [ { child => 'person' },
56 [ { father => 'person' },
57 { 'father.person_id' => 'child.father_id' }
58 ]
59 ],
60 { 'mother.person_id' => 'child.mother_id' }
61 ],
62);
63$match = 'person mother JOIN (person child JOIN person father ON ('
64 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
65 . 'child.mother_id )'
66 ;
9b459129 67is_same_sql_bind(
68 $sa->_recurse_from(@j2), [],
69 $match, [],
70 'join 2 ok'
71);
72
0567538f 73
74my @j3 = (
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' } ],
78);
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 )'
82 ;
83
9b459129 84is_same_sql_bind(
85 $sa->_recurse_from(@j3), [],
86 $match, [],
87 'join 3 (inner join) ok'
88);
0567538f 89
ca7b9fdf 90my @j4 = (
91 { mother => 'person' },
92 [ [ { child => 'person', -join_type => 'left' },
93 [ { father => 'person', -join_type => 'right' },
94 { 'father.person_id' => 'child.father_id' }
95 ]
96 ],
97 { 'mother.person_id' => 'child.mother_id' }
98 ],
99);
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 )'
103 ;
9b459129 104is_same_sql_bind(
105 $sa->_recurse_from(@j4), [],
106 $match, [],
107 'join 4 (nested joins + join types) ok'
108);
ca7b9fdf 109
635b9634 110my @j5 = (
111 { child => 'person' },
112 [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
113 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
114);
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 )'
118 ;
9b459129 119is_same_sql_bind(
120 $sa->_recurse_from(@j5), [],
121 $match, [],
122 'join 5 (SCALAR reference for ON statement) ok'
123);
635b9634 124
125my @j6 = (
126 { child => 'person' },
127 [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ],
128 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
129);
11aada86 130$match = qr/^HASH reference arguments are not supported in JOINS - try using "\.\.\." instead/;
635b9634 131eval { $sa->_recurse_from(@j6) };
132like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' );
133
f9db5527 134my $rs = $schema->resultset("CD")->search(
0567538f 135 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
136 { from => [ { 'me' => 'cd' },
137 [
138 { artist => 'artist' },
139 { 'me.artist' => 'artist.artistid' }
140 ] ] }
141 );
142
ebaefbc2 143cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 144
145is($rs->first->title, 'Forkful of bees', 'Correct record returned');
146
f9db5527 147$rs = $schema->resultset("CD")->search(
0567538f 148 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
149 { join => 'artist' });
150
ebaefbc2 151cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 152
153is($rs->first->title, 'Forkful of bees', 'Correct record returned');
154
f9db5527 155$rs = $schema->resultset("CD")->search(
0567538f 156 { 'artist.name' => 'We Are Goth',
157 'liner_notes.notes' => 'Kill Yourself!' },
158 { join => [ qw/artist liner_notes/ ] });
159
ebaefbc2 160cmp_ok( $rs + 0, '==', 1, "Single record in resultset");
0567538f 161
162is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
163
8fe164b9 164# when using join attribute, make sure slice()ing all objects has same count as all()
f9db5527 165$rs = $schema->resultset("CD")->search(
8fe164b9 166 { 'artist' => 1 },
167 { join => [qw/artist/], order_by => 'artist.name' }
168);
169cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
170
2bd9c7c0 171ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
172 'Slicing beyond end of rs returns a zero count');
173
f9db5527 174$rs = $schema->resultset("Artist")->search(
0567538f 175 { 'liner_notes.notes' => 'Kill Yourself!' },
176 { join => { 'cds' => 'liner_notes' } });
177
178cmp_ok( $rs->count, '==', 1, "Single record in resultset");
179
180is($rs->first->name, 'We Are Goth', 'Correct record returned');
181