added tests for RT#63709
[dbsrgits/DBIx-Class.git] / t / sqlmaker / msaccess.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use lib qw(t/lib);
5 use DBICTest ':DiffSQL';
6
7 # the entire point of the subclass is that parenthesis have to be
8 # just right for ACCESS to be happy
9 # globalize for entirety of the test
10 $SQL::Abstract::Test::parenthesis_significant = 1;
11
12 my $schema = DBICTest->init_schema (storage_type => 'DBIx::Class::Storage::DBI::ACCESS', no_deploy => 1, quote_names => 1);
13
14 is_same_sql_bind(
15   $schema->resultset('Artist')->search(
16     {
17       artistid => 1,
18     },
19     {
20       join => [{ cds => 'tracks' }],
21       '+select' => [ 'tracks.title' ],
22       '+as'     => [ 'track_title'  ],
23     }
24   )->as_query,
25   '(
26     SELECT [me].[artistid], [me].[name], [me].[rank], [me].[charfield],
27            [tracks].[title]
28       FROM (
29         (
30           [artist] [me]
31           LEFT JOIN cd [cds]
32             ON [cds].[artist] = [me].[artistid]
33         )
34         LEFT JOIN [track] [tracks]
35           ON [tracks].[cd] = [cds].[cdid]
36       )
37     WHERE ( [artistid] = ? )
38   )',
39   [
40     [{ sqlt_datatype => 'integer', dbic_colname => 'artistid' }
41       => 1 ],
42   ],
43   'correct SQL for two-step left join'
44 );
45
46 is_same_sql_bind(
47   $schema->resultset('Track')->search(
48     {
49       trackid => 1,
50     },
51     {
52       join => [{ cd => 'artist' }],
53       '+select' => [ 'artist.name' ],
54       '+as'     => [ 'artist_name'  ],
55     }
56   )->as_query,
57   '(
58     SELECT [me].[trackid], [me].[cd], [me].[position], [me].[title], [me].[last_updated_on], [me].[last_updated_at],
59            [artist].[name]
60       FROM (
61         (
62           [track] [me]
63           INNER JOIN cd [cd]
64             ON [cd].[cdid] = [me].[cd]
65         )
66         INNER JOIN [artist] [artist]
67           ON [artist].[artistid] = [cd].[artist]
68       )
69     WHERE ( [trackid] = ? )
70   )',
71   [
72     [{ sqlt_datatype => 'integer', dbic_colname => 'trackid' }
73       => 1 ],
74   ],
75   'correct SQL for two-step inner join',
76 );
77
78
79 my $sa = $schema->storage->sql_maker;
80 # the legacy tests assume no quoting - leave things as-is
81 local $sa->{quote_char};
82
83 #  my ($self, $table, $fields, $where, $order, @rest) = @_;
84 my ($sql, @bind) = $sa->select(
85     [
86         { me => "cd" },
87         [
88             { "-join_type" => "LEFT", artist => "artist" },
89             { "artist.artistid" => { -ident => "me.artist" } },
90         ],
91     ],
92     [ 'cd.cdid', 'cd.artist', 'cd.title', 'cd.year', 'artist.artistid', 'artist.name' ],
93     undef,
94     undef
95 );
96 is_same_sql_bind(
97   $sql, \@bind,
98   'SELECT cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM (cd me LEFT JOIN artist artist ON artist.artistid = me.artist)', [],
99   'one-step join parenthesized'
100 );
101
102 ($sql, @bind) = $sa->select(
103     [
104         { me => "cd" },
105         [
106             { "-join_type" => "LEFT", track => "track" },
107             { "track.cd" => { -ident => "me.cdid" } },
108         ],
109         [
110             { artist => "artist" },
111             { "artist.artistid" => { -ident => "me.artist" } },
112         ],
113     ],
114     [ 'track.title', 'cd.cdid', 'cd.artist', 'cd.title', 'cd.year', 'artist.artistid', 'artist.name' ],
115     undef,
116     undef
117 );
118 is_same_sql_bind(
119   $sql, \@bind,
120   'SELECT track.title, cd.cdid, cd.artist, cd.title, cd.year, artist.artistid, artist.name FROM ((cd me LEFT JOIN track track ON track.cd = me.cdid) INNER JOIN artist artist ON artist.artistid = me.artist)', [],
121   'two-step join parenthesized and inner join prepended with INNER'
122 );
123
124 done_testing;