9 use DBIC::SqlMakerTest;
11 my $schema = DBICTest->init_schema;
14 my $debugobj = DBIC::DebugObj->new (\$sql, \@bind);
15 my $orig_debugobj = $schema->storage->debugobj;
16 my $orig_debug = $schema->storage->debug;
18 my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
20 my ($fa, $fb, $fc) = $tkfks->related_resultset ('fourkeys')->populate ([
21 [qw/foo bar hello goodbye sensors read_count/],
27 # This is already provided by DBICTest
28 #my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
33 my ($ta, $tb) = $schema->resultset ('TwoKeys')
34 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
37 my $tkfk_cnt = $tkfks->count;
39 my $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 },
45 is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
48 # Make sure the forced group by works (i.e. the joining does not cause double-updates)
51 # create a resultset matching $fa and $fb only
52 my $fks = $schema->resultset ('FourKeys')->search (
54 sensors => { '!=', 'c' },
55 ( map { $_ => [1, 2] } qw/foo bar hello goodbye/ ),
56 }, { join => 'fourkeys_to_twokeys'}
59 is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
61 $schema->storage->debugobj ($debugobj);
62 $schema->storage->debug (1);
63 $fks->update ({ read_count => \ 'read_count + 1' });
64 $schema->storage->debugobj ($orig_debugobj);
65 $schema->storage->debug ($orig_debug);
71 SET read_count = read_count + 1
72 WHERE ( ( ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? ) )
74 [ ("'1'", "'2'") x 4, "'c'" ],
75 'Correct update-SQL with multijoin with pruning',
78 is ($fa->discard_changes->read_count, 11, 'Update ran only once on discard-join resultset');
79 is ($fb->discard_changes->read_count, 21, 'Update ran only once on discard-join resultset');
80 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
82 # make the multi-join stick
83 $fks = $fks->search({ 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } });
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);
95 SET read_count = read_count + 1
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 ) ],
98 'Correct update-SQL with multijoin without pruning',
101 is ($fa->discard_changes->read_count, 12, 'Update ran only once on joined resultset');
102 is ($fb->discard_changes->read_count, 22, 'Update ran only once on joined resultset');
103 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
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);
109 throws_ok { $fks->update ({ read_count => \ 'read_count + 1' }) } # this can't actually execute, we just need the "as_query"
110 qr/\Q DBI Exception:/ or do { $sql = ''; @bind = () };
111 $schema->storage->_use_multicolumn_in (undef);
112 $schema->storage->debugobj ($orig_debugobj);
113 $schema->storage->debug ($orig_debug);
119 SET read_count = read_count + 1
121 (foo, bar, hello, goodbye) IN (
122 SELECT me.foo, me.bar, me.hello, me.goodbye
124 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys ON
125 fourkeys_to_twokeys.f_bar = me.bar
126 AND fourkeys_to_twokeys.f_foo = me.foo
127 AND fourkeys_to_twokeys.f_goodbye = me.goodbye
128 AND fourkeys_to_twokeys.f_hello = me.hello
129 WHERE fourkeys_to_twokeys.pilot_sequence != ? AND ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
138 'Correct update-SQL with multicolumn in support',
142 # Make sure multicolumn in or the equivalent functions correctly
145 my $sub_rs = $tkfks->search (
147 { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
148 { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
151 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
155 is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
157 # attempts to delete a grouped rs should fail miserably
159 sub { $sub_rs->search ({}, { distinct => 1 })->delete },
160 qr/attempted a delete operation on a resultset which does group_by/,
161 'Grouped rs update/delete not allowed',
164 # grouping on PKs only should pass
168 group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works
170 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
173 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
174 ->get_column ('pilot_sequence')->all
177 'Only two rows incremented',
180 # also make sure weird scalarref usage works (RT#51409)
182 \ 'pilot_sequence BETWEEN 11 AND 21',
183 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
186 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
187 ->get_column ('pilot_sequence')->all
190 'Only two rows incremented (where => scalarref works)',
194 my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
197 { 'me.pilot_sequence' => 12 },
198 { 'me.autopilot' => 'b' },
202 lives_ok { $rs->update({ autopilot => 'z' }) }
203 'Update with table name qualifier in -or conditions lives';
205 [ $tkfks->search ({ pilot_sequence => [12, 22]})
206 ->get_column ('autopilot')->all
209 '... and yields the right data',
215 is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
217 # make sure limit-only deletion works
218 cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
219 $tkfks->search ({}, { rows => 1 })->delete;
220 is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
223 # check with sql-equality, as sqlite will accept most bad sql just fine
224 $schema->storage->debugobj ($debugobj);
225 $schema->storage->debug (1);
228 my $rs = $schema->resultset('CD')->search(
229 { 'me.year' => { '!=' => 2010 } },
232 $rs->search({}, { join => 'liner_notes' })->delete;
236 'DELETE FROM cd WHERE ( year != ? )',
238 'Non-restricting multijoins properly thrown out'
241 $rs->search({}, { prefetch => 'liner_notes' })->delete;
245 'DELETE FROM cd WHERE ( year != ? )',
247 'Non-restricting multiprefetch thrown out'
250 $rs->search({}, { prefetch => 'artist' })->delete;
254 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
256 'Restricting prefetch left in, selector thrown out'
259 $rs->result_source->name('schema_qualified.cd');
260 # this is expected to fail - we only want to collect the generated SQL
261 eval { $rs->delete };
265 'DELETE FROM schema_qualified.cd WHERE ( year != ? )',
267 'delete with fully qualified table name and subquery correct'
270 eval { $rs->search({}, { prefetch => 'artist' })->delete };
274 '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 != ? ) ) )',
276 'delete with fully qualified table name and subquery correct'
279 $rs->result_source->name('cd');
282 $schema->storage->debugobj ($orig_debugobj);
283 $schema->storage->debug ($orig_debug);