From: Dagfinn Ilmari Mannsåker Date: Sun, 24 Mar 2013 16:23:37 +0000 (+0000) Subject: Factor out ORDER BY direction detection/stripping X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cb3e87f5f530f76ddf46924e6ec6bd3cc414ccd3;p=dbsrgits%2FDBIx-Class-Historic.git Factor out ORDER BY direction detection/stripping The slightly unobvious regex is so that it's easier to add support for more clauses (e.g. NULLS FIRST/LAST) later. --- diff --git a/lib/DBIx/Class/SQLMaker.pm b/lib/DBIx/Class/SQLMaker.pm index c0a464c..cac1db0 100644 --- a/lib/DBIx/Class/SQLMaker.pm +++ b/lib/DBIx/Class/SQLMaker.pm @@ -319,6 +319,18 @@ sub _order_by { } } +sub _split_order_chunk { + my ($self, $chunk) = @_; + + # strip off sort modifiers, but always succeed, so $1 gets reset + $chunk =~ s/ (?: \s+ (ASC|DESC) )? \s* $//ix; + + return ( + $chunk, + ( $1 and uc($1) eq 'DESC' ) ? 1 : 0, + ); +} + sub _table { # optimized due to hotttnesss # my ($self, $from) = @_; diff --git a/lib/DBIx/Class/SQLMaker/LimitDialects.pm b/lib/DBIx/Class/SQLMaker/LimitDialects.pm index 1f06377..ec9300a 100644 --- a/lib/DBIx/Class/SQLMaker/LimitDialects.pm +++ b/lib/DBIx/Class/SQLMaker/LimitDialects.pm @@ -358,12 +358,10 @@ sub _prep_for_skimming_limit { for my $ch ($self->_order_by_chunks ($inner_order)) { $ch = $ch->[0] if ref $ch eq 'ARRAY'; - my $is_desc = ( - $ch =~ s/\s+ ( ASC|DESC ) \s* $//ix - and - uc($1) eq 'DESC' - ) ? 1 : 0; - push @out_chunks, \join (' ', $ch, $is_desc ? 'ASC' : 'DESC' ); + ($ch, my $is_desc) = $self->_split_order_chunk($ch); + + # !NOTE! outside chunks come in reverse order ( !$is_desc ) + push @out_chunks, { ($is_desc ? '-asc' : '-desc') => \$ch }; } $sq_attrs->{order_by_middle} = $self->_order_by (\@out_chunks); @@ -583,11 +581,7 @@ sub _GenericSubQ { for my $bit (@order_bits) { - my $is_desc = ( - $bit =~ s/\s+ ( ASC|DESC ) \s* $//ix - and - uc($1) eq 'DESC' - ) ? 1 : 0; + ($bit, my $is_desc) = $self->_split_order_chunk($bit); push @is_desc, $is_desc; push @unqualified_names, $usable_order_ci->{$bit}{-colname}; @@ -805,7 +799,7 @@ sub _subqueried_limit_attrs { for my $chunk ($self->_order_by_chunks ($rs_attrs->{order_by})) { # order with bind $chunk = $chunk->[0] if (ref $chunk) eq 'ARRAY'; - $chunk =~ s/\s+ (?: ASC|DESC ) \s* $//ix; + ($chunk) = $self->_split_order_chunk($chunk); next if $in_sel_index->{$chunk}; diff --git a/lib/DBIx/Class/Storage/DBIHacks.pm b/lib/DBIx/Class/Storage/DBIHacks.pm index 58df6a1..a9f3bff 100644 --- a/lib/DBIx/Class/Storage/DBIHacks.pm +++ b/lib/DBIx/Class/Storage/DBIHacks.pm @@ -266,8 +266,7 @@ sub _adjust_select_args_for_complex_prefetch { # skip ourselves next if $chunk =~ $own_re; - my $is_desc = $chunk =~ s/\sDESC$//i; - $chunk =~ s/\sASC$//i; + ($chunk, my $is_desc) = $sql_maker->_split_order_chunk($chunk); # maybe our own unqualified column my $ord_bit = ( @@ -797,7 +796,7 @@ sub _extract_order_criteria { my @chunks; for ($sql_maker->_order_by_chunks ($order_by) ) { my $chunk = ref $_ ? [ @$_ ] : [ $_ ]; - $chunk->[0] =~ s/\s+ (?: ASC|DESC ) \s* $//ix; + ($chunk->[0]) = $sql_maker->_split_order_chunk($chunk->[0]); # order criteria may have come back pre-quoted (literals and whatnot) # this is fragile, but the best we can currently do