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