From: Peter Rabbitson Date: Wed, 24 Mar 2010 11:11:12 +0000 (+0000) Subject: Straight_join support RT55579 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b8391c875ed35fd7c5c20aed6dcae724d9366a56;hp=b1d8e3fd4c2fc5cbfa48b1fd62318760112d35ab;p=dbsrgits%2FDBIx-Class-Historic.git Straight_join support RT55579 --- diff --git a/Changes b/Changes index ee2c97e..8ca8f3c 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,7 @@ Revision history for DBIx::Class - DBIx::Class::InflateColumn::File entered deprecated state - DBIx::Class::Optional::Dependencies left experimental state - Add req_group_list to Opt::Deps (RT#55211) + - Add support for mysql-specific STRAIGHT_JOIN (RT#55579) - Cascading delete/update are now wrapped in a transaction for atomicity - Fix multiple deficiencies when using MultiCreate with diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index 7c5d783..4da469e 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -509,6 +509,14 @@ sub _table { } } +sub _generate_join_clause { + my ($self, $join_type) = @_; + + return sprintf ('%s JOIN ', + $join_type ? ' ' . uc($join_type) : '' + ); +} + sub _recurse_from { my ($self, $from, @join) = @_; my @sqlf; @@ -527,10 +535,7 @@ sub _recurse_from { $join_type = $self->{_default_jointype} if not defined $join_type; - my $join_clause = sprintf ('%s JOIN ', - $join_type ? ' ' . uc($join_type) : '' - ); - push @sqlf, $join_clause; + push @sqlf, $self->_generate_join_clause( $join_type ); if (ref $to eq 'ARRAY') { push(@sqlf, '(', $self->_recurse_from(@$to), ')'); diff --git a/lib/DBIx/Class/SQLAHacks/MySQL.pm b/lib/DBIx/Class/SQLAHacks/MySQL.pm index 687a793..cc177f1 100644 --- a/lib/DBIx/Class/SQLAHacks/MySQL.pm +++ b/lib/DBIx/Class/SQLAHacks/MySQL.pm @@ -21,4 +21,14 @@ sub insert { return $self->SUPER::insert (@_); } +# Allow STRAIGHT_JOIN's +sub _generate_join_clause { + my ($self, $join_type) = @_; + + if( $join_type && $join_type =~ /^STRAIGHT\z/i ) { + return ' STRAIGHT_JOIN ' + } + + return $self->SUPER::_generate_join_clause( $join_type ); +} 1; diff --git a/lib/DBIx/Class/Storage/DBI/mysql.pm b/lib/DBIx/Class/Storage/DBI/mysql.pm index 486594e..ecdc29a 100644 --- a/lib/DBIx/Class/Storage/DBI/mysql.pm +++ b/lib/DBIx/Class/Storage/DBI/mysql.pm @@ -99,7 +99,12 @@ C<$storage> object into this class. =head1 DESCRIPTION -This class implements MySQL specific bits of L. +This class implements MySQL specific bits of L, +like AutoIncrement column support and savepoints. Also it augments the +SQL maker to support the MySQL-specific C join type, which +you can use by specifying C<< join_type => 'straight' >> in the +L + It also provides a one-stop on-connect macro C which sets session variables such that MySQL behaves more predictably as far as the diff --git a/t/71mysql.t b/t/71mysql.t index b51947c..755bbf6 100644 --- a/t/71mysql.t +++ b/t/71mysql.t @@ -194,6 +194,29 @@ lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die'; ); } +{ + # Test support for straight joins + my $cdsrc = $schema->source('CD'); + my $artrel_info = $cdsrc->relationship_info ('artist'); + $cdsrc->add_relationship( + 'straight_artist', + $artrel_info->{class}, + $artrel_info->{cond}, + { %{$artrel_info->{attrs}}, join_type => 'straight' }, + ); + is_same_sql_bind ( + $cdsrc->resultset->search({}, { prefetch => 'straight_artist' })->as_query, + '( + SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, + straight_artist.artistid, straight_artist.name, straight_artist.rank, straight_artist.charfield + FROM cd me + STRAIGHT_JOIN artist straight_artist ON straight_artist.artistid = me.artist + )', + [], + 'straight joins correctly supported for mysql' + ); +} + ## Can we properly deal with the null search problem? ## ## Only way is to do a SET SQL_AUTO_IS_NULL = 0; on connect