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