Commit | Line | Data |
4c2b30d6 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
8 | use DBIC::SqlMakerTest; |
9 | |
10 | my $schema = DBICTest->init_schema(); |
11 | my $orig_debug = $schema->storage->debug; |
12 | |
13 | my $cdrs = $schema->resultset('CD')->search({ 'me.artist' => { '!=', 2 }}); |
14 | |
15 | my $cd_data = { map { |
16 | $_->cdid => { |
17 | siblings => $cdrs->search ({ artist => $_->get_column('artist') })->count - 1, |
18 | track_titles => [ map { $_->title } ($_->tracks->all) ], |
19 | }, |
20 | } ( $cdrs->all ) }; |
21 | |
4c2b30d6 |
22 | my $c_rs = $cdrs->search ({}, { |
23 | prefetch => 'tracks', |
24 | '+columns' => { sibling_count => $cdrs->search( |
25 | { |
26 | 'siblings.artist' => { -ident => 'me.artist' }, |
27 | 'siblings.cdid' => { '!=' => ['-and', { -ident => 'me.cdid' }, 'bogus condition'] }, |
28 | }, { alias => 'siblings' }, |
29 | )->count_rs->as_query, |
30 | }, |
31 | }); |
32 | |
33 | is_same_sql_bind( |
34 | $c_rs->as_query, |
35 | '( |
36 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, |
37 | (SELECT COUNT( * ) |
38 | FROM cd siblings |
39 | WHERE siblings.artist = me.artist |
40 | AND siblings.cdid != me.cdid |
41 | AND siblings.cdid != ? |
42 | AND me.artist != ? |
43 | ), |
44 | tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at |
45 | FROM cd me |
46 | LEFT JOIN track tracks |
47 | ON tracks.cd = me.cdid |
48 | WHERE me.artist != ? |
49 | ORDER BY tracks.cd |
50 | )', |
51 | [ |
36fd7f07 |
52 | |
53 | # subselect |
4c2b30d6 |
54 | [ 'siblings.cdid' => 'bogus condition' ], |
55 | [ 'me.artist' => 2 ], |
36fd7f07 |
56 | |
57 | # outher WHERE |
4c2b30d6 |
58 | [ 'me.artist' => 2 ], |
59 | ], |
60 | 'Expected SQL on correlated realiased subquery' |
61 | ); |
62 | |
36fd7f07 |
63 | my $queries = 0; |
64 | $schema->storage->debugcb(sub { $queries++; }); |
65 | $schema->storage->debug(1); |
66 | |
4c2b30d6 |
67 | is_deeply ( |
68 | { map |
69 | { $_->cdid => { |
70 | track_titles => [ map { $_->title } ($_->tracks->all) ], |
71 | siblings => $_->get_column ('sibling_count'), |
72 | } } |
73 | $c_rs->all |
74 | }, |
75 | $cd_data, |
76 | 'Proper information retrieved from correlated subquery' |
77 | ); |
78 | |
79 | is ($queries, 1, 'Only 1 query fired to retrieve everything'); |
80 | |
81 | $schema->storage->debug($orig_debug); |
82 | $schema->storage->debugcb(undef); |
83 | |
36fd7f07 |
84 | # try to unbalance the select |
85 | |
86 | # first add a lone non-as-ed select |
87 | # it should be reordered to appear at the end without throwing prefetch/bind off |
88 | $c_rs = $c_rs->search({}, { '+select' => \[ 'me.cdid + ?', [ __add => 1 ] ] }); |
89 | |
90 | # now add an unbalanced select/as pair |
91 | $c_rs = $c_rs->search ({}, { |
92 | '+select' => $cdrs->search( |
93 | { 'siblings.artist' => { -ident => 'me.artist' } }, |
94 | { alias => 'siblings', columns => [ |
95 | { first_year => { min => 'year' }}, |
96 | { last_year => { max => 'year' }}, |
97 | ]}, |
98 | )->as_query, |
99 | '+as' => [qw/active_from active_to/], |
100 | }); |
101 | |
102 | |
103 | is_same_sql_bind( |
104 | $c_rs->as_query, |
105 | '( |
106 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, |
107 | (SELECT COUNT( * ) |
108 | FROM cd siblings |
109 | WHERE siblings.artist = me.artist |
110 | AND siblings.cdid != me.cdid |
111 | AND siblings.cdid != ? |
112 | AND me.artist != ? |
113 | ), |
114 | (SELECT MIN( year ), MAX( year ) |
115 | FROM cd siblings |
116 | WHERE siblings.artist = me.artist |
117 | AND me.artist != ? |
118 | ), |
119 | tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, |
120 | me.cdid + ? |
121 | FROM cd me |
122 | LEFT JOIN track tracks |
123 | ON tracks.cd = me.cdid |
124 | WHERE me.artist != ? |
125 | ORDER BY tracks.cd |
126 | )', |
127 | [ |
128 | |
129 | # first subselect |
130 | [ 'siblings.cdid' => 'bogus condition' ], |
131 | [ 'me.artist' => 2 ], |
132 | |
133 | # second subselect |
134 | [ 'me.artist' => 2 ], |
135 | |
136 | # the addition |
137 | [ __add => 1 ], |
138 | |
139 | # outher WHERE |
140 | [ 'me.artist' => 2 ], |
141 | ], |
142 | 'Expected SQL on correlated realiased subquery' |
143 | ); |
144 | |
4c2b30d6 |
145 | done_testing; |