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 eval { $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);
118 SET read_count = read_count + 1
120 (foo, bar, hello, goodbye) IN (
121 SELECT me.foo, me.bar, me.hello, me.goodbye
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 != ?
137 'Correct update-SQL with multicolumn in support',
141 # Make sure multicolumn in or the equivalent functions correctly
144 my $sub_rs = $tkfks->search (
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/ },
150 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
154 is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
156 # attempts to delete a grouped rs should fail miserably
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',
163 # grouping on PKs only should pass
167 group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works
169 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
172 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
173 ->get_column ('pilot_sequence')->all
176 'Only two rows incremented',
179 # also make sure weird scalarref usage works (RT#51409)
181 \ 'pilot_sequence BETWEEN 11 AND 21',
182 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
185 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
186 ->get_column ('pilot_sequence')->all
189 'Only two rows incremented (where => scalarref works)',
193 my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
196 { 'me.pilot_sequence' => 12 },
197 { 'me.autopilot' => 'b' },
201 lives_ok { $rs->update({ autopilot => 'z' }) }
202 'Update with table name qualifier in -or conditions lives';
204 [ $tkfks->search ({ pilot_sequence => [12, 22]})
205 ->get_column ('autopilot')->all
208 '... and yields the right data',
214 is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
216 # make sure limit-only deletion works
217 cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
218 $tkfks->search ({}, { rows => 1 })->delete;
219 is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
222 # check with sql-equality, as sqlite will accept most bad sql just fine
223 $schema->storage->debugobj ($debugobj);
224 $schema->storage->debug (1);
227 my $rs = $schema->resultset('CD')->search(
228 { 'me.year' => { '!=' => 2010 } },
231 $rs->search({}, { join => 'liner_notes' })->delete;
235 'DELETE FROM cd WHERE ( year != ? )',
237 'Non-restricting multijoins properly thrown out'
240 $rs->search({}, { prefetch => 'liner_notes' })->delete;
244 'DELETE FROM cd WHERE ( year != ? )',
246 'Non-restricting multiprefetch thrown out'
249 $rs->search({}, { prefetch => 'artist' })->delete;
253 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
255 'Restricting prefetch left in, selector thrown out'
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 };
264 'DELETE FROM schema_qualified.cd WHERE ( year != ? )',
266 'delete with fully qualified table name and subquery correct'
269 eval { $rs->search({}, { prefetch => 'artist' })->delete };
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 != ? ) ) )',
275 'delete with fully qualified table name and subquery correct'
278 $rs->result_source->name('cd');
281 $schema->storage->debugobj ($orig_debugobj);
282 $schema->storage->debug ($orig_debug);