From: Matt S Trout Date: Fri, 14 Jul 2006 19:51:55 +0000 (+0000) Subject: fixup for RowNum limit syntax with functions X-Git-Tag: v0.07002~75^2~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=eac29141febdcd3fb838c56995c7ac86ee7b010e;p=dbsrgits%2FDBIx-Class.git fixup for RowNum limit syntax with functions --- diff --git a/Changes b/Changes index 73fc93e..6a0c4fc 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for DBIx::Class - fixups to ORDER BY, tweaks to deepen some copies in ResultSet + - fixup for RowNum limit syntax with functions 0.06999_07 2006-07-12 20:58:05 - fix issue with from attr copying introduced in last release diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 3368ac1..4116595 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -39,6 +39,8 @@ sub _find_syntax { sub select { my ($self, $table, $fields, $where, $order, @rest) = @_; $table = $self->_quote($table) unless ref($table); + local $self->{rownum_hack_count} = 1 + if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum'); @rest = (-1) unless defined $rest[0]; die "LIMIT 0 Does Not Compute" if $rest[0] == 0; # and anyway, SQL::Abstract::Limit will cause a barf if we don't first @@ -86,7 +88,12 @@ sub _recurse_fields { return $$fields if $ref eq 'SCALAR'; if ($ref eq 'ARRAY') { - return join(', ', map { $self->_recurse_fields($_) } @$fields); + return join(', ', map { + $self->_recurse_fields($_) + .(exists $self->{rownum_hack_count} + ? ' AS col'.$self->{rownum_hack_count}++ + : '') + } @$fields); } elsif ($ref eq 'HASH') { foreach my $func (keys %$fields) { return $self->_sqlcase($func) @@ -223,14 +230,6 @@ sub _quote { return $self->SUPER::_quote($label); } -sub _RowNum { - my $self = shift; - my $c; - $_[0] =~ s/SELECT (.*?) FROM/ - 'SELECT '.join(', ', map { $_.' AS col'.++$c } split(', ', $1)).' FROM'/e; - $self->SUPER::_RowNum(@_); -} - sub limit_dialect { my $self = shift; $self->{limit_dialect} = shift if @_; diff --git a/t/41orrible.t b/t/41orrible.t index bd391da..e8e22df 100644 --- a/t/41orrible.t +++ b/t/41orrible.t @@ -11,13 +11,13 @@ my $sa = new DBIC::SQL::Abstract; $sa->limit_dialect('RowNum'); is($sa->select('rubbish', - [ 'foo.id', 'bar.id' ], + [ 'foo.id', 'bar.id', \'TO_CHAR(foo.womble, "blah")' ], undef, undef, 1, 3), 'SELECT * FROM ( SELECT A.*, ROWNUM r FROM ( - SELECT foo.id AS col1, bar.id AS col2 FROM rubbish + SELECT foo.id AS col1, bar.id AS col2, TO_CHAR(foo.womble, "blah") AS col3 FROM rubbish ) A WHERE ROWNUM < 5 ) B