my $sql_maker = $self->sql_maker;
my ($lquote, $rquote, $sep) = map { quotemeta $_ } ($sql_maker->_quote_chars, $sql_maker->name_sep);
my $own_re = qr/ $lquote \Q$root_alias\E $rquote $sep | \b \Q$root_alias\E $sep /x;
- my @order = @{$attrs->{order_by}};
- my @order_chunks = map { ref $_ eq 'ARRAY' ? $_ : [ $_ ] } $sql_maker->_order_by_chunks (\@order);
- $self->throw_exception ('Order By parsing failed...') if @order != @order_chunks;
- for my $i (0 .. $#order) {
- # skip ourselves, and anything that looks like a literal
- next if $order_chunks[$i][0] =~ $own_re;
- next if (ref $order[$i] and ref $order[$i] ne 'HASH');
-
- my $is_desc = $order_chunks[$i][0] =~ s/\sDESC$//i;
- $order_chunks[$i][0] =~ s/\sASC$//i;
-
- $order[$i] = \[
+ my @order_chunks = map { ref $_ eq 'ARRAY' ? $_ : [ $_ ] } $sql_maker->_order_by_chunks($attrs->{order_by});
+ my @new_order = map { \$_ } @order_chunks;
+ my $inner_columns_info = $self->_resolve_column_info($inner_from);
+
+ # loop through and replace stuff that is not "ours" with a min/max func
+ # everything is a literal at this point, since we are likely properly
+ # quoted and stuff
+ for my $i (0 .. $#new_order) {
+ my $chunk = $order_chunks[$i][0];
+
+ # skip ourselves
+ next if $chunk =~ $own_re;
+
+ my $is_desc = $chunk =~ s/\sDESC$//i;
+ $chunk =~ s/\sASC$//i;
+
+ # maybe our own unqualified column
+ my ($ord_bit) = ($lquote and $sep)
+ ? $chunk =~ /^ $lquote ([^$sep]+) $rquote $/x
+ : $chunk
+ ;
+ next if (
+ $ord_bit
+ and
+ $inner_columns_info->{$ord_bit}
+ and
+ $inner_columns_info->{$ord_bit}{-source_alias} eq $root_alias
+ );
+
+ $new_order[$i] = \[
sprintf(
'%s(%s)%s',
($is_desc ? 'MAX' : 'MIN'),
- $order_chunks[$i][0],
+ $chunk,
($is_desc ? ' DESC' : ''),
),
@ {$order_chunks[$i]} [ 1 .. $#{$order_chunks[$i]} ]
];
}
- $inner_attrs->{order_by} = \@order;
+ $inner_attrs->{order_by} = \@new_order;
+
+ # do not care about leftovers here - it will be all the functions
+ # we just created
($inner_attrs->{group_by}) = $self->_group_over_selection (
$inner_from, $inner_select, $inner_attrs->{order_by}
);
my ($self, $order_by, $sql_maker) = @_;
my $parser = sub {
- my ($sql_maker, $order_by) = @_;
+ my ($sql_maker, $order_by, $orig_quote_chars) = @_;
return scalar $sql_maker->_order_by_chunks ($order_by)
unless wantarray;
+ my ($lq, $rq, $sep) = map { quotemeta($_) } (
+ ($orig_quote_chars ? @$orig_quote_chars : $sql_maker->_quote_chars),
+ $sql_maker->name_sep
+ );
+
my @chunks;
for ($sql_maker->_order_by_chunks ($order_by) ) {
- my $chunk = ref $_ ? $_ : [ $_ ];
+ my $chunk = ref $_ ? [ @$_ ] : [ $_ ];
$chunk->[0] =~ s/\s+ (?: ASC|DESC ) \s* $//ix;
+
+ # order criteria may have come back pre-quoted (literals and whatnot)
+ # this is fragile, but the best we can currently do
+ $chunk->[0] =~ s/^ $lq (.+?) $rq $sep $lq (.+?) $rq $/"$1.$2"/xe
+ or $chunk->[0] =~ s/^ $lq (.+) $rq $/$1/x;
+
push @chunks, $chunk;
}
}
else {
$sql_maker = $self->sql_maker;
+
+ # pass these in to deal with literals coming from
+ # the user or the deep guts of prefetch
+ my $orig_quote_chars = [$sql_maker->_quote_chars];
+
local $sql_maker->{quote_char};
- return $parser->($sql_maker, $order_by);
+ return $parser->($sql_maker, $order_by, $orig_quote_chars);
}
}
return undef;
}
+# this is almost identical to the above, except it accepts only
+# a single rsrc, and will succeed only if the first portion of the order
+# by is stable.
+# returns that portion as a colinfo hashref on success
+sub _main_source_order_by_portion_is_stable {
+ my ($self, $main_rsrc, $order_by, $where) = @_;
+
+ die "Huh... I expect a blessed result_source..."
+ if ref($main_rsrc) eq 'ARRAY';
+
+ my @ord_cols = map
+ { $_->[0] }
+ ( $self->_extract_order_criteria($order_by) )
+ ;
+ return unless @ord_cols;
+
+ my $colinfos = $self->_resolve_column_info($main_rsrc, \@ord_cols);
+ for (0 .. $#ord_cols) {
+ if (
+ ! $colinfos->{$ord_cols[$_]}
+ or
+ $colinfos->{$ord_cols[$_]}{-result_source} != $main_rsrc
+ ) {
+ $#ord_cols = $_ - 1;
+ last;
+ }
+ }
+
+ # we just truncated it above
+ return unless @ord_cols;
+
+ # since all we check here are the start of the order_by belonging to the
+ # top level $rsrc, a present identifying set will mean that the resultset
+ # is ordered by its leftmost table in a stable manner
+ #
+ # single source - safely use both qualified and unqualified name
+ my $order_portion_ci = { map {
+ $colinfos->{$_}{-colname} => $colinfos->{$_},
+ $colinfos->{$_}{-fq_colname} => $colinfos->{$_},
+ } @ord_cols };
+
+ $where = $where ? $self->_resolve_column_info(
+ $main_rsrc, $self->_extract_fixed_condition_columns($where)
+ ) : {};
+
+ return (
+ $main_rsrc->_identifying_column_set({ %$where, %$order_portion_ci })
+ ? $order_portion_ci
+ : undef
+ );
+}
+
# returns an arrayref of column names which *definitely* have som
# sort of non-nullable equality requested in the given condition
# specification. This is used to figure out if a resultset is