my @values;
# build ORDER BY clause
- my $columns = $options->{columns} || [];
+ my $columns = [];
+
+ my $use_alias = 0;
+ if (ref($options->{columns}) eq 'ARRAY') {
+ $columns = $options->{columns};
+ }
+ elsif (ref($options->{columns}) eq 'HASH') {
+ $columns = [keys %{$options->{columns}}];
+ $use_alias = 1;
+ }
if (defined $params->{order_by}) {
if (ref($params->{order_by}) eq "ARRAY") {
my @keys = keys %$k;
my $column = shift @keys;
next if !any {$_ eq $column} @$columns;
- if (lc($k->{$column}) eq 'desc') {
- push @clauses, "ORDER BY $column DESC";
+
+ my $order = "";
+ $order = "DESC" if lc($k->{$column}) eq 'desc';
+ $order = "ASC" if lc($k->{$column}) eq 'asc';
+
+ if ($use_alias) {
+ push @clauses, "ORDER BY $options->{columns}->{$column} $order";
}
- elsif (lc($k->{$column}) eq 'asc') {
- push @clauses, "ORDER BY $column ASC";
+ else {
+ push @clauses, "ORDER BY $column $order";
}
}
elsif (any {$_ eq $k} $columns) {
- push @clauses, "ORDER BY $k";
+ if ($use_alias) {
+ push @clauses, "ORDER BY $options->{columns}->{$k}";
+ }
+ else {
+ push @clauses, "ORDER BY $k";
+ }
}
}
}
for my $k (@keys) {
next if !any {$_ eq $k} @$columns;
- if (lc($params->{order_by}->{$k}) eq 'desc') {
- push @clauses, "ORDER BY $k DESC";
+ my $order = "";
+ $order = "DESC" if lc($params->{order_by}->{$k}) eq 'desc';
+ $order = "ASC" if lc($params->{order_by}->{$k}) eq 'asc';
+
+ if ($use_alias) {
+ push @clauses, "ORDER BY $options->{columns}->{$k} $order";
}
- elsif (lc($params->{order_by}->{$k}) eq 'asc') {
- push @clauses, "ORDER BY $k ASC";
+ else {
+ push @clauses, "ORDER BY $k $order";
}
}
}
elsif (any {$_ eq $params->{order_by} } @$columns) {
- push @clauses, "ORDER BY $params->{order_by}";
+ if ($use_alias) {
+ push @clauses, "ORDER BY $options->{columns}->{$params->{order_by}}";
+ }
+ else {
+ push @clauses, "ORDER BY $params->{order_by}";
+ }
}
}