Revision f174fe7f
Von Sven Schöling vor fast 14 Jahren hinzugefügt
SL/DB/Helpers/Sorted.pm | ||
---|---|---|
11 | 11 |
sub make_sort_string { |
12 | 12 |
my ($class, %params) = @_; |
13 | 13 |
|
14 |
_make_sort_spec($class) unless %sort_spec;
|
|
14 |
my $sort_spec = _get_sort_spec($class);
|
|
15 | 15 |
|
16 |
my $sort_dir = defined($params{sort_dir}) ? $params{sort_dir} * 1 : $sort_spec{default}->[1]; |
|
16 |
my $sort_dir = defined($params{sort_dir}) ? $params{sort_dir} * 1 : $sort_spec->{default}->[1];
|
|
17 | 17 |
my $sort_dir_str = $sort_dir ? 'ASC' : 'DESC'; |
18 | 18 |
|
19 | 19 |
my $sort_by = $params{sort_by}; |
20 |
$sort_by = $sort_spec{default}->[0] unless $sort_spec{columns}->{$sort_by};
|
|
20 |
$sort_by = $sort_spec->{default}->[0] unless $sort_spec->{columns}->{$sort_by};
|
|
21 | 21 |
|
22 | 22 |
my $nulls_str = ''; |
23 |
if ($sort_spec{nulls}) { |
|
24 |
$nulls_str = ref($sort_spec{nulls}) ? ($sort_spec{nulls}->{$sort_by} || $sort_spec{nulls}->{default}) : $sort_spec{nulls};
|
|
23 |
if ($sort_spec->{nulls}) {
|
|
24 |
$nulls_str = ref($sort_spec->{nulls}) ? ($sort_spec->{nulls}->{$sort_by} || $sort_spec->{nulls}->{default}) : $sort_spec->{nulls};
|
|
25 | 25 |
$nulls_str = " NULLS ${nulls_str}" if $nulls_str; |
26 | 26 |
} |
27 | 27 |
|
28 |
my $sort_by_str = $sort_spec{columns}->{$sort_by}; |
|
28 |
my $sort_by_str = $sort_spec->{columns}->{$sort_by};
|
|
29 | 29 |
$sort_by_str = [ $sort_by_str ] unless ref($sort_by_str) eq 'ARRAY'; |
30 | 30 |
$sort_by_str = join(', ', map { "${_} ${sort_dir_str}${nulls_str}" } @{ $sort_by_str }); |
31 | 31 |
|
... | ... | |
39 | 39 |
return $class->get_all(sort_by => $sort_str, %params); |
40 | 40 |
} |
41 | 41 |
|
42 |
sub _get_sort_spec { |
|
43 |
my ($class) = @_; |
|
44 |
return $sort_spec{$class} ||= _make_sort_spec($class); |
|
45 |
} |
|
46 |
|
|
42 | 47 |
sub _make_sort_spec { |
43 | 48 |
my ($class) = @_; |
44 | 49 |
|
45 |
%sort_spec = $class->_sort_spec if defined &{ "${class}::_sort_spec" }; |
|
50 |
my %sort_spec = $class->_sort_spec if defined &{ "${class}::_sort_spec" };
|
|
46 | 51 |
|
47 | 52 |
my $meta = $class->object_class->meta; |
48 | 53 |
|
... | ... | |
63 | 68 |
map { $sort_spec{columns}->{$_} = "${table}.${_}" } @{ delete($sort_spec{columns}->{SIMPLE}) }; |
64 | 69 |
} |
65 | 70 |
} |
71 |
|
|
72 |
return \%sort_spec; |
|
66 | 73 |
} |
67 | 74 |
|
68 | 75 |
1; |
Auch abrufbar als: Unified diff
SL::DB::Helper::Sorted muss die sort_specs pro Klasse speichern, nicht global für alle.