Stop stripping table name qualifier on complex $rs->update/delete
[dbsrgits/DBIx-Class.git] / t / resultset / update_delete.t
CommitLineData
ff1234ad 1use strict;
2use warnings;
3
4use lib qw(t/lib);
5use Test::More;
6use Test::Exception;
7use DBICTest;
887d8da0 8use DBIC::DebugObj;
9use DBIC::SqlMakerTest;
ff1234ad 10
be64931c 11my $schema = DBICTest->init_schema;
12
13my ($sql, @bind);
14my $debugobj = DBIC::DebugObj->new (\$sql, \@bind);
15my $orig_debugobj = $schema->storage->debugobj;
16my $orig_debug = $schema->storage->debug;
ff1234ad 17
18my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
19
fd8076c8 20my ($fa, $fb, $fc) = $tkfks->related_resultset ('fourkeys')->populate ([
ff1234ad 21 [qw/foo bar hello goodbye sensors read_count/],
22 [qw/1 1 1 1 a 10 /],
23 [qw/2 2 2 2 b 20 /],
fd8076c8 24 [qw/1 1 1 2 c 30 /],
ff1234ad 25]);
26
27# This is already provided by DBICTest
28#my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
29# [qw/artist cd /],
30# [qw/1 1 /],
31# [qw/2 2 /],
32#]);
33my ($ta, $tb) = $schema->resultset ('TwoKeys')
34 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
35 ->all;
36
37my $tkfk_cnt = $tkfks->count;
38
39my $non_void_ctx = $tkfks->populate ([
40 { autopilot => 'a', fourkeys => $fa, twokeys => $ta, pilot_sequence => 10 },
41 { autopilot => 'b', fourkeys => $fb, twokeys => $tb, pilot_sequence => 20 },
42 { autopilot => 'x', fourkeys => $fa, twokeys => $tb, pilot_sequence => 30 },
43 { autopilot => 'y', fourkeys => $fb, twokeys => $ta, pilot_sequence => 40 },
44]);
45is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
46
47#
48# Make sure the forced group by works (i.e. the joining does not cause double-updates)
49#
50
51# create a resultset matching $fa and $fb only
fd8076c8 52my $fks = $schema->resultset ('FourKeys')->search (
53 {
54 sensors => { '!=', 'c' },
55 ( map { $_ => [1, 2] } qw/foo bar hello goodbye/ ),
56 }, { join => 'fourkeys_to_twokeys'}
57);
ff1234ad 58
59is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
be64931c 60
61$schema->storage->debugobj ($debugobj);
62$schema->storage->debug (1);
ff1234ad 63$fks->update ({ read_count => \ 'read_count + 1' });
be64931c 64$schema->storage->debugobj ($orig_debugobj);
65$schema->storage->debug ($orig_debug);
ff1234ad 66
be64931c 67is_same_sql_bind (
68 $sql,
69 \@bind,
70 'UPDATE fourkeys
71 SET read_count = read_count + 1
fd8076c8 72 WHERE ( ( ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? ) )
73 ',
74 [ ("'1'", "'2'") x 4, "'c'" ],
75 'Correct update-SQL with multijoin with pruning',
76);
77
78is ($fa->discard_changes->read_count, 11, 'Update ran only once on discard-join resultset');
79is ($fb->discard_changes->read_count, 21, 'Update ran only once on discard-join resultset');
80is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
81
82# make the multi-join stick
83$fks = $fks->search({ 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } });
84
85$schema->storage->debugobj ($debugobj);
86$schema->storage->debug (1);
87$fks->update ({ read_count => \ 'read_count + 1' });
88$schema->storage->debugobj ($orig_debugobj);
89$schema->storage->debug ($orig_debug);
90
91is_same_sql_bind (
92 $sql,
93 \@bind,
94 'UPDATE fourkeys
95 SET read_count = read_count + 1
be64931c 96 WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )',
97 [ map { "'$_'" } ( (1) x 4, (2) x 4 ) ],
fd8076c8 98 'Correct update-SQL with multijoin without pruning',
be64931c 99);
ff1234ad 100
fd8076c8 101is ($fa->discard_changes->read_count, 12, 'Update ran only once on joined resultset');
102is ($fb->discard_changes->read_count, 22, 'Update ran only once on joined resultset');
103is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
be64931c 104
105# try the same sql with forced multicolumn in
106$schema->storage->_use_multicolumn_in (1);
107$schema->storage->debugobj ($debugobj);
108$schema->storage->debug (1);
109eval { $fks->update ({ read_count => \ 'read_count + 1' }) }; # this can't actually execute, we just need the "as_query"
110$schema->storage->_use_multicolumn_in (undef);
111$schema->storage->debugobj ($orig_debugobj);
112$schema->storage->debug ($orig_debug);
113
114is_same_sql_bind (
115 $sql,
116 \@bind,
117 'UPDATE fourkeys
118 SET read_count = read_count + 1
119 WHERE (
120 (foo, bar, hello, goodbye) IN (
121 SELECT me.foo, me.bar, me.hello, me.goodbye
122 FROM fourkeys me
fd8076c8 123 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys ON
124 fourkeys_to_twokeys.f_bar = me.bar
125 AND fourkeys_to_twokeys.f_foo = me.foo
126 AND fourkeys_to_twokeys.f_goodbye = me.goodbye
127 AND fourkeys_to_twokeys.f_hello = me.hello
128 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 129 )
130 )
131 ',
fd8076c8 132 [
133 "'666'",
134 ("'1'", "'2'") x 4,
135 "'c'",
136 ],
be64931c 137 'Correct update-SQL with multicolumn in support',
138);
ff1234ad 139
140#
be64931c 141# Make sure multicolumn in or the equivalent functions correctly
ff1234ad 142#
143
144my $sub_rs = $tkfks->search (
145 [
146 { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
147 { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
148 ],
149 {
150 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
151 },
152);
153
154is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
155
156# attempts to delete a grouped rs should fail miserably
157throws_ok (
158 sub { $sub_rs->search ({}, { distinct => 1 })->delete },
159 qr/attempted a delete operation on a resultset which does group_by/,
160 'Grouped rs update/delete not allowed',
161);
162
163# grouping on PKs only should pass
af668ad6 164$sub_rs->search (
165 {},
166 {
be64931c 167 group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works
af668ad6 168 },
169)->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
ff1234ad 170
171is_deeply (
172 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
8273e845 173 ->get_column ('pilot_sequence')->all
ff1234ad 174 ],
175 [qw/11 21 30 40/],
176 'Only two rows incremented',
177);
178
af668ad6 179# also make sure weird scalarref usage works (RT#51409)
180$tkfks->search (
181 \ 'pilot_sequence BETWEEN 11 AND 21',
182)->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
183
184is_deeply (
185 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
8273e845 186 ->get_column ('pilot_sequence')->all
af668ad6 187 ],
188 [qw/12 22 30 40/],
189 'Only two rows incremented (where => scalarref works)',
190);
191
59ac6523 192{
193 my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
194 {
195 -or => [
196 { 'me.pilot_sequence' => 12 },
197 { 'me.autopilot' => 'b' },
198 ],
199 }
200 );
201 lives_ok { $rs->update({ autopilot => 'z' }) }
202 'Update with table name qualifier in -or conditions lives';
203 is_deeply (
204 [ $tkfks->search ({ pilot_sequence => [12, 22]})
205 ->get_column ('autopilot')->all
206 ],
207 [qw/z z/],
208 '... and yields the right data',
209 );
210}
211
212
ff1234ad 213$sub_rs->delete;
ff1234ad 214is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
fef47a8e 215
216# make sure limit-only deletion works
217cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
218$tkfks->search ({}, { rows => 1 })->delete;
219is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
220
887d8da0 221
fd8076c8 222# check with sql-equality, as sqlite will accept most bad sql just fine
be64931c 223$schema->storage->debugobj ($debugobj);
887d8da0 224$schema->storage->debug (1);
fd8076c8 225
226{
227 my $rs = $schema->resultset('CD')->search(
228 { 'me.year' => { '!=' => 2010 } },
229 );
230
231 $rs->search({}, { join => 'liner_notes' })->delete;
232 is_same_sql_bind (
233 $sql,
234 \@bind,
235 'DELETE FROM cd WHERE ( year != ? )',
236 ["'2010'"],
237 'Non-restricting multijoins properly thrown out'
238 );
239
240 $rs->search({}, { prefetch => 'liner_notes' })->delete;
241 is_same_sql_bind (
242 $sql,
243 \@bind,
244 'DELETE FROM cd WHERE ( year != ? )',
245 ["'2010'"],
246 'Non-restricting multiprefetch thrown out'
247 );
248
249 $rs->search({}, { prefetch => 'artist' })->delete;
250 is_same_sql_bind (
251 $sql,
252 \@bind,
253 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
254 ["'2010'"],
255 'Restricting prefetch left in, selector thrown out'
256 );
257
258 $rs->result_source->name('schema_qualified.cd');
259 # this is expected to fail - we only want to collect the generated SQL
260 eval { $rs->delete };
261 is_same_sql_bind (
262 $sql,
263 \@bind,
264 'DELETE FROM schema_qualified.cd WHERE ( year != ? )',
265 ["'2010'"],
266 'delete with fully qualified table name and subquery correct'
267 );
268
269 eval { $rs->search({}, { prefetch => 'artist' })->delete };
270 is_same_sql_bind (
271 $sql,
272 \@bind,
273 'DELETE FROM schema_qualified.cd WHERE ( cdid IN ( SELECT me.cdid FROM schema_qualified.cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
274 ["'2010'"],
275 'delete with fully qualified table name and subquery correct'
276 );
277
278 $rs->result_source->name('cd');
279}
887d8da0 280
47980c14 281$schema->storage->debugobj ($orig_debugobj);
282$schema->storage->debug ($orig_debug);
283
887d8da0 284
fef47a8e 285done_testing;