1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
9 use DBICTest ':DiffSQL';
11 my $schema = DBICTest->init_schema();
13 my $rs = $schema->resultset('CD')->search({},
15 '+select' => \ 'COUNT(*)',
19 lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present');
20 dies_ok(sub { $rs->first->get_column('nonexistent_column') }, 'nonexistant column requests still throw exceptions');
22 $rs = $schema->resultset('CD')->search({},
24 '+select' => [ \ 'COUNT(*)', 'title' ],
25 '+as' => [ 'count', 'addedtitle' ]
28 lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present');
29 lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present');
31 $rs = $schema->resultset('CD')->search({},
33 '+select' => [ \ 'COUNT(*)', 'title' ],
34 '+as' => [ 'count', 'addedtitle' ]
39 '+as' => 'addedtitle2'
42 lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present');
43 lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present');
44 lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present');
47 # test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects)
48 # also shows that outer select attributes are ok (i.e. order_by)
50 # from doesn't seem to be useful without using a scalarref - there were no initial tests >:(
52 my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent
53 cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
55 my $table = $cds->result_source->name;
56 $table = $$table if ref $table eq 'SCALAR';
57 my $subsel = $cds->search ({}, {
58 columns => [qw/cdid title/],
59 from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
62 is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
63 is ($subsel->next->title, $cds->next->title, 'First CD title match');
64 is ($subsel->next->title, $cds->next->title, 'Second CD title match');
67 is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');
71 $rs = $schema->resultset('CD')->search({},
74 'columns' => ['cdid', 'title', 'artist.name'],
80 '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
82 'Use of columns attribute results in proper sql'
86 $rs->first->get_column('cdid')
87 }, 'columns 1st rscolumn present');
90 $rs->first->get_column('title')
91 }, 'columns 2nd rscolumn present');
94 $rs->first->artist->get_column('name')
95 }, 'columns 3rd rscolumn present');
99 $rs = $schema->resultset('CD')->search({},
102 '+columns' => ['cdid', 'title', 'artist.name'],
108 '(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)',
110 'Use of columns attribute results in proper sql'
114 $rs->first->get_column('cdid')
115 }, 'columns 1st rscolumn present');
118 $rs->first->get_column('title')
119 }, 'columns 2nd rscolumn present');
122 $rs->first->artist->get_column('name')
123 }, 'columns 3rd rscolumn present');
126 $rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } },
129 columns => [qw/me.cdid me.title/],
130 '+select' => ['tracks.position'],
131 '+as' => ['track_position'],
133 # get a hashref of CD1 only (the first with a second track)
134 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
145 title => 'Spoonful of bees',
147 'limited prefetch via column works on a multi-relationship',
150 my $sub_rs = $rs->search ({},
152 columns => [qw/artist tracks.trackid/], # columns should not be merged but override $rs columns
153 '+select' => ['tracks.title'],
154 '+as' => ['tracks.title'],
167 'columns/select/as fold properly on sub-searches',
170 # *very* esoteric use-case, yet valid (the "empty" object should not be undef):
171 $rs = $schema->resultset('Artist');
172 $rs->create({ artistid => 69, name => 'Ranetki' });
174 my $relations_or_1_count =
175 $rs->search_related('cds')->count
177 $rs->search({ 'cds.cdid' => undef }, { join => 'cds' })->count
180 my $weird_rs = $rs->search({}, {
181 order_by => { -desc => [ 'me.artistid', 'cds.cdid' ] },
182 columns => [{ cd_title => 'cds.title', cd_year => 'cds.year' }],
186 my $weird_rs_hri = $weird_rs->search({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' });
188 for my $rs ($weird_rs, $weird_rs_hri) {
189 is ($rs->count, $relations_or_1_count, 'count on rhs data injection matches');
192 while (my $r = $rs->next) {
196 is (scalar @all, $relations_or_1_count, 'object count on rhs data injection matches');
198 ( $rs->result_class eq 'DBIx::Class::ResultClass::HashRefInflator'
200 : [ map { +{$_->get_columns} } @all ]
208 cd_title => "Come Be Depressed With Us",
212 cd_title => "Generic Manufactured Singles",
216 cd_title => "Caterwaulin' Blues",
220 cd_title => "Forkful of bees",
224 cd_title => "Spoonful of bees",
228 'Correct data retrieved'
231 is_deeply( [ $rs->all ], \@all, '->all matches' );