From: Peter Rabbitson Date: Wed, 2 Sep 2009 10:19:11 +0000 (+0000) Subject: First part of mysql insanity X-Git-Tag: v0.08111~31 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aa82ce29dcf525f22133a0c6bc9cd9c611767792;hp=0e4fb877c0d647259a7b7f1f38f9dbe65b31cf6b;p=dbsrgits%2FDBIx-Class.git First part of mysql insanity --- diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index d5041ba..f44ed4f 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -508,15 +508,21 @@ sub _recurse_from { foreach my $j (@join) { my ($to, $on) = @$j; + # check whether a join type exists - my $join_clause = ''; my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to; - if (ref($to_jt) eq 'HASH' and exists($to_jt->{-join_type})) { - $join_clause = ' '.uc($to_jt->{-join_type}).' JOIN '; - } else { - $join_clause = ' JOIN '; + my $join_type; + if (ref($to_jt) eq 'HASH' and defined($to_jt->{-join_type})) { + $join_type = $to_jt->{-join_type}; + $join_type =~ s/^\s+ | \s+$//xg; } - push(@sqlf, $join_clause); + + $join_type ||= $self->_default_jointype; + + my $join_clause = sprintf ('%s JOIN ', + $join_type ? ' ' . uc($join_type) : '' + ); + push @sqlf, $join_clause; if (ref $to eq 'ARRAY') { push(@sqlf, '(', $self->_recurse_from(@$to), ')'); @@ -528,6 +534,8 @@ sub _recurse_from { return join('', @sqlf); } +sub _default_jointype {}; + sub _fold_sqlbind { my ($self, $sqlbind) = @_; diff --git a/t/71mysql.t b/t/71mysql.t index 031529c..c58c142 100644 --- a/t/71mysql.t +++ b/t/71mysql.t @@ -6,6 +6,7 @@ use Test::Exception; use lib qw(t/lib); use DBICTest; use DBI::Const::GetInfoType; +use DBIC::SqlMakerTest; my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; @@ -14,8 +15,6 @@ my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MYSQL_${_}" } qw/DSN USER PASS/}; plan skip_all => 'Set $ENV{DBICTEST_MYSQL_DSN}, _USER and _PASS to run this test' unless ($dsn && $user); -plan tests => 19; - my $schema = DBICTest::Schema->connect($dsn, $user, $pass); my $dbh = $schema->storage->dbh; @@ -153,12 +152,42 @@ SKIP: { my $type_info = $schema->storage->columns_info_for('artist'); is_deeply($type_info, $test_type_info, 'columns_info_for - column data types'); + + } my $cd = $schema->resultset ('CD')->create ({}); my $producer = $schema->resultset ('Producer')->create ({}); lives_ok { $cd->set_producers ([ $producer ]) } 'set_relationship doesnt die'; +{ + my $artist = $schema->resultset('Artist')->next; + my $cd = $schema->resultset('CD')->next; + $cd->set_from_related ('artist', $artist); + $cd->update; + + my $rs = $schema->resultset('CD')->search ({}, { prefetch => 'artist' }); + + lives_ok sub { + my $cd = $rs->next; + is ($cd->artist->name, $artist->name, 'Prefetched artist'); + }, 'join does not throw (mysql 3 test)'; + + # induce a jointype override, make sure it works even if we don't have mysql3 + no warnings qw/redefine/; + local *DBIx::Class::SQLAHacks::MySQL::_default_jointype = sub {'inner'}; + is_same_sql_bind ( + $rs->as_query, + '( + SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, + artist.artistid, artist.name, artist.rank, artist.charfield + FROM cd me + INNER JOIN artist artist ON artist.artistid = me.artist + )', + [], + 'overriden default join type works', + ); +} ## Can we properly deal with the null search problem? ## @@ -190,3 +219,5 @@ NULLINSEARCH: { is $artist => undef => 'Nothing Found!'; } + +done_testing;