8 use DBIC::SqlMakerTest;
10 my $schema = DBICTest->init_schema();
12 my $rs = $schema->resultset('CD')->search({},
14 '+select' => \ 'COUNT(*)',
18 lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present');
19 dies_ok(sub { $rs->first->get_column('nonexistent_column') }, 'nonexistant column requests still throw exceptions');
21 $rs = $schema->resultset('CD')->search({},
23 '+select' => [ \ 'COUNT(*)', 'title' ],
24 '+as' => [ 'count', 'addedtitle' ]
27 lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present');
28 lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present');
30 $rs = $schema->resultset('CD')->search({},
32 '+select' => [ \ 'COUNT(*)', 'title' ],
33 '+as' => [ 'count', 'addedtitle' ]
38 '+as' => 'addedtitle2'
41 lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present');
42 lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present');
43 lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present');
46 # test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects)
47 # also shows that outer select attributes are ok (i.e. order_by)
49 # from doesn't seem to be useful without using a scalarref - there were no initial tests >:(
51 my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent
52 cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
54 my $table = $cds->result_source->name;
55 $table = $$table if ref $table eq 'SCALAR';
56 my $subsel = $cds->search ({}, {
57 columns => [qw/cdid title/],
58 from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
61 is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
62 is ($subsel->next->title, $cds->next->title, 'First CD title match');
63 is ($subsel->next->title, $cds->next->title, 'Second CD title match');
65 is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');
69 $rs = $schema->resultset('CD')->search({},
72 'columns' => ['cdid', 'title', 'artist.name'],
78 '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
80 'Use of columns attribute results in proper sql'
84 $rs->first->get_column('cdid')
85 }, 'columns 1st rscolumn present');
88 $rs->first->get_column('title')
89 }, 'columns 2nd rscolumn present');
92 $rs->first->artist->get_column('name')
93 }, 'columns 3rd rscolumn present');
97 $rs = $schema->resultset('CD')->search({},
100 '+columns' => ['cdid', 'title', 'artist.name'],
106 '(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)',
108 'Use of columns attribute results in proper sql'
112 $rs->first->get_column('cdid')
113 }, 'columns 1st rscolumn present');
116 $rs->first->get_column('title')
117 }, 'columns 2nd rscolumn present');
120 $rs->first->artist->get_column('name')
121 }, 'columns 3rd rscolumn present');
124 $rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } },
127 columns => [qw/me.cdid me.title/],
128 '+select' => ['tracks.position'],
129 '+as' => ['track_position'],
131 # get a hashref of CD1 only (the first with a second track)
132 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
143 title => 'Spoonful of bees',
145 'limited prefetch via column works on a multi-relationship',
148 my $sub_rs = $rs->search ({},
150 columns => [qw/artist tracks.trackid/], # columns should not be merged but override $rs columns
151 '+select' => ['tracks.title'],
152 '+as' => ['tracks.title'],
165 'columns/select/as fold properly on sub-searches',
168 # *very* esoteric use-case, yet valid (the "empty" object should not be undef):
169 $rs = $schema->resultset('Artist');
170 $rs->create({ artistid => 69, name => 'Ranetki' });
172 my $relations_or_1_count =
173 $rs->search_related('cds')->count
175 $rs->search({ 'cds.cdid' => undef }, { join => 'cds' })->count
178 my $weird_rs = $rs->search({}, {
179 order_by => { -desc => [ 'me.artistid', 'cds.cdid' ] },
180 columns => [{ cd_title => 'cds.title', cd_year => 'cds.year' }],
184 my $weird_rs_hri = $weird_rs->search({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
186 for my $rs ($weird_rs, $weird_rs_hri) {
187 is ($rs->count, $relations_or_1_count, 'count on rhs data injection matches');
190 while (my $r = $rs->next) {
194 is (scalar @all, $relations_or_1_count, 'object count on rhs data injection matches');
196 ( $rs->result_class eq 'DBIx::Class::ResultClass::HashRefInflator'
198 : [ map { +{$_->get_columns} } @all ]
206 cd_title => "Come Be Depressed With Us",
210 cd_title => "Generic Manufactured Singles",
214 cd_title => "Caterwaulin' Blues",
218 cd_title => "Forkful of bees",
222 cd_title => "Spoonful of bees",
226 'Correct data retrieved'
229 is_deeply( [ $rs->all ], \@all, '->all matches' );