da1176b002efbb435fcb7ecdab6c1f28baa5a198
[dbsrgits/DBIx-Class.git] / t / sqlmaker / bind_transport.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Math::BigInt;
7
8 local $TODO = 'Temporarily todo-ed for dq2eb';
9
10 use lib qw(t/lib);
11 use DBICTest;
12 use DBIC::SqlMakerTest;
13 use DBIx::Class::SQLMaker::LimitDialects;
14
15 my ($ROWS, $OFFSET) = (
16    DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
17    DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
18 );
19
20 my $schema = DBICTest->init_schema();
21
22 my $rs = $schema->resultset('CD')->search({ -and => [
23   'me.artist' => { '!=', '666' },
24   'me.artist' => { '!=', \[ '?', [ _ne => 'bar' ] ] },
25 ]});
26
27 # bogus sql query to make sure bind composition happens properly
28 my $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
38 for (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 * ?
50       LIMIT ? OFFSET ?
51     )',
52     [
53       [ { sqlt_datatype => 'integer', dbic_colname => 'me.artist' } => 666 ],
54       [ { dbic_colname => '_ne' } => 'bar' ],
55       [ { dbic_colname => '_add' } => 1 ],
56       [ { sqlt_datatype => 'integer', dbic_colname => 'me.artist' } => 666 ],
57       [ { dbic_colname => '_ne' } => 'bar' ],
58       [ { dbic_colname => '_sub' } => 2 ],
59       [ { dbic_colname => '_lt' } => 3 ],
60       [ { dbic_colname => '_mu' } => 4 ],
61       [ $ROWS => 1 ],
62       [ $OFFSET => 2 ],
63     ],
64     'Correct crazy sql',
65   );
66 }
67
68 # see if we get anything back at all
69 isa_ok ($complex_rs->next, 'DBIx::Class::Row');
70
71 # Make sure that the bind shorthand syntax translation is accurate (and doesn't error)
72 shorthand_check(
73   [ _sub => 2 ],
74   [ { dbic_colname => '_sub' } => 2 ],
75   '[ $name => $val ] === [ { dbic_colname => $name }, $val ]',
76 );
77 shorthand_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 );
82 shorthand_check(
83   [ \ 'number' => 2 ],
84   [ { sqlt_datatype => 'number' } => 2 ],
85   '[ \$dt => $val ] === [ { sqlt_datatype => $dt }, $val ]',
86 );
87 shorthand_check(
88   [ {} => 2 ],
89   [ {} => 2 ],
90   '[ {} => $val ] === [ {}, $val ]',
91 );
92 shorthand_check(
93   [ undef, 2 ],
94   [ {} => 2 ],
95   '[ undef, $val ] === [ {}, $val ]',
96 );
97 shorthand_check(
98   2,
99   [ {} => 2 ],
100   '$val === [ {}, $val ]',
101 );
102
103 shorthand_check(
104   Math::BigInt->new(42),
105   [ {} => Math::BigInt->new(42) ],
106   'stringifyable $object === [ {}, $object ]',
107 );
108
109 throws_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
117 throws_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
125 throws_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
133 throws_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
141 throws_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
150 sub 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
170 undef $schema;
171
172 done_testing;