8 use DBIC::SqlMakerTest;
10 my $schema = DBICTest->init_schema();
14 my $rs = $schema->resultset('CD')->search({},
16 '+select' => \ 'COUNT(*)',
20 lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present');
21 dies_ok(sub { $rs->first->get_column('nonexistent_column') }, 'nonexistant column requests still throw exceptions');
23 $rs = $schema->resultset('CD')->search({},
25 '+select' => [ \ 'COUNT(*)', 'title' ],
26 '+as' => [ 'count', 'addedtitle' ]
29 lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present');
30 lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present');
32 # Tests a regression in ResultSetColumn wrt +select
33 $rs = $schema->resultset('CD')->search(undef,
35 '+select' => [ \'COUNT(*) AS year_count' ],
36 order_by => 'year_count'
39 my @counts = $rs->get_column('cdid')->all;
40 ok(scalar(@counts), 'got rows from ->all using +select');
42 $rs = $schema->resultset('CD')->search({},
44 '+select' => [ \ 'COUNT(*)', 'title' ],
45 '+as' => [ 'count', 'addedtitle' ]
50 '+as' => 'addedtitle2'
53 lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present');
54 lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present');
55 lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present');
58 # test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects)
59 # also shows that outer select attributes are ok (i.e. order_by)
61 # from doesn't seem to be useful without using a scalarref - there were no initial tests >:(
63 my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent
64 cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
66 my $table = $cds->result_source->name;
67 my $subsel = $cds->search ({}, {
68 columns => [qw/cdid title/],
69 from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
72 is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
73 is ($subsel->next->title, $cds->next->title, 'First CD title match');
74 is ($subsel->next->title, $cds->next->title, 'Second CD title match');
76 is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');
80 $rs = $schema->resultset('CD')->search({},
83 'columns' => ['cdid', 'title', 'artist.name'],
89 '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
91 'Use of columns attribute results in proper sql'
95 $rs->first->get_column('cdid')
96 }, 'columns 1st rscolumn present');
99 $rs->first->get_column('title')
100 }, 'columns 2nd rscolumn present');
103 $rs->first->artist->get_column('name')
104 }, 'columns 3rd rscolumn present');
108 $rs = $schema->resultset('CD')->search({},
111 '+columns' => ['cdid', 'title', 'artist.name'],
117 '(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)',
119 'Use of columns attribute results in proper sql'
123 $rs->first->get_column('cdid')
124 }, 'columns 1st rscolumn present');
127 $rs->first->get_column('title')
128 }, 'columns 2nd rscolumn present');
131 $rs->first->artist->get_column('name')
132 }, 'columns 3rd rscolumn present');
135 $rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } },
138 columns => [qw/me.cdid me.title/],
139 '+select' => ['tracks.position'],
140 '+as' => ['track_position'],
142 # get a hashref of CD1 only (the first with a second track)
143 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
154 title => 'Spoonful of bees',
156 'limited prefetch via column works on a multi-relationship',
159 my $sub_rs = $rs->search ({},
161 columns => [qw/artist tracks.trackid/], # columns should not be merged but override $rs columns
162 '+select' => ['tracks.title'],
163 '+as' => ['tracks.title'],
178 'columns/select/as fold properly on sub-searches',
182 local $TODO = "Multi-collapsing still doesn't work right - HRI should be getting an arrayref, not an individual hash";
195 'columns/select/as fold properly on sub-searches',