7 use DBICTest ':DiffSQL';
9 my $schema = DBICTest->init_schema();
11 my $rs = $schema->resultset('CD')->search({},
13 '+select' => \ 'COUNT(*)',
17 lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present');
18 dies_ok(sub { $rs->first->get_column('nonexistent_column') }, 'nonexistant column requests still throw exceptions');
20 $rs = $schema->resultset('CD')->search({},
22 '+select' => [ \ 'COUNT(*)', 'title' ],
23 '+as' => [ 'count', 'addedtitle' ]
26 lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present');
27 lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present');
29 $rs = $schema->resultset('CD')->search({},
31 '+select' => [ \ 'COUNT(*)', 'title' ],
32 '+as' => [ 'count', 'addedtitle' ]
37 '+as' => 'addedtitle2'
40 lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present');
41 lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present');
42 lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present');
45 # test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects)
46 # also shows that outer select attributes are ok (i.e. order_by)
48 # from doesn't seem to be useful without using a scalarref - there were no initial tests >:(
50 my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent
51 cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
53 my $table = $cds->result_source->name;
54 $table = $$table if ref $table eq 'SCALAR';
55 my $subsel = $cds->search ({}, {
56 columns => [qw/cdid title/],
57 from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
60 is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
61 is ($subsel->next->title, $cds->next->title, 'First CD title match');
62 is ($subsel->next->title, $cds->next->title, 'Second CD title match');
64 is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');
68 $rs = $schema->resultset('CD')->search({},
71 'columns' => ['cdid', 'title', 'artist.name'],
77 '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
79 'Use of columns attribute results in proper sql'
83 $rs->first->get_column('cdid')
84 }, 'columns 1st rscolumn present');
87 $rs->first->get_column('title')
88 }, 'columns 2nd rscolumn present');
91 $rs->first->artist->get_column('name')
92 }, 'columns 3rd rscolumn present');
96 $rs = $schema->resultset('CD')->search({},
99 '+columns' => ['cdid', 'title', 'artist.name'],
105 '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
107 'Use of columns attribute results in proper sql'
111 $rs->first->get_column('cdid')
112 }, 'columns 1st rscolumn present');
115 $rs->first->get_column('title')
116 }, 'columns 2nd rscolumn present');
119 $rs->first->artist->get_column('name')
120 }, 'columns 3rd rscolumn present');
123 $rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } },
126 columns => [qw/me.cdid me.title/],
127 '+select' => ['tracks.position'],
128 '+as' => ['track_position'],
130 # get a hashref of CD1 only (the first with a second track)
131 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
142 title => 'Spoonful of bees',
144 'limited prefetch via column works on a multi-relationship',
147 my $sub_rs = $rs->search ({},
149 columns => [qw/artist tracks.trackid/], # columns should not be merged but override $rs columns
150 '+select' => ['tracks.title'],
151 '+as' => ['tracks.title'],
164 'columns/select/as fold properly on sub-searches',
167 # *very* esoteric use-case, yet valid (the "empty" object should not be undef):
168 $rs = $schema->resultset('Artist');
169 $rs->create({ artistid => 69, name => 'Ranetki' });
171 my $relations_or_1_count =
172 $rs->search_related('cds')->count
174 $rs->search({ 'cds.cdid' => undef }, { join => 'cds' })->count
177 my $weird_rs = $rs->search({}, {
178 order_by => { -desc => [ 'me.artistid', 'cds.cdid' ] },
179 columns => [{ cd_title => 'cds.title', cd_year => 'cds.year' }],
183 my $weird_rs_hri = $weird_rs->search({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
185 for my $rs ($weird_rs, $weird_rs_hri) {
186 is ($rs->count, $relations_or_1_count, 'count on rhs data injection matches');
189 while (my $r = $rs->next) {
193 is (scalar @all, $relations_or_1_count, 'object count on rhs data injection matches');
195 ( $rs->result_class eq 'DBIx::Class::ResultClass::HashRefInflator'
197 : [ map { +{$_->get_columns} } @all ]
205 cd_title => "Come Be Depressed With Us",
209 cd_title => "Generic Manufactured Singles",
213 cd_title => "Caterwaulin' Blues",
217 cd_title => "Forkful of bees",
221 cd_title => "Spoonful of bees",
225 'Correct data retrieved'
228 is_deeply( [ $rs->all ], \@all, '->all matches' );