Smarter todoification (this doesn't sound like a bad idea for CPAN in general)
[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);
9use DBICTest;
10use DBIC::SqlMakerTest;
fcb7fcbb 11use DBIx::Class::SQLMaker::LimitDialects;
12
13my ($ROWS, $OFFSET) = (
14 DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
15 DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
16);
0542ec57 17
18my $schema = DBICTest->init_schema();
19
0542ec57 20my $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
26my $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
36for (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
67isa_ok ($complex_rs->next, 'DBIx::Class::Row');
68
1b5ddf23 69# Make sure that the bind shorthand syntax translation is accurate (and doesn't error)
70shorthand_check(
71 [ _sub => 2 ],
72 [ { dbic_colname => '_sub' } => 2 ],
73 '[ $name => $val ] === [ { dbic_colname => $name }, $val ]',
74);
75shorthand_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);
80shorthand_check(
81 [ \ 'number' => 2 ],
82 [ { sqlt_datatype => 'number' } => 2 ],
83 '[ \$dt => $val ] === [ { sqlt_datatype => $dt }, $val ]',
84);
85shorthand_check(
86 [ {} => 2 ],
87 [ {} => 2 ],
88 '[ {} => $val ] === [ {}, $val ]',
89);
90shorthand_check(
91 [ undef, 2 ],
92 [ {} => 2 ],
93 '[ undef, $val ] === [ {}, $val ]',
94);
95shorthand_check(
96 2,
97 [ {} => 2 ],
98 '$val === [ {}, $val ]',
99);
100
101shorthand_check(
102 Math::BigInt->new(42),
103 [ {} => Math::BigInt->new(42) ],
104 'stringifyable $object === [ {}, $object ]',
105);
106
107throws_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
115throws_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
123throws_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
131throws_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
139throws_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
148sub 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
168undef $schema;
169
0542ec57 170done_testing;