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