8 use DBICTest::Schema::CD;
10 # the default scalarref table name will not work well for this test
11 DBICTest::Schema::CD->table('cd');
16 use DBIC::SqlMakerTest;
18 my $schema = DBICTest->init_schema;
21 my $debugobj = DBIC::DebugObj->new (\$sql, \@bind);
22 my $orig_debugobj = $schema->storage->debugobj;
23 my $orig_debug = $schema->storage->debug;
25 my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
27 my ($fa, $fb, $fc) = $tkfks->related_resultset ('fourkeys')->populate ([
28 [qw/foo bar hello goodbye sensors read_count/],
34 # This is already provided by DBICTest
35 #my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
40 my ($ta, $tb) = $schema->resultset ('TwoKeys')
41 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
44 my $tkfk_cnt = $tkfks->count;
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 },
52 is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
55 # Make sure the forced group by works (i.e. the joining does not cause double-updates)
58 # create a resultset matching $fa and $fb only
59 my $fks = $schema->resultset ('FourKeys')->search (
61 sensors => { '!=', 'c' },
62 ( map { $_ => [1, 2] } qw/foo bar hello goodbye/ ),
63 }, { join => 'fourkeys_to_twokeys'}
66 is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
68 $schema->storage->debugobj ($debugobj);
69 $schema->storage->debug (1);
70 $fks->update ({ read_count => \ 'read_count + 1' });
71 $schema->storage->debugobj ($orig_debugobj);
72 $schema->storage->debug ($orig_debug);
78 SET read_count = read_count + 1
79 WHERE ( ( ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? ) )
81 [ ("'1'", "'2'") x 4, "'c'" ],
82 'Correct update-SQL with multijoin with pruning',
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');
89 # make the multi-join stick
90 $fks = $fks->search({ 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } });
92 $schema->storage->debugobj ($debugobj);
93 $schema->storage->debug (1);
94 $fks->update ({ read_count => \ 'read_count + 1' });
95 $schema->storage->debugobj ($orig_debugobj);
96 $schema->storage->debug ($orig_debug);
102 SET read_count = read_count + 1
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 ) ],
105 'Correct update-SQL with multijoin without pruning',
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');
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);
116 throws_ok { $fks->update ({ read_count => \ 'read_count + 1' }) } # this can't actually execute, we just need the "as_query"
117 qr/\Q DBI Exception:/ or do { $sql = ''; @bind = () };
118 $schema->storage->_use_multicolumn_in (undef);
119 $schema->storage->debugobj ($orig_debugobj);
120 $schema->storage->debug ($orig_debug);
126 SET read_count = read_count + 1
128 (foo, bar, hello, goodbye) IN (
129 SELECT me.foo, me.bar, me.hello, me.goodbye
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 != ?
145 'Correct update-SQL with multicolumn in support',
149 # Make sure multicolumn in or the equivalent functions correctly
152 my $sub_rs = $tkfks->search (
154 { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
155 { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
158 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
162 is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
164 # attempts to delete a grouped rs should fail miserably
166 sub { $sub_rs->search ({}, { distinct => 1 })->delete },
167 qr/attempted a delete operation on a resultset which does group_by/,
168 'Grouped rs update/delete not allowed',
171 # grouping on PKs only should pass
175 group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works
177 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
180 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
181 ->get_column ('pilot_sequence')->all
184 'Only two rows incremented',
187 # also make sure weird scalarref usage works (RT#51409)
189 \ 'pilot_sequence BETWEEN 11 AND 21',
190 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
193 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
194 ->get_column ('pilot_sequence')->all
197 'Only two rows incremented (where => scalarref works)',
201 my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
204 { 'me.pilot_sequence' => 12 },
205 { 'me.autopilot' => 'b' },
209 lives_ok { $rs->update({ autopilot => 'z' }) }
210 'Update with table name qualifier in -or conditions lives';
212 [ $tkfks->search ({ pilot_sequence => [12, 22]})
213 ->get_column ('autopilot')->all
216 '... and yields the right data',
222 is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
224 # make sure limit-only deletion works
225 cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
226 $tkfks->search ({}, { rows => 1 })->delete;
227 is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
230 # check with sql-equality, as sqlite will accept most bad sql just fine
231 $schema->storage->debugobj ($debugobj);
232 $schema->storage->debug (1);
235 my $rs = $schema->resultset('CD')->search(
236 { 'me.year' => { '!=' => 2010 } },
239 $rs->search({}, { join => 'liner_notes' })->delete;
243 'DELETE FROM cd WHERE ( year != ? )',
245 'Non-restricting multijoins properly thrown out'
248 $rs->search({}, { prefetch => 'liner_notes' })->delete;
252 'DELETE FROM cd WHERE ( year != ? )',
254 'Non-restricting multiprefetch thrown out'
257 $rs->search({}, { prefetch => 'artist' })->delete;
261 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
263 'Restricting prefetch left in, selector thrown out'
266 $rs->result_source->name('schema_qualified.cd');
267 # this is expected to fail - we only want to collect the generated SQL
268 eval { $rs->delete };
272 'DELETE FROM schema_qualified.cd WHERE ( year != ? )',
274 'delete with fully qualified table name and subquery correct'
277 # this is expected to fail - we only want to collect the generated SQL
278 eval { $rs->search({}, { prefetch => 'artist' })->delete };
282 '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 != ? ) ) )',
284 'delete with fully qualified table name and subquery correct'
287 $rs->result_source->name('cd');
289 # check that as_subselect_rs works ok
290 # inner query is untouched, then a selector
291 # and an IN condition
292 $schema->resultset('CD')->search({
294 'artist.name' => 'partytimecity',
297 })->as_subselect_rs->delete;
308 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
310 JOIN artist artist ON artist.artistid = me.artist
311 WHERE artist.name = ? AND me.cdid = ?
316 ["'partytimecity'", "'1'"],
317 'Delete from as_subselect_rs works correctly'
321 $schema->storage->debugobj ($orig_debugobj);
322 $schema->storage->debug ($orig_debug);