From: ostmann Date: Mon, 10 Jan 2011 10:56:49 +0000 (+0100) Subject: Fix losing order of columns provided in select/as X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=dc81dba3b6a3675dcc003be84fbaad2f0df7d6a2;p=dbsrgits%2FDBIx-Class-Historic.git Fix losing order of columns provided in select/as --- diff --git a/Changes b/Changes index be15c28..b250b33 100644 --- a/Changes +++ b/Changes @@ -2,7 +2,9 @@ Revision history for DBIx::Class * Fixes - Revert default selection to being lazy again (eagerness introduced - in 0.08126) - fixes DBIx::Class::Helper::ResultSet::RemoveColumns + in 0.08125) - fixes DBIx::Class::Helper::ResultSet::RemoveColumns + - Fix losing order of columns provided in select/as (regression from + 0.08125) - Unaliased "dark" selectors no longer throw off prefetch - Fix proper composition of bind values across all possible SQL areas ( group_by => \[ ... ] now works properly ) diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index 979b653..cf95368 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -386,6 +386,8 @@ robkinyon: Rob Kinyon Roman: Roman Filippov +Sadrak: Felix Antonius Wilhelm Ostmann + sc_: Just Another Perl Hacker scotty: Scotty Allen diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 5da7f9a..0793e05 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -526,7 +526,7 @@ sub _normalize_selection { # if balanced - treat as a columns entry $attrs->{"${pref}columns"} = $self->_merge_attr( $attrs->{"${pref}columns"}, - { map { $as->[$_] => $sel->[$_] } ( 0 .. $#$as ) } + [ map { +{ $as->[$_] => $sel->[$_] } } ( 0 .. $#$as ) ] ); } else { @@ -3235,18 +3235,18 @@ sub _resolved_attrs { # disassemble columns my (@sel, @as); - for my $c (@{ - ref $attrs->{columns} eq 'ARRAY' ? $attrs->{columns} : [ $attrs->{columns} || () ] - }) { - if (ref $c eq 'HASH') { - for my $as (keys %$c) { - push @sel, $c->{$as}; - push @as, $as; + if (my $cols = delete $attrs->{columns}) { + for my $c (ref $cols eq 'ARRAY' ? @$cols : $cols) { + if (ref $c eq 'HASH') { + for my $as (keys %$c) { + push @sel, $c->{$as}; + push @as, $as; + } + } + else { + push @sel, $c; + push @as, $c; } - } - else { - push @sel, $c; - push @as, $c; } } diff --git a/t/count/count_rs.t b/t/count/count_rs.t index a5e4098..30ca0ca 100644 --- a/t/count/count_rs.t +++ b/t/count/count_rs.t @@ -133,7 +133,7 @@ my $schema = DBICTest->init_schema(); $crs->as_query, '(SELECT COUNT( * ) FROM ( - SELECT MAX( cds.year ) AS newest_cd_year, me.artistid + SELECT me.artistid, MAX( cds.year ) AS newest_cd_year, FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid GROUP BY me.artistid diff --git a/t/prefetch/grouped.t b/t/prefetch/grouped.t index 2f43e03..d0b8e6c 100644 --- a/t/prefetch/grouped.t +++ b/t/prefetch/grouped.t @@ -160,11 +160,11 @@ for ($cd_rs->all) { is_same_sql_bind ( $most_tracks_rs->as_query, '( - SELECT me.maxtr, me.cdid, me.track_count, + SELECT me.cdid, me.track_count, me.maxtr, tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, liner_notes.liner_id, liner_notes.notes FROM ( - SELECT MAX( tracks.trackid ) AS maxtr, me.cdid, COUNT( tracks.trackid ) AS track_count, + SELECT me.cdid, COUNT( tracks.trackid ) AS track_count, MAX( tracks.trackid ) AS maxtr FROM cd me LEFT JOIN track tracks ON tracks.cd = me.cdid WHERE ( me.cdid IS NOT NULL ) diff --git a/t/search/select_chains.t b/t/search/select_chains.t index 234439d..e039fb7 100644 --- a/t/search/select_chains.t +++ b/t/search/select_chains.t @@ -121,4 +121,20 @@ is_same_sql_bind ( 'Correct chaining before attr resolution' ); +# Test the order of columns +$rs = $schema->resultset('CD')->search ({}, { + 'select' => [ 'me.cdid', 'me.title' ], +}); + +is_same_sql_bind ( + $rs->as_query, + '( SELECT + me.cdid, + me.title + FROM cd me + )', + [], + 'Correct order of selected columns' +); + done_testing;