Yet another missed sanity check in the relcond resolver
[dbsrgits/DBIx-Class.git] / t / sqlmaker / bind_transport.t
CommitLineData
0542ec57 1use strict;
2use warnings;
1b5ddf23 3
0542ec57 4use Test::More;
1b5ddf23 5use Test::Exception;
6use Math::BigInt;
0542ec57 7
8use lib qw(t/lib);
a5a7bb73 9use DBICTest ':DiffSQL';
fcb7fcbb 10use DBIx::Class::SQLMaker::LimitDialects;
11
12my ($ROWS, $OFFSET) = (
13 DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
14 DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
15);
0542ec57 16
17my $schema = DBICTest->init_schema();
18
0542ec57 19my $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
25my $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
35for (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
66isa_ok ($complex_rs->next, 'DBIx::Class::Row');
67
1b5ddf23 68# Make sure that the bind shorthand syntax translation is accurate (and doesn't error)
69shorthand_check(
70 [ _sub => 2 ],
71 [ { dbic_colname => '_sub' } => 2 ],
72 '[ $name => $val ] === [ { dbic_colname => $name }, $val ]',
73);
74shorthand_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);
79shorthand_check(
80 [ \ 'number' => 2 ],
81 [ { sqlt_datatype => 'number' } => 2 ],
82 '[ \$dt => $val ] === [ { sqlt_datatype => $dt }, $val ]',
83);
84shorthand_check(
85 [ {} => 2 ],
86 [ {} => 2 ],
87 '[ {} => $val ] === [ {}, $val ]',
88);
89shorthand_check(
90 [ undef, 2 ],
91 [ {} => 2 ],
92 '[ undef, $val ] === [ {}, $val ]',
93);
94shorthand_check(
95 2,
96 [ {} => 2 ],
97 '$val === [ {}, $val ]',
98);
99
100shorthand_check(
101 Math::BigInt->new(42),
102 [ {} => Math::BigInt->new(42) ],
103 'stringifyable $object === [ {}, $object ]',
104);
105
106throws_ok {
107 shorthand_check(
108 [ 2 ],
109 [],
110 )
111} qr !You must supply a datatype/bindtype .+ for non-scalar value \Q[ 2 ]!,
112 'exception on bare array bindvalue';
113
114throws_ok {
115 shorthand_check(
116 [ {} => [ 2 ] ],
117 [],
118 )
119} qr !You must supply a datatype/bindtype .+ for non-scalar value \Q[ 2 ]!,
120 'exception on untyped array bindvalue';
121
122throws_ok {
123 shorthand_check(
124 [ {}, 2, 3 ],
125 [],
126 )
127} qr !You must supply a datatype/bindtype .+ for non-scalar value \[ 'HASH\(\w+\)', 2, 3 \]!,
128 'exception on bare multielement array bindvalue';
129
130throws_ok {
131 shorthand_check(
132 bless( {}, 'Foo'),
133 [],
134 )
135} qr !You must supply a datatype/bindtype .+ for non-scalar value \Qbless( {}, 'Foo' )!,
136 'exception on bare object';
137
138throws_ok {
139 shorthand_check(
140 [ {}, bless( {}, 'Foo') ],
141 [],
142 )
143} qr !You must supply a datatype/bindtype .+ for non-scalar value \Qbless( {}, 'Foo' )!,
144 'exception on untyped object';
145
146
147sub shorthand_check {
148 my ($bind_shorthand, $bind_expected, $testname) = @_;
149
150 local $Test::Builder::Level = $Test::Builder::Level + 1;
151
152 is_same_sql_bind (
153 $schema->resultset('CD')->search({}, {
154 columns => [qw(cdid artist)],
155 group_by => ['cdid', \[ 'artist - ?', $bind_shorthand ] ],
156 })->as_query,
157 '(
158 SELECT me.cdid, me.artist
159 FROM cd me
160 GROUP BY cdid, artist - ?
161 )',
162 [ $bind_expected ],
163 $testname||(),
164 );
165}
166
167undef $schema;
168
0542ec57 169done_testing;