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