Restore backwards compatibility - introduce intent to collapse flag
[dbsrgits/DBIx-Class.git] / t / 76select.t
CommitLineData
218aa968 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
faf79003 8use DBIC::SqlMakerTest;
218aa968 9
10my $schema = DBICTest->init_schema();
11
218aa968 12my $rs = $schema->resultset('CD')->search({},
13 {
14 '+select' => \ 'COUNT(*)',
15 '+as' => 'count'
16 }
17);
18lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present');
19dies_ok(sub { $rs->first->get_column('nonexistent_column') }, 'nonexistant column requests still throw exceptions');
20
21$rs = $schema->resultset('CD')->search({},
22 {
23 '+select' => [ \ 'COUNT(*)', 'title' ],
24 '+as' => [ 'count', 'addedtitle' ]
25 }
26);
27lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present');
28lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present');
29
121068ec 30# Tests a regression in ResultSetColumn wrt +select
9dbe8766 31$rs = $schema->resultset('CD')->search(undef,
121068ec 32 {
33 '+select' => [ \'COUNT(*) AS year_count' ],
34 order_by => 'year_count'
35 }
36);
37my @counts = $rs->get_column('cdid')->all;
38ok(scalar(@counts), 'got rows from ->all using +select');
39
218aa968 40$rs = $schema->resultset('CD')->search({},
41 {
42 '+select' => [ \ 'COUNT(*)', 'title' ],
43 '+as' => [ 'count', 'addedtitle' ]
44 }
45)->search({},
46 {
47 '+select' => 'title',
48 '+as' => 'addedtitle2'
49 }
50);
51lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present');
52lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present');
53lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present');
0eb27426 54
55
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)
58#
59# from doesn't seem to be useful without using a scalarref - there were no initial tests >:(
60#
0eb27426 61my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent
62cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
63
64my $table = $cds->result_source->name;
80625830 65$table = $$table if ref $table eq 'SCALAR';
0eb27426 66my $subsel = $cds->search ({}, {
67 columns => [qw/cdid title/],
68 from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
69});
70
71is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
72is ($subsel->next->title, $cds->next->title, 'First CD title match');
73is ($subsel->next->title, $cds->next->title, 'Second CD title match');
bbdff861 74
75is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');
61d26fae 76
faf79003 77
78
79$rs = $schema->resultset('CD')->search({},
61d26fae 80 {
81 'join' => 'artist',
faf79003 82 'columns' => ['cdid', 'title', 'artist.name'],
61d26fae 83 }
84);
faf79003 85
faf79003 86is_same_sql_bind (
af6aac2d 87 $rs->as_query,
faf79003 88 '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)',
89 [],
90 'Use of columns attribute results in proper sql'
91);
92
93lives_ok(sub {
94 $rs->first->get_column('cdid')
95}, 'columns 1st rscolumn present');
96
97lives_ok(sub {
98 $rs->first->get_column('title')
99}, 'columns 2nd rscolumn present');
100
0ca2f0d1 101lives_ok(sub {
102 $rs->first->artist->get_column('name')
103}, 'columns 3rd rscolumn present');
4c253752 104
105
faf79003 106
107$rs = $schema->resultset('CD')->search({},
108 {
109 'join' => 'artist',
110 '+columns' => ['cdid', 'title', 'artist.name'],
111 }
112);
113
faf79003 114is_same_sql_bind (
af6aac2d 115 $rs->as_query,
faf79003 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)',
117 [],
118 'Use of columns attribute results in proper sql'
119);
120
121lives_ok(sub {
122 $rs->first->get_column('cdid')
123}, 'columns 1st rscolumn present');
124
125lives_ok(sub {
126 $rs->first->get_column('title')
127}, 'columns 2nd rscolumn present');
4c253752 128
0ca2f0d1 129lives_ok(sub {
130 $rs->first->artist->get_column('name')
131}, 'columns 3rd rscolumn present');
b3b13ec9 132
133
134$rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } },
135 {
136 join => 'tracks',
137 columns => [qw/me.cdid me.title/],
138 '+select' => ['tracks.position'],
139 '+as' => ['track_position'],
140
141 # get a hashref of CD1 only (the first with a second track)
142 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
143 order_by => 'cdid',
144 rows => 1,
145 }
146);
147
148is_deeply (
149 $rs->single,
150 {
151 cdid => 1,
152 track_position => 2,
153 title => 'Spoonful of bees',
154 },
155 'limited prefetch via column works on a multi-relationship',
156);
157
158my $sub_rs = $rs->search ({},
159 {
160 columns => [qw/artist tracks.trackid/], # columns should not be merged but override $rs columns
161 '+select' => ['tracks.title'],
162 '+as' => ['tracks.title'],
163 }
164);
165
27ffa6c0 166is_deeply(
b3b13ec9 167 $sub_rs->single,
168 {
27ffa6c0 169 artist => 1,
170 track_position => 2,
9f6555d3 171 tracks => {
172 trackid => 17,
173 title => 'Apiary',
174 },
b3b13ec9 175 },
176 'columns/select/as fold properly on sub-searches',
27ffa6c0 177);
178
9f6555d3 179done_testing;