Commit | Line | Data |
0542ec57 |
1 | use strict; |
2 | use warnings; |
1b5ddf23 |
3 | |
0542ec57 |
4 | use Test::More; |
1b5ddf23 |
5 | use Test::Exception; |
6 | use Math::BigInt; |
0542ec57 |
7 | |
8 | use lib qw(t/lib); |
9 | use DBICTest; |
10 | use DBIC::SqlMakerTest; |
fcb7fcbb |
11 | use DBIx::Class::SQLMaker::LimitDialects; |
12 | |
13 | my ($ROWS, $OFFSET) = ( |
14 | DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype, |
15 | DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype, |
16 | ); |
0542ec57 |
17 | |
18 | my $schema = DBICTest->init_schema(); |
19 | |
0542ec57 |
20 | my $rs = $schema->resultset('CD')->search({ -and => [ |
0e773352 |
21 | 'me.artist' => { '!=', '666' }, |
22 | 'me.artist' => { '!=', \[ '?', [ _ne => 'bar' ] ] }, |
0542ec57 |
23 | ]}); |
24 | |
25 | # bogus sql query to make sure bind composition happens properly |
26 | my $complex_rs = $rs->search({}, { |
27 | '+columns' => { cnt => $rs->count_rs->as_query }, |
28 | '+select' => \[ 'me.artist + ?', [ _add => 1 ] ], # free select |
29 | group_by => ['me.cdid', \[ 'me.artist - ?', [ _sub => 2 ] ] ], |
30 | having => \[ 'me.artist < ?', [ _lt => 3 ] ], |
31 | order_by => \[ 'me.artist * ? ', [ _mu => 4 ] ], |
32 | rows => 1, |
33 | page => 3, |
34 | }); |
35 | |
36 | for (1,2) { |
37 | is_same_sql_bind ( |
38 | $complex_rs->as_query, |
39 | '( |
40 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, |
41 | (SELECT COUNT( * ) FROM cd me WHERE me.artist != ? AND me.artist != ?), |
42 | me.artist + ? |
43 | FROM cd me |
44 | WHERE me.artist != ? AND me.artist != ? |
45 | GROUP BY me.cdid, me.artist - ? |
46 | HAVING me.artist < ? |
47 | ORDER BY me.artist * ? |
fcb7fcbb |
48 | LIMIT ? OFFSET ? |
0542ec57 |
49 | )', |
50 | [ |
fcb7fcbb |
51 | [ { sqlt_datatype => 'integer', dbic_colname => 'me.artist' } => 666 ], |
0e773352 |
52 | [ { dbic_colname => '_ne' } => 'bar' ], |
53 | [ { dbic_colname => '_add' } => 1 ], |
fcb7fcbb |
54 | [ { sqlt_datatype => 'integer', dbic_colname => 'me.artist' } => 666 ], |
0e773352 |
55 | [ { dbic_colname => '_ne' } => 'bar' ], |
56 | [ { dbic_colname => '_sub' } => 2 ], |
57 | [ { dbic_colname => '_lt' } => 3 ], |
58 | [ { dbic_colname => '_mu' } => 4 ], |
fcb7fcbb |
59 | [ $ROWS => 1 ], |
60 | [ $OFFSET => 2 ], |
0542ec57 |
61 | ], |
62 | 'Correct crazy sql', |
63 | ); |
64 | } |
65 | |
66 | # see if we get anything back at all |
67 | isa_ok ($complex_rs->next, 'DBIx::Class::Row'); |
68 | |
1b5ddf23 |
69 | # Make sure that the bind shorthand syntax translation is accurate (and doesn't error) |
70 | shorthand_check( |
71 | [ _sub => 2 ], |
72 | [ { dbic_colname => '_sub' } => 2 ], |
73 | '[ $name => $val ] === [ { dbic_colname => $name }, $val ]', |
74 | ); |
75 | shorthand_check( |
76 | [ artist => 2 ], |
77 | [ { dbic_colname => 'artist', sqlt_datatype => 'integer' } => 2 ], |
78 | 'resolution of known column during [ $name => $val ] === [ { dbic_colname => $name }, $val ]', |
79 | ); |
80 | shorthand_check( |
81 | [ \ 'number' => 2 ], |
82 | [ { sqlt_datatype => 'number' } => 2 ], |
83 | '[ \$dt => $val ] === [ { sqlt_datatype => $dt }, $val ]', |
84 | ); |
85 | shorthand_check( |
86 | [ {} => 2 ], |
87 | [ {} => 2 ], |
88 | '[ {} => $val ] === [ {}, $val ]', |
89 | ); |
90 | shorthand_check( |
91 | [ undef, 2 ], |
92 | [ {} => 2 ], |
93 | '[ undef, $val ] === [ {}, $val ]', |
94 | ); |
95 | shorthand_check( |
96 | 2, |
97 | [ {} => 2 ], |
98 | '$val === [ {}, $val ]', |
99 | ); |
100 | |
101 | shorthand_check( |
102 | Math::BigInt->new(42), |
103 | [ {} => Math::BigInt->new(42) ], |
104 | 'stringifyable $object === [ {}, $object ]', |
105 | ); |
106 | |
107 | throws_ok { |
108 | shorthand_check( |
109 | [ 2 ], |
110 | [], |
111 | ) |
112 | } qr !You must supply a datatype/bindtype .+ for non-scalar value \Q[ 2 ]!, |
113 | 'exception on bare array bindvalue'; |
114 | |
115 | throws_ok { |
116 | shorthand_check( |
117 | [ {} => [ 2 ] ], |
118 | [], |
119 | ) |
120 | } qr !You must supply a datatype/bindtype .+ for non-scalar value \Q[ 2 ]!, |
121 | 'exception on untyped array bindvalue'; |
122 | |
123 | throws_ok { |
124 | shorthand_check( |
125 | [ {}, 2, 3 ], |
126 | [], |
127 | ) |
128 | } qr !You must supply a datatype/bindtype .+ for non-scalar value \[ 'HASH\(\w+\)', 2, 3 \]!, |
129 | 'exception on bare multielement array bindvalue'; |
130 | |
131 | throws_ok { |
132 | shorthand_check( |
133 | bless( {}, 'Foo'), |
134 | [], |
135 | ) |
136 | } qr !You must supply a datatype/bindtype .+ for non-scalar value \Qbless( {}, 'Foo' )!, |
137 | 'exception on bare object'; |
138 | |
139 | throws_ok { |
140 | shorthand_check( |
141 | [ {}, bless( {}, 'Foo') ], |
142 | [], |
143 | ) |
144 | } qr !You must supply a datatype/bindtype .+ for non-scalar value \Qbless( {}, 'Foo' )!, |
145 | 'exception on untyped object'; |
146 | |
147 | |
148 | sub shorthand_check { |
149 | my ($bind_shorthand, $bind_expected, $testname) = @_; |
150 | |
151 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
152 | |
153 | is_same_sql_bind ( |
154 | $schema->resultset('CD')->search({}, { |
155 | columns => [qw(cdid artist)], |
156 | group_by => ['cdid', \[ 'artist - ?', $bind_shorthand ] ], |
157 | })->as_query, |
158 | '( |
159 | SELECT me.cdid, me.artist |
160 | FROM cd me |
161 | GROUP BY cdid, artist - ? |
162 | )', |
163 | [ $bind_expected ], |
164 | $testname||(), |
165 | ); |
166 | } |
167 | |
168 | undef $schema; |
169 | |
0542ec57 |
170 | done_testing; |