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