Revision b04c9926
Von Moritz Bunkus 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 |
%sort_spec = $class->_sort_spec unless %sort_spec;
|
|
14 |
_make_sort_spec($class) unless %sort_spec;
|
|
15 | 15 |
|
16 | 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'; |
... | ... | |
39 | 39 |
return $class->get_all(sort_by => $sort_str, %params); |
40 | 40 |
} |
41 | 41 |
|
42 |
sub _make_sort_spec { |
|
43 |
my ($class) = @_; |
|
44 |
|
|
45 |
%sort_spec = $class->_sort_spec if defined &{ "${class}::_sort_spec" }; |
|
46 |
|
|
47 |
my $meta = $class->object_class->meta; |
|
48 |
|
|
49 |
if (!$sort_spec{default}) { |
|
50 |
my @primary_keys = $meta->primary_key; |
|
51 |
$sort_spec{default} = [ "" . $primary_keys[0], 0 ]; |
|
52 |
} |
|
53 |
|
|
54 |
$sort_spec{columns} ||= { SIMPLE => [ map { "$_" } $meta->columns ] }; |
|
55 |
|
|
56 |
if ($sort_spec{columns}->{SIMPLE}) { |
|
57 |
my $table = $meta->table; |
|
58 |
|
|
59 |
if (!ref($sort_spec{columns}->{SIMPLE}) && ($sort_spec{columns}->{SIMPLE} eq 'ALL')) { |
|
60 |
map { $sort_spec{columns}->{"$_"} ||= "${table}.${_}"} @{ $meta->columns }; |
|
61 |
delete $sort_spec{columns}->{SIMPLE}; |
|
62 |
} else { |
|
63 |
map { $sort_spec{columns}->{$_} = "${table}.${_}" } @{ delete($sort_spec{columns}->{SIMPLE}) }; |
|
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
42 | 68 |
1; |
43 | 69 |
|
44 | 70 |
__END__ |
... | ... | |
111 | 137 |
|
112 | 138 |
=item C<_sort_spec> |
113 | 139 |
|
114 |
This method is actually not part of this package but must be provided |
|
115 |
by the package this helper is mixed into. |
|
140 |
This method is actually not part of this package but can be provided |
|
141 |
by the package this helper is mixed into. If it isn't then all columns |
|
142 |
of the corresponding table (as returned by the model's meta data) will |
|
143 |
be eligible for sorting. |
|
116 | 144 |
|
117 |
Returns a has with the following keys: |
|
145 |
Returns a hash with the following keys:
|
|
118 | 146 |
|
119 | 147 |
=over 2 |
120 | 148 |
|
... | ... | |
125 | 153 |
|
126 | 154 |
default => [ 'name', 1 ], |
127 | 155 |
|
156 |
Defaults to the table's primary key column (the first column if the |
|
157 |
primary key is composited). |
|
158 |
|
|
128 | 159 |
=item C<columns> |
129 | 160 |
|
130 | 161 |
A hash reference. Its keys are column names, and its values are SQL |
131 | 162 |
strings by which to sort. Example: |
132 | 163 |
|
133 |
columns => { transaction_description => 'oe.transaction_description', |
|
164 |
columns => { SIMPLE => [ 'transaction_description', 'orddate' ], |
|
165 |
the_date => 'CASE WHEN oe.quotation THEN oe.quodate ELSE oe.orddate END', |
|
134 | 166 |
customer_name => 'lower(customer.name)', |
135 | 167 |
}, |
136 | 168 |
|
... | ... | |
141 | 173 |
latter then both the sort direction as well as the null handling will |
142 | 174 |
be appended to each of its members. |
143 | 175 |
|
176 |
The special key C<SIMPLE> can be a scalar or an array reference. If it |
|
177 |
is an array reference then it contains column names that are mapped |
|
178 |
1:1 onto the table's columns. If it is the scalar 'ALL' then all |
|
179 |
columns in that model's meta data are mapped 1:1 unless the C<columns> |
|
180 |
hash already contains a key for that column. |
|
181 |
|
|
182 |
If C<columns> is missing then all columns of the model will be |
|
183 |
eligible for sorting. The list of columns is looked up in the model's |
|
184 |
meta data. |
|
185 |
|
|
144 | 186 |
=item C<nulls> |
145 | 187 |
|
146 | 188 |
Either a scalar or a hash reference determining where C<NULL> values |
Auch abrufbar als: Unified diff
Automatisches Ergänzen von nicht aufgelisteten Werten bei Benutzung von Sorter
Damit muss "sub _sort_spec" für einfache Fälle nicht mehr angegeben
werden.