Commit | Line | Data |
ff1234ad |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use lib qw(t/lib); |
5 | use Test::More; |
6 | use Test::Exception; |
31073ac7 |
7 | |
8 | use DBICTest::Schema::CD; |
9 | BEGIN { |
10 | # the default scalarref table name will not work well for this test |
11 | DBICTest::Schema::CD->table('cd'); |
12 | } |
13 | |
ff1234ad |
14 | use DBICTest; |
887d8da0 |
15 | use DBIC::DebugObj; |
16 | use DBIC::SqlMakerTest; |
ff1234ad |
17 | |
be64931c |
18 | my $schema = DBICTest->init_schema; |
19 | |
20 | my ($sql, @bind); |
21 | my $debugobj = DBIC::DebugObj->new (\$sql, \@bind); |
22 | my $orig_debugobj = $schema->storage->debugobj; |
23 | my $orig_debug = $schema->storage->debug; |
ff1234ad |
24 | |
25 | my $tkfks = $schema->resultset('FourKeys_to_TwoKeys'); |
26 | |
fd8076c8 |
27 | my ($fa, $fb, $fc) = $tkfks->related_resultset ('fourkeys')->populate ([ |
ff1234ad |
28 | [qw/foo bar hello goodbye sensors read_count/], |
29 | [qw/1 1 1 1 a 10 /], |
30 | [qw/2 2 2 2 b 20 /], |
fd8076c8 |
31 | [qw/1 1 1 2 c 30 /], |
ff1234ad |
32 | ]); |
33 | |
34 | # This is already provided by DBICTest |
35 | #my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([ |
36 | # [qw/artist cd /], |
37 | # [qw/1 1 /], |
38 | # [qw/2 2 /], |
39 | #]); |
40 | my ($ta, $tb) = $schema->resultset ('TwoKeys') |
41 | ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ]) |
42 | ->all; |
43 | |
44 | my $tkfk_cnt = $tkfks->count; |
45 | |
46 | my $non_void_ctx = $tkfks->populate ([ |
47 | { autopilot => 'a', fourkeys => $fa, twokeys => $ta, pilot_sequence => 10 }, |
48 | { autopilot => 'b', fourkeys => $fb, twokeys => $tb, pilot_sequence => 20 }, |
49 | { autopilot => 'x', fourkeys => $fa, twokeys => $tb, pilot_sequence => 30 }, |
50 | { autopilot => 'y', fourkeys => $fb, twokeys => $ta, pilot_sequence => 40 }, |
51 | ]); |
52 | is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully'); |
53 | |
54 | # |
55 | # Make sure the forced group by works (i.e. the joining does not cause double-updates) |
56 | # |
57 | |
58 | # create a resultset matching $fa and $fb only |
fd8076c8 |
59 | my $fks = $schema->resultset ('FourKeys')->search ( |
60 | { |
61 | sensors => { '!=', 'c' }, |
62 | ( map { $_ => [1, 2] } qw/foo bar hello goodbye/ ), |
eb58c082 |
63 | }, { join => { fourkeys_to_twokeys => 'twokeys' }} |
fd8076c8 |
64 | ); |
ff1234ad |
65 | |
66 | is ($fks->count, 4, 'Joined FourKey count correct (2x2)'); |
be64931c |
67 | |
68 | $schema->storage->debugobj ($debugobj); |
69 | $schema->storage->debug (1); |
ff1234ad |
70 | $fks->update ({ read_count => \ 'read_count + 1' }); |
be64931c |
71 | $schema->storage->debugobj ($orig_debugobj); |
72 | $schema->storage->debug ($orig_debug); |
ff1234ad |
73 | |
be64931c |
74 | is_same_sql_bind ( |
75 | $sql, |
76 | \@bind, |
77 | 'UPDATE fourkeys |
78 | SET read_count = read_count + 1 |
fd8076c8 |
79 | WHERE ( ( ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? ) ) |
80 | ', |
81 | [ ("'1'", "'2'") x 4, "'c'" ], |
82 | 'Correct update-SQL with multijoin with pruning', |
83 | ); |
84 | |
85 | is ($fa->discard_changes->read_count, 11, 'Update ran only once on discard-join resultset'); |
86 | is ($fb->discard_changes->read_count, 21, 'Update ran only once on discard-join resultset'); |
87 | is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier'); |
88 | |
89 | # make the multi-join stick |
eb58c082 |
90 | my $fks_multi = $fks->search({ 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } }); |
fd8076c8 |
91 | |
92 | $schema->storage->debugobj ($debugobj); |
93 | $schema->storage->debug (1); |
eb58c082 |
94 | $fks_multi->update ({ read_count => \ 'read_count + 1' }); |
fd8076c8 |
95 | $schema->storage->debugobj ($orig_debugobj); |
96 | $schema->storage->debug ($orig_debug); |
97 | |
98 | is_same_sql_bind ( |
99 | $sql, |
100 | \@bind, |
101 | 'UPDATE fourkeys |
102 | SET read_count = read_count + 1 |
be64931c |
103 | WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )', |
104 | [ map { "'$_'" } ( (1) x 4, (2) x 4 ) ], |
fd8076c8 |
105 | 'Correct update-SQL with multijoin without pruning', |
be64931c |
106 | ); |
ff1234ad |
107 | |
fd8076c8 |
108 | is ($fa->discard_changes->read_count, 12, 'Update ran only once on joined resultset'); |
109 | is ($fb->discard_changes->read_count, 22, 'Update ran only once on joined resultset'); |
110 | is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier'); |
be64931c |
111 | |
112 | # try the same sql with forced multicolumn in |
113 | $schema->storage->_use_multicolumn_in (1); |
114 | $schema->storage->debugobj ($debugobj); |
115 | $schema->storage->debug (1); |
eb58c082 |
116 | throws_ok { $fks_multi->update ({ read_count => \ 'read_count + 1' }) } # this can't actually execute, we just need the "as_query" |
eba59b7e |
117 | qr/\QDBI Exception:/ or do { $sql = ''; @bind = () }; |
be64931c |
118 | $schema->storage->_use_multicolumn_in (undef); |
119 | $schema->storage->debugobj ($orig_debugobj); |
120 | $schema->storage->debug ($orig_debug); |
121 | |
122 | is_same_sql_bind ( |
123 | $sql, |
124 | \@bind, |
125 | 'UPDATE fourkeys |
126 | SET read_count = read_count + 1 |
127 | WHERE ( |
128 | (foo, bar, hello, goodbye) IN ( |
129 | SELECT me.foo, me.bar, me.hello, me.goodbye |
130 | FROM fourkeys me |
fd8076c8 |
131 | LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys ON |
132 | fourkeys_to_twokeys.f_bar = me.bar |
133 | AND fourkeys_to_twokeys.f_foo = me.foo |
134 | AND fourkeys_to_twokeys.f_goodbye = me.goodbye |
135 | AND fourkeys_to_twokeys.f_hello = me.hello |
136 | WHERE fourkeys_to_twokeys.pilot_sequence != ? AND ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? |
be64931c |
137 | ) |
138 | ) |
139 | ', |
fd8076c8 |
140 | [ |
141 | "'666'", |
142 | ("'1'", "'2'") x 4, |
143 | "'c'", |
144 | ], |
be64931c |
145 | 'Correct update-SQL with multicolumn in support', |
146 | ); |
ff1234ad |
147 | |
eb58c082 |
148 | # make a *premultiplied* join stick |
149 | my $fks_premulti = $fks->search({ 'twokeys.artist' => { '!=' => 666 } }); |
150 | |
151 | $schema->storage->debugobj ($debugobj); |
152 | $schema->storage->debug (1); |
153 | $fks_premulti->update ({ read_count => \ 'read_count + 1' }); |
154 | $schema->storage->debugobj ($orig_debugobj); |
155 | $schema->storage->debug ($orig_debug); |
156 | |
157 | is_same_sql_bind ( |
158 | $sql, |
159 | \@bind, |
160 | 'UPDATE fourkeys |
161 | SET read_count = read_count + 1 |
162 | WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )', |
163 | [ map { "'$_'" } ( (1) x 4, (2) x 4 ) ], |
164 | 'Correct update-SQL with premultiplied restricting join without pruning', |
165 | ); |
166 | |
167 | is ($fa->discard_changes->read_count, 13, 'Update ran only once on joined resultset'); |
168 | is ($fb->discard_changes->read_count, 23, 'Update ran only once on joined resultset'); |
169 | is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier'); |
170 | |
171 | |
ff1234ad |
172 | # |
be64931c |
173 | # Make sure multicolumn in or the equivalent functions correctly |
ff1234ad |
174 | # |
175 | |
176 | my $sub_rs = $tkfks->search ( |
177 | [ |
178 | { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ }, |
179 | { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ }, |
180 | ], |
181 | { |
182 | join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ], |
183 | }, |
184 | ); |
185 | |
186 | is ($sub_rs->count, 2, 'Only two rows from fourkeys match'); |
187 | |
188 | # attempts to delete a grouped rs should fail miserably |
189 | throws_ok ( |
190 | sub { $sub_rs->search ({}, { distinct => 1 })->delete }, |
191 | qr/attempted a delete operation on a resultset which does group_by/, |
192 | 'Grouped rs update/delete not allowed', |
193 | ); |
194 | |
195 | # grouping on PKs only should pass |
af668ad6 |
196 | $sub_rs->search ( |
197 | {}, |
198 | { |
be64931c |
199 | group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works |
af668ad6 |
200 | }, |
201 | )->update ({ pilot_sequence => \ 'pilot_sequence + 1' }); |
ff1234ad |
202 | |
203 | is_deeply ( |
204 | [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' }) |
8273e845 |
205 | ->get_column ('pilot_sequence')->all |
ff1234ad |
206 | ], |
207 | [qw/11 21 30 40/], |
208 | 'Only two rows incremented', |
209 | ); |
210 | |
af668ad6 |
211 | # also make sure weird scalarref usage works (RT#51409) |
212 | $tkfks->search ( |
213 | \ 'pilot_sequence BETWEEN 11 AND 21', |
214 | )->update ({ pilot_sequence => \ 'pilot_sequence + 1' }); |
215 | |
216 | is_deeply ( |
217 | [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' }) |
8273e845 |
218 | ->get_column ('pilot_sequence')->all |
af668ad6 |
219 | ], |
220 | [qw/12 22 30 40/], |
221 | 'Only two rows incremented (where => scalarref works)', |
222 | ); |
223 | |
59ac6523 |
224 | { |
225 | my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search ( |
226 | { |
227 | -or => [ |
228 | { 'me.pilot_sequence' => 12 }, |
229 | { 'me.autopilot' => 'b' }, |
230 | ], |
231 | } |
232 | ); |
233 | lives_ok { $rs->update({ autopilot => 'z' }) } |
234 | 'Update with table name qualifier in -or conditions lives'; |
235 | is_deeply ( |
236 | [ $tkfks->search ({ pilot_sequence => [12, 22]}) |
237 | ->get_column ('autopilot')->all |
238 | ], |
239 | [qw/z z/], |
240 | '... and yields the right data', |
241 | ); |
242 | } |
243 | |
244 | |
ff1234ad |
245 | $sub_rs->delete; |
ff1234ad |
246 | is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted'); |
fef47a8e |
247 | |
248 | # make sure limit-only deletion works |
249 | cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left'); |
250 | $tkfks->search ({}, { rows => 1 })->delete; |
251 | is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted'); |
252 | |
887d8da0 |
253 | |
fd8076c8 |
254 | # check with sql-equality, as sqlite will accept most bad sql just fine |
be64931c |
255 | $schema->storage->debugobj ($debugobj); |
887d8da0 |
256 | $schema->storage->debug (1); |
fd8076c8 |
257 | |
258 | { |
259 | my $rs = $schema->resultset('CD')->search( |
260 | { 'me.year' => { '!=' => 2010 } }, |
261 | ); |
262 | |
263 | $rs->search({}, { join => 'liner_notes' })->delete; |
264 | is_same_sql_bind ( |
265 | $sql, |
266 | \@bind, |
267 | 'DELETE FROM cd WHERE ( year != ? )', |
268 | ["'2010'"], |
269 | 'Non-restricting multijoins properly thrown out' |
270 | ); |
271 | |
272 | $rs->search({}, { prefetch => 'liner_notes' })->delete; |
273 | is_same_sql_bind ( |
274 | $sql, |
275 | \@bind, |
276 | 'DELETE FROM cd WHERE ( year != ? )', |
277 | ["'2010'"], |
278 | 'Non-restricting multiprefetch thrown out' |
279 | ); |
280 | |
281 | $rs->search({}, { prefetch => 'artist' })->delete; |
282 | is_same_sql_bind ( |
283 | $sql, |
284 | \@bind, |
285 | 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )', |
286 | ["'2010'"], |
287 | 'Restricting prefetch left in, selector thrown out' |
288 | ); |
289 | |
554f3621 |
290 | # switch artist and cd to fully qualified table names |
291 | # make sure nothing is stripped out |
292 | my $cd_rsrc = $schema->source('CD'); |
293 | $cd_rsrc->name('main.cd'); |
294 | $cd_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0 |
295 | for $cd_rsrc->relationships; |
296 | |
297 | my $art_rsrc = $schema->source('Artist'); |
298 | $art_rsrc->name(\'main.artist'); |
299 | $art_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0 |
300 | for $art_rsrc->relationships; |
301 | |
302 | $rs->delete; |
fd8076c8 |
303 | is_same_sql_bind ( |
304 | $sql, |
305 | \@bind, |
554f3621 |
306 | 'DELETE FROM main.cd WHERE ( year != ? )', |
fd8076c8 |
307 | ["'2010'"], |
554f3621 |
308 | 'delete with fully qualified table name' |
309 | ); |
310 | |
311 | $rs->create({ title => 'foo', artist => 1, year => 2000 }); |
312 | $rs->delete_all; |
313 | is_same_sql_bind ( |
314 | $sql, |
315 | \@bind, |
316 | 'DELETE FROM main.cd WHERE ( cdid = ? )', |
317 | ["'1'"], |
318 | 'delete_all with fully qualified table name' |
319 | ); |
320 | |
321 | $rs->create({ cdid => 42, title => 'foo', artist => 2, year => 2000 }); |
322 | $rs->find(42)->delete; |
323 | is_same_sql_bind ( |
324 | $sql, |
325 | \@bind, |
326 | 'DELETE FROM main.cd WHERE ( cdid = ? )', |
327 | ["'42'"], |
328 | 'delete of object from table with fully qualified name' |
329 | ); |
330 | |
331 | $rs->create({ cdid => 42, title => 'foo', artist => 2, year => 2000 }); |
332 | $rs->find(42)->related_resultset('artist')->delete; |
333 | is_same_sql_bind ( |
334 | $sql, |
335 | \@bind, |
336 | 'DELETE FROM main.artist WHERE ( artistid IN ( SELECT me.artistid FROM main.artist me WHERE ( me.artistid = ? ) ) )', |
337 | ["'2'"], |
338 | 'delete of related object from scalarref fully qualified named table', |
339 | ); |
340 | |
341 | $schema->resultset('Artist')->find(3)->related_resultset('cds')->delete; |
342 | is_same_sql_bind ( |
343 | $sql, |
344 | \@bind, |
345 | 'DELETE FROM main.cd WHERE ( artist = ? )', |
346 | ["'3'"], |
347 | 'delete of related object from fully qualified named table', |
fd8076c8 |
348 | ); |
349 | |
554f3621 |
350 | $schema->resultset('Artist')->find(3)->cds_unordered->delete; |
fd8076c8 |
351 | is_same_sql_bind ( |
352 | $sql, |
353 | \@bind, |
554f3621 |
354 | 'DELETE FROM main.cd WHERE ( artist = ? )', |
355 | ["'3'"], |
356 | 'delete of related object from fully qualified named table via relaccessor', |
357 | ); |
358 | |
359 | $rs->search({}, { prefetch => 'artist' })->delete; |
360 | is_same_sql_bind ( |
361 | $sql, |
362 | \@bind, |
363 | 'DELETE FROM main.cd WHERE ( cdid IN ( SELECT me.cdid FROM main.cd me JOIN main.artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )', |
fd8076c8 |
364 | ["'2010'"], |
365 | 'delete with fully qualified table name and subquery correct' |
366 | ); |
367 | |
31073ac7 |
368 | # check that as_subselect_rs works ok |
369 | # inner query is untouched, then a selector |
370 | # and an IN condition |
371 | $schema->resultset('CD')->search({ |
372 | 'me.cdid' => 1, |
373 | 'artist.name' => 'partytimecity', |
374 | }, { |
375 | join => 'artist', |
376 | })->as_subselect_rs->delete; |
377 | |
378 | is_same_sql_bind ( |
379 | $sql, |
380 | \@bind, |
381 | ' |
554f3621 |
382 | DELETE FROM main.cd |
31073ac7 |
383 | WHERE ( |
384 | cdid IN ( |
385 | SELECT me.cdid |
386 | FROM ( |
387 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track |
554f3621 |
388 | FROM main.cd me |
389 | JOIN main.artist artist ON artist.artistid = me.artist |
31073ac7 |
390 | WHERE artist.name = ? AND me.cdid = ? |
391 | ) me |
392 | ) |
393 | ) |
394 | ', |
395 | ["'partytimecity'", "'1'"], |
396 | 'Delete from as_subselect_rs works correctly' |
397 | ); |
fd8076c8 |
398 | } |
887d8da0 |
399 | |
47980c14 |
400 | $schema->storage->debugobj ($orig_debugobj); |
401 | $schema->storage->debug ($orig_debug); |
402 | |
fef47a8e |
403 | done_testing; |