9 use DBIC::SqlMakerTest;
11 my $schema = DBICTest->init_schema();
13 my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
15 my ($fa, $fb) = $tkfks->related_resultset ('fourkeys')->populate ([
16 [qw/foo bar hello goodbye sensors read_count/],
21 # This is already provided by DBICTest
22 #my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
27 my ($ta, $tb) = $schema->resultset ('TwoKeys')
28 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
31 my $tkfk_cnt = $tkfks->count;
33 my $non_void_ctx = $tkfks->populate ([
34 { autopilot => 'a', fourkeys => $fa, twokeys => $ta, pilot_sequence => 10 },
35 { autopilot => 'b', fourkeys => $fb, twokeys => $tb, pilot_sequence => 20 },
36 { autopilot => 'x', fourkeys => $fa, twokeys => $tb, pilot_sequence => 30 },
37 { autopilot => 'y', fourkeys => $fb, twokeys => $ta, pilot_sequence => 40 },
39 is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
42 # Make sure the forced group by works (i.e. the joining does not cause double-updates)
45 # create a resultset matching $fa and $fb only
46 my $fks = $schema->resultset ('FourKeys')
47 ->search ({ map { $_ => [1, 2] } qw/foo bar hello goodbye/}, { join => 'fourkeys_to_twokeys' });
49 is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
50 $fks->update ({ read_count => \ 'read_count + 1' });
51 $_->discard_changes for ($fa, $fb);
53 is ($fa->read_count, 11, 'Update ran only once on joined resultset');
54 is ($fb->read_count, 21, 'Update ran only once on joined resultset');
58 # Make sure multicolumn in or the equivalen functions correctly
61 my $sub_rs = $tkfks->search (
63 { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
64 { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
67 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
71 is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
73 # attempts to delete a grouped rs should fail miserably
75 sub { $sub_rs->search ({}, { distinct => 1 })->delete },
76 qr/attempted a delete operation on a resultset which does group_by/,
77 'Grouped rs update/delete not allowed',
80 # grouping on PKs only should pass
84 group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comaprison works
86 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
89 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
90 ->get_column ('pilot_sequence')->all
93 'Only two rows incremented',
96 # also make sure weird scalarref usage works (RT#51409)
98 \ 'pilot_sequence BETWEEN 11 AND 21',
99 )->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
102 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
103 ->get_column ('pilot_sequence')->all
106 'Only two rows incremented (where => scalarref works)',
110 my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
113 { 'me.pilot_sequence' => 12 },
114 { 'me.autopilot' => 'b' },
118 lives_ok { $rs->update({ autopilot => 'z' }) }
119 'Update with table name qualifier in -or conditions lives';
121 [ $tkfks->search ({ pilot_sequence => [12, 22]})
122 ->get_column ('autopilot')->all
125 '... and yields the right data',
131 is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
133 # make sure limit-only deletion works
134 cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
135 $tkfks->search ({}, { rows => 1 })->delete;
136 is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
139 # Make sure prefetch is properly stripped too
140 # check with sql-equality, as sqlite will accept bad sql just fine
142 my $orig_debugobj = $schema->storage->debugobj;
143 my $orig_debug = $schema->storage->debug;
145 $schema->storage->debugobj (DBIC::DebugObj->new (\$sql, \@bind) );
146 $schema->storage->debug (1);
147 $schema->resultset('CD')->search(
148 { year => { '!=' => 2010 } },
149 { prefetch => 'liner_notes' },
152 $schema->storage->debugobj ($orig_debugobj);
153 $schema->storage->debug ($orig_debug);
158 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me WHERE ( year != ? ) GROUP BY me.cdid ) )',
160 'Update on prefetching resultset strips prefetch correctly'