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