Merge the relationship resolution rework
[dbsrgits/DBIx-Class.git] / t / sqlmaker / bind_transport.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
0542ec57 3use strict;
4use warnings;
1b5ddf23 5
0542ec57 6use Test::More;
1b5ddf23 7use Test::Exception;
8use Math::BigInt;
0542ec57 9
c0329273 10
a5a7bb73 11use DBICTest ':DiffSQL';
fcb7fcbb 12use DBIx::Class::SQLMaker::LimitDialects;
13
14my ($ROWS, $OFFSET) = (
15 DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
16 DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
17);
0542ec57 18
19my $schema = DBICTest->init_schema();
20
cc10d685 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
0542ec57 32my $rs = $schema->resultset('CD')->search({ -and => [
0e773352 33 'me.artist' => { '!=', '666' },
34 'me.artist' => { '!=', \[ '?', [ _ne => 'bar' ] ] },
0542ec57 35]});
36
37# bogus sql query to make sure bind composition happens properly
38my $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
48for (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 * ?
fcb7fcbb 60 LIMIT ? OFFSET ?
0542ec57 61 )',
62 [
fcb7fcbb 63 [ { sqlt_datatype => 'integer', dbic_colname => 'me.artist' } => 666 ],
0e773352 64 [ { dbic_colname => '_ne' } => 'bar' ],
65 [ { dbic_colname => '_add' } => 1 ],
fcb7fcbb 66 [ { sqlt_datatype => 'integer', dbic_colname => 'me.artist' } => 666 ],
0e773352 67 [ { dbic_colname => '_ne' } => 'bar' ],
68 [ { dbic_colname => '_sub' } => 2 ],
69 [ { dbic_colname => '_lt' } => 3 ],
70 [ { dbic_colname => '_mu' } => 4 ],
fcb7fcbb 71 [ $ROWS => 1 ],
72 [ $OFFSET => 2 ],
0542ec57 73 ],
74 'Correct crazy sql',
75 );
76}
77
78# see if we get anything back at all
79isa_ok ($complex_rs->next, 'DBIx::Class::Row');
80
1b5ddf23 81# Make sure that the bind shorthand syntax translation is accurate (and doesn't error)
82shorthand_check(
83 [ _sub => 2 ],
84 [ { dbic_colname => '_sub' } => 2 ],
85 '[ $name => $val ] === [ { dbic_colname => $name }, $val ]',
86);
87shorthand_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);
92shorthand_check(
93 [ \ 'number' => 2 ],
94 [ { sqlt_datatype => 'number' } => 2 ],
95 '[ \$dt => $val ] === [ { sqlt_datatype => $dt }, $val ]',
96);
97shorthand_check(
98 [ {} => 2 ],
99 [ {} => 2 ],
100 '[ {} => $val ] === [ {}, $val ]',
101);
102shorthand_check(
103 [ undef, 2 ],
104 [ {} => 2 ],
105 '[ undef, $val ] === [ {}, $val ]',
106);
107shorthand_check(
108 2,
109 [ {} => 2 ],
110 '$val === [ {}, $val ]',
111);
112
113shorthand_check(
114 Math::BigInt->new(42),
115 [ {} => Math::BigInt->new(42) ],
116 'stringifyable $object === [ {}, $object ]',
117);
118
0e364749 119shorthand_check(
1b5ddf23 120 [ 2 ],
0e364749 121 [ {} => [ 2 ] ],
122);
1b5ddf23 123
0e364749 124shorthand_check(
1b5ddf23 125 [ {} => [ 2 ] ],
0e364749 126 [ {} => [ 2 ] ],
127);
1b5ddf23 128
0e364749 129shorthand_check(
1b5ddf23 130 [ {}, 2, 3 ],
0e364749 131 [ {} => [ {}, 2, 3 ] ],
132);
1b5ddf23 133
0e364749 134shorthand_check(
1b5ddf23 135 bless( {}, 'Foo'),
0e364749 136 [ {} => bless( {}, 'Foo') ],
137);
1b5ddf23 138
0e364749 139shorthand_check(
140 [ {}, bless( {}, 'Foo') ],
1b5ddf23 141 [ {}, bless( {}, 'Foo') ],
0e364749 142);
1b5ddf23 143
144
145sub 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
165undef $schema;
166
0542ec57 167done_testing;