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 # Tests a regression in ResultSetColumn wrt +select
31 $rs = $schema->resultset('CD')->search(undef,
33 '+select' => [ \'COUNT(*) AS year_count' ],
34 order_by => 'year_count'
37 my @counts = $rs->get_column('cdid')->all;
38 ok(scalar(@counts), 'got rows from ->all using +select');
40 $rs = $schema->resultset('CD')->search({},
42 '+select' => [ \ 'COUNT(*)', 'title' ],
43 '+as' => [ 'count', 'addedtitle' ]
48 '+as' => 'addedtitle2'
51 lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present');
52 lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present');
53 lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present');
56 # test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects)
57 # also shows that outer select attributes are ok (i.e. order_by)
59 # from doesn't seem to be useful without using a scalarref - there were no initial tests >:(
61 my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent
62 cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
64 my $table = $cds->result_source->name;
65 $table = $$table if ref $table eq 'SCALAR';
66 my $subsel = $cds->search ({}, {
67 columns => [qw/cdid title/],
68 from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
71 is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
72 is ($subsel->next->title, $cds->next->title, 'First CD title match');
73 is ($subsel->next->title, $cds->next->title, 'Second CD title match');
75 is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');
79 $rs = $schema->resultset('CD')->search({},
82 'columns' => ['cdid', 'title', 'artist.name'],
88 '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
90 'Use of columns attribute results in proper sql'
94 $rs->first->get_column('cdid')
95 }, 'columns 1st rscolumn present');
98 $rs->first->get_column('title')
99 }, 'columns 2nd rscolumn present');
102 $rs->first->artist->get_column('name')
103 }, 'columns 3rd rscolumn present');
107 $rs = $schema->resultset('CD')->search({},
110 '+columns' => ['cdid', 'title', 'artist.name'],
116 '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
118 'Use of columns attribute results in proper sql'
122 $rs->first->get_column('cdid')
123 }, 'columns 1st rscolumn present');
126 $rs->first->get_column('title')
127 }, 'columns 2nd rscolumn present');
130 $rs->first->artist->get_column('name')
131 }, 'columns 3rd rscolumn present');
134 $rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } },
137 columns => [qw/me.cdid me.title/],
138 '+select' => ['tracks.position'],
139 '+as' => ['track_position'],
141 # get a hashref of CD1 only (the first with a second track)
142 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
153 title => 'Spoonful of bees',
155 'limited prefetch via column works on a multi-relationship',
158 my $sub_rs = $rs->search ({},
160 columns => [qw/artist tracks.trackid/], # columns should not be merged but override $rs columns
161 '+select' => ['tracks.title'],
162 '+as' => ['tracks.title'],
176 'columns/select/as fold properly on sub-searches',