From: Rafael Kitover Date: Wed, 8 Jun 2011 12:38:20 +0000 (-0400) Subject: Make Access inner joins 'INNER JOIN' to avoid JOIN syntax errors X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=696ba760d112264ea22e8b20db55987746677b81;p=dbsrgits%2FDBIx-Class-Historic.git Make Access inner joins 'INNER JOIN' to avoid JOIN syntax errors --- diff --git a/Changes b/Changes index 6ddee27..9083f4e 100644 --- a/Changes +++ b/Changes @@ -19,6 +19,7 @@ Revision history for DBIx::Class with new default format expected by DateTime::Format::Sybase) - Fix a bug in update_all() resulting in the first row receiving a different dataset than the subsequent ones + - Accomodate MSAccess supporting only 'INNER JOIN' (not plain 'JOIN') 0.08192 2011-05-10 04:20 (UTC) * Fixes diff --git a/lib/DBIx/Class/SQLMaker/ACCESS.pm b/lib/DBIx/Class/SQLMaker/ACCESS.pm index aec276d..331bf52 100644 --- a/lib/DBIx/Class/SQLMaker/ACCESS.pm +++ b/lib/DBIx/Class/SQLMaker/ACCESS.pm @@ -5,6 +5,16 @@ use strict; use warnings; use base 'DBIx::Class::SQLMaker'; +# inner joins must be prefixed with 'INNER ' +sub new { + my $class = shift; + my $self = $class->next::method(@_); + + $self->{_default_jointype} = 'INNER'; + + return $self; +} + # MSAccess is retarded wrt multiple joins in FROM - it requires a certain # way of parenthesizing each left part before each next right part sub _recurse_from { diff --git a/lib/DBIx/Class/Storage/DBI/ACCESS.pm b/lib/DBIx/Class/Storage/DBI/ACCESS.pm index 35b076e..a6f174e 100644 --- a/lib/DBIx/Class/Storage/DBI/ACCESS.pm +++ b/lib/DBIx/Class/Storage/DBI/ACCESS.pm @@ -5,6 +5,7 @@ use warnings; use base 'DBIx::Class::Storage::DBI::UniqueIdentifier'; use mro 'c3'; +use DBI (); use List::Util 'first'; use namespace::clean; diff --git a/t/751msaccess.t b/t/751msaccess.t index 62012ac..27eeac9 100644 --- a/t/751msaccess.t +++ b/t/751msaccess.t @@ -7,6 +7,8 @@ use Scope::Guard (); use Try::Tiny; use lib qw(t/lib); use DBICTest; +use DBIC::DebugObj (); +use DBIC::SqlMakerTest; DBICTest::Schema->load_classes('ArtistGUID'); @@ -141,7 +143,12 @@ EOF title => 'my track', }); + my ($sql, @bind); + my $joined_track = try { + local $schema->storage->{debug} = 1; + local $schema->storage->{debugobj} = DBIC::DebugObj->new(\$sql, \@bind); + $schema->resultset('Artist')->search({ artistid => $first_artistid, }, { @@ -151,11 +158,52 @@ EOF })->next; } catch { - diag "Could not execute two-step join: $_"; + diag "Could not execute two-step left join: $_"; }; + s/^'//, s/'\z// for @bind; + + is_same_sql_bind( + $sql, + \@bind, + 'SELECT [me].[artistid], [me].[name], [me].[rank], [me].[charfield], [tracks].[title] FROM ( ( [artist] [me] LEFT JOIN cd [cds] ON [cds].[artist] = [me].[artistid] ) LEFT JOIN [track] [tracks] ON [tracks].[cd] = [cds].[cdid] ) WHERE ( [artistid] = ? )', + [1], + 'correct SQL for two-step left join', + ); + is try { $joined_track->get_column('track_title') }, 'my track', - 'two-step join works'; + 'two-step left join works'; + + ($sql, @bind) = (); + + $joined_artist = try { + local $schema->storage->{debug} = 1; + local $schema->storage->{debugobj} = DBIC::DebugObj->new(\$sql, \@bind); + + $schema->resultset('Track')->search({ + trackid => $track->trackid, + }, { + join => [{ cd => 'artist' }], + '+select' => [ 'artist.name' ], + '+as' => [ 'artist_name' ], + })->next; + } + catch { + diag "Could not execute two-step inner join: $_"; + }; + + s/^'//, s/'\z// for @bind; + + is_same_sql_bind( + $sql, + \@bind, + 'SELECT [me].[trackid], [me].[cd], [me].[position], [me].[title], [me].[last_updated_on], [me].[last_updated_at], [artist].[name] FROM ( ( [track] [me] INNER JOIN cd [cd] ON [cd].[cdid] = [me].[cd] ) INNER JOIN [artist] [artist] ON [artist].[artistid] = [cd].[artist] ) WHERE ( [trackid] = ? )', + [$track->trackid], + 'correct SQL for two-step inner join', + ); + + is try { $joined_artist->get_column('artist_name') }, 'foo', + 'two-step inner join works'; # test basic transactions $schema->txn_do(sub { diff --git a/t/sqlmaker/msaccess.t b/t/sqlmaker/msaccess.t index 77e6cd4..6d76f82 100644 --- a/t/sqlmaker/msaccess.t +++ b/t/sqlmaker/msaccess.t @@ -5,7 +5,7 @@ use lib qw(t/lib); use DBICTest; use DBIC::SqlMakerTest; -use DBIx::Class::SQLMaker::ACCESS; +use DBIx::Class::SQLMaker::ACCESS (); my $sa = DBIx::Class::SQLMaker::ACCESS->new; @@ -36,7 +36,7 @@ is_same_sql_bind( { "track.cd" => "me.cdid" }, ], [ - { "-join_type" => "LEFT", artist => "artist" }, + { artist => "artist" }, { "artist.artistid" => "me.artist" }, ], ], @@ -46,8 +46,8 @@ is_same_sql_bind( ); is_same_sql_bind( $sql, \@bind, - '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) LEFT JOIN artist artist ON artist.artistid = me.artist)', [], - 'two-step join parenthesized' + '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)', [], + 'two-step join parenthesized and inner join prepended with INNER' ); done_testing;