Enabled count sub query test, isn't broken anymore
[dbsrgits/DBIx-Class.git] / t / search / subquery.t
CommitLineData
cbbd7939 1#!/usr/bin/perl
2
3use strict;
69989ea9 4use warnings;
cbbd7939 5
cbbd7939 6use Test::More;
d0a3e4e2 7
cbbd7939 8use lib qw(t/lib);
9use DBICTest;
10use DBIC::SqlMakerTest;
11
cbbd7939 12my $schema = DBICTest->init_schema();
13my $art_rs = $schema->resultset('Artist');
14my $cdrs = $schema->resultset('CD');
15
69989ea9 16my @tests = (
17 {
18 rs => $cdrs,
f21e372a 19 search => \[ "title = ? AND year LIKE ?", 'buahaha', '20%' ],
20 attrs => { rows => 5 },
21 sqlbind => \[
0491b597 22 "( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE (title = ? AND year LIKE ?) LIMIT 5)",
f21e372a 23 'buahaha',
24 '20%',
25 ],
26 },
27
28 {
29 rs => $cdrs,
69989ea9 30 search => {
31 artist_id => { 'in' => $art_rs->search({}, { rows => 1 })->get_column( 'id' )->as_query },
32 },
33 sqlbind => \[
0491b597 34 "( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE artist_id IN ( SELECT id FROM artist me LIMIT 1 ) )",
69989ea9 35 ],
36 },
cbbd7939 37
69989ea9 38 {
39 rs => $art_rs,
40 attrs => {
0a62f675 41 'select' => [
7f51e1c8 42 $cdrs->search({}, { rows => 1 })->get_column('id')->as_query,
43 ],
0a62f675 44 },
69989ea9 45 sqlbind => \[
46 "( SELECT (SELECT id FROM cd me LIMIT 1) FROM artist me )",
47 ],
48 },
0a62f675 49
69989ea9 50 {
51 rs => $art_rs,
52 attrs => {
0a62f675 53 '+select' => [
54 $cdrs->search({}, { rows => 1 })->get_column('id')->as_query,
7f51e1c8 55 ],
56 },
69989ea9 57 sqlbind => \[
58 "( SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cd me LIMIT 1) FROM artist me )",
59 ],
60 },
7f51e1c8 61
69989ea9 62 {
63 rs => $cdrs,
64 attrs => {
7f51e1c8 65 alias => 'cd2',
66 from => [
67 { cd2 => $cdrs->search({ id => { '>' => 20 } })->as_query },
68 ],
69 },
69989ea9 70 sqlbind => \[
0491b597 71 "( SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (
72 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE id > ?
73 ) cd2
74 )",
866557f9 75 [ 'id', 20 ]
76 ],
69989ea9 77 },
7f51e1c8 78
a3c8d695 79 {
69989ea9 80 rs => $art_rs,
81 attrs => {
539ffe87 82 from => [
83 { 'me' => 'artist' },
84 [
85 { 'cds' => $cdrs->search({}, { 'select' => [\'me.artist as cds_artist' ]})->as_query },
86 { 'me.artistid' => 'cds_artist' }
87 ]
88 ]
69989ea9 89 },
90 sqlbind => \[
91 "( SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me JOIN (SELECT me.artist as cds_artist FROM cd me) cds ON me.artistid = cds_artist )"
92 ],
93 },
a3c8d695 94
69989ea9 95 {
96 rs => $cdrs,
97 attrs => {
d20198ab 98 alias => 'cd2',
99 from => [
100 { cd2 => $cdrs->search(
101 { id => { '>' => 20 } },
102 {
103 alias => 'cd3',
104 from => [
105 { cd3 => $cdrs->search( { id => { '<' => 40 } } )->as_query }
106 ],
107 }, )->as_query },
108 ],
109 },
69989ea9 110 sqlbind => \[
111 "( SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track
112 FROM
0491b597 113 (SELECT cd3.cdid, cd3.artist, cd3.title, cd3.year, cd3.genreid, cd3.single_track
69989ea9 114 FROM
0491b597 115 (SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
69989ea9 116 FROM cd me WHERE id < ?) cd3
117 WHERE id > ?) cd2
118 )",
119 [ 'id', 40 ],
866557f9 120 [ 'id', 20 ]
121 ],
69989ea9 122 },
d20198ab 123
69989ea9 124 {
125 rs => $cdrs,
126 search => {
127 year => {
128 '=' => $cdrs->search(
129 { artistid => { '=' => \'me.artistid' } },
130 { alias => 'inner' }
131 )->get_column('year')->max_rs->as_query,
132 },
4fa7bc22 133 },
69989ea9 134 sqlbind => \[
135 "( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE year = (SELECT MAX(inner.year) FROM cd inner WHERE artistid = me.artistid) )",
136 ],
137 },
4fa7bc22 138
69989ea9 139 {
140 rs => $cdrs,
141 attrs => {
8996a659 142 alias => 'cd2',
143 from => [
144 { cd2 => $cdrs->search({ title => 'Thriller' })->as_query },
145 ],
146 },
69989ea9 147 sqlbind => \[
0491b597 148 "(SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM (
149 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE title = ?
150 ) cd2
151 )",
828ceb09 152 [ 'title',
153 'Thriller'
154 ]
155 ],
69989ea9 156 },
157);
158
159
160plan tests => @tests * 2;
161
162for my $i (0 .. $#tests) {
163 my $t = $tests[$i];
164 for my $p (1, 2) { # repeat everything twice, make sure we do not clobber search arguments
165 is_same_sql_bind (
166 $t->{rs}->search ($t->{search}, $t->{attrs})->as_query,
167 $t->{sqlbind},
168 sprintf 'Testcase %d, pass %d', $i+1, $p,
169 );
170 }
8996a659 171}